-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
utils.js
81 lines (64 loc) · 1.86 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
export function noop() {}
export const identity = x => x;
export function assign(tar, src) {
for (const k in src) tar[k] = src[k];
return tar;
}
export function is_promise(value) {
return value && 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) {
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;
}