@oscarpalmer/atoms 0.202.0 → 0.203.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 (156) hide show
  1. package/dist/array/difference.d.mts +4 -5
  2. package/dist/array/exists.d.mts +5 -6
  3. package/dist/array/filter.d.mts +10 -11
  4. package/dist/array/find.d.mts +10 -11
  5. package/dist/array/first.d.mts +10 -11
  6. package/dist/array/flatten.d.mts +2 -3
  7. package/dist/array/from.d.mts +7 -8
  8. package/dist/array/group-by.d.mts +7 -8
  9. package/dist/array/insert.d.mts +3 -4
  10. package/dist/array/intersection.d.mts +4 -5
  11. package/dist/array/last.d.mts +10 -11
  12. package/dist/array/move.d.mts +9 -10
  13. package/dist/array/partition.d.mts +5 -6
  14. package/dist/array/push.d.mts +2 -3
  15. package/dist/array/reverse.d.mts +2 -3
  16. package/dist/array/select.d.mts +11 -12
  17. package/dist/array/single.d.mts +4 -5
  18. package/dist/array/slice.d.mts +12 -13
  19. package/dist/array/sort.d.mts +31 -32
  20. package/dist/array/sort.mjs +41 -33
  21. package/dist/array/splice.d.mts +5 -6
  22. package/dist/array/swap.d.mts +8 -9
  23. package/dist/array/to-map.d.mts +7 -8
  24. package/dist/array/to-record.d.mts +7 -8
  25. package/dist/array/to-set.d.mts +4 -5
  26. package/dist/array/toggle.d.mts +4 -5
  27. package/dist/array/union.d.mts +4 -5
  28. package/dist/array/unique.d.mts +4 -5
  29. package/dist/array/update.d.mts +4 -5
  30. package/dist/beacon.d.mts +8 -9
  31. package/dist/beacon.mjs +90 -65
  32. package/dist/color/constants.d.mts +14 -13
  33. package/dist/color/constants.mjs +9 -2
  34. package/dist/color/index.d.mts +2 -2
  35. package/dist/color/instance.d.mts +3 -3
  36. package/dist/color/instance.mjs +125 -61
  37. package/dist/color/misc/alpha.d.mts +4 -5
  38. package/dist/color/misc/format.d.mts +6 -7
  39. package/dist/color/misc/format.mjs +15 -12
  40. package/dist/color/misc/get.d.mts +14 -15
  41. package/dist/color/misc/is.d.mts +16 -17
  42. package/dist/color/misc/is.mjs +3 -4
  43. package/dist/color/misc/state.d.mts +8 -10
  44. package/dist/color/misc/state.mjs +22 -20
  45. package/dist/color/models.d.mts +25 -19
  46. package/dist/color/space/hex.d.mts +9 -10
  47. package/dist/color/space/hsl.d.mts +8 -9
  48. package/dist/color/space/hwb.d.mts +9 -10
  49. package/dist/color/space/rgb.d.mts +11 -12
  50. package/dist/function/limit.d.mts +9 -8
  51. package/dist/function/limit.mjs +5 -5
  52. package/dist/function/once.d.mts +26 -6
  53. package/dist/function/once.mjs +135 -76
  54. package/dist/function/retry.d.mts +5 -6
  55. package/dist/function/retry.mjs +3 -3
  56. package/dist/index.d.mts +182 -58
  57. package/dist/index.mjs +1191 -810
  58. package/dist/internal/abort.d.mts +3 -4
  59. package/dist/internal/array/callbacks.d.mts +4 -5
  60. package/dist/internal/array/chunk.d.mts +2 -3
  61. package/dist/internal/array/compact.d.mts +3 -4
  62. package/dist/internal/array/find.d.mts +10 -11
  63. package/dist/internal/array/get.d.mts +7 -8
  64. package/dist/internal/array/group.d.mts +2 -3
  65. package/dist/internal/array/index-of.d.mts +10 -11
  66. package/dist/internal/array/insert.d.mts +5 -6
  67. package/dist/internal/array/match.d.mts +17 -18
  68. package/dist/internal/array/overlap.d.mts +2 -3
  69. package/dist/internal/array/sets.d.mts +5 -6
  70. package/dist/internal/array/shuffle.d.mts +2 -3
  71. package/dist/internal/array/update.d.mts +2 -3
  72. package/dist/internal/defaults.d.mts +3 -4
  73. package/dist/internal/function/assert.d.mts +10 -11
  74. package/dist/internal/function/limit.d.mts +41 -0
  75. package/dist/internal/function/limit.mjs +110 -0
  76. package/dist/internal/function/memoize.d.mts +4 -5
  77. package/dist/internal/function/memoize.mjs +53 -31
  78. package/dist/internal/function/misc.d.mts +2 -3
  79. package/dist/internal/function/work.d.mts +53 -54
  80. package/dist/internal/herald.d.mts +8 -9
  81. package/dist/internal/herald.mjs +38 -26
  82. package/dist/internal/is.d.mts +19 -20
  83. package/dist/internal/math/aggregate.d.mts +14 -15
  84. package/dist/internal/math/misc.d.mts +3 -4
  85. package/dist/internal/number.d.mts +4 -5
  86. package/dist/internal/random.d.mts +3 -4
  87. package/dist/internal/result/misc.d.mts +29 -14
  88. package/dist/internal/result/misc.mjs +4 -0
  89. package/dist/internal/result/models.d.mts +9 -10
  90. package/dist/internal/sized/map.d.mts +2 -3
  91. package/dist/internal/sized/misc.d.mts +2 -3
  92. package/dist/internal/string/case.d.mts +9 -10
  93. package/dist/internal/string/misc.d.mts +8 -9
  94. package/dist/internal/subscription.d.mts +10 -11
  95. package/dist/internal/subscription.mjs +69 -57
  96. package/dist/internal/value/compare.d.mts +17 -9
  97. package/dist/internal/value/compare.mjs +7 -7
  98. package/dist/internal/value/equal.d.mts +34 -15
  99. package/dist/internal/value/equal.mjs +28 -13
  100. package/dist/internal/value/get.d.mts +3 -4
  101. package/dist/internal/value/handlers.d.mts +19 -9
  102. package/dist/internal/value/handlers.mjs +50 -50
  103. package/dist/internal/value/has.d.mts +6 -7
  104. package/dist/internal/value/misc.d.mts +6 -7
  105. package/dist/internal/value/partial.d.mts +3 -4
  106. package/dist/internal/value/set.d.mts +4 -5
  107. package/dist/is.d.mts +13 -13
  108. package/dist/logger.d.mts +5 -4
  109. package/dist/logger.mjs +61 -54
  110. package/dist/math.d.mts +20 -20
  111. package/dist/models.d.mts +45 -49
  112. package/dist/promise/constants.d.mts +14 -15
  113. package/dist/promise/delay.d.mts +3 -4
  114. package/dist/promise/delay.mjs +4 -4
  115. package/dist/promise/helpers.d.mts +7 -8
  116. package/dist/promise/index.d.mts +13 -14
  117. package/dist/promise/misc.d.mts +6 -6
  118. package/dist/promise/models.d.mts +16 -17
  119. package/dist/promise/timed.d.mts +4 -5
  120. package/dist/promise/timed.mjs +5 -5
  121. package/dist/query.d.mts +3 -4
  122. package/dist/queue.d.mts +14 -15
  123. package/dist/queue.mjs +185 -129
  124. package/dist/random.d.mts +8 -8
  125. package/dist/result/index.d.mts +8 -9
  126. package/dist/result/match.d.mts +6 -7
  127. package/dist/result/misc.d.mts +3 -3
  128. package/dist/result/work/flow.d.mts +28 -29
  129. package/dist/result/work/pipe.d.mts +24 -25
  130. package/dist/sized/set.d.mts +2 -3
  131. package/dist/string/fuzzy.d.mts +21 -12
  132. package/dist/string/fuzzy.mjs +77 -49
  133. package/dist/string/index.d.mts +8 -8
  134. package/dist/string/match.d.mts +4 -5
  135. package/dist/string/normalize.d.mts +8 -9
  136. package/dist/string/normalize.mjs +9 -4
  137. package/dist/string/template.d.mts +9 -9
  138. package/dist/string/template.mjs +11 -4
  139. package/dist/value/clone.d.mts +21 -13
  140. package/dist/value/clone.mjs +22 -20
  141. package/dist/value/collection.d.mts +4 -5
  142. package/dist/value/diff.d.mts +5 -6
  143. package/dist/value/freeze.d.mts +18 -19
  144. package/dist/value/freeze.mjs +1 -1
  145. package/dist/value/merge.d.mts +13 -14
  146. package/dist/value/merge.mjs +21 -5
  147. package/dist/value/omit.d.mts +2 -3
  148. package/dist/value/pick.d.mts +2 -3
  149. package/dist/value/shake.d.mts +3 -4
  150. package/dist/value/smush.d.mts +3 -4
  151. package/dist/value/transform.d.mts +15 -10
  152. package/dist/value/transform.mjs +12 -7
  153. package/dist/value/unsmush.d.mts +3 -4
  154. package/package.json +1 -1
  155. package/dist/internal/function/timer.d.mts +0 -10
  156. package/dist/internal/function/timer.mjs +0 -87
