@cult-frog/primitives 0.1.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.
Files changed (78) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +82 -0
  3. package/dist/callbacks/constant.d.ts +21 -0
  4. package/dist/callbacks/constant.d.ts.map +1 -0
  5. package/dist/callbacks/constant.js +23 -0
  6. package/dist/callbacks/constant.js.map +1 -0
  7. package/dist/callbacks/identity.d.ts +20 -0
  8. package/dist/callbacks/identity.d.ts.map +1 -0
  9. package/dist/callbacks/identity.js +22 -0
  10. package/dist/callbacks/identity.js.map +1 -0
  11. package/dist/callbacks/index.d.ts +5 -0
  12. package/dist/callbacks/index.d.ts.map +1 -0
  13. package/dist/callbacks/index.js +5 -0
  14. package/dist/callbacks/index.js.map +1 -0
  15. package/dist/callbacks/noop.d.ts +16 -0
  16. package/dist/callbacks/noop.d.ts.map +1 -0
  17. package/dist/callbacks/noop.js +18 -0
  18. package/dist/callbacks/noop.js.map +1 -0
  19. package/dist/callbacks/once.d.ts +28 -0
  20. package/dist/callbacks/once.d.ts.map +1 -0
  21. package/dist/callbacks/once.js +38 -0
  22. package/dist/callbacks/once.js.map +1 -0
  23. package/dist/index.d.ts +3 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +3 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/predicates/index.d.ts +7 -0
  28. package/dist/predicates/index.d.ts.map +1 -0
  29. package/dist/predicates/index.js +7 -0
  30. package/dist/predicates/index.js.map +1 -0
  31. package/dist/predicates/nil.d.ts +31 -0
  32. package/dist/predicates/nil.d.ts.map +1 -0
  33. package/dist/predicates/nil.js +35 -0
  34. package/dist/predicates/nil.js.map +1 -0
  35. package/dist/predicates/null.d.ts +29 -0
  36. package/dist/predicates/null.d.ts.map +1 -0
  37. package/dist/predicates/null.js +33 -0
  38. package/dist/predicates/null.js.map +1 -0
  39. package/dist/predicates/number.d.ts +20 -0
  40. package/dist/predicates/number.d.ts.map +1 -0
  41. package/dist/predicates/number.js +22 -0
  42. package/dist/predicates/number.js.map +1 -0
  43. package/dist/predicates/record.d.ts +59 -0
  44. package/dist/predicates/record.d.ts.map +1 -0
  45. package/dist/predicates/record.js +69 -0
  46. package/dist/predicates/record.js.map +1 -0
  47. package/dist/predicates/string.d.ts +15 -0
  48. package/dist/predicates/string.d.ts.map +1 -0
  49. package/dist/predicates/string.js +17 -0
  50. package/dist/predicates/string.js.map +1 -0
  51. package/dist/predicates/undefined.d.ts +29 -0
  52. package/dist/predicates/undefined.d.ts.map +1 -0
  53. package/dist/predicates/undefined.js +33 -0
  54. package/dist/predicates/undefined.js.map +1 -0
  55. package/package.json +68 -0
  56. package/src/callbacks/constant.test.ts +31 -0
  57. package/src/callbacks/constant.ts +22 -0
  58. package/src/callbacks/identity.test.ts +24 -0
  59. package/src/callbacks/identity.ts +21 -0
  60. package/src/callbacks/index.ts +4 -0
  61. package/src/callbacks/noop.test.ts +19 -0
  62. package/src/callbacks/noop.ts +17 -0
  63. package/src/callbacks/once.test.ts +93 -0
  64. package/src/callbacks/once.ts +41 -0
  65. package/src/index.ts +2 -0
  66. package/src/predicates/index.ts +6 -0
  67. package/src/predicates/nil.test.ts +43 -0
  68. package/src/predicates/nil.ts +35 -0
  69. package/src/predicates/null.test.ts +42 -0
  70. package/src/predicates/null.ts +33 -0
  71. package/src/predicates/number.test.ts +38 -0
  72. package/src/predicates/number.ts +21 -0
  73. package/src/predicates/record.test.ts +107 -0
  74. package/src/predicates/record.ts +74 -0
  75. package/src/predicates/string.test.ts +25 -0
  76. package/src/predicates/string.ts +16 -0
  77. package/src/predicates/undefined.test.ts +42 -0
  78. package/src/predicates/undefined.ts +33 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Quin Partridge
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # @cult-frog/primitives
2
+
3
+ Small, dependency-free TypeScript utilities for everyday code: type-narrowing predicates and common callback helpers.
4
+
5
+ - **Type-safe.** Every predicate is a TypeScript type guard, so it narrows types in `if` statements and in `Array.prototype.filter`.
6
+ - **Tree-shakeable.** It is ESM-only, has no side effects and no dependencies, so you only ship what you import.
7
+ - **Predictable.** Edge cases like `NaN`, class instances and inherited properties are handled deliberately and documented.
8
+
9
+ ## Installation
10
+
11
+ ```sh
12
+ pnpm add @cult-frog/primitives
13
+ # or
14
+ npm install @cult-frog/primitives
15
+ ```
16
+
17
+ Requires Node.js 20.19 or later. The package is published as ES modules with bundled type declarations.
18
+
19
+ ## Usage
20
+
21
+ ```ts
22
+ import { hasProperty, isNotNil, isNumber, isRecord, once } from '@cult-frog/primitives';
23
+
24
+ // Remove null and undefined while keeping the array's element type.
25
+ const ids = [1, null, 2, undefined].filter(isNotNil); // number[]
26
+
27
+ // Safely read properties from unknown data.
28
+ const data: unknown = JSON.parse(input);
29
+ if (isRecord(data) && hasProperty(data, 'id') && isNumber(data.id)) {
30
+ console.log(data.id); // number
31
+ }
32
+
33
+ // Run expensive setup only once.
34
+ const getClient = once(() => createClient());
35
+ ```
36
+
37
+ ## API
38
+
39
+ ### Predicates
40
+
41
+ Every predicate is a type guard. Each `isX` check has an `isNotX` counterpart where the negation is useful for narrowing.
42
+
43
+ | Function | Returns `true` when the value is |
44
+ | --- | --- |
45
+ | `isNil(value)` | `null` or `undefined` |
46
+ | `isNotNil(value)` | neither `null` nor `undefined` |
47
+ | `isNull(value)` | `null` |
48
+ | `isNotNull(value)` | not `null` |
49
+ | `isUndefined(value)` | `undefined` |
50
+ | `isNotUndefined(value)` | not `undefined` |
51
+ | `isNumber(value)` | a finite number |
52
+ | `isString(value)` | a string primitive |
53
+ | `isRecord(value)` | a plain object |
54
+ | `isNotRecord(value)` | not a plain object |
55
+ | `hasProperty(object, key)` | an object with `key` as its own property |
56
+
57
+ Worth knowing:
58
+
59
+ - **`isNumber`** rejects `NaN`, `Infinity` and `-Infinity`. It never coerces, so `'42'` is not a number.
60
+ - **`isRecord`** accepts only object literals and `Object.create(null)` objects. Arrays, class instances and built-ins such as `Date` and `Map` are rejected, which makes it a safe first check on parsed JSON. Objects from another realm, such as an iframe, are also rejected.
61
+ - **`hasProperty`** ignores inherited properties such as `toString`. The property it finds can still hold `undefined`.
62
+ - **`isNotNil`** keeps falsy values such as `0`, `''` and `false`, unlike `filter(Boolean)`.
63
+
64
+ ### Callbacks
65
+
66
+ | Function | Description |
67
+ | --- | --- |
68
+ | `noop()` | Does nothing and returns `undefined`. Use it as a default for optional callbacks. |
69
+ | `identity(value)` | Returns `value` unchanged. Use it as a default transform. |
70
+ | `constant(value)` | Returns a function that always returns `value`. |
71
+ | `once(fn)` | Returns a function that calls `fn` at most once and caches its result. |
72
+
73
+ How `once` behaves:
74
+
75
+ - Calls after the first successful one return the cached result and ignore their arguments.
76
+ - `this` is forwarded, so the wrapped function can be used as a method.
77
+ - If `fn` throws, nothing is cached and the next call tries again.
78
+ - When used on a shared prototype, `fn` runs once in total rather than once per instance.
79
+
80
+ ## License
81
+
82
+ [MIT](./LICENSE) © Quin Partridge
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Creates a function that always returns the given value.
3
+ *
4
+ * Useful when an API expects a callback or factory but you already have the value, e.g. a
5
+ * default provider or a stubbed dependency in tests.
6
+ *
7
+ * The same value is returned on every call; objects are not copied.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const getDefaultName = constant('anonymous');
12
+ * getDefaultName(); // 'anonymous'
13
+ *
14
+ * const names = users.map((user) => user.name ?? getDefaultName());
15
+ * ```
16
+ *
17
+ * @param value - The value the returned function should produce.
18
+ * @returns A function that returns `value` every time it is called.
19
+ */
20
+ export declare function constant<T>(value: T): () => T;
21
+ //# sourceMappingURL=constant.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constant.d.ts","sourceRoot":"","sources":["../../src/callbacks/constant.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,CAE7C"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Creates a function that always returns the given value.
3
+ *
4
+ * Useful when an API expects a callback or factory but you already have the value, e.g. a
5
+ * default provider or a stubbed dependency in tests.
6
+ *
7
+ * The same value is returned on every call; objects are not copied.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const getDefaultName = constant('anonymous');
12
+ * getDefaultName(); // 'anonymous'
13
+ *
14
+ * const names = users.map((user) => user.name ?? getDefaultName());
15
+ * ```
16
+ *
17
+ * @param value - The value the returned function should produce.
18
+ * @returns A function that returns `value` every time it is called.
19
+ */
20
+ export function constant(value) {
21
+ return () => value;
22
+ }
23
+ //# sourceMappingURL=constant.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constant.js","sourceRoot":"","sources":["../../src/callbacks/constant.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,QAAQ,CAAI,KAAQ;IAClC,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC;AACrB,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Returns the value it is given, unchanged.
3
+ *
4
+ * Useful as a default transform or a pass-through callback, so callers don't need to branch on
5
+ * whether a transform was provided.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * function formatAll<T>(items: T[], format: (item: T) => T = identity): T[] {
10
+ * return items.map(format);
11
+ * }
12
+ *
13
+ * formatAll([1, 2, 3]); // [1, 2, 3]
14
+ * ```
15
+ *
16
+ * @param value - The value to return.
17
+ * @returns `value`, unchanged.
18
+ */
19
+ export declare function identity<T>(value: T): T;
20
+ //# sourceMappingURL=identity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../../src/callbacks/identity.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAEvC"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Returns the value it is given, unchanged.
3
+ *
4
+ * Useful as a default transform or a pass-through callback, so callers don't need to branch on
5
+ * whether a transform was provided.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * function formatAll<T>(items: T[], format: (item: T) => T = identity): T[] {
10
+ * return items.map(format);
11
+ * }
12
+ *
13
+ * formatAll([1, 2, 3]); // [1, 2, 3]
14
+ * ```
15
+ *
16
+ * @param value - The value to return.
17
+ * @returns `value`, unchanged.
18
+ */
19
+ export function identity(value) {
20
+ return value;
21
+ }
22
+ //# sourceMappingURL=identity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity.js","sourceRoot":"","sources":["../../src/callbacks/identity.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,QAAQ,CAAI,KAAQ;IAClC,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,5 @@
1
+ export * from './constant.js';
2
+ export * from './identity.js';
3
+ export * from './noop.js';
4
+ export * from './once.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/callbacks/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC"}
@@ -0,0 +1,5 @@
1
+ export * from './constant.js';
2
+ export * from './identity.js';
3
+ export * from './noop.js';
4
+ export * from './once.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/callbacks/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * A function that does nothing and returns `undefined`.
3
+ *
4
+ * Useful as a default for optional callbacks, so callers can invoke them without checking
5
+ * whether one was provided. Using a shared `noop` also makes intent clearer than an inline
6
+ * `() => {}`.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * function load(onProgress: (percent: number) => void = noop) {
11
+ * onProgress(50);
12
+ * }
13
+ * ```
14
+ */
15
+ export declare function noop(): void;
16
+ //# sourceMappingURL=noop.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"noop.d.ts","sourceRoot":"","sources":["../../src/callbacks/noop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,IAAI,IAAI,IAAI,CAE3B"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * A function that does nothing and returns `undefined`.
3
+ *
4
+ * Useful as a default for optional callbacks, so callers can invoke them without checking
5
+ * whether one was provided. Using a shared `noop` also makes intent clearer than an inline
6
+ * `() => {}`.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * function load(onProgress: (percent: number) => void = noop) {
11
+ * onProgress(50);
12
+ * }
13
+ * ```
14
+ */
15
+ export function noop() {
16
+ return;
17
+ }
18
+ //# sourceMappingURL=noop.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"noop.js","sourceRoot":"","sources":["../../src/callbacks/noop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,IAAI;IAClB,OAAO;AACT,CAAC"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Wraps a function so it runs at most once. Later calls return the result of the first call.
3
+ *
4
+ * Useful for lazy initialisation and for guarding side effects that must only happen once,
5
+ * such as setup, teardown or one-time warnings.
6
+ *
7
+ * - Arguments passed after the first successful call are ignored.
8
+ * - `this` is forwarded to `fn`, so the wrapper can be used as a method.
9
+ * - If `fn` throws, nothing is cached and the next call tries again.
10
+ * - After a successful call the reference to `fn` is released.
11
+ * - When used as a method on a shared prototype, `fn` runs once in total, not once per instance.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const init = once(() => {
16
+ * console.log('initialising');
17
+ * return createClient();
18
+ * });
19
+ *
20
+ * init(); // logs 'initialising' and returns the client
21
+ * init(); // returns the same client without logging
22
+ * ```
23
+ *
24
+ * @param fn - The function to wrap.
25
+ * @returns A function with the same signature as `fn` that only invokes it once.
26
+ */
27
+ export declare function once<This, Args extends unknown[], R>(fn: (this: This, ...args: Args) => R): (this: This, ...args: Args) => R;
28
+ //# sourceMappingURL=once.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"once.d.ts","sourceRoot":"","sources":["../../src/callbacks/once.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,IAAI,SAAS,OAAO,EAAE,EAAE,CAAC,EAClD,EAAE,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,IAAI,KAAK,CAAC,GACnC,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,IAAI,KAAK,CAAC,CAYlC"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Wraps a function so it runs at most once. Later calls return the result of the first call.
3
+ *
4
+ * Useful for lazy initialisation and for guarding side effects that must only happen once,
5
+ * such as setup, teardown or one-time warnings.
6
+ *
7
+ * - Arguments passed after the first successful call are ignored.
8
+ * - `this` is forwarded to `fn`, so the wrapper can be used as a method.
9
+ * - If `fn` throws, nothing is cached and the next call tries again.
10
+ * - After a successful call the reference to `fn` is released.
11
+ * - When used as a method on a shared prototype, `fn` runs once in total, not once per instance.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const init = once(() => {
16
+ * console.log('initialising');
17
+ * return createClient();
18
+ * });
19
+ *
20
+ * init(); // logs 'initialising' and returns the client
21
+ * init(); // returns the same client without logging
22
+ * ```
23
+ *
24
+ * @param fn - The function to wrap.
25
+ * @returns A function with the same signature as `fn` that only invokes it once.
26
+ */
27
+ export function once(fn) {
28
+ let pending = fn;
29
+ let result;
30
+ return function (...args) {
31
+ if (pending) {
32
+ result = pending.apply(this, args);
33
+ pending = undefined;
34
+ }
35
+ return result;
36
+ };
37
+ }
38
+ //# sourceMappingURL=once.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"once.js","sourceRoot":"","sources":["../../src/callbacks/once.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,IAAI,CAClB,EAAoC;IAEpC,IAAI,OAAO,GAAmD,EAAE,CAAC;IACjE,IAAI,MAAS,CAAC;IAEd,OAAO,UAAsB,GAAG,IAAU;QACxC,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YACnC,OAAO,GAAG,SAAS,CAAC;QACtB,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from './callbacks/index.js';
2
+ export * from './predicates/index.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './callbacks/index.js';
2
+ export * from './predicates/index.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC"}
@@ -0,0 +1,7 @@
1
+ export * from './nil.js';
2
+ export * from './null.js';
3
+ export * from './number.js';
4
+ export * from './record.js';
5
+ export * from './string.js';
6
+ export * from './undefined.js';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/predicates/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,7 @@
1
+ export * from './nil.js';
2
+ export * from './null.js';
3
+ export * from './number.js';
4
+ export * from './record.js';
5
+ export * from './string.js';
6
+ export * from './undefined.js';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/predicates/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Checks if a value is `null` or `undefined`.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * isNil(null); // true
7
+ * isNil(undefined); // true
8
+ * isNil(0); // false
9
+ * isNil(''); // false
10
+ * ```
11
+ *
12
+ * @param value - The value to check.
13
+ * @returns `true` if `value` is `null` or `undefined`, otherwise `false`.
14
+ */
15
+ export declare function isNil<T>(value: T | null | undefined): value is null | undefined;
16
+ /**
17
+ * Checks if a value is neither `null` nor `undefined`, narrowing it to its non-nullable type.
18
+ *
19
+ * Unlike a truthiness check, falsy values such as `0`, `''` and `false` are kept.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * const values = [1, null, 0, undefined, 2];
24
+ * const present = values.filter(isNotNil); // number[]: [1, 0, 2]
25
+ * ```
26
+ *
27
+ * @param value - The value to check.
28
+ * @returns `true` if `value` is not `null` or `undefined`, otherwise `false`.
29
+ */
30
+ export declare function isNotNil<T>(value: T | null | undefined): value is T;
31
+ //# sourceMappingURL=nil.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nil.d.ts","sourceRoot":"","sources":["../../src/predicates/nil.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,GAAG,KAAK,IAAI,IAAI,GAAG,SAAS,CAE/E;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,GAAG,KAAK,IAAI,CAAC,CAEnE"}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Checks if a value is `null` or `undefined`.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * isNil(null); // true
7
+ * isNil(undefined); // true
8
+ * isNil(0); // false
9
+ * isNil(''); // false
10
+ * ```
11
+ *
12
+ * @param value - The value to check.
13
+ * @returns `true` if `value` is `null` or `undefined`, otherwise `false`.
14
+ */
15
+ export function isNil(value) {
16
+ return value == null;
17
+ }
18
+ /**
19
+ * Checks if a value is neither `null` nor `undefined`, narrowing it to its non-nullable type.
20
+ *
21
+ * Unlike a truthiness check, falsy values such as `0`, `''` and `false` are kept.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * const values = [1, null, 0, undefined, 2];
26
+ * const present = values.filter(isNotNil); // number[]: [1, 0, 2]
27
+ * ```
28
+ *
29
+ * @param value - The value to check.
30
+ * @returns `true` if `value` is not `null` or `undefined`, otherwise `false`.
31
+ */
32
+ export function isNotNil(value) {
33
+ return value != null;
34
+ }
35
+ //# sourceMappingURL=nil.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nil.js","sourceRoot":"","sources":["../../src/predicates/nil.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,KAAK,CAAI,KAA2B;IAClD,OAAO,KAAK,IAAI,IAAI,CAAC;AACvB,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,QAAQ,CAAI,KAA2B;IACrD,OAAO,KAAK,IAAI,IAAI,CAAC;AACvB,CAAC"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Checks if a value is `null`. `undefined` is not considered `null`; use {@link isNil} to check
3
+ * for both.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * isNull(null); // true
8
+ * isNull(undefined); // false
9
+ * ```
10
+ *
11
+ * @param value - The value to check.
12
+ * @returns `true` if `value` is `null`, otherwise `false`.
13
+ */
14
+ export declare function isNull<T>(value: T | null): value is null;
15
+ /**
16
+ * Checks if a value is not `null`, narrowing `null` out of its type. `undefined` passes this
17
+ * check; use {@link isNotNil} to exclude both.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * const rows: (Row | null)[] = await fetchRows();
22
+ * const found = rows.filter(isNotNull); // Row[]
23
+ * ```
24
+ *
25
+ * @param value - The value to check.
26
+ * @returns `true` if `value` is not `null`, otherwise `false`.
27
+ */
28
+ export declare function isNotNull<T>(value: T | null): value is T;
29
+ //# sourceMappingURL=null.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"null.d.ts","sourceRoot":"","sources":["../../src/predicates/null.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,KAAK,IAAI,IAAI,CAExD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,KAAK,IAAI,CAAC,CAExD"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Checks if a value is `null`. `undefined` is not considered `null`; use {@link isNil} to check
3
+ * for both.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * isNull(null); // true
8
+ * isNull(undefined); // false
9
+ * ```
10
+ *
11
+ * @param value - The value to check.
12
+ * @returns `true` if `value` is `null`, otherwise `false`.
13
+ */
14
+ export function isNull(value) {
15
+ return value === null;
16
+ }
17
+ /**
18
+ * Checks if a value is not `null`, narrowing `null` out of its type. `undefined` passes this
19
+ * check; use {@link isNotNil} to exclude both.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * const rows: (Row | null)[] = await fetchRows();
24
+ * const found = rows.filter(isNotNull); // Row[]
25
+ * ```
26
+ *
27
+ * @param value - The value to check.
28
+ * @returns `true` if `value` is not `null`, otherwise `false`.
29
+ */
30
+ export function isNotNull(value) {
31
+ return value !== null;
32
+ }
33
+ //# sourceMappingURL=null.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"null.js","sourceRoot":"","sources":["../../src/predicates/null.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,MAAM,CAAI,KAAe;IACvC,OAAO,KAAK,KAAK,IAAI,CAAC;AACxB,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,SAAS,CAAI,KAAe;IAC1C,OAAO,KAAK,KAAK,IAAI,CAAC;AACxB,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Checks if a value is a finite number.
3
+ *
4
+ * `NaN`, `Infinity` and `-Infinity` are rejected, as are numeric strings such as `'42'`; no
5
+ * coercion is performed.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * isNumber(42); // true
10
+ * isNumber(-1.5); // true
11
+ * isNumber(NaN); // false
12
+ * isNumber(Infinity); // false
13
+ * isNumber('42'); // false
14
+ * ```
15
+ *
16
+ * @param value - The value to check.
17
+ * @returns `true` if `value` is a finite number, otherwise `false`.
18
+ */
19
+ export declare function isNumber(value: unknown): value is number;
20
+ //# sourceMappingURL=number.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../../src/predicates/number.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAExD"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Checks if a value is a finite number.
3
+ *
4
+ * `NaN`, `Infinity` and `-Infinity` are rejected, as are numeric strings such as `'42'`; no
5
+ * coercion is performed.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * isNumber(42); // true
10
+ * isNumber(-1.5); // true
11
+ * isNumber(NaN); // false
12
+ * isNumber(Infinity); // false
13
+ * isNumber('42'); // false
14
+ * ```
15
+ *
16
+ * @param value - The value to check.
17
+ * @returns `true` if `value` is a finite number, otherwise `false`.
18
+ */
19
+ export function isNumber(value) {
20
+ return Number.isFinite(value);
21
+ }
22
+ //# sourceMappingURL=number.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number.js","sourceRoot":"","sources":["../../src/predicates/number.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Checks if a value is a plain object, i.e. an object literal or an object created with
3
+ * `Object.create(null)`.
4
+ *
5
+ * Arrays, class instances and built-ins such as `Date`, `Map` and `RegExp` are rejected. This
6
+ * makes it suitable for validating parsed data such as JSON before reading its properties.
7
+ *
8
+ * Objects created in another realm (an iframe or a Node `vm` context) are also rejected, because
9
+ * their prototype is a different `Object.prototype`.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * isRecord({ id: 1 }); // true
14
+ * isRecord(Object.create(null)); // true
15
+ * isRecord([]); // false
16
+ * isRecord(new Date()); // false
17
+ * isRecord(null); // false
18
+ * ```
19
+ *
20
+ * @param value - The value to check.
21
+ * @returns `true` if `value` is a plain object, otherwise `false`.
22
+ */
23
+ export declare function isRecord(value: unknown): value is Record<PropertyKey, unknown>;
24
+ /**
25
+ * Checks if a value is not a plain object. This is the inverse of {@link isRecord}.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * isNotRecord(new Date()); // true
30
+ * isNotRecord([]); // true
31
+ * isNotRecord({ id: 1 }); // false
32
+ * ```
33
+ *
34
+ * @param value - The value to check.
35
+ * @returns `true` if `value` is not a plain object, otherwise `false`.
36
+ */
37
+ export declare function isNotRecord<T>(value: T): value is Exclude<T, Record<PropertyKey, unknown>>;
38
+ /**
39
+ * Checks if an object has the given key as its own property, narrowing the object so the
40
+ * property can be read.
41
+ *
42
+ * Inherited properties are not counted, so keys such as `toString` do not match. The property
43
+ * may still hold `undefined`.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const data: unknown = JSON.parse(input);
48
+ *
49
+ * if (isRecord(data) && hasProperty(data, 'id') && isNumber(data.id)) {
50
+ * data.id; // number
51
+ * }
52
+ * ```
53
+ *
54
+ * @param value - The object to check.
55
+ * @param key - The property key to look for.
56
+ * @returns `true` if `value` has `key` as an own property, otherwise `false`.
57
+ */
58
+ export declare function hasProperty<K extends PropertyKey>(value: object, key: K): value is Record<K, unknown>;
59
+ //# sourceMappingURL=record.d.ts.map