Convert src/runtime/internal/utils.ts to JavaScript

pull/8569/head
S. Elliott Johnson 3 years ago
parent d464cca428
commit adf62ad908

@ -1,218 +1,216 @@
import type { Readable } from '../store'; /** @returns {void} */
export function noop() { }
export function noop() {} /** @returns {any} */
export const identity = (x) => x; export const identity = (x) => x;
/** @param {T} tar
export function assign<T, S>(tar: T, src: S): T & S { * @param {S} src
// @ts-ignore * @returns {T & S}
for (const k in src) tar[k] = src[k]; */
return tar as T & S; export function assign(tar, src) {
// @ts-ignore
for (const k in src)
tar[k] = src[k];
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
export function is_promise<T = any>(value: any): value is PromiseLike<T> { /** @param {any} value
return ( * @returns {boolean}
!!value && */
(typeof value === 'object' || typeof value === 'function') && export function is_promise(value) {
typeof value.then === 'function' return (!!value &&
); (typeof value === 'object' || typeof value === 'function') &&
} typeof value.then === 'function');
}
/** @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} */
export function run(fn) { export function run(fn) {
return fn(); return fn();
} }
/** @returns {any} */
export function blank_object() { export function blank_object() {
return Object.create(null); return Object.create(null);
} }
/** @param {Function[]} fns
export function run_all(fns: Function[]) { * @returns {void}
fns.forEach(run); */
export function run_all(fns) {
fns.forEach(run);
} }
/** @param {any} thing
export function is_function(thing: any): thing is Function { * @returns {boolean}
return typeof thing === 'function'; */
export function is_function(thing) {
return typeof thing === 'function';
} }
/** @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} */
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) {
src_url_equal_anchor = document.createElement('a'); src_url_equal_anchor = document.createElement('a');
} }
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} */
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} */
export function is_empty(obj) { export function is_empty(obj) {
return Object.keys(obj).length === 0; return Object.keys(obj).length === 0;
} }
/** @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} */
export function subscribe(store, ...callbacks) { export function subscribe(store, ...callbacks) {
if (store == null) { if (store == null) {
for (const callback of callbacks) { for (const callback of callbacks) {
callback(undefined); callback(undefined);
} }
return noop; return noop;
} }
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
export function get_store_value<T>(store: Readable<T>): T { * @returns {T}
let value; */
subscribe(store, (_) => (value = _))(); export function get_store_value(store) {
return value; let value;
} subscribe(store, (_) => (value = _))();
return value;
}
/** @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} */
export function create_slot(definition, ctx, $$scope, fn) { export function create_slot(definition, ctx, $$scope, fn) {
if (definition) { if (definition) {
const slot_ctx = get_slot_context(definition, ctx, $$scope, fn); const slot_ctx = get_slot_context(definition, ctx, $$scope, fn);
return definition[0](slot_ctx); return definition[0](slot_ctx);
} }
} }
/** @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} */
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) {
const lets = definition[2](fn(dirty)); const lets = definition[2](fn(dirty));
if ($$scope.dirty === undefined) {
if ($$scope.dirty === undefined) { return lets;
return lets; }
} if (typeof lets === 'object') {
const merged = [];
if (typeof lets === 'object') { const len = Math.max($$scope.dirty.length, lets.length);
const merged = []; for (let i = 0; i < len; i += 1) {
const len = Math.max($$scope.dirty.length, lets.length); merged[i] = $$scope.dirty[i] | lets[i];
for (let i = 0; i < len; i += 1) { }
merged[i] = $$scope.dirty[i] | lets[i]; return merged;
} }
return $$scope.dirty | lets;
return merged; }
} return $$scope.dirty;
}
return $$scope.dirty | lets; /** @returns {void} */
} export function update_slot_base(slot, slot_definition, ctx, $$scope, slot_changes, get_slot_context_fn) {
if (slot_changes) {
return $$scope.dirty; const slot_context = get_slot_context(slot_definition, ctx, $$scope, get_slot_context_fn);
} slot.p(slot_context, slot_changes);
}
export function update_slot_base( }
slot, /** @returns {void} */
slot_definition, export function update_slot(slot, slot_definition, ctx, $$scope, dirty, get_slot_changes_fn, get_slot_context_fn) {
ctx, const slot_changes = get_slot_changes(slot_definition, $$scope, dirty, get_slot_changes_fn);
$$scope, update_slot_base(slot, slot_definition, ctx, $$scope, slot_changes, get_slot_context_fn);
slot_changes, }
get_slot_context_fn /** @returns {any[] | -1} */
) {
if (slot_changes) {
const slot_context = get_slot_context(slot_definition, ctx, $$scope, get_slot_context_fn);
slot.p(slot_context, slot_changes);
}
}
export function update_slot(
slot,
slot_definition,
ctx,
$$scope,
dirty,
get_slot_changes_fn,
get_slot_context_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);
}
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) {
const dirty = []; const dirty = [];
const length = $$scope.ctx.length / 32; const length = $$scope.ctx.length / 32;
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
dirty[i] = -1; dirty[i] = -1;
} }
return dirty; return dirty;
} }
return -1; return -1;
} }
/** @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)
return result; if (k[0] !== '$')
result[k] = props[k];
return result;
} }
/** @returns {{}} */
export function compute_rest_props(props, keys) { export function compute_rest_props(props, keys) {
const rest = {}; const rest = {};
keys = new Set(keys); keys = new Set(keys);
for (const k in props) if (!keys.has(k) && k[0] !== '$') rest[k] = props[k]; for (const k in props)
return rest; if (!keys.has(k) && k[0] !== '$')
} rest[k] = props[k];
return rest;
}
/** @returns {{}} */
export function compute_slots(slots) { export function compute_slots(slots) {
const result = {}; const result = {};
for (const key in slots) { for (const key in slots) {
result[key] = true; result[key] = true;
} }
return result; return result;
} }
/** @returns {(this: any, ...args: any[]) => void} */
export function once(fn) { export function once(fn) {
let ran = false; let ran = false;
return function (this: any, ...args) { return function (...args) {
if (ran) return; if (ran)
ran = true; return;
fn.call(this, ...args); ran = true;
}; fn.call(this, ...args);
} };
}
/** @returns {any} */
export function null_to_empty(value) { export function null_to_empty(value) {
return value == null ? '' : value; return value == null ? '' : value;
} }
/** @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} */
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} */
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
export function split_css_unit(value: number | string): [number, string] { * @returns {[number, string]}
const split = typeof value === 'string' && value.match(/^\s*(-?[\d.]+)([^\s]*)\s*$/); */
return split ? [parseFloat(split[1]), split[2] || 'px'] : [value as number, 'px']; export function split_css_unit(value) {
const split = typeof value === 'string' && value.match(/^\s*(-?[\d.]+)([^\s]*)\s*$/);
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