@@ -17,7 +17,7 @@ import { NestedKeys, NestedValue, PlainObject } from "../../models.mjs";
17
17
  * getValue(data, 'foo.nope'); // => undefined
18
18
  * ```
19
19
  */
20
- declare function getValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): NestedValue<Data, Path>;
20
+ export declare function getValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): NestedValue<Data, Path>;
21
21
  /**
22
22
  * Get the value from an object using an unknown path
23
23
  *
@@ -36,6 +36,5 @@ declare function getValue<Data extends PlainObject, Path extends NestedKeys<Data
36
36
  * getValue(data, 'foo.nope'); // => undefined
37
37
  * ```
38
38
  */
39
- declare function getValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): unknown;
40
- //#endregion
41
- export { getValue };
39
+ export declare function getValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): unknown;
40
+ //#endregion
@@ -1,18 +1,28 @@
1
1
  import { Constructor, GenericCallback } from "../../models.mjs";
2
2
  //#region src/internal/value/handlers.d.ts
3
- type Options = {
3
+ type BaseHandler = {
4
+ handlers: WeakMap<Constructor, string | GenericCallback>;
5
+ options: BaseHandlerOptions;
6
+ owner: GenericCallback;
7
+ deregister: (constructor: Constructor) => void;
8
+ get: (first: unknown, second: unknown) => string | GenericCallback | undefined;
9
+ register: (constructor: Constructor, handler?: string | GenericCallback) => void;
10
+ };
11
+ type BaseHandlerOptions = {
4
12
  callback: GenericCallback;
5
13
  method?: string;
6
14
  };
7
- declare function createCompareHandlers<Value>(owner: GenericCallback, options: Options): {
8
- deregister(constructor: Constructor): void;
15
+ type CompareHandler<Value> = {
16
+ base: BaseHandler;
9
17
  handle(first: unknown, second: unknown, ...parameters: unknown[]): Value;
10
- register(constructor: Constructor, handler?: string | GenericCallback): void;
11
18
  };
12
- declare function createSelfHandlers(owner: GenericCallback, options: Options): {
13
- deregister(constructor: Constructor): void;
19
+ type ValueHandler = {
20
+ base: BaseHandler;
14
21
  handle(value: unknown, ...parameters: unknown[]): unknown;
15
- register(constructor: Constructor, handler?: string | GenericCallback): void;
16
22
  };
17
- //#endregion
18
- export { createCompareHandlers, createSelfHandlers };
23
+ declare function BaseHandler(this: any, owner: GenericCallback, options: BaseHandlerOptions): void;
24
+ declare function CompareHandler(this: any, base: BaseHandler): void;
25
+ declare function ValueHandler(this: any, value: BaseHandler): void;
26
+ export declare function createCompareHandler<Value>(owner: GenericCallback, options: BaseHandlerOptions): CompareHandler<Value>;
27
+ export declare function createValueHandler(owner: GenericCallback, options: BaseHandlerOptions): ValueHandler;
28
+ //#endregion
@@ -1,57 +1,57 @@
1
1
  import { isNonConstructor } from "../is.mjs";
2
2
  //#region src/internal/value/handlers.ts
3
- function createCompareHandlers(owner, options) {
4
- const handlers = createHandlers(owner, options);
5
- return {
6
- deregister(constructor) {
7
- handlers.deregister(constructor);
8
- },
9
- handle(first, second, ...parameters) {
10
- const handler = handlers.get(first, second);
11
- if (handler == null) return options.callback(first, second, ...parameters);
12
- return typeof handler === "function" ? handler(first, second) : first[handler](second);
13
- },
14
- register(constructor, handler) {
15
- handlers.register(constructor, handler);
16
- }
17
- };
18
- }
19
- function createHandlers(owner, options) {
20
- const handlers = /* @__PURE__ */ new WeakMap();
21
- return {
22
- deregister(constructor) {
23
- handlers.delete(constructor);
24
- },
25
- get(first, second) {
26
- if (isConstructable(first) && isConstructable(second) && first.constructor === second.constructor) return handlers.get(first.constructor);
27
- },
28
- register(constructor, handler) {
29
- if (isNonConstructor(constructor) || handler === owner) return;
30
- let actual = handler ?? options.method;
31
- if (typeof actual !== "function" && typeof actual !== "string") return;
32
- if (typeof actual === "string") actual = typeof constructor.prototype[actual] === "function" ? actual : void 0;
33
- if (actual != null) handlers.set(constructor, actual);
34
- }
35
- };
36
- }
37
- function createSelfHandlers(owner, options) {
38
- const handlers = createHandlers(owner, options);
39
- return {
40
- deregister(constructor) {
41
- handlers.deregister(constructor);
42
- },
43
- handle(value, ...parameters) {
44
- const handler = handlers.get(value, value);
45
- if (handler == null) return options.callback(value, ...parameters);
46
- return typeof handler === "function" ? handler(value) : value[handler]();
47
- },
48
- register(constructor, handler) {
49
- handlers.register(constructor, handler);
50
- }
51
- };
3
+ function BaseHandler(owner, options) {
4
+ this.handlers = /* @__PURE__ */ new WeakMap();
5
+ this.owner = owner;
6
+ this.options = options;
7
+ }
8
+ Object.defineProperties(BaseHandler.prototype, {
9
+ deregister: { value: deregisterHandler },
10
+ get: { value: getHandler },
11
+ register: { value: registerHandler }
12
+ });
13
+ function CompareHandler(base) {
14
+ this.base = base;
15
+ }
16
+ Object.defineProperties(CompareHandler.prototype, { handle: { value: handleComparison } });
17
+ function ValueHandler(value) {
18
+ this.base = value;
19
+ }
20
+ Object.defineProperties(ValueHandler.prototype, { handle: { value: handleValue } });
21
+ function createBaseHandler(owner, options) {
22
+ return new BaseHandler(owner, options);
23
+ }
24
+ function createCompareHandler(owner, options) {
25
+ return new CompareHandler(createBaseHandler(owner, options));
26
+ }
27
+ function createValueHandler(owner, options) {
28
+ return new ValueHandler(createBaseHandler(owner, options));
29
+ }
30
+ function deregisterHandler(value) {
31
+ this.handlers.delete(value);
32
+ }
33
+ function getHandler(first, second) {
34
+ if (isConstructable(first) && isConstructable(second) && first.constructor === second.constructor) return this.handlers.get(first.constructor);
35
+ }
36
+ function handleComparison(first, second, ...parameters) {
37
+ const handler = this.base.get(first, second);
38
+ if (handler == null) return this.base.options.callback(first, second, ...parameters);
39
+ return typeof handler === "function" ? handler(first, second) : first[handler](second);
52
40
  }
53
41
  function isConstructable(value) {
54
42
  return typeof value === "object" && value !== null;
55
43
  }
44
+ function registerHandler(constructor, handler) {
45
+ if (isNonConstructor(constructor) || handler === this.owner) return;
46
+ let actual = handler ?? this.options.method;
47
+ if (typeof actual !== "function" && typeof actual !== "string") return;
48
+ if (typeof actual === "string") actual = typeof constructor.prototype[actual] === "function" ? actual : void 0;
49
+ if (actual != null) this.handlers.set(constructor, actual);
50
+ }
51
+ function handleValue(value, ...parameters) {
52
+ const handler = this.base.get(value, value);
53
+ if (handler == null) return this.base.options.callback(value, ...parameters);
54
+ return typeof handler === "function" ? handler(value) : value[handler]();
55
+ }
56
56
  //#endregion
57
- export { createCompareHandlers, createSelfHandlers };
57
+ export { createCompareHandler, createValueHandler };
@@ -18,7 +18,7 @@ import { Result } from "../result/models.mjs";
18
18
  * hasValue(data, 'foo.nope'); // => false
19
19
  * ```
20
20
  */
21
- declare function hasValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): boolean;
21
+ export declare function hasValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): boolean;
22
22
  /**
23
23
  * Check if a property is defined in an object
24
24
  *
@@ -37,8 +37,8 @@ declare function hasValue<Data extends PlainObject, Path extends NestedKeys<Data
37
37
  * hasValue(data, 'foo.nope'); // => false
38
38
  * ```
39
39
  */
40
- declare function hasValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): boolean;
41
- declare namespace hasValue {
40
+ export declare function hasValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): boolean;
41
+ export declare namespace hasValue {
42
42
  var get: typeof hasValueResult;
43
43
  }
44
44
  /**
@@ -61,7 +61,7 @@ declare namespace hasValue {
61
61
  * hasValueResult(data, 'foo.nope'); // => {ok: false, error: 'Expected property to exist in object'}
62
62
  * ```
63
63
  */
64
- declare function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): Result<NestedValue<Data, ToString<Path>>, string>;
64
+ export declare function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): Result<NestedValue<Data, ToString<Path>>, string>;
65
65
  /**
66
66
  * Check if a property is defined in an object, and get its value if it is
67
67
  *
@@ -82,6 +82,5 @@ declare function hasValueResult<Data extends PlainObject, Path extends NestedKey
82
82
  * hasValueResult(data, 'foo.nope'); // => {ok: false, error: 'Expected property to exist in object'}
83
83
  * ```
84
84
  */
85
- declare function hasValueResult<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): Result<unknown, string>;
86
- //#endregion
87
- export { hasValue, hasValueResult };
85
+ export declare function hasValueResult<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): Result<unknown, string>;
86
+ //#endregion
@@ -1,9 +1,8 @@
1
1
  import { Result } from "../result/models.mjs";
