Manual cleanup of src/runtime/internal/utils.js

pull/8569/head
S. Elliott Johnson 3 years ago
parent 7da65e0904
commit 6552bb949f

@ -1,7 +1,9 @@
/** @returns {void} */ /** @returns {void} */
export function noop() {} export function noop() {}
/** @returns {any} */ /** @returns {any} */
export const identity = (x) => x; export const identity = (x) => x;
/** @param {T} tar /** @param {T} tar
* @param {S} src * @param {S} src
* @returns {T & S} * @returns {T & S}
@ -11,6 +13,7 @@ export function assign(tar, src) {
for (const k in src) tar[k] = src[k]; for (const k in src) tar[k] = src[k];
return tar; return tar;
} }
// Adapted from https://github.com/then/is-promise/blob/master/index.js // Adapted from https://github.com/then/is-promise/blob/master/index.js
// Distributed under MIT License https://github.com/then/is-promise/blob/master/LICENSE // Distributed under MIT License https://github.com/then/is-promise/blob/master/LICENSE
/** @param {any} value /** @param {any} value
@ -23,37 +26,45 @@ export function is_promise(value) {
typeof value.then === 'function' typeof value.then === 'function'
); );
} }
/** @returns {void} */ /** @returns {void} */
export function add_location(element, file, line, column, char) { export function add_location(element, file, line, column, char) {
element.__svelte_meta = { element.__svelte_meta = {
loc: { file, line, column, char } loc: { file, line, column, char }
}; };
} }
/** @returns {any} */ /** @returns {any} */
export function run(fn) { export function run(fn) {
return fn(); return fn();
} }
/** @returns {any} */ /** @returns {any} */
export function blank_object() { export function blank_object() {
return Object.create(null); return Object.create(null);
} }
/** @param {Function[]} fns /** @param {Function[]} fns
* @returns {void} * @returns {void}
*/ */
export function run_all(fns) { export function run_all(fns) {
fns.forEach(run); fns.forEach(run);
} }
/** @param {any} thing /** @param {any} thing
* @returns {boolean} * @returns {boolean}
*/ */
export function is_function(thing) { export function is_function(thing) {
return typeof thing === 'function'; return typeof thing === 'function';
} }
/** @returns {boolean} */ /** @returns {boolean} */
export function safe_not_equal(a, b) { export function safe_not_equal(a, b) {
return a != a ? b == b : a !== b || (a && typeof a === 'object') || typeof a === 'function'; return a != a ? b == b : a !== b || (a && typeof a === 'object') || typeof a === 'function';
} }
let src_url_equal_anchor; let src_url_equal_anchor;
/** @returns {boolean} */ /** @returns {boolean} */
export function src_url_equal(element_src, url) { export function src_url_equal(element_src, url) {
if (!src_url_equal_anchor) { if (!src_url_equal_anchor) {
@ -62,20 +73,24 @@ export function src_url_equal(element_src, url) {
src_url_equal_anchor.href = url; src_url_equal_anchor.href = url;
return element_src === src_url_equal_anchor.href; return element_src === src_url_equal_anchor.href;
} }
/** @returns {boolean} */ /** @returns {boolean} */
export function not_equal(a, b) { export function not_equal(a, b) {
return a != a ? b == b : a !== b; return a != a ? b == b : a !== b;
} }
/** @returns {boolean} */ /** @returns {boolean} */
export function is_empty(obj) { export function is_empty(obj) {
return Object.keys(obj).length === 0; return Object.keys(obj).length === 0;
} }
/** @returns {void} */ /** @returns {void} */
export function validate_store(store, name) { export function validate_store(store, name) {
if (store != null && typeof store.subscribe !== 'function') { if (store != null && typeof store.subscribe !== 'function') {
throw new Error(`'${name}' is not a store with a 'subscribe' method`); throw new Error(`'${name}' is not a store with a 'subscribe' method`);
} }
} }
/** @returns {any} */ /** @returns {any} */
export function subscribe(store, ...callbacks) { export function subscribe(store, ...callbacks) {
if (store == null) { if (store == null) {
@ -87,6 +102,7 @@ export function subscribe(store, ...callbacks) {
const unsub = store.subscribe(...callbacks); const unsub = store.subscribe(...callbacks);
return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub; return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub;
} }
/** @param {Readable<T>} store /** @param {Readable<T>} store
* @returns {T} * @returns {T}
*/ */
@ -95,10 +111,12 @@ export function get_store_value(store) {
subscribe(store, (_) => (value = _))(); subscribe(store, (_) => (value = _))();
return value; return value;
} }
/** @returns {void} */ /** @returns {void} */
export function component_subscribe(component, store, callback) { export function component_subscribe(component, store, callback) {
component.$$.on_destroy.push(subscribe(store, callback)); component.$$.on_destroy.push(subscribe(store, callback));
} }
/** @returns {any} */ /** @returns {any} */
export function create_slot(definition, ctx, $$scope, fn) { export function create_slot(definition, ctx, $$scope, fn) {
if (definition) { if (definition) {
@ -106,10 +124,12 @@ export function create_slot(definition, ctx, $$scope, fn) {
return definition[0](slot_ctx); return definition[0](slot_ctx);
} }
} }
/** @returns {any} */ /** @returns {any} */
function get_slot_context(definition, ctx, $$scope, fn) { function get_slot_context(definition, ctx, $$scope, fn) {
return definition[1] && fn ? assign($$scope.ctx.slice(), definition[1](fn(ctx))) : $$scope.ctx; return definition[1] && fn ? assign($$scope.ctx.slice(), definition[1](fn(ctx))) : $$scope.ctx;
} }
/** @returns {any} */ /** @returns {any} */
export function get_slot_changes(definition, $$scope, dirty, fn) { export function get_slot_changes(definition, $$scope, dirty, fn) {
if (definition[2] && fn) { if (definition[2] && fn) {
@ -129,6 +149,7 @@ export function get_slot_changes(definition, $$scope, dirty, fn) {
} }
return $$scope.dirty; return $$scope.dirty;
} }
/** @returns {void} */ /** @returns {void} */
export function update_slot_base( export function update_slot_base(
slot, slot,
@ -143,6 +164,7 @@ export function update_slot_base(
slot.p(slot_context, slot_changes); slot.p(slot_context, slot_changes);
} }
} }
/** @returns {void} */ /** @returns {void} */
export function update_slot( export function update_slot(
slot, slot,
@ -156,6 +178,7 @@ export function update_slot(
const slot_changes = get_slot_changes(slot_definition, $$scope, dirty, get_slot_changes_fn); const slot_changes = get_slot_changes(slot_definition, $$scope, dirty, get_slot_changes_fn);
update_slot_base(slot, slot_definition, ctx, $$scope, slot_changes, get_slot_context_fn); update_slot_base(slot, slot_definition, ctx, $$scope, slot_changes, get_slot_context_fn);
} }
/** @returns {any[] | -1} */ /** @returns {any[] | -1} */
export function get_all_dirty_from_scope($$scope) { export function get_all_dirty_from_scope($$scope) {
if ($$scope.ctx.length > 32) { if ($$scope.ctx.length > 32) {
@ -168,12 +191,14 @@ export function get_all_dirty_from_scope($$scope) {
} }
return -1; return -1;
} }
/** @returns {{}} */ /** @returns {{}} */
export function exclude_internal_props(props) { export function exclude_internal_props(props) {
const result = {}; const result = {};
for (const k in props) if (k[0] !== '$') result[k] = props[k]; for (const k in props) if (k[0] !== '$') result[k] = props[k];
return result; return result;
} }
/** @returns {{}} */ /** @returns {{}} */
export function compute_rest_props(props, keys) { export function compute_rest_props(props, keys) {
const rest = {}; const rest = {};
@ -181,6 +206,7 @@ export function compute_rest_props(props, keys) {
for (const k in props) if (!keys.has(k) && k[0] !== '$') rest[k] = props[k]; for (const k in props) if (!keys.has(k) && k[0] !== '$') rest[k] = props[k];
return rest; return rest;
} }
/** @returns {{}} */ /** @returns {{}} */
export function compute_slots(slots) { export function compute_slots(slots) {
const result = {}; const result = {};
@ -189,6 +215,7 @@ export function compute_slots(slots) {
} }
return result; return result;
} }
/** @returns {(this: any, ...args: any[]) => void} */ /** @returns {(this: any, ...args: any[]) => void} */
export function once(fn) { export function once(fn) {
let ran = false; let ran = false;
@ -198,21 +225,26 @@ export function once(fn) {
fn.call(this, ...args); fn.call(this, ...args);
}; };
} }
/** @returns {any} */ /** @returns {any} */
export function null_to_empty(value) { export function null_to_empty(value) {
return value == null ? '' : value; return value == null ? '' : value;
} }
/** @returns {any} */ /** @returns {any} */
export function set_store_value(store, ret, value) { export function set_store_value(store, ret, value) {
store.set(value); store.set(value);
return ret; return ret;
} }
/** @returns {any} */ /** @returns {any} */
export const has_prop = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop); export const has_prop = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
/** @returns {any} */ /** @returns {any} */
export function action_destroyer(action_result) { export function action_destroyer(action_result) {
return action_result && is_function(action_result.destroy) ? action_result.destroy : noop; return action_result && is_function(action_result.destroy) ? action_result.destroy : noop;
} }
/** @param {number | string} value /** @param {number | string} value
* @returns {[number, string]} * @returns {[number, string]}
*/ */
@ -220,4 +252,5 @@ export function split_css_unit(value) {
const split = typeof value === 'string' && value.match(/^\s*(-?[\d.]+)([^\s]*)\s*$/); const split = typeof value === 'string' && value.match(/^\s*(-?[\d.]+)([^\s]*)\s*$/);
return split ? [parseFloat(split[1]), split[2] || 'px'] : [value, 'px']; return split ? [parseFloat(split[1]), split[2] || 'px'] : [value, 'px'];
} }
export const contenteditable_truthy_values = ['', true, 1, 'true', 'contenteditable']; export const contenteditable_truthy_values = ['', true, 1, 'true', 'contenteditable'];

Loading…
Cancel
Save