import { Lookup, Arrify, AnyFn, Any } from '@react-spring/types' // eslint-disable-next-line @typescript-eslint/no-empty-function export function noop() {} export const defineHidden = (obj: any, key: any, value: any) => Object.defineProperty(obj, key, { value, writable: true, configurable: true }) type IsType = (arg: T & any) => arg is Narrow type Narrow = [T] extends [Any] ? U : [T] extends [U] ? Extract : U type PlainObject = Exclude export const is = { arr: Array.isArray as IsType, obj: (a: T & any): a is PlainObject => !!a && a.constructor.name === 'Object', fun: ((a: unknown) => typeof a === 'function') as IsType, str: (a: unknown): a is string => typeof a === 'string', num: (a: unknown): a is number => typeof a === 'number', und: (a: unknown): a is undefined => a === undefined, } /** Compare animatable values */ export function isEqual(a: any, b: any) { if (is.arr(a)) { if (!is.arr(b) || a.length !== b.length) return false for (let i = 0; i < a.length; i++) { if (a[i] !== b[i]) return false } return true } return a === b } type EachFn = (this: This, value: Value, key: Key) => void type Eachable = { forEach(cb: EachFn, ctx?: This): void } /** Minifiable `.forEach` call */ export const each = ( obj: Eachable, fn: EachFn ) => obj.forEach(fn) /** Iterate the properties of an object */ export function eachProp( obj: T, fn: ( this: This, value: T extends any[] ? T[number] : T[keyof T], key: string ) => void, ctx?: This ) { if (is.arr(obj)) { for (let i = 0; i < obj.length; i++) { fn.call(ctx as any, obj[i] as any, `${i}`) } return } for (const key in obj) { if (obj.hasOwnProperty(key)) { fn.call(ctx as any, obj[key] as any, key) } } } export const toArray = (a: T): Arrify> => is.und(a) ? [] : is.arr(a) ? (a as any) : [a] /** Copy the `queue`, then iterate it after the `queue` is cleared */ export function flush( queue: Map, iterator: (entry: [P, T]) => void ): void export function flush(queue: Set, iterator: (value: T) => void): void export function flush(queue: any, iterator: any) { if (queue.size) { const items = Array.from(queue) queue.clear() each(items, iterator) } } /** Call every function in the queue with the same arguments. */ export const flushCalls = ( queue: Set, ...args: Parameters ) => flush(queue, fn => fn(...args)) // For server-side rendering: https://github.com/react-spring/zustand/pull/34 // Deno support: https://github.com/pmndrs/zustand/issues/347 export const isSSR = () => typeof window === 'undefined' || !window.navigator || /ServerSideRendering|^Deno\//.test(window.navigator.userAgent)