2
2
  //#region src/internal/value/misc.d.ts
3
- declare function findKey(needle: string, haystack: object): string;
4
- declare function getNestedValue(data: object, path: string, ignoreCase: boolean): Result<unknown, string>;
5
- declare function getPaths(path: string, lowercase: boolean): string | string[];
6
- declare function handleValue(data: object, path: string, value: unknown, get: true, ignoreCase: boolean): Result<unknown, string>;
7
- declare function handleValue(data: object, path: string, value: unknown, get: false, ignoreCase: boolean): void;
8
- //#endregion
9
- export { findKey, getNestedValue, getPaths, handleValue };
3
+ export declare function findKey(needle: string, haystack: object): string;
4
+ export declare function getNestedValue(data: object, path: string, ignoreCase: boolean): Result<unknown, string>;
5
+ export declare function getPaths(path: string, lowercase: boolean): string | string[];
6
+ export declare function handleValue(data: object, path: string, value: unknown, get: true, ignoreCase: boolean): Result<unknown, string>;
7
+ export declare function handleValue(data: object, path: string, value: unknown, get: false, ignoreCase: boolean): void;
8
+ //#endregion
@@ -1,5 +1,4 @@
1
1
  //#region src/internal/value/partial.d.ts
2
- declare function partial<Value extends object, ValueKey extends keyof Value>(value: unknown, keys: ValueKey[], omit: true): Omit<Value, ValueKey>;
3
- declare function partial<Value extends object, ValueKey extends keyof Value>(value: unknown, keys: ValueKey[], omit: false): Pick<Value, ValueKey>;
4
- //#endregion
5
- export { partial };
2
+ export declare function partial<Value extends object, ValueKey extends keyof Value>(value: unknown, keys: ValueKey[], omit: true): Omit<Value, ValueKey>;
3
+ export declare function partial<Value extends object, ValueKey extends keyof Value>(value: unknown, keys: ValueKey[], omit: false): Pick<Value, ValueKey>;
4
+ //#endregion
@@ -15,7 +15,7 @@ import { NestedKeys, NestedValue, PlainObject } from "../../models.mjs";
15
15
  * setValue(data, 'foo.bar.baz', (current) => current + 1); // => {foo: {bar: {baz: 43}}}
