import type { ChildNodeValue, NodeValue } from './observableInterfaces'; const hasOwnProperty = Object.prototype.hasOwnProperty; export function isArray(obj: unknown): obj is Array { return Array.isArray(obj); } export function isString(obj: unknown): obj is string { return typeof obj === 'string'; } export function isObject(obj: unknown): obj is Record { return !!obj && typeof obj === 'object' && !isArray(obj); } export function isFunction(obj: unknown): obj is Function { return typeof obj === 'function'; } export function isPrimitive(arg: unknown): arg is string | number | bigint | boolean | symbol { const type = typeof arg; return arg !== undefined && type !== 'object' && type !== 'function'; } export function isSymbol(obj: unknown): obj is symbol { return typeof obj === 'symbol'; } export function isBoolean(obj: unknown): obj is boolean { return typeof obj === 'boolean'; } export function isPromise(obj: unknown): obj is Promise { return obj instanceof Promise; } export function isEmpty(obj: object): boolean { // Looping and returning false on the first property is faster than Object.keys(obj).length === 0 // https://jsbench.me/qfkqv692c8 if (!obj) return false; if (isArray(obj)) return obj.length === 0; for (const key in obj) { if (hasOwnProperty.call(obj, key)) { return false; } } return true; } const setPrimitives = new Set(['boolean', 'string', 'number']); /** @internal */ export function isActualPrimitive(arg: unknown): arg is boolean | string | number { return setPrimitives.has(typeof arg); } /** @internal */ export function isChildNodeValue(node: NodeValue): node is ChildNodeValue { return !!node.parent; }