import { Signal } from "@preact-signals/unified-signals"; /** * Always return false. */ export const NO = () => false; const onRE = /^on[^a-z]/; export const isOn = (key: string) => onRE.test(key); export const isModelListener = (key: string) => key.startsWith("onUpdate:"); export const extend = Object.assign; export const remove = (arr: T[], el: T) => { const i = arr.indexOf(el); if (i > -1) { arr.splice(i, 1); } }; export const hasChanged = (value: any, oldValue: any): boolean => !Object.is(value, oldValue); const hasOwnProperty = Object.prototype.hasOwnProperty; export const hasOwn = ( val: object, key: string | symbol ): key is keyof typeof val => hasOwnProperty.call(val, key); export const isArray = Array.isArray; export const isMap = (val: unknown): val is Map => toTypeString(val) === "[object Map]"; export const isSet = (val: unknown): val is Set => toTypeString(val) === "[object Set]"; export const isDate = (val: unknown): val is Date => toTypeString(val) === "[object Date]"; export const isRegExp = (val: unknown): val is RegExp => toTypeString(val) === "[object RegExp]"; export const isFunction = (val: unknown): val is Function => typeof val === "function"; export const isString = (val: unknown): val is string => typeof val === "string"; export const isSymbol = (val: unknown): val is symbol => typeof val === "symbol"; export const isObject = (val: unknown): val is Record => val !== null && typeof val === "object"; export const isPromise = (val: unknown): val is Promise => { return ( (isObject(val) || isFunction(val)) && isFunction((val as any).then) && isFunction((val as any).catch) ); }; /** * Make a map and return a function for checking if a key * is in that map. * IMPORTANT: all calls of this function must be prefixed with * \/\*#\_\_PURE\_\_\*\/ * So that rollup can tree-shake them if necessary. */ export function makeMap( str: string, expectsLowerCase?: boolean ): (key: string) => boolean { const map: Record = Object.create(null); const list: Array = str.split(","); for (let i = 0; i < list.length; i++) { map[list[i]!] = true; } return expectsLowerCase ? (val) => !!map[val.toLowerCase()] : (val) => !!map[val]; } export const objectToString = Object.prototype.toString; export const toTypeString = (value: unknown): string => objectToString.call(value); export const toRawType = (value: unknown): string => { // extract "RawType" from strings like "[object RawType]" return toTypeString(value).slice(8, -1); }; export const isPlainObject = (val: unknown): val is object => toTypeString(val) === "[object Object]"; export const isIntegerKey = (key: unknown) => isString(key) && key !== "NaN" && key[0] !== "-" && "" + parseInt(key, 10) === key; const cacheStringFunction = string>(fn: T): T => { const cache: Record = Object.create(null); return ((str: string) => { const hit = cache[str]; return hit || (cache[str] = fn(str)); }) as T; }; /** * @private */ export const capitalize = cacheStringFunction((str: T) => { return (str.charAt(0).toUpperCase() + str.slice(1)) as Capitalize; }); export const def = (obj: object, key: string | symbol, value: any) => { Object.defineProperty(obj, key, { configurable: true, enumerable: false, value, }); }; export const isSignal = (val: unknown): val is Signal => isObject(val) && val instanceof Signal;