16
16
  * ```
17
17
  */
18
- declare function setValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path, updater: (current: NestedValue<Data, Path>) => NestedValue<Data, Path>): Data;
18
+ export declare function setValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path, updater: (current: NestedValue<Data, Path>) => NestedValue<Data, Path>): Data;
19
19
  /**
20
20
  * Set the value in an object using a known path
21
21
  *
@@ -31,7 +31,7 @@ declare function setValue<Data extends PlainObject, Path extends NestedKeys<Data
31
31
  * setValue(data, 'foo.bar.baz', 99); // => {foo: {bar: {baz: 99}}}
32
32
  * ```
33
33
  */
34
- declare function setValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path, value: NestedValue<Data, Path>): Data;
34
+ export declare function setValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path, value: NestedValue<Data, Path>): Data;
35
35
  /**
36
36
  * Set the value in an object using an unknown path
37
37
  *
@@ -48,6 +48,5 @@ declare function setValue<Data extends PlainObject, Path extends NestedKeys<Data
48
48
  * setValue(data, 'Foo.Bar.Baz', 99, true); // => {foo: {bar: {baz: 99}}}
49
49
  * ```
50
50
  */
51
- declare function setValue<Data extends PlainObject>(data: Data, path: string, value: unknown, ignoreCase?: boolean): Data;
52
- //#endregion
53
- export { setValue };
51
+ export declare function setValue<Data extends PlainObject>(data: Data, path: string, value: unknown, ignoreCase?: boolean): Data;
52
+ //#endregion
package/dist/is.d.mts CHANGED
@@ -6,83 +6,83 @@ import { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNonArrayOrP
6
6
  * @param value Value to check
7
7
  * @returns `true` if the value is considered empty, otherwise `false`
8
8
  */
9
- declare function isEmpty(value: unknown): boolean;
9
+ export declare function isEmpty(value: unknown): boolean;
10
10
  /**
11
11
  * Is the value not empty, or holding non-empty values?
12
12
  *
13
13
  * @param value Value to check
14
14
  * @returns `true` if the value is not considered empty, otherwise `false`
15
15
  */
16
- declare function isNonEmpty(value: unknown): boolean;
16
+ export declare function isNonEmpty(value: unknown): boolean;
17
17
  /**
18
18
  * Is the value not `undefined` or `null`?
19
19
  *
20
20
  * @param value Value to check
21
21
  * @returns `true` if the value is not `undefined` or `null`, otherwise `false`
22
22
  */
23
- declare function isNonNullable<Value>(value: Value): value is Exclude<Value, undefined | null>;
23
+ export declare function isNonNullable<Value>(value: Value): value is Exclude<Value, undefined | null>;
24
24
  /**
25
25
  * Is the value not `undefined`, `null`, or stringified as an empty _(no whitespace)_ string?
26
26
  *
27
27
  * @param value Value to check
28
28
  * @returns `true` if the value is not `undefined`, `null`, or matches an empty string, otherwise `false`
29
29
  */
30
- declare function isNonNullableOrEmpty<Value>(value: Value): value is Exclude<Value, undefined | null | ''>;
30
+ export declare function isNonNullableOrEmpty<Value>(value: Value): value is Exclude<Value, undefined | null | ''>;
31
31
  /**
32
32
  * Is the value not `undefined`, `null`, or stringified as a whitespace-only string?
33
33
  *
34
34
  * @param value Value to check
35
35
  * @returns `true` if the value is not `undefined`, `null`, or matches a whitespace-only string, otherwise `false`
36
36
  */
37
- declare function isNonNullableOrWhitespace<Value>(value: Value): value is Exclude<Value, undefined | null | ''>;
37
+ export declare function isNonNullableOrWhitespace<Value>(value: Value): value is Exclude<Value, undefined | null | ''>;
38
38
  /**
39
39
  * Is the value not a number or a number-like string?
40
40
  *
41
41
  * @param value Value to check
42
42
  * @returns `true` if the value is not a number or a number-like string, otherwise `false`
43
43
  */
44
- declare function isNonNumerical<Value>(value: Value): value is Exclude<Value, number | `${number}`>;
44
+ export declare function isNonNumerical<Value>(value: Value): value is Exclude<Value, number | `${number}`>;
45
45
  /**
46
46
  * Is the value not an object _(or function)_?
47
47
  *
48
48
  * @param value Value to check
49
49
  * @returns `true` if the value is not an object, otherwise `false`
50
50
  */
51
- declare function isNonObject<Value>(value: Value): value is Exclude<Value, object>;
51
+ export declare function isNonObject<Value>(value: Value): value is Exclude<Value, object>;
52
52
  /**
53
53
  * Is the value `undefined` or `null`?
54
54
  *
55
55
  * @param value Value to check
56
56
  * @returns `true` if the value is `undefined` or `null`, otherwise `false`
57
57
  */
58
- declare function isNullable(value: unknown): value is undefined | null;
58
+ export declare function isNullable(value: unknown): value is undefined | null;
59
59
  /**
60
60
  * Is the value `undefined`, `null`, or stringified as an empty _(no whitespace)_ string?
61
61
  *
62
62
  * @param value Value to check
63
63
  * @returns `true` if the value is nullable or matches an empty string, otherwise `false`
64
64
  */
65
- declare function isNullableOrEmpty(value: unknown): value is undefined | null | '';
65
+ export declare function isNullableOrEmpty(value: unknown): value is undefined | null | '';
66
66
  /**
67
67
  * Is the value `undefined`, `null`, or stringified as a whitespace-only string?
68
68
  *
69
69
  * @param value Value to check
70
70
  * @returns `true` if the value is nullable or matches a whitespace-only string, otherwise `false`
71
71
  */
72
- declare function isNullableOrWhitespace(value: unknown): value is undefined | null | '';
72
+ export declare function isNullableOrWhitespace(value: unknown): value is undefined | null | '';
73
73
  /**
74
74
  * Is the value a number or a number-like string?
75
75
  *
76
76
  * @param value Value to check
77
77
  * @returns `true` if the value is a number or a number-like string, otherwise `false`
78
78
  */
79
- declare function isNumerical(value: unknown): value is number | `${number}`;
79
+ export declare function isNumerical(value: unknown): value is number | `${number}`;
80
80
  /**
81
81
  * Is the value an object _(or function)_?
82
82
  *
83
83
  * @param value Value to check
84
84
  * @returns `true` if the value matches, otherwise `false`
85
85
  */
86
- declare function isObject(value: unknown): value is object;
86
+ export declare function isObject(value: unknown): value is object;
87
87
  //#endregion
88
- export { isArrayOrPlainObject, isConstructor, isEmpty, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isPlainObject, isPrimitive, isTypedArray };
88
+ export { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonInstanceOf, isNonKey, isNonNumber, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNumber, isPlainObject, isPrimitive, isTypedArray };
package/dist/logger.d.mts CHANGED
@@ -46,12 +46,12 @@ type Logger = {
46
46
  * @param label Label for the logger
47
47
  * @returns _TimedLogger_ instance
48
48
  */
49
- time(label: string): TimedLogger;
49
+ time(label: string): TimeLogger;
50
50
  };
51
51
  /**
52
52
  * A named timer that can be used to log durations to the console
53
53
  */
54
- type TimedLogger = {
54
+ type TimeLogger = {
55
55
  /**
56
56
  * Is the timer active? _(i.e. has it been started and not stopped, and is logging enabled?)_
57
57
  */
@@ -67,8 +67,9 @@ type TimedLogger = {
67
67
  */
68
68
  get stop(): () => void;
69
69
  };
70
+ declare function TimeLogger(this: any, label: string): void;
70
71
  declare function isLogger(value: unknown): value is Logger;
71
- declare function isTimedLogger(value: unknown): value is TimedLogger;
72
+ declare function isTimeLogger(value: unknown): value is TimeLogger;
72
73
  /**
73
74
  * A logger that can be used to log messages to the console
74
75
  *
@@ -76,4 +77,4 @@ declare function isTimedLogger(value: unknown): value is TimedLogger;
76
77
  */
77
78
  declare const Logger: Logger;
78
79
  //#endregion
79
- export { Logger, type TimedLogger, isLogger, isTimedLogger };
80
+ export { Logger, type TimeLogger, isLogger, isTimeLogger };
package/dist/logger.mjs CHANGED
@@ -1,46 +1,73 @@
1
1
  import { noop } from "./internal/function/misc.mjs";
2
2
  import { getString } from "./internal/string/misc.mjs";
3
3
  //#region src/logger.ts
4
+ const LOGGER_NAME = "Logger";
5
+ const LOGGER_NAME_TIMED = "TimedLogger";
6
+ const LOGGER_PROPERTY = "$logger";
7
+ function Lumberjack() {
8
+ for (const method of methods) {
9
+ const bound = console[method].bind(console);
10
+ Object.defineProperty(this, method, {
11
+ enumerable: true,
12
+ get() {
13
+ return enabled ? bound : noop;
14
+ }
15
+ });
16
+ }
17
+ }
18
+ Lumberjack.prototype[LOGGER_PROPERTY] = LOGGER_NAME;
19
+ Object.defineProperties(Lumberjack.prototype, {
20
+ enabled: {
21
+ enumerable: true,
22
+ get: () => enabled,
23
+ set: (value) => {
24
+ enabled = typeof value === "boolean" ? value : enabled;
25
+ }
26
+ },
27
+ time: { value: (label) => new TimeLogger(getString(label)) }
28
+ });
29
+ function TimeLogger(label) {
30
+ Object.defineProperty(this, LOGGER_SYMBOL, { value: {
31
+ label,
32
+ started: enabled,
33
+ stopped: false
34
+ } });
35
+ this[LOGGER_SYMBOL].isActive = () => {
36
+ return this[LOGGER_SYMBOL].started && !this[LOGGER_SYMBOL].stopped && enabled;
37
+ };
38
+ if (this[LOGGER_SYMBOL].started) console.time(label);
39
+ }
40
+ TimeLogger.prototype[LOGGER_PROPERTY] = LOGGER_NAME_TIMED;
41
+ Object.defineProperties(TimeLogger.prototype, {
42
+ active: {
43
+ enumerable: true,
44
+ get() {
45
+ return this[LOGGER_SYMBOL].isActive();
46
+ }
47
+ },
48
+ log: { get() {
49
+ const state = this[LOGGER_SYMBOL];
50
+ return state.isActive() ? console.timeLog.bind(console, state.label) : noop;
51
+ } },
52
+ stop: { get() {
53
+ const state = this[LOGGER_SYMBOL];
54
+ if (state.isActive()) {
55
+ state.stopped = true;
56
+ return console.timeEnd.bind(console, state.label);
57
+ }
58
+ return noop;
59
+ } }
60
+ });
4
61
  function isLogger(value) {
5
62
  return isLoggerInstance(LOGGER_NAME, value);
6
63
  }
7
64
  function isLoggerInstance(name, value) {
8
65
  return typeof value === "object" && value !== null && value[LOGGER_PROPERTY] === name;
9
66
  }
10
- function isTimedLogger(value) {
67
+ function isTimeLogger(value) {
11
68
  return isLoggerInstance(LOGGER_NAME_TIMED, value);
12
69
  }
13
- function timedLogger(label) {
14
- function stop() {
15
- const fn = stopper;
16
- state.stopped = true;
17
- logger = void 0;
18
- stopper = void 0;
19
- return fn;
20
- }
21
- let logger = console.timeLog.bind(console, label);
22
- let stopper = console.timeEnd.bind(console, label);
23
- const state = {
24
- label,
25
- started: enabled,
26
- stopped: false
27
- };
28
- const instance = {};
29
- Object.defineProperties(instance, {
30
- [LOGGER_PROPERTY]: { value: LOGGER_NAME_TIMED },
31
- active: {
32
- enumerable: true,
33
- get: () => state.started && !state.stopped && enabled
34
- },
35
- log: { get: () => instance.active ? logger : noop },
36
- stop: { get: () => instance.active ? stop() : noop }
37
- });
38
- if (state.started) console.time(label);
39
- return Object.freeze(instance);
40
- }
41
- const LOGGER_NAME = "Logger";
42
- const LOGGER_NAME_TIMED = "TimedLogger";
43
- const LOGGER_PROPERTY = "$logger";
70
+ const LOGGER_SYMBOL = Symbol(LOGGER_PROPERTY);
44
71
  const methods = [
45
72
  "debug",
46
73
  "dir",
@@ -56,27 +83,7 @@ const methods = [
56
83
  *
57
84
  * _(Logging can be enabled or disabled by setting the `enabled` property)_
58
85
  */
59
- const Logger = (() => {
60
- const instance = {};
61
- Object.defineProperties(instance, {
62
- [LOGGER_PROPERTY]: { value: LOGGER_NAME },
63
- enabled: {
64
- enumerable: true,
65
- get: () => enabled,
66
- set: (value) => {
67
- enabled = typeof value === "boolean" ? value : enabled;
68
- }
69
- },
70
- time: { value: (label) => timedLogger(getString(label)) }
71
- });
72
- for (const method of methods) Object.defineProperty(instance, method, {
73
- enumerable: true,
74
- get() {
75
- return enabled ? console[method].bind(console) : noop;
76
- }
77
- });
78
- return Object.freeze(instance);
79
- })();
86
+ const Logger = new Lumberjack();
80
87
  let enabled = true;
81
88
  //#endregion
82
- export { Logger, isLogger, isTimedLogger };
89
+ export { Logger, isLogger, isTimeLogger };