From fcaa238e7de7be8b17b6ca5d336e9154b777ebb1 Mon Sep 17 00:00:00 2001 From: Ivan Hofer Date: Wed, 20 Jul 2022 16:30:09 +0200 Subject: [PATCH] use `is_function` everywhere --- src/compiler/utils/clone.ts | 4 +++- src/runtime/internal/Component.ts | 2 +- src/runtime/internal/dev.ts | 3 ++- src/runtime/internal/utils.ts | 6 +++--- src/runtime/motion/tweened.ts | 4 ++-- src/runtime/transition/index.ts | 2 +- 6 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/compiler/utils/clone.ts b/src/compiler/utils/clone.ts index f11ed94967..d22bac9114 100644 --- a/src/compiler/utils/clone.ts +++ b/src/compiler/utils/clone.ts @@ -1,3 +1,5 @@ +import { is_function} from '../../runtime/internal/utils'; + // adapted from klona v2.0.4 - https://github.com/lukeed/klona // (c) Luke Edwards, under MIT License @@ -22,7 +24,7 @@ export function clone(val) { enumerable: true, writable: true }); - } else if (typeof val[k] !== 'function') { // MODIFICATION: skip functions + } else if (!is_function(val[k])) { // MODIFICATION: skip functions out[k] = (tmp = val[k]) && typeof tmp === 'object' ? clone(tmp) : tmp; } } diff --git a/src/runtime/internal/Component.ts b/src/runtime/internal/Component.ts index c51e7f70ea..f0ddc12133 100644 --- a/src/runtime/internal/Component.ts +++ b/src/runtime/internal/Component.ts @@ -177,7 +177,7 @@ export function init(component, options, instance, create_fragment, not_equal, p } export let SvelteElement; -if (typeof HTMLElement === 'function') { +if (is_function(HTMLElement)) { SvelteElement = class extends HTMLElement { $$: T$$; $$set?: ($$props: any) => void; diff --git a/src/runtime/internal/dev.ts b/src/runtime/internal/dev.ts index 5b00e7dc5b..5c3af0b32b 100644 --- a/src/runtime/internal/dev.ts +++ b/src/runtime/internal/dev.ts @@ -1,6 +1,7 @@ import { custom_event, append, append_hydration, insert, insert_hydration, detach, listen, attr } from './dom'; import { SvelteComponent } from './Component'; import { is_void } from '../../shared/utils/names'; +import { is_function } from './utils'; export function dispatch_dev(type: string, detail?: T) { document.dispatchEvent(custom_event(type, { version: '__VERSION__', ...detail }, { bubbles: true })); @@ -93,7 +94,7 @@ export function set_data_dev(text, data) { export function validate_each_argument(arg) { if (typeof arg !== 'string' && !(arg && typeof arg === 'object' && 'length' in arg)) { let msg = '{#each} only iterates over array-like objects.'; - if (typeof Symbol === 'function' && arg && Symbol.iterator in arg) { + if (is_function(Symbol) && arg && Symbol.iterator in arg) { msg += ' You can use a spread to convert this iterable into an array.'; } throw new Error(msg); diff --git a/src/runtime/internal/utils.ts b/src/runtime/internal/utils.ts index 5cb7832add..cc81e9cb15 100644 --- a/src/runtime/internal/utils.ts +++ b/src/runtime/internal/utils.ts @@ -11,7 +11,7 @@ export function assign(tar: T, src: S): T & S { } export function is_promise(value: any): value is PromiseLike { - return value && typeof value === 'object' && typeof value.then === 'function'; + return value && typeof value === 'object' && is_function(value.then); } export function add_location(element, file, line, column, char) { @@ -33,7 +33,7 @@ export function is_function(thing: any): thing is Function { } 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') || is_function(a)); } let src_url_equal_anchor; @@ -55,7 +55,7 @@ export function is_empty(obj) { } export function validate_store(store, name) { - if (store != null && typeof store.subscribe !== 'function') { + if (store != null && !is_function(store.subscribe)) { throw new Error(`'${name}' is not a store with a 'subscribe' method`); } } diff --git a/src/runtime/motion/tweened.ts b/src/runtime/motion/tweened.ts index 950c7d22fa..4d62caf527 100644 --- a/src/runtime/motion/tweened.ts +++ b/src/runtime/motion/tweened.ts @@ -1,5 +1,5 @@ import { Readable, writable } from 'svelte/store'; -import { assign, loop, now, Task } from 'svelte/internal'; +import { assign, loop, now, Task, is_function } from 'svelte/internal'; import { linear } from 'svelte/easing'; import { is_date } from './utils'; import { resolved_promise } from 'svelte/internal/constants'; @@ -112,7 +112,7 @@ export function tweened(value?: T, defaults: Options = {}): Tweened { if (!started) { fn = interpolate(value, new_value); - if (typeof duration === 'function') duration = duration(value, new_value); + if (is_function(duration)) duration = duration(value, new_value); started = true; } diff --git a/src/runtime/transition/index.ts b/src/runtime/transition/index.ts index 9315cd77d5..d939d4d444 100644 --- a/src/runtime/transition/index.ts +++ b/src/runtime/transition/index.ts @@ -190,7 +190,7 @@ export function draw(node: SVGElement & { getTotalLength(): number }, { } else { duration = len / speed; } - } else if (typeof duration === 'function') { + } else if (is_function(duration)) { duration = duration(len); }