yummies 7.11.0 → 7.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -87
- package/async.cjs +179 -48
- package/async.cjs.map +1 -1
- package/async.d.ts +125 -7
- package/async.js +180 -54
- package/async.js.map +1 -1
- package/chunk-CVq3Gv4J.cjs +50 -0
- package/chunk-YKewjYmz.js +37 -0
- package/common.cjs +48 -8
- package/common.cjs.map +1 -1
- package/common.d.ts +53 -2
- package/common.js +49 -11
- package/common.js.map +1 -1
- package/complex.cjs +275 -128
- package/complex.cjs.map +1 -1
- package/complex.d.ts +66 -0
- package/complex.js +275 -133
- package/complex.js.map +1 -1
- package/cookie.cjs +17 -7
- package/cookie.cjs.map +1 -1
- package/cookie.d.ts +26 -0
- package/cookie.js +18 -9
- package/cookie.js.map +1 -1
- package/css.cjs +163 -39
- package/css.cjs.map +1 -1
- package/css.d.ts +115 -6
- package/css.js +159 -41
- package/css.js.map +1 -1
- package/data.cjs +90 -55
- package/data.cjs.map +1 -1
- package/data.d.ts +50 -0
- package/data.js +91 -61
- package/data.js.map +1 -1
- package/date-time.cjs +594 -412
- package/date-time.cjs.map +1 -1
- package/date-time.d.ts +105 -0
- package/date-time.js +591 -421
- package/date-time.js.map +1 -1
- package/device.cjs +65 -23
- package/device.cjs.map +1 -1
- package/device.d.ts +49 -0
- package/device.js +66 -31
- package/device.js.map +1 -1
- package/encodings.cjs +275 -266
- package/encodings.cjs.map +1 -1
- package/encodings.d.ts +25 -0
- package/encodings.js +276 -268
- package/encodings.js.map +1 -1
- package/errors.cjs +36 -18
- package/errors.cjs.map +1 -1
- package/errors.d.ts +17 -0
- package/errors.js +35 -19
- package/errors.js.map +1 -1
- package/file.cjs +58 -24
- package/file.cjs.map +1 -1
- package/file.d.ts +32 -0
- package/file.js +59 -27
- package/file.js.map +1 -1
- package/format.cjs +125 -83
- package/format.cjs.map +1 -1
- package/format.d.ts +18 -0
- package/format.js +118 -82
- package/format.js.map +1 -1
- package/html.cjs +242 -137
- package/html.cjs.map +1 -1
- package/html.d.ts +81 -0
- package/html.js +239 -150
- package/html.js.map +1 -1
- package/id.cjs +90 -17
- package/id.cjs.map +1 -1
- package/id.d.ts +16 -0
- package/id.js +89 -24
- package/id.js.map +1 -1
- package/imports.cjs +57 -29
- package/imports.cjs.map +1 -1
- package/imports.d.ts +24 -0
- package/imports.js +56 -31
- package/imports.js.map +1 -1
- package/math.cjs +32 -6
- package/math.cjs.map +1 -1
- package/math.d.ts +33 -0
- package/math.js +33 -10
- package/math.js.map +1 -1
- package/media.cjs +291 -84
- package/media.cjs.map +1 -1
- package/media.d.ts +204 -2
- package/media.js +290 -93
- package/media.js.map +1 -1
- package/mobx.cjs +449 -193
- package/mobx.cjs.map +1 -1
- package/mobx.d.ts +108 -0
- package/mobx.js +447 -200
- package/mobx.js.map +1 -1
- package/ms.cjs +37 -10
- package/ms.cjs.map +1 -1
- package/ms.d.ts +16 -0
- package/ms.js +38 -13
- package/ms.js.map +1 -1
- package/number.cjs +29 -7
- package/number.cjs.map +1 -1
- package/number.d.ts +16 -0
- package/number.js +30 -9
- package/number.js.map +1 -1
- package/package.json +11 -3
- package/parser.cjs +117 -64
- package/parser.cjs.map +1 -1
- package/parser.d.ts +17 -0
- package/parser.js +111 -64
- package/parser.js.map +1 -1
- package/price.cjs +24 -18
- package/price.cjs.map +1 -1
- package/price.d.ts +24 -0
- package/price.js +25 -20
- package/price.js.map +1 -1
- package/random.cjs +95 -13
- package/random.cjs.map +1 -1
- package/random.d.ts +80 -0
- package/random.js +96 -22
- package/random.js.map +1 -1
- package/react.cjs +673 -214
- package/react.cjs.map +1 -1
- package/react.d.ts +21 -0
- package/react.js +674 -239
- package/react.js.map +1 -1
- package/sound.cjs +30 -9
- package/sound.cjs.map +1 -1
- package/sound.d.ts +16 -0
- package/sound.js +31 -11
- package/sound.js.map +1 -1
- package/storage.cjs +49 -50
- package/storage.cjs.map +1 -1
- package/storage.d.ts +24 -0
- package/storage.js +50 -53
- package/storage.js.map +1 -1
- package/text.cjs +67 -34
- package/text.cjs.map +1 -1
- package/text.d.ts +16 -0
- package/text.js +68 -37
- package/text.js.map +1 -1
- package/type-guard.cjs +292 -72
- package/type-guard.cjs.map +1 -1
- package/type-guard.d.ts +18 -0
- package/type-guard.js +288 -73
- package/type-guard.js.map +1 -1
- package/types.cjs +0 -2
- package/types.d.ts +41 -0
- package/types.global.cjs +0 -2
- package/types.global.d.ts +41 -0
- package/types.global.js +0 -2
- package/types.js +0 -2
- package/vibrate.cjs +47 -6
- package/vibrate.cjs.map +1 -1
- package/vibrate.d.ts +39 -1
- package/vibrate.js +48 -8
- package/vibrate.js.map +1 -1
- package/types.cjs.map +0 -1
- package/types.global.cjs.map +0 -1
- package/types.global.js.map +0 -1
- package/types.js.map +0 -1
package/type-guard.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-guard.cjs","sources":["../src/type-guard/_exports.ts"],"sourcesContent":["import type { AnyFunction, AnyObject, ValueOf } from 'yummies/types';\n\nconst TYPE = {\n Null: 'null',\n Undefined: 'undefined',\n NaN: 'nan',\n Object: '[object Object]',\n Array: '[object Array]',\n String: '[object String]',\n Number: '[object Number]',\n Boolean: '[object Boolean]',\n Function: '[object Function]',\n AsyncFunction: '[object AsyncFunction]',\n RegExp: '[object RegExp]',\n Symbol: '[object Symbol]',\n Infinite: 'infinite',\n Element: 'element',\n};\n\ntype Type = ValueOf<typeof TYPE>;\n\nfunction getType(value: unknown): Type {\n if (value === undefined) {\n return TYPE.Undefined;\n }\n if (value === null) {\n return TYPE.Null;\n }\n\n // handle DOM elements\n // @ts-expect-error\n if (value && (value.nodeType === 1 || value.nodeType === 9)) {\n return TYPE.Element;\n }\n\n const stringifiedValue = Object.prototype.toString.call(value);\n\n // handle NaN and Infinity\n if (stringifiedValue === TYPE.Number) {\n if (Number.isNaN(value as number)) {\n return TYPE.NaN;\n }\n if (!Number.isFinite(value as number)) {\n return TYPE.Infinite;\n }\n }\n\n return stringifiedValue as Type;\n}\n\nconst createTypeGuard =\n <T>(...types: Type[]) =>\n (value: unknown): value is T =>\n types.includes(getType(value));\n\n/**\n * Checks that a value is neither `null` nor `undefined`.\n *\n * @template T Value type without nullish branches.\n * @param value Value to test.\n * @returns `true` when the value is defined.\n *\n * @example\n * ```ts\n * isDefined(0); // true\n * ```\n *\n * @example\n * ```ts\n * isDefined(null); // false\n * ```\n */\nexport const isDefined = <T>(value: T | undefined | null): value is T =>\n value != null;\n\n/**\n * Checks whether a value is exactly `null`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `null`.\n *\n * @example\n * ```ts\n * isNull(null); // true\n * ```\n *\n * @example\n * ```ts\n * isNull(undefined); // false\n * ```\n */\nexport const isNull = createTypeGuard<null>(TYPE.Null);\n\n/**\n * Checks whether a value is exactly `undefined`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `undefined`.\n *\n * @example\n * ```ts\n * isUndefined(undefined); // true\n * ```\n *\n * @example\n * ```ts\n * isUndefined('value'); // false\n * ```\n */\nexport const isUndefined = createTypeGuard<undefined>(TYPE.Undefined);\n\n/**\n * Checks whether a value is a plain object.\n *\n * @param value Value to test.\n * @returns `true` when the value matches `[object Object]`.\n *\n * @example\n * ```ts\n * isObject({ id: 1 }); // true\n * ```\n *\n * @example\n * ```ts\n * isObject([]); // false\n * ```\n */\nexport const isObject = createTypeGuard<AnyObject>(TYPE.Object);\n\n/**\n * Checks whether a value is an array.\n *\n * @param value Value to test.\n * @returns `true` when the value is an array.\n *\n * @example\n * ```ts\n * isArray([1, 2, 3]); // true\n * ```\n *\n * @example\n * ```ts\n * isArray({ length: 1 }); // false\n * ```\n */\nexport const isArray = createTypeGuard<unknown[]>(TYPE.Array);\n\n/**\n * Checks whether a value is a string object or primitive string.\n *\n * @param value Value to test.\n * @returns `true` when the value is a string.\n *\n * @example\n * ```ts\n * isString('hello'); // true\n * ```\n *\n * @example\n * ```ts\n * isString(123); // false\n * ```\n */\nexport const isString = createTypeGuard<string>(TYPE.String);\n\n/**\n * Checks whether a value is a finite number.\n *\n * Unlike `isNaN` and `isInfinite`, this guard only matches regular numeric values.\n *\n * @param value Value to test.\n * @returns `true` when the value is a non-NaN finite number.\n *\n * @example\n * ```ts\n * isNumber(123); // true\n * ```\n *\n * @example\n * ```ts\n * isNumber(Number.NaN); // false\n * ```\n */\nexport const isNumber = createTypeGuard<number>(TYPE.Number);\n\n/**\n * Checks whether a value is a boolean.\n *\n * @param value Value to test.\n * @returns `true` when the value is a boolean.\n *\n * @example\n * ```ts\n * isBoolean(true); // true\n * ```\n *\n * @example\n * ```ts\n * isBoolean('true'); // false\n * ```\n */\nexport const isBoolean = createTypeGuard<boolean>(TYPE.Boolean);\n\n/**\n * Checks whether a value is a synchronous or asynchronous function.\n *\n * @param value Value to test.\n * @returns `true` when the value is a function.\n *\n * @example\n * ```ts\n * isFunction(() => {}); // true\n * ```\n *\n * @example\n * ```ts\n * isFunction(async () => {}); // true\n * ```\n */\nexport const isFunction = createTypeGuard<AnyFunction>(\n TYPE.Function,\n TYPE.AsyncFunction,\n);\n\n/**\n * Checks whether a value is a regular expression.\n *\n * @param value Value to test.\n * @returns `true` when the value is a `RegExp`.\n *\n * @example\n * ```ts\n * isRegExp(/foo/); // true\n * ```\n *\n * @example\n * ```ts\n * isRegExp('foo'); // false\n * ```\n */\nexport const isRegExp = createTypeGuard<RegExp>(TYPE.RegExp);\n\n/**\n * Checks whether a value looks like a DOM element or document node.\n *\n * @param value Value to test.\n * @returns `true` when the value has an element-like node type.\n *\n * @example\n * ```ts\n * isElement(document.body); // true\n * ```\n *\n * @example\n * ```ts\n * isElement({ nodeType: 3 }); // false\n * ```\n */\nexport const isElement = createTypeGuard<HTMLElement>(TYPE.Element);\n\n/**\n * Checks whether a value is `NaN`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `NaN`.\n *\n * @example\n * ```ts\n * isNaN(Number.NaN); // true\n * ```\n *\n * @example\n * ```ts\n * isNaN(5); // false\n * ```\n */\nexport const isNaN = createTypeGuard<number>(TYPE.NaN) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is positive or negative infinity.\n *\n * @param value Value to test.\n * @returns `true` when the value is not finite.\n *\n * @example\n * ```ts\n * isInfinite(Infinity); // true\n * ```\n *\n * @example\n * ```ts\n * isInfinite(10); // false\n * ```\n */\nexport const isInfinite = createTypeGuard<number>(TYPE.Infinite) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is a symbol.\n *\n * @param value Value to test.\n * @returns `true` when the value is a symbol.\n *\n * @example\n * ```ts\n * isSymbol(Symbol('id')); // true\n * ```\n *\n * @example\n * ```ts\n * isSymbol('id'); // false\n * ```\n */\nexport const isSymbol = createTypeGuard<symbol>(TYPE.Symbol);\n"],"
|
|
1
|
+
{"version":3,"file":"type-guard.cjs","names":[],"sources":["../src/type-guard/_exports.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/type-guard\n *\n * ## Description\n *\n * Runtime **type guards** and narrowers for primitives, DOM nodes, `NaN`, and common app values.\n * Each helper is implemented once with consistent `typeof` / `Object.prototype.toString` logic so\n * `unknown` from JSON, events, or third-party scripts becomes safe typed branches without ad-hoc\n * checks scattered through the codebase.\n *\n * ## Usage\n *\n * ```ts\n * import { typeGuard } from \"yummies/type-guard\";\n * ```\n */\n\nimport type { AnyFunction, AnyObject, ValueOf } from 'yummies/types';\n\nconst TYPE = {\n Null: 'null',\n Undefined: 'undefined',\n NaN: 'nan',\n Object: '[object Object]',\n Array: '[object Array]',\n String: '[object String]',\n Number: '[object Number]',\n Boolean: '[object Boolean]',\n Function: '[object Function]',\n AsyncFunction: '[object AsyncFunction]',\n RegExp: '[object RegExp]',\n Symbol: '[object Symbol]',\n Infinite: 'infinite',\n Element: 'element',\n};\n\ntype Type = ValueOf<typeof TYPE>;\n\nfunction getType(value: unknown): Type {\n if (value === undefined) {\n return TYPE.Undefined;\n }\n if (value === null) {\n return TYPE.Null;\n }\n\n // handle DOM elements\n // @ts-expect-error\n if (value && (value.nodeType === 1 || value.nodeType === 9)) {\n return TYPE.Element;\n }\n\n const stringifiedValue = Object.prototype.toString.call(value);\n\n // handle NaN and Infinity\n if (stringifiedValue === TYPE.Number) {\n if (Number.isNaN(value as number)) {\n return TYPE.NaN;\n }\n if (!Number.isFinite(value as number)) {\n return TYPE.Infinite;\n }\n }\n\n return stringifiedValue as Type;\n}\n\nconst createTypeGuard =\n <T>(...types: Type[]) =>\n (value: unknown): value is T =>\n types.includes(getType(value));\n\n/**\n * Checks that a value is neither `null` nor `undefined`.\n *\n * @template T Value type without nullish branches.\n * @param value Value to test.\n * @returns `true` when the value is defined.\n *\n * @example\n * ```ts\n * isDefined(0); // true\n * ```\n *\n * @example\n * ```ts\n * isDefined(null); // false\n * ```\n */\nexport const isDefined = <T>(value: T | undefined | null): value is T =>\n value != null;\n\n/**\n * Checks whether a value is exactly `null`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `null`.\n *\n * @example\n * ```ts\n * isNull(null); // true\n * ```\n *\n * @example\n * ```ts\n * isNull(undefined); // false\n * ```\n */\nexport const isNull = createTypeGuard<null>(TYPE.Null);\n\n/**\n * Checks whether a value is exactly `undefined`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `undefined`.\n *\n * @example\n * ```ts\n * isUndefined(undefined); // true\n * ```\n *\n * @example\n * ```ts\n * isUndefined('value'); // false\n * ```\n */\nexport const isUndefined = createTypeGuard<undefined>(TYPE.Undefined);\n\n/**\n * Checks whether a value is a plain object.\n *\n * @param value Value to test.\n * @returns `true` when the value matches `[object Object]`.\n *\n * @example\n * ```ts\n * isObject({ id: 1 }); // true\n * ```\n *\n * @example\n * ```ts\n * isObject([]); // false\n * ```\n */\nexport const isObject = createTypeGuard<AnyObject>(TYPE.Object);\n\n/**\n * Checks whether a value is an array.\n *\n * @param value Value to test.\n * @returns `true` when the value is an array.\n *\n * @example\n * ```ts\n * isArray([1, 2, 3]); // true\n * ```\n *\n * @example\n * ```ts\n * isArray({ length: 1 }); // false\n * ```\n */\nexport const isArray = createTypeGuard<unknown[]>(TYPE.Array);\n\n/**\n * Checks whether a value is a string object or primitive string.\n *\n * @param value Value to test.\n * @returns `true` when the value is a string.\n *\n * @example\n * ```ts\n * isString('hello'); // true\n * ```\n *\n * @example\n * ```ts\n * isString(123); // false\n * ```\n */\nexport const isString = createTypeGuard<string>(TYPE.String);\n\n/**\n * Checks whether a value is a finite number.\n *\n * Unlike `isNaN` and `isInfinite`, this guard only matches regular numeric values.\n *\n * @param value Value to test.\n * @returns `true` when the value is a non-NaN finite number.\n *\n * @example\n * ```ts\n * isNumber(123); // true\n * ```\n *\n * @example\n * ```ts\n * isNumber(Number.NaN); // false\n * ```\n */\nexport const isNumber = createTypeGuard<number>(TYPE.Number);\n\n/**\n * Checks whether a value is a boolean.\n *\n * @param value Value to test.\n * @returns `true` when the value is a boolean.\n *\n * @example\n * ```ts\n * isBoolean(true); // true\n * ```\n *\n * @example\n * ```ts\n * isBoolean('true'); // false\n * ```\n */\nexport const isBoolean = createTypeGuard<boolean>(TYPE.Boolean);\n\n/**\n * Checks whether a value is a synchronous or asynchronous function.\n *\n * @param value Value to test.\n * @returns `true` when the value is a function.\n *\n * @example\n * ```ts\n * isFunction(() => {}); // true\n * ```\n *\n * @example\n * ```ts\n * isFunction(async () => {}); // true\n * ```\n */\nexport const isFunction = createTypeGuard<AnyFunction>(\n TYPE.Function,\n TYPE.AsyncFunction,\n);\n\n/**\n * Checks whether a value is a regular expression.\n *\n * @param value Value to test.\n * @returns `true` when the value is a `RegExp`.\n *\n * @example\n * ```ts\n * isRegExp(/foo/); // true\n * ```\n *\n * @example\n * ```ts\n * isRegExp('foo'); // false\n * ```\n */\nexport const isRegExp = createTypeGuard<RegExp>(TYPE.RegExp);\n\n/**\n * Checks whether a value looks like a DOM element or document node.\n *\n * @param value Value to test.\n * @returns `true` when the value has an element-like node type.\n *\n * @example\n * ```ts\n * isElement(document.body); // true\n * ```\n *\n * @example\n * ```ts\n * isElement({ nodeType: 3 }); // false\n * ```\n */\nexport const isElement = createTypeGuard<HTMLElement>(TYPE.Element);\n\n/**\n * Checks whether a value is `NaN`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `NaN`.\n *\n * @example\n * ```ts\n * isNaN(Number.NaN); // true\n * ```\n *\n * @example\n * ```ts\n * isNaN(5); // false\n * ```\n */\nexport const isNaN = createTypeGuard<number>(TYPE.NaN) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is positive or negative infinity.\n *\n * @param value Value to test.\n * @returns `true` when the value is not finite.\n *\n * @example\n * ```ts\n * isInfinite(Infinity); // true\n * ```\n *\n * @example\n * ```ts\n * isInfinite(10); // false\n * ```\n */\nexport const isInfinite = createTypeGuard<number>(TYPE.Infinite) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is a symbol.\n *\n * @param value Value to test.\n * @returns `true` when the value is a symbol.\n *\n * @example\n * ```ts\n * isSymbol(Symbol('id')); // true\n * ```\n *\n * @example\n * ```ts\n * isSymbol('id'); // false\n * ```\n */\nexport const isSymbol = createTypeGuard<symbol>(TYPE.Symbol);\n"],"mappings":";;;;;;;;;;;;;;;;;;AAoBA,IAAM,OAAO;CACX,MAAM;CACN,WAAW;CACX,KAAK;CACL,QAAQ;CACR,OAAO;CACP,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,UAAU;CACV,eAAe;CACf,QAAQ;CACR,QAAQ;CACR,UAAU;CACV,SAAS;CACV;AAID,SAAS,QAAQ,OAAsB;AACrC,KAAI,UAAU,KAAA,EACZ,QAAO,KAAK;AAEd,KAAI,UAAU,KACZ,QAAO,KAAK;AAKd,KAAI,UAAU,MAAM,aAAa,KAAK,MAAM,aAAa,GACvD,QAAO,KAAK;CAGd,MAAM,mBAAmB,OAAO,UAAU,SAAS,KAAK,MAAM;AAG9D,KAAI,qBAAqB,KAAK,QAAQ;AACpC,MAAI,OAAO,MAAM,MAAgB,CAC/B,QAAO,KAAK;AAEd,MAAI,CAAC,OAAO,SAAS,MAAgB,CACnC,QAAO,KAAK;;AAIhB,QAAO;;AAGT,IAAM,mBACA,GAAG,WACN,UACC,MAAM,SAAS,QAAQ,MAAM,CAAC;;;;;;;;;;;;;;;;;;AAmBlC,IAAa,aAAgB,UAC3B,SAAS;;;;;;;;;;;;;;;;;AAkBX,IAAa,SAAS,gBAAsB,KAAK,KAAK;;;;;;;;;;;;;;;;;AAkBtD,IAAa,cAAc,gBAA2B,KAAK,UAAU;;;;;;;;;;;;;;;;;AAkBrE,IAAa,WAAW,gBAA2B,KAAK,OAAO;;;;;;;;;;;;;;;;;AAkB/D,IAAa,UAAU,gBAA2B,KAAK,MAAM;;;;;;;;;;;;;;;;;AAkB7D,IAAa,WAAW,gBAAwB,KAAK,OAAO;;;;;;;;;;;;;;;;;;;AAoB5D,IAAa,WAAW,gBAAwB,KAAK,OAAO;;;;;;;;;;;;;;;;;AAkB5D,IAAa,YAAY,gBAAyB,KAAK,QAAQ;;;;;;;;;;;;;;;;;AAkB/D,IAAa,aAAa,gBACxB,KAAK,UACL,KAAK,cACN;;;;;;;;;;;;;;;;;AAkBD,IAAa,WAAW,gBAAwB,KAAK,OAAO;;;;;;;;;;;;;;;;;AAkB5D,IAAa,YAAY,gBAA6B,KAAK,QAAQ;;;;;;;;;;;;;;;;;AAkBnE,IAAa,QAAQ,gBAAwB,KAAK,IAAI;;;;;;;;;;;;;;;;;AAoBtD,IAAa,aAAa,gBAAwB,KAAK,SAAS;;;;;;;;;;;;;;;;;AAoBhE,IAAa,WAAW,gBAAwB,KAAK,OAAO"}
|
package/type-guard.d.ts
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
import { AnyFunction, AnyObject } from 'yummies/types';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* ---header-docs-section---
|
|
5
|
+
* # yummies/type-guard
|
|
6
|
+
*
|
|
7
|
+
* ## Description
|
|
8
|
+
*
|
|
9
|
+
* Runtime **type guards** and narrowers for primitives, DOM nodes, `NaN`, and common app values.
|
|
10
|
+
* Each helper is implemented once with consistent `typeof` / `Object.prototype.toString` logic so
|
|
11
|
+
* `unknown` from JSON, events, or third-party scripts becomes safe typed branches without ad-hoc
|
|
12
|
+
* checks scattered through the codebase.
|
|
13
|
+
*
|
|
14
|
+
* ## Usage
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { typeGuard } from "yummies/type-guard";
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
|
|
3
21
|
/**
|
|
4
22
|
* Checks that a value is neither `null` nor `undefined`.
|
|
5
23
|
*
|
package/type-guard.js
CHANGED
|
@@ -1,76 +1,291 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import { n as __exportAll } from "./chunk-YKewjYmz.js";
|
|
2
|
+
//#region src/type-guard/_exports.ts
|
|
3
|
+
var _exports_exports = /* @__PURE__ */ __exportAll({
|
|
4
|
+
isArray: () => isArray,
|
|
5
|
+
isBoolean: () => isBoolean,
|
|
6
|
+
isDefined: () => isDefined,
|
|
7
|
+
isElement: () => isElement,
|
|
8
|
+
isFunction: () => isFunction,
|
|
9
|
+
isInfinite: () => isInfinite,
|
|
10
|
+
isNaN: () => isNaN,
|
|
11
|
+
isNull: () => isNull,
|
|
12
|
+
isNumber: () => isNumber,
|
|
13
|
+
isObject: () => isObject,
|
|
14
|
+
isRegExp: () => isRegExp,
|
|
15
|
+
isString: () => isString,
|
|
16
|
+
isSymbol: () => isSymbol,
|
|
17
|
+
isUndefined: () => isUndefined
|
|
18
|
+
});
|
|
19
|
+
var TYPE = {
|
|
20
|
+
Null: "null",
|
|
21
|
+
Undefined: "undefined",
|
|
22
|
+
NaN: "nan",
|
|
23
|
+
Object: "[object Object]",
|
|
24
|
+
Array: "[object Array]",
|
|
25
|
+
String: "[object String]",
|
|
26
|
+
Number: "[object Number]",
|
|
27
|
+
Boolean: "[object Boolean]",
|
|
28
|
+
Function: "[object Function]",
|
|
29
|
+
AsyncFunction: "[object AsyncFunction]",
|
|
30
|
+
RegExp: "[object RegExp]",
|
|
31
|
+
Symbol: "[object Symbol]",
|
|
32
|
+
Infinite: "infinite",
|
|
33
|
+
Element: "element"
|
|
16
34
|
};
|
|
17
35
|
function getType(value) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const stringifiedValue = Object.prototype.toString.call(value);
|
|
28
|
-
if (stringifiedValue === TYPE.Number) {
|
|
29
|
-
if (Number.isNaN(value)) {
|
|
30
|
-
return TYPE.NaN;
|
|
31
|
-
}
|
|
32
|
-
if (!Number.isFinite(value)) {
|
|
33
|
-
return TYPE.Infinite;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return stringifiedValue;
|
|
36
|
+
if (value === void 0) return TYPE.Undefined;
|
|
37
|
+
if (value === null) return TYPE.Null;
|
|
38
|
+
if (value && (value.nodeType === 1 || value.nodeType === 9)) return TYPE.Element;
|
|
39
|
+
const stringifiedValue = Object.prototype.toString.call(value);
|
|
40
|
+
if (stringifiedValue === TYPE.Number) {
|
|
41
|
+
if (Number.isNaN(value)) return TYPE.NaN;
|
|
42
|
+
if (!Number.isFinite(value)) return TYPE.Infinite;
|
|
43
|
+
}
|
|
44
|
+
return stringifiedValue;
|
|
37
45
|
}
|
|
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
|
-
|
|
46
|
+
var createTypeGuard = (...types) => (value) => types.includes(getType(value));
|
|
47
|
+
/**
|
|
48
|
+
* Checks that a value is neither `null` nor `undefined`.
|
|
49
|
+
*
|
|
50
|
+
* @template T Value type without nullish branches.
|
|
51
|
+
* @param value Value to test.
|
|
52
|
+
* @returns `true` when the value is defined.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* isDefined(0); // true
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* isDefined(null); // false
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
var isDefined = (value) => value != null;
|
|
65
|
+
/**
|
|
66
|
+
* Checks whether a value is exactly `null`.
|
|
67
|
+
*
|
|
68
|
+
* @param value Value to test.
|
|
69
|
+
* @returns `true` when the value is `null`.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* isNull(null); // true
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* isNull(undefined); // false
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
var isNull = createTypeGuard(TYPE.Null);
|
|
82
|
+
/**
|
|
83
|
+
* Checks whether a value is exactly `undefined`.
|
|
84
|
+
*
|
|
85
|
+
* @param value Value to test.
|
|
86
|
+
* @returns `true` when the value is `undefined`.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* isUndefined(undefined); // true
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* isUndefined('value'); // false
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
var isUndefined = createTypeGuard(TYPE.Undefined);
|
|
99
|
+
/**
|
|
100
|
+
* Checks whether a value is a plain object.
|
|
101
|
+
*
|
|
102
|
+
* @param value Value to test.
|
|
103
|
+
* @returns `true` when the value matches `[object Object]`.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* isObject({ id: 1 }); // true
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* isObject([]); // false
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
var isObject = createTypeGuard(TYPE.Object);
|
|
116
|
+
/**
|
|
117
|
+
* Checks whether a value is an array.
|
|
118
|
+
*
|
|
119
|
+
* @param value Value to test.
|
|
120
|
+
* @returns `true` when the value is an array.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* isArray([1, 2, 3]); // true
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* isArray({ length: 1 }); // false
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
var isArray = createTypeGuard(TYPE.Array);
|
|
133
|
+
/**
|
|
134
|
+
* Checks whether a value is a string object or primitive string.
|
|
135
|
+
*
|
|
136
|
+
* @param value Value to test.
|
|
137
|
+
* @returns `true` when the value is a string.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* isString('hello'); // true
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* isString(123); // false
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
var isString = createTypeGuard(TYPE.String);
|
|
150
|
+
/**
|
|
151
|
+
* Checks whether a value is a finite number.
|
|
152
|
+
*
|
|
153
|
+
* Unlike `isNaN` and `isInfinite`, this guard only matches regular numeric values.
|
|
154
|
+
*
|
|
155
|
+
* @param value Value to test.
|
|
156
|
+
* @returns `true` when the value is a non-NaN finite number.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* isNumber(123); // true
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* isNumber(Number.NaN); // false
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
var isNumber = createTypeGuard(TYPE.Number);
|
|
169
|
+
/**
|
|
170
|
+
* Checks whether a value is a boolean.
|
|
171
|
+
*
|
|
172
|
+
* @param value Value to test.
|
|
173
|
+
* @returns `true` when the value is a boolean.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* isBoolean(true); // true
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* isBoolean('true'); // false
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
var isBoolean = createTypeGuard(TYPE.Boolean);
|
|
186
|
+
/**
|
|
187
|
+
* Checks whether a value is a synchronous or asynchronous function.
|
|
188
|
+
*
|
|
189
|
+
* @param value Value to test.
|
|
190
|
+
* @returns `true` when the value is a function.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* isFunction(() => {}); // true
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* isFunction(async () => {}); // true
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
var isFunction = createTypeGuard(TYPE.Function, TYPE.AsyncFunction);
|
|
203
|
+
/**
|
|
204
|
+
* Checks whether a value is a regular expression.
|
|
205
|
+
*
|
|
206
|
+
* @param value Value to test.
|
|
207
|
+
* @returns `true` when the value is a `RegExp`.
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* ```ts
|
|
211
|
+
* isRegExp(/foo/); // true
|
|
212
|
+
* ```
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* isRegExp('foo'); // false
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
var isRegExp = createTypeGuard(TYPE.RegExp);
|
|
220
|
+
/**
|
|
221
|
+
* Checks whether a value looks like a DOM element or document node.
|
|
222
|
+
*
|
|
223
|
+
* @param value Value to test.
|
|
224
|
+
* @returns `true` when the value has an element-like node type.
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* isElement(document.body); // true
|
|
229
|
+
* ```
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```ts
|
|
233
|
+
* isElement({ nodeType: 3 }); // false
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
var isElement = createTypeGuard(TYPE.Element);
|
|
237
|
+
/**
|
|
238
|
+
* Checks whether a value is `NaN`.
|
|
239
|
+
*
|
|
240
|
+
* @param value Value to test.
|
|
241
|
+
* @returns `true` when the value is `NaN`.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* isNaN(Number.NaN); // true
|
|
246
|
+
* ```
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```ts
|
|
250
|
+
* isNaN(5); // false
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
var isNaN = createTypeGuard(TYPE.NaN);
|
|
254
|
+
/**
|
|
255
|
+
* Checks whether a value is positive or negative infinity.
|
|
256
|
+
*
|
|
257
|
+
* @param value Value to test.
|
|
258
|
+
* @returns `true` when the value is not finite.
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* ```ts
|
|
262
|
+
* isInfinite(Infinity); // true
|
|
263
|
+
* ```
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```ts
|
|
267
|
+
* isInfinite(10); // false
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
var isInfinite = createTypeGuard(TYPE.Infinite);
|
|
271
|
+
/**
|
|
272
|
+
* Checks whether a value is a symbol.
|
|
273
|
+
*
|
|
274
|
+
* @param value Value to test.
|
|
275
|
+
* @returns `true` when the value is a symbol.
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* ```ts
|
|
279
|
+
* isSymbol(Symbol('id')); // true
|
|
280
|
+
* ```
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```ts
|
|
284
|
+
* isSymbol('id'); // false
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
var isSymbol = createTypeGuard(TYPE.Symbol);
|
|
288
|
+
//#endregion
|
|
289
|
+
export { _exports_exports as typeGuard };
|
|
290
|
+
|
|
291
|
+
//# sourceMappingURL=type-guard.js.map
|
package/type-guard.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-guard.js","sources":["../src/type-guard/_exports.ts"],"sourcesContent":["import type { AnyFunction, AnyObject, ValueOf } from 'yummies/types';\n\nconst TYPE = {\n Null: 'null',\n Undefined: 'undefined',\n NaN: 'nan',\n Object: '[object Object]',\n Array: '[object Array]',\n String: '[object String]',\n Number: '[object Number]',\n Boolean: '[object Boolean]',\n Function: '[object Function]',\n AsyncFunction: '[object AsyncFunction]',\n RegExp: '[object RegExp]',\n Symbol: '[object Symbol]',\n Infinite: 'infinite',\n Element: 'element',\n};\n\ntype Type = ValueOf<typeof TYPE>;\n\nfunction getType(value: unknown): Type {\n if (value === undefined) {\n return TYPE.Undefined;\n }\n if (value === null) {\n return TYPE.Null;\n }\n\n // handle DOM elements\n // @ts-expect-error\n if (value && (value.nodeType === 1 || value.nodeType === 9)) {\n return TYPE.Element;\n }\n\n const stringifiedValue = Object.prototype.toString.call(value);\n\n // handle NaN and Infinity\n if (stringifiedValue === TYPE.Number) {\n if (Number.isNaN(value as number)) {\n return TYPE.NaN;\n }\n if (!Number.isFinite(value as number)) {\n return TYPE.Infinite;\n }\n }\n\n return stringifiedValue as Type;\n}\n\nconst createTypeGuard =\n <T>(...types: Type[]) =>\n (value: unknown): value is T =>\n types.includes(getType(value));\n\n/**\n * Checks that a value is neither `null` nor `undefined`.\n *\n * @template T Value type without nullish branches.\n * @param value Value to test.\n * @returns `true` when the value is defined.\n *\n * @example\n * ```ts\n * isDefined(0); // true\n * ```\n *\n * @example\n * ```ts\n * isDefined(null); // false\n * ```\n */\nexport const isDefined = <T>(value: T | undefined | null): value is T =>\n value != null;\n\n/**\n * Checks whether a value is exactly `null`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `null`.\n *\n * @example\n * ```ts\n * isNull(null); // true\n * ```\n *\n * @example\n * ```ts\n * isNull(undefined); // false\n * ```\n */\nexport const isNull = createTypeGuard<null>(TYPE.Null);\n\n/**\n * Checks whether a value is exactly `undefined`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `undefined`.\n *\n * @example\n * ```ts\n * isUndefined(undefined); // true\n * ```\n *\n * @example\n * ```ts\n * isUndefined('value'); // false\n * ```\n */\nexport const isUndefined = createTypeGuard<undefined>(TYPE.Undefined);\n\n/**\n * Checks whether a value is a plain object.\n *\n * @param value Value to test.\n * @returns `true` when the value matches `[object Object]`.\n *\n * @example\n * ```ts\n * isObject({ id: 1 }); // true\n * ```\n *\n * @example\n * ```ts\n * isObject([]); // false\n * ```\n */\nexport const isObject = createTypeGuard<AnyObject>(TYPE.Object);\n\n/**\n * Checks whether a value is an array.\n *\n * @param value Value to test.\n * @returns `true` when the value is an array.\n *\n * @example\n * ```ts\n * isArray([1, 2, 3]); // true\n * ```\n *\n * @example\n * ```ts\n * isArray({ length: 1 }); // false\n * ```\n */\nexport const isArray = createTypeGuard<unknown[]>(TYPE.Array);\n\n/**\n * Checks whether a value is a string object or primitive string.\n *\n * @param value Value to test.\n * @returns `true` when the value is a string.\n *\n * @example\n * ```ts\n * isString('hello'); // true\n * ```\n *\n * @example\n * ```ts\n * isString(123); // false\n * ```\n */\nexport const isString = createTypeGuard<string>(TYPE.String);\n\n/**\n * Checks whether a value is a finite number.\n *\n * Unlike `isNaN` and `isInfinite`, this guard only matches regular numeric values.\n *\n * @param value Value to test.\n * @returns `true` when the value is a non-NaN finite number.\n *\n * @example\n * ```ts\n * isNumber(123); // true\n * ```\n *\n * @example\n * ```ts\n * isNumber(Number.NaN); // false\n * ```\n */\nexport const isNumber = createTypeGuard<number>(TYPE.Number);\n\n/**\n * Checks whether a value is a boolean.\n *\n * @param value Value to test.\n * @returns `true` when the value is a boolean.\n *\n * @example\n * ```ts\n * isBoolean(true); // true\n * ```\n *\n * @example\n * ```ts\n * isBoolean('true'); // false\n * ```\n */\nexport const isBoolean = createTypeGuard<boolean>(TYPE.Boolean);\n\n/**\n * Checks whether a value is a synchronous or asynchronous function.\n *\n * @param value Value to test.\n * @returns `true` when the value is a function.\n *\n * @example\n * ```ts\n * isFunction(() => {}); // true\n * ```\n *\n * @example\n * ```ts\n * isFunction(async () => {}); // true\n * ```\n */\nexport const isFunction = createTypeGuard<AnyFunction>(\n TYPE.Function,\n TYPE.AsyncFunction,\n);\n\n/**\n * Checks whether a value is a regular expression.\n *\n * @param value Value to test.\n * @returns `true` when the value is a `RegExp`.\n *\n * @example\n * ```ts\n * isRegExp(/foo/); // true\n * ```\n *\n * @example\n * ```ts\n * isRegExp('foo'); // false\n * ```\n */\nexport const isRegExp = createTypeGuard<RegExp>(TYPE.RegExp);\n\n/**\n * Checks whether a value looks like a DOM element or document node.\n *\n * @param value Value to test.\n * @returns `true` when the value has an element-like node type.\n *\n * @example\n * ```ts\n * isElement(document.body); // true\n * ```\n *\n * @example\n * ```ts\n * isElement({ nodeType: 3 }); // false\n * ```\n */\nexport const isElement = createTypeGuard<HTMLElement>(TYPE.Element);\n\n/**\n * Checks whether a value is `NaN`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `NaN`.\n *\n * @example\n * ```ts\n * isNaN(Number.NaN); // true\n * ```\n *\n * @example\n * ```ts\n * isNaN(5); // false\n * ```\n */\nexport const isNaN = createTypeGuard<number>(TYPE.NaN) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is positive or negative infinity.\n *\n * @param value Value to test.\n * @returns `true` when the value is not finite.\n *\n * @example\n * ```ts\n * isInfinite(Infinity); // true\n * ```\n *\n * @example\n * ```ts\n * isInfinite(10); // false\n * ```\n */\nexport const isInfinite = createTypeGuard<number>(TYPE.Infinite) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is a symbol.\n *\n * @param value Value to test.\n * @returns `true` when the value is a symbol.\n *\n * @example\n * ```ts\n * isSymbol(Symbol('id')); // true\n * ```\n *\n * @example\n * ```ts\n * isSymbol('id'); // false\n * ```\n */\nexport const isSymbol = createTypeGuard<symbol>(TYPE.Symbol);\n"],"
|
|
1
|
+
{"version":3,"file":"type-guard.js","names":[],"sources":["../src/type-guard/_exports.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/type-guard\n *\n * ## Description\n *\n * Runtime **type guards** and narrowers for primitives, DOM nodes, `NaN`, and common app values.\n * Each helper is implemented once with consistent `typeof` / `Object.prototype.toString` logic so\n * `unknown` from JSON, events, or third-party scripts becomes safe typed branches without ad-hoc\n * checks scattered through the codebase.\n *\n * ## Usage\n *\n * ```ts\n * import { typeGuard } from \"yummies/type-guard\";\n * ```\n */\n\nimport type { AnyFunction, AnyObject, ValueOf } from 'yummies/types';\n\nconst TYPE = {\n Null: 'null',\n Undefined: 'undefined',\n NaN: 'nan',\n Object: '[object Object]',\n Array: '[object Array]',\n String: '[object String]',\n Number: '[object Number]',\n Boolean: '[object Boolean]',\n Function: '[object Function]',\n AsyncFunction: '[object AsyncFunction]',\n RegExp: '[object RegExp]',\n Symbol: '[object Symbol]',\n Infinite: 'infinite',\n Element: 'element',\n};\n\ntype Type = ValueOf<typeof TYPE>;\n\nfunction getType(value: unknown): Type {\n if (value === undefined) {\n return TYPE.Undefined;\n }\n if (value === null) {\n return TYPE.Null;\n }\n\n // handle DOM elements\n // @ts-expect-error\n if (value && (value.nodeType === 1 || value.nodeType === 9)) {\n return TYPE.Element;\n }\n\n const stringifiedValue = Object.prototype.toString.call(value);\n\n // handle NaN and Infinity\n if (stringifiedValue === TYPE.Number) {\n if (Number.isNaN(value as number)) {\n return TYPE.NaN;\n }\n if (!Number.isFinite(value as number)) {\n return TYPE.Infinite;\n }\n }\n\n return stringifiedValue as Type;\n}\n\nconst createTypeGuard =\n <T>(...types: Type[]) =>\n (value: unknown): value is T =>\n types.includes(getType(value));\n\n/**\n * Checks that a value is neither `null` nor `undefined`.\n *\n * @template T Value type without nullish branches.\n * @param value Value to test.\n * @returns `true` when the value is defined.\n *\n * @example\n * ```ts\n * isDefined(0); // true\n * ```\n *\n * @example\n * ```ts\n * isDefined(null); // false\n * ```\n */\nexport const isDefined = <T>(value: T | undefined | null): value is T =>\n value != null;\n\n/**\n * Checks whether a value is exactly `null`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `null`.\n *\n * @example\n * ```ts\n * isNull(null); // true\n * ```\n *\n * @example\n * ```ts\n * isNull(undefined); // false\n * ```\n */\nexport const isNull = createTypeGuard<null>(TYPE.Null);\n\n/**\n * Checks whether a value is exactly `undefined`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `undefined`.\n *\n * @example\n * ```ts\n * isUndefined(undefined); // true\n * ```\n *\n * @example\n * ```ts\n * isUndefined('value'); // false\n * ```\n */\nexport const isUndefined = createTypeGuard<undefined>(TYPE.Undefined);\n\n/**\n * Checks whether a value is a plain object.\n *\n * @param value Value to test.\n * @returns `true` when the value matches `[object Object]`.\n *\n * @example\n * ```ts\n * isObject({ id: 1 }); // true\n * ```\n *\n * @example\n * ```ts\n * isObject([]); // false\n * ```\n */\nexport const isObject = createTypeGuard<AnyObject>(TYPE.Object);\n\n/**\n * Checks whether a value is an array.\n *\n * @param value Value to test.\n * @returns `true` when the value is an array.\n *\n * @example\n * ```ts\n * isArray([1, 2, 3]); // true\n * ```\n *\n * @example\n * ```ts\n * isArray({ length: 1 }); // false\n * ```\n */\nexport const isArray = createTypeGuard<unknown[]>(TYPE.Array);\n\n/**\n * Checks whether a value is a string object or primitive string.\n *\n * @param value Value to test.\n * @returns `true` when the value is a string.\n *\n * @example\n * ```ts\n * isString('hello'); // true\n * ```\n *\n * @example\n * ```ts\n * isString(123); // false\n * ```\n */\nexport const isString = createTypeGuard<string>(TYPE.String);\n\n/**\n * Checks whether a value is a finite number.\n *\n * Unlike `isNaN` and `isInfinite`, this guard only matches regular numeric values.\n *\n * @param value Value to test.\n * @returns `true` when the value is a non-NaN finite number.\n *\n * @example\n * ```ts\n * isNumber(123); // true\n * ```\n *\n * @example\n * ```ts\n * isNumber(Number.NaN); // false\n * ```\n */\nexport const isNumber = createTypeGuard<number>(TYPE.Number);\n\n/**\n * Checks whether a value is a boolean.\n *\n * @param value Value to test.\n * @returns `true` when the value is a boolean.\n *\n * @example\n * ```ts\n * isBoolean(true); // true\n * ```\n *\n * @example\n * ```ts\n * isBoolean('true'); // false\n * ```\n */\nexport const isBoolean = createTypeGuard<boolean>(TYPE.Boolean);\n\n/**\n * Checks whether a value is a synchronous or asynchronous function.\n *\n * @param value Value to test.\n * @returns `true` when the value is a function.\n *\n * @example\n * ```ts\n * isFunction(() => {}); // true\n * ```\n *\n * @example\n * ```ts\n * isFunction(async () => {}); // true\n * ```\n */\nexport const isFunction = createTypeGuard<AnyFunction>(\n TYPE.Function,\n TYPE.AsyncFunction,\n);\n\n/**\n * Checks whether a value is a regular expression.\n *\n * @param value Value to test.\n * @returns `true` when the value is a `RegExp`.\n *\n * @example\n * ```ts\n * isRegExp(/foo/); // true\n * ```\n *\n * @example\n * ```ts\n * isRegExp('foo'); // false\n * ```\n */\nexport const isRegExp = createTypeGuard<RegExp>(TYPE.RegExp);\n\n/**\n * Checks whether a value looks like a DOM element or document node.\n *\n * @param value Value to test.\n * @returns `true` when the value has an element-like node type.\n *\n * @example\n * ```ts\n * isElement(document.body); // true\n * ```\n *\n * @example\n * ```ts\n * isElement({ nodeType: 3 }); // false\n * ```\n */\nexport const isElement = createTypeGuard<HTMLElement>(TYPE.Element);\n\n/**\n * Checks whether a value is `NaN`.\n *\n * @param value Value to test.\n * @returns `true` when the value is `NaN`.\n *\n * @example\n * ```ts\n * isNaN(Number.NaN); // true\n * ```\n *\n * @example\n * ```ts\n * isNaN(5); // false\n * ```\n */\nexport const isNaN = createTypeGuard<number>(TYPE.NaN) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is positive or negative infinity.\n *\n * @param value Value to test.\n * @returns `true` when the value is not finite.\n *\n * @example\n * ```ts\n * isInfinite(Infinity); // true\n * ```\n *\n * @example\n * ```ts\n * isInfinite(10); // false\n * ```\n */\nexport const isInfinite = createTypeGuard<number>(TYPE.Infinite) as (\n value: unknown,\n) => boolean;\n\n/**\n * Checks whether a value is a symbol.\n *\n * @param value Value to test.\n * @returns `true` when the value is a symbol.\n *\n * @example\n * ```ts\n * isSymbol(Symbol('id')); // true\n * ```\n *\n * @example\n * ```ts\n * isSymbol('id'); // false\n * ```\n */\nexport const isSymbol = createTypeGuard<symbol>(TYPE.Symbol);\n"],"mappings":";;;;;;;;;;;;;;;;;;AAoBA,IAAM,OAAO;CACX,MAAM;CACN,WAAW;CACX,KAAK;CACL,QAAQ;CACR,OAAO;CACP,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,UAAU;CACV,eAAe;CACf,QAAQ;CACR,QAAQ;CACR,UAAU;CACV,SAAS;CACV;AAID,SAAS,QAAQ,OAAsB;AACrC,KAAI,UAAU,KAAA,EACZ,QAAO,KAAK;AAEd,KAAI,UAAU,KACZ,QAAO,KAAK;AAKd,KAAI,UAAU,MAAM,aAAa,KAAK,MAAM,aAAa,GACvD,QAAO,KAAK;CAGd,MAAM,mBAAmB,OAAO,UAAU,SAAS,KAAK,MAAM;AAG9D,KAAI,qBAAqB,KAAK,QAAQ;AACpC,MAAI,OAAO,MAAM,MAAgB,CAC/B,QAAO,KAAK;AAEd,MAAI,CAAC,OAAO,SAAS,MAAgB,CACnC,QAAO,KAAK;;AAIhB,QAAO;;AAGT,IAAM,mBACA,GAAG,WACN,UACC,MAAM,SAAS,QAAQ,MAAM,CAAC;;;;;;;;;;;;;;;;;;AAmBlC,IAAa,aAAgB,UAC3B,SAAS;;;;;;;;;;;;;;;;;AAkBX,IAAa,SAAS,gBAAsB,KAAK,KAAK;;;;;;;;;;;;;;;;;AAkBtD,IAAa,cAAc,gBAA2B,KAAK,UAAU;;;;;;;;;;;;;;;;;AAkBrE,IAAa,WAAW,gBAA2B,KAAK,OAAO;;;;;;;;;;;;;;;;;AAkB/D,IAAa,UAAU,gBAA2B,KAAK,MAAM;;;;;;;;;;;;;;;;;AAkB7D,IAAa,WAAW,gBAAwB,KAAK,OAAO;;;;;;;;;;;;;;;;;;;AAoB5D,IAAa,WAAW,gBAAwB,KAAK,OAAO;;;;;;;;;;;;;;;;;AAkB5D,IAAa,YAAY,gBAAyB,KAAK,QAAQ;;;;;;;;;;;;;;;;;AAkB/D,IAAa,aAAa,gBACxB,KAAK,UACL,KAAK,cACN;;;;;;;;;;;;;;;;;AAkBD,IAAa,WAAW,gBAAwB,KAAK,OAAO;;;;;;;;;;;;;;;;;AAkB5D,IAAa,YAAY,gBAA6B,KAAK,QAAQ;;;;;;;;;;;;;;;;;AAkBnE,IAAa,QAAQ,gBAAwB,KAAK,IAAI;;;;;;;;;;;;;;;;;AAoBtD,IAAa,aAAa,gBAAwB,KAAK,SAAS;;;;;;;;;;;;;;;;;AAoBhE,IAAa,WAAW,gBAAwB,KAAK,OAAO"}
|
package/types.cjs
CHANGED
package/types.d.ts
CHANGED
|
@@ -1,3 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ---header-docs-section---
|
|
3
|
+
* # yummies/types
|
|
4
|
+
*
|
|
5
|
+
* ## Description
|
|
6
|
+
*
|
|
7
|
+
* This module collects **TypeScript-only** helpers: `ValueOf`, nullable aliases, branded types, and
|
|
8
|
+
* patterns for props and generics. Nothing here exists at runtime — use `import type` so bundlers
|
|
9
|
+
* drop the imports entirely. The same ideas are also published as **ambient** globals under a
|
|
10
|
+
* separate entry (see below) when you want types in scope without repeating imports in every file.
|
|
11
|
+
*
|
|
12
|
+
* ## `yummies/types.global`
|
|
13
|
+
*
|
|
14
|
+
* Use this subpath when you prefer **global** declarations (no per-file imports). After installing
|
|
15
|
+
* the package, reference the built `.d.ts` bundle — typically via a side-effect import that pulls
|
|
16
|
+
* ambient types into the compilation unit:
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* import "yummies/types.global";
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* Or list it under `compilerOptions.types` in **`tsconfig.json`** so `tsc` loads it for the project:
|
|
23
|
+
*
|
|
24
|
+
* ```json
|
|
25
|
+
* {
|
|
26
|
+
* "compilerOptions": {
|
|
27
|
+
* "types": ["yummies/types.global"]
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* ## `yummies/types` (library)
|
|
33
|
+
*
|
|
34
|
+
* Prefer this entry when you want **explicit**, traceable imports at each call site (tree-shaking
|
|
35
|
+
* friendly and clearer in reviews). The root package may re-export some symbols, but
|
|
36
|
+
* **`yummies/types`** is the dedicated surface for type-only utilities.
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* import type { AnyObject, Maybe } from "yummies/types";
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
1
42
|
/**
|
|
2
43
|
* Extracts all values from an object or array type.
|
|
3
44
|
*
|
package/types.global.cjs
CHANGED
package/types.global.d.ts
CHANGED
|
@@ -1,4 +1,45 @@
|
|
|
1
1
|
/** THIS IS AUTOGENERATED FILE FROM "utils/types.ts" */
|
|
2
|
+
/**
|
|
3
|
+
* ---header-docs-section---
|
|
4
|
+
* # yummies/types
|
|
5
|
+
*
|
|
6
|
+
* ## Description
|
|
7
|
+
*
|
|
8
|
+
* This module collects **TypeScript-only** helpers: `ValueOf`, nullable aliases, branded types, and
|
|
9
|
+
* patterns for props and generics. Nothing here exists at runtime — use `import type` so bundlers
|
|
10
|
+
* drop the imports entirely. The same ideas are also published as **ambient** globals under a
|
|
11
|
+
* separate entry (see below) when you want types in scope without repeating imports in every file.
|
|
12
|
+
*
|
|
13
|
+
* ## `yummies/types.global`
|
|
14
|
+
*
|
|
15
|
+
* Use this subpath when you prefer **global** declarations (no per-file imports). After installing
|
|
16
|
+
* the package, reference the built `.d.ts` bundle — typically via a side-effect import that pulls
|
|
17
|
+
* ambient types into the compilation unit:
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* import "yummies/types.global";
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* Or list it under `compilerOptions.types` in **`tsconfig.json`** so `tsc` loads it for the project:
|
|
24
|
+
*
|
|
25
|
+
* ```json
|
|
26
|
+
* {
|
|
27
|
+
* "compilerOptions": {
|
|
28
|
+
* "types": ["yummies/types.global"]
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* ## `yummies/types` (library)
|
|
34
|
+
*
|
|
35
|
+
* Prefer this entry when you want **explicit**, traceable imports at each call site (tree-shaking
|
|
36
|
+
* friendly and clearer in reviews). The root package may re-export some symbols, but
|
|
37
|
+
* **`yummies/types`** is the dedicated surface for type-only utilities.
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* import type { AnyObject, Maybe } from "yummies/types";
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
2
43
|
/**
|
|
3
44
|
* Extracts all values from an object or array type.
|
|
4
45
|
*
|
package/types.global.js
CHANGED
package/types.js
CHANGED
package/vibrate.cjs
CHANGED
|
@@ -1,9 +1,50 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
//#region src/vibrate.ts
|
|
3
|
+
/**
|
|
4
|
+
* ---header-docs-section---
|
|
5
|
+
* # yummies/vibrate
|
|
6
|
+
*
|
|
7
|
+
* ## Description
|
|
8
|
+
*
|
|
9
|
+
* Thin wrapper around **`navigator.vibrate`** for mobile-style haptics: single pulses or patterned
|
|
10
|
+
* on/off sequences. It no-ops when the API is missing or disabled, so UI code stays simple. Do not
|
|
11
|
+
* rely on vibration for critical feedback because iOS and many desktops omit the API entirely.
|
|
12
|
+
*
|
|
13
|
+
* ## Usage
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { vibrate } from "yummies/vibrate";
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Triggers haptic feedback via the {@link https://developer.mozilla.org/en-US/docs/Web/API/Vibration_API | Vibration API}
|
|
21
|
+
* when `navigator.vibrate` exists (common on Android Chrome; often missing on iOS Safari / desktop).
|
|
22
|
+
*
|
|
23
|
+
* - **Single number** — vibrate for that many milliseconds, then stop.
|
|
24
|
+
* - **Array of numbers** — alternating vibration and pause lengths in ms: `[vibrate, pause, vibrate, ...]`.
|
|
25
|
+
* Per the spec, values are clamped; odd-length arrays imply a trailing vibrate segment.
|
|
26
|
+
*
|
|
27
|
+
* If the API is unavailable or the user has disabled vibration, this function **does nothing**
|
|
28
|
+
* (no throw). Call sites should not rely on vibration for critical UX.
|
|
29
|
+
*
|
|
30
|
+
* @param pattern - Duration in ms, or a pattern array as described above.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* Short confirmation buzz (200 ms):
|
|
34
|
+
* ```ts
|
|
35
|
+
* vibrate(200);
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* Double-pulse pattern: 100 ms on, 50 ms off, 100 ms on:
|
|
40
|
+
* ```ts
|
|
41
|
+
* vibrate([100, 50, 100]);
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
var vibrate = (pattern) => {
|
|
45
|
+
if ("vibrate" in navigator) navigator.vibrate(pattern);
|
|
7
46
|
};
|
|
47
|
+
//#endregion
|
|
8
48
|
exports.vibrate = vibrate;
|
|
9
|
-
|
|
49
|
+
|
|
50
|
+
//# sourceMappingURL=vibrate.cjs.map
|
package/vibrate.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vibrate.cjs","sources":["../src/vibrate.ts"],"sourcesContent":["/**\n * Triggers vibration
|
|
1
|
+
{"version":3,"file":"vibrate.cjs","names":[],"sources":["../src/vibrate.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/vibrate\n *\n * ## Description\n *\n * Thin wrapper around **`navigator.vibrate`** for mobile-style haptics: single pulses or patterned\n * on/off sequences. It no-ops when the API is missing or disabled, so UI code stays simple. Do not\n * rely on vibration for critical feedback because iOS and many desktops omit the API entirely.\n *\n * ## Usage\n *\n * ```ts\n * import { vibrate } from \"yummies/vibrate\";\n * ```\n */\n\n/**\n * Triggers haptic feedback via the {@link https://developer.mozilla.org/en-US/docs/Web/API/Vibration_API | Vibration API}\n * when `navigator.vibrate` exists (common on Android Chrome; often missing on iOS Safari / desktop).\n *\n * - **Single number** — vibrate for that many milliseconds, then stop.\n * - **Array of numbers** — alternating vibration and pause lengths in ms: `[vibrate, pause, vibrate, ...]`.\n * Per the spec, values are clamped; odd-length arrays imply a trailing vibrate segment.\n *\n * If the API is unavailable or the user has disabled vibration, this function **does nothing**\n * (no throw). Call sites should not rely on vibration for critical UX.\n *\n * @param pattern - Duration in ms, or a pattern array as described above.\n *\n * @example\n * Short confirmation buzz (200 ms):\n * ```ts\n * vibrate(200);\n * ```\n *\n * @example\n * Double-pulse pattern: 100 ms on, 50 ms off, 100 ms on:\n * ```ts\n * vibrate([100, 50, 100]);\n * ```\n */\nexport const vibrate = (pattern: number | number[]) => {\n if ('vibrate' in navigator) {\n navigator.vibrate(pattern);\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,IAAa,WAAW,YAA+B;AACrD,KAAI,aAAa,UACf,WAAU,QAAQ,QAAQ"}
|