mirror of https://github.com/sveltejs/svelte
100 lines
2.3 KiB
100 lines
2.3 KiB
export function noop() {}
|
|
|
|
export const identity = x => x;
|
|
|
|
export function assign<T, S>(tar:T, src:S): T & S {
|
|
// @ts-ignore
|
|
for (const k in src) tar[k] = src[k];
|
|
return tar as T & S;
|
|
}
|
|
|
|
export function is_promise<T = any>(value: any): value is PromiseLike<T> {
|
|
return value && typeof value === 'object' && typeof value.then === 'function';
|
|
}
|
|
|
|
export function add_location(element, file, line, column, char) {
|
|
element.__svelte_meta = {
|
|
loc: { file, line, column, char }
|
|
};
|
|
}
|
|
|
|
export function run(fn) {
|
|
return fn();
|
|
}
|
|
|
|
export function blank_object() {
|
|
return Object.create(null);
|
|
}
|
|
|
|
export function run_all(fns) {
|
|
fns.forEach(run);
|
|
}
|
|
|
|
export function is_function(thing: any): thing is Function {
|
|
return typeof thing === 'function';
|
|
}
|
|
|
|
export function safe_not_equal(a, b) {
|
|
return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function');
|
|
}
|
|
|
|
export function not_equal(a, b) {
|
|
return a != a ? b == b : a !== b;
|
|
}
|
|
|
|
export function validate_store(store, name) {
|
|
if (!store || typeof store.subscribe !== 'function') {
|
|
throw new Error(`'${name}' is not a store with a 'subscribe' method`);
|
|
}
|
|
}
|
|
|
|
export function subscribe(component, store, callback) {
|
|
const unsub = store.subscribe(callback);
|
|
|
|
component.$$.on_destroy.push(unsub.unsubscribe
|
|
? () => unsub.unsubscribe()
|
|
: unsub);
|
|
}
|
|
|
|
export function create_slot(definition, ctx, fn) {
|
|
if (definition) {
|
|
const slot_ctx = get_slot_context(definition, ctx, fn);
|
|
return definition[0](slot_ctx);
|
|
}
|
|
}
|
|
|
|
export function get_slot_context(definition, ctx, fn) {
|
|
return definition[1]
|
|
? assign({}, assign(ctx.$$scope.ctx, definition[1](fn ? fn(ctx) : {})))
|
|
: ctx.$$scope.ctx;
|
|
}
|
|
|
|
export function get_slot_changes(definition, ctx, changed, fn) {
|
|
return definition[1]
|
|
? assign({}, assign(ctx.$$scope.changed || {}, definition[1](fn ? fn(changed) : {})))
|
|
: ctx.$$scope.changed || {};
|
|
}
|
|
|
|
export function exclude_internal_props(props) {
|
|
const result = {};
|
|
for (const k in props) if (k[0] !== '$') result[k] = props[k];
|
|
return result;
|
|
}
|
|
|
|
const is_client = typeof window !== 'undefined';
|
|
|
|
export let now: () => number = is_client
|
|
? () => window.performance.now()
|
|
: () => Date.now();
|
|
|
|
export let raf = is_client ? requestAnimationFrame : noop;
|
|
|
|
// used internally for testing
|
|
export function set_now(fn) {
|
|
now = fn;
|
|
}
|
|
|
|
export function set_raf(fn) {
|
|
raf = fn;
|
|
}
|