@oscarpalmer/atoms 0.204.1 → 0.206.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 (102) hide show
  1. package/dist/array/filter.d.mts +1 -1
  2. package/dist/array/find.d.mts +3 -3
  3. package/dist/array/first.d.mts +3 -4
  4. package/dist/array/first.mjs +1 -1
  5. package/dist/array/group-by.d.mts +3 -3
  6. package/dist/array/index.d.mts +2 -5
  7. package/dist/array/index.mjs +2 -5
  8. package/dist/array/last.d.mts +3 -4
  9. package/dist/array/last.mjs +1 -1
  10. package/dist/array/move.d.mts +4 -4
  11. package/dist/array/push.d.mts +2 -1
  12. package/dist/array/push.mjs +1 -1
  13. package/dist/array/reverse.mjs +2 -1
  14. package/dist/array/select.d.mts +95 -84
  15. package/dist/array/select.mjs +6 -6
  16. package/dist/array/single.d.mts +3 -3
  17. package/dist/array/sort.d.mts +6 -6
  18. package/dist/array/sort.mjs +3 -2
  19. package/dist/array/swap.d.mts +3 -3
  20. package/dist/array/swap.mjs +1 -1
  21. package/dist/array/to-map.d.mts +3 -3
  22. package/dist/array/to-record.d.mts +3 -3
  23. package/dist/beacon.mjs +2 -2
  24. package/dist/color/constants.d.mts +14 -41
  25. package/dist/color/instance.d.mts +0 -1
  26. package/dist/color/misc/alpha.mjs +1 -1
  27. package/dist/color/misc/get.mjs +1 -1
  28. package/dist/color/misc/state.d.mts +2 -29
  29. package/dist/color/models.d.mts +18 -4
  30. package/dist/color/space/hsl.mjs +7 -2
  31. package/dist/color/space/hwb.mjs +2 -1
  32. package/dist/color/space/rgb.mjs +22 -13
  33. package/dist/function/limit.d.mts +4 -4
  34. package/dist/function/once.d.mts +6 -8
  35. package/dist/function/retry.d.mts +2 -2
  36. package/dist/function/retry.mjs +12 -14
  37. package/dist/index.d.mts +1104 -1127
  38. package/dist/index.mjs +385 -383
  39. package/dist/internal/array/compact.d.mts +1 -1
  40. package/dist/internal/array/find.d.mts +4 -4
  41. package/dist/internal/array/index-of.d.mts +3 -3
  42. package/dist/internal/array/insert.d.mts +1 -1
  43. package/dist/internal/array/insert.mjs +2 -1
  44. package/dist/internal/array/match.d.mts +1 -1
  45. package/dist/internal/array/sets.d.mts +1 -1
  46. package/dist/internal/function/assert.d.mts +7 -7
  47. package/dist/internal/function/limit.d.mts +1 -1
  48. package/dist/internal/function/limit.mjs +9 -9
  49. package/dist/internal/function/work.d.mts +4 -4
  50. package/dist/internal/herald.d.mts +1 -1
  51. package/dist/internal/math/aggregate.d.mts +43 -2
  52. package/dist/internal/math/aggregate.mjs +4 -1
  53. package/dist/internal/math/misc.d.mts +8 -0
  54. package/dist/internal/math/misc.mjs +11 -1
  55. package/dist/internal/random.mjs +2 -1
  56. package/dist/internal/result/attempt.d.mts +59 -0
  57. package/dist/internal/result/attempt.mjs +24 -0
  58. package/dist/internal/result/misc.d.mts +6 -6
  59. package/dist/internal/sized/map.d.mts +2 -2
  60. package/dist/internal/string/misc.mjs +4 -5
  61. package/dist/internal/value/compare.d.mts +7 -19
  62. package/dist/internal/value/equal.d.mts +8 -31
  63. package/dist/internal/value/equal.mjs +2 -1
  64. package/dist/internal/value/handlers.d.mts +2 -5
  65. package/dist/internal/value/has.d.mts +3 -3
  66. package/dist/internal/value/misc.mjs +1 -1
  67. package/dist/is.d.mts +4 -4
  68. package/dist/math.d.mts +3 -52
  69. package/dist/math.mjs +3 -16
  70. package/dist/promise/constants.d.mts +0 -1
  71. package/dist/promise/constants.mjs +1 -2
  72. package/dist/promise/helpers.d.mts +2 -2
  73. package/dist/promise/helpers.mjs +1 -1
  74. package/dist/promise/index.d.mts +7 -7
  75. package/dist/promise/index.mjs +9 -9
  76. package/dist/promise/misc.mjs +1 -1
  77. package/dist/promise/models.d.mts +1 -1
  78. package/dist/promise/timed.d.mts +1 -1
  79. package/dist/queue.d.mts +1 -1
  80. package/dist/queue.mjs +11 -6
  81. package/dist/result/index.d.mts +2 -59
  82. package/dist/result/index.mjs +1 -21
  83. package/dist/result/match.d.mts +3 -3
  84. package/dist/result/work/flow.d.mts +1 -1
  85. package/dist/result/work/flow.mjs +2 -1
  86. package/dist/result/work/pipe.d.mts +1 -1
  87. package/dist/result/work/pipe.mjs +2 -1
  88. package/dist/sized/set.d.mts +1 -1
  89. package/dist/string/fuzzy.d.mts +4 -4
  90. package/dist/string/fuzzy.mjs +7 -5
  91. package/dist/string/index.mjs +2 -1
  92. package/dist/string/normalize.d.mts +1 -1
  93. package/dist/string/template.d.mts +1 -2
  94. package/dist/value/clone.d.mts +8 -20
  95. package/dist/value/clone.mjs +5 -8
  96. package/dist/value/diff.d.mts +1 -1
  97. package/dist/value/diff.mjs +3 -2
  98. package/dist/value/freeze.d.mts +4 -4
  99. package/dist/value/merge.d.mts +5 -5
  100. package/dist/value/transform.d.mts +1 -1
  101. package/package.json +25 -10
  102. package/plugin/index.js +1 -1
@@ -11,7 +11,7 @@
11
11
  * compact([0, 1, '', 'hello', false, true, null, undefined]); // => [1, 'hello', true]
12
12
  * ```
13
13
  */
14
- export declare function compact<Item>(array: Item[], strict: true): Array<Exclude<Item, 0 | '' | false | null | undefined>>;
14
+ export declare function compact<Item>(array: Item[], strict: true): Array<Exclude<Item, 0 | "" | false | null | undefined>>;
15
15
  /**
16
16
  * Compact an array _(removing all `null` and `undefined` values)_
17
17
  *
@@ -3,20 +3,20 @@ type FindMapper = {
3
3
  callback: unknown;
4
4
  reverse: boolean;
5
5
  };
6
- type FindValueType = 'index' | 'item';
6
+ type FindValueType = "index" | "item";
7
7
  type FindValuesResult = {
8
8
  matched: unknown[];
9
9
  notMatched: unknown[];
10
10
  };
11
- type FindValuesType = 'all' | 'unique';
11
+ type FindValuesType = "all" | "unique";
12
12
  type Parameters = {
13
13
  bool?: unknown;
14
14
  key?: unknown;
15
15
  value?: unknown;
16
16
  };
17
17
  export declare function createFindParameters(original: unknown[]): Parameters;
18
- export declare function findValue(type: Exclude<FindValueType, 'item'>, array: unknown[], parameters: unknown[], reversed: boolean): number;
19
- export declare function findValue(type: Exclude<FindValueType, 'index'>, array: unknown[], parameters: unknown[], reversed: boolean): unknown;
18
+ export declare function findValue(type: Exclude<FindValueType, "item">, array: unknown[], parameters: unknown[], reversed: boolean): number;
19
+ export declare function findValue(type: Exclude<FindValueType, "index">, array: unknown[], parameters: unknown[], reversed: boolean): unknown;
20
20
  export declare function findAbsoluteValueOrDefault(array: unknown[], parameters: unknown[], defaultValue: unknown, useDefaultValue: boolean, reversed: boolean): unknown;
21
21
  export declare function findValues(type: FindValuesType, array: unknown[], parameters: unknown[], mapper?: FindMapper): FindValuesResult;
22
22
  export declare const FIND_VALUE_INDEX = "index";
@@ -65,9 +65,6 @@ export declare function indexOf<Item>(array: Item[], filter: (item: Item, index:
65
65
  * ```
66
66
  */
67
67
  export declare function indexOf<Item>(array: Item[], item: Item): number;
68
- export declare namespace indexOf {
69
- var last: typeof lastIndexOf;
70
- }
71
68
  /**
72
69
  * Get the index of the last matching item by callback
73
70
  *
@@ -141,4 +138,7 @@ export declare function lastIndexOf<Item>(array: Item[], filter: (item: Item, in
141
138
  * ```
142
139
  */
143
140
  export declare function lastIndexOf<Item>(array: Item[], item: Item): number;
141
+ export declare namespace indexOf {
142
+ export var last: typeof lastIndexOf;
143
+ }
144
144
  //#endregion
@@ -1,5 +1,5 @@
1
1
  //#region src/internal/array/insert.d.ts
2
- type InsertType = 'insert' | 'push' | 'splice';
2
+ type InsertType = "insert" | "push" | "splice";
3
3
  export declare function insertValues(type: InsertType, array: unknown, items: unknown, start: unknown, deleteCount: number): unknown;
4
4
  export declare const INSERT_TYPE_INSERT: InsertType;
5
5
  export declare const INSERT_TYPE_PUSH: InsertType;
@@ -1,8 +1,9 @@
1
1
  import { chunk } from "./chunk.mjs";
2
+ import { max, min } from "../math/aggregate.mjs";
2
3
  //#region src/internal/array/insert.ts
3
4
  function insertChunkedValues(type, array, items, start, deleteCount) {
4
5
  const actualDeleteCount = deleteCount < 0 ? 0 : deleteCount;
5
- const actualStart = Math.min(Math.max(0, start), array.length);
6
+ const actualStart = min([max([0, start]), array.length]);
6
7
  const chunked = chunk(items);
7
8
  const lastIndex = chunked.length - 1;
8
9
  let index = chunked.length;
@@ -3,7 +3,7 @@ import { PlainObject } from "../../models.mjs";
3
3
  /**
4
4
  * Comparison of an array within another array
5
5
  */
6
- export type ArrayComparison = 'end' | 'inside' | 'invalid' | 'outside' | 'same' | 'start';
6
+ export type ArrayComparison = "end" | "inside" | "invalid" | "outside" | "same" | "start";
7
7
  /**
8
8
  * Is the needle array at the end of the haystack array?
9
9
  *
@@ -1,5 +1,5 @@
1
1
  //#region src/internal/array/sets.d.ts
2
- type CompareSetsType = 'difference' | 'intersection' | 'union';
2
+ type CompareSetsType = "difference" | "intersection" | "union";
3
3
  export declare function compareSets(type: CompareSetsType, first: unknown[], second: unknown[], key?: unknown): unknown[];
4
4
  export declare const SETS_COMPARE_DIFFERENCE: CompareSetsType;
5
5
  export declare const SETS_COMPARE_INTERSECTION: CompareSetsType;
@@ -17,13 +17,6 @@ type NestedPick<Value, Path extends string> = Value extends PlainObject ? Path e
17
17
  * @param error Error constructor _(defaults to `Error`)_
18
18
  */
19
19
  export declare function assert<Condition extends () => boolean>(condition: Condition, message: string, error?: ErrorConstructor): asserts condition;
20
- export declare namespace assert {
21
- var condition: typeof assertCondition;
22
- var defined: typeof assertDefined;
23
- var instanceOf: typeof assertInstanceOf;
24
- var is: typeof assertIs;
25
- var property: typeof assertProperty;
26
- }
27
20
  /**
28
21
  * Creates an _Asserter_ that asserts a condition is true, throwing an error if it is not
29
22
  *
@@ -79,4 +72,11 @@ export declare function assertIs<Value>(condition: (value: unknown) => value is
79
72
  * @returns _Asserter_
80
73
  */
81
74
  export declare function assertProperty<Value extends PlainObject, Path extends NestedKeys<Value>>(path: Path, condition: (value: NestedValue<Value, Path>) => boolean, message: string, error?: ErrorConstructor): Asserter<NestedPick<Value, Path>>;
75
+ export declare namespace assert {
76
+ export var condition: typeof assertCondition;
77
+ export var defined: typeof assertDefined;
78
+ export var instanceOf: typeof assertInstanceOf;
79
+ export var is: typeof assertIs;
80
+ export var property: typeof assertProperty;
81
+ }
82
82
  //#endregion
@@ -32,7 +32,7 @@ export type Limiter<Callback extends GenericCallback> = {
32
32
  */
33
33
  run(...parameters: Parameters<Callback>): ReturnType<Callback>;
34
34
  };
35
- type LimiterType = 'debounce' | 'throttle' | 'wait';
35
+ type LimiterType = "debounce" | "throttle" | "wait";
36
36
  export declare function getAsyncLimiter<Callback extends GenericAsyncCallback | GenericCallback>(type: LimiterType, callback: Callback, time?: number): AsyncLimiter<Callback>;
37
37
  export declare function getLimiter<Callback extends GenericCallback>(type: LimiterType, callback: Callback, time?: number): Limiter<Callback>;
38
38
  export declare const LIMITER_DEBOUNCE: LimiterType;
@@ -1,3 +1,4 @@
1
+ import { asyncAttempt } from "../result/attempt.mjs";
1
2
  import { getNumberOrDefault } from "../defaults.mjs";
2
3
  //#region src/internal/function/limit.ts
3
4
  const LIMITER_NAME_ASYNC = "asyncLimiter";
@@ -51,15 +52,14 @@ async function handleAsyncLimiter(state, item) {
51
52
  if (state.interval === 0 || now - state.start >= state.interval - LIMITER_OFFSET) {
52
53
  state.start = state.throttle ? now : void 0;
53
54
  item.running = true;
54
- try {
55
- let result = state.callback(...item.parameters);
56
- if (result instanceof Promise) result = await result;
57
- item.resolve(result);
58
- } catch (error) {
59
- item.reject(error);
60
- } finally {
61
- item.running = false;
62
- }
55
+ const result = await asyncAttempt(async () => {
56
+ let value = state.callback(...item.parameters);
57
+ if (value instanceof Promise) value = await value;
58
+ return value;
59
+ });
60
+ if (result.ok) item.resolve(result.value);
61
+ else item.reject(result.error);
62
+ item.running = false;
63
63
  } else state.timer = startTimer(() => handleAsyncLimiter(state, item));
64
64
  }
65
65
  function handleLimiter(state) {
@@ -177,9 +177,6 @@ export declare function flow<First extends GenericCallback>(first: First, ...fns
177
177
  * @returns _Flow_ function
178
178
  */
179
179
  export declare function flow(...fns: GenericCallback[]): (...args: unknown[]) => unknown;
180
- export declare namespace flow {
181
- var async: typeof asyncFlow;
182
- }
183
180
  /**
184
181
  * Pipe a value through a function
185
182
  *
@@ -372,7 +369,10 @@ export declare function pipe<Value>(value: Value, ...pipes: Array<(value: Value)
372
369
  * @returns _Piped_ result
373
370
  */
374
371
  export declare function pipe(value: unknown, ...pipes: Array<(value: unknown) => unknown>): unknown;
372
+ export declare namespace flow {
373
+ export var async: typeof asyncFlow;
374
+ }
375
375
  export declare namespace pipe {
376
- var async: typeof asyncPipe;
376
+ export var async: typeof asyncPipe;
377
377
  }
378
378
  //#endregion
@@ -31,7 +31,7 @@ export type Herald<Events extends Record<string, GenericCallback>> = {
31
31
  */
32
32
  subscribe<Event extends keyof Events>(event: Event, callback: Events[Event], signal?: AbortSignal): Subscription;
33
33
  };
34
- export type HeraldEvents<Events extends Record<string, GenericCallback>> = { [Key in keyof Events]: Key extends '*' ? never : (callback: Events[Key], signal?: AbortSignal) => void; };
34
+ export type HeraldEvents<Events extends Record<string, GenericCallback>> = { [Key in keyof Events]: Key extends "*" ? never : (callback: Events[Key], signal?: AbortSignal) => void; };
35
35
  type HeraldOnCreate<Events extends Record<string, GenericCallback>> = <Event extends keyof Events>(event: Event, callback: Events[Event], subscription: Subscription) => void;
36
36
  export type HeraldOptions<Events extends Record<string, GenericCallback>> = {
37
37
  names: Array<keyof Events>;
@@ -7,8 +7,8 @@ type Aggregation = {
7
7
  items?: Record<number, unknown[]>;
8
8
  value: number;
9
9
  };
10
- export type AggregationType = 'average' | 'max' | 'min' | 'sum';
11
- type NonAverageAggregationType = 'max' | 'min' | 'sum';
10
+ export type AggregationType = "average" | "max" | "min" | "sum";
11
+ type NonAverageAggregationType = "max" | "min" | "sum";
12
12
  export declare function aggregate(type: AggregationType, array: unknown[], key: unknown, first?: unknown): Aggregation;
13
13
  export declare function getAggregateCallback(key: unknown): Function | undefined;
14
14
  export declare function getAggregated(type: NonAverageAggregationType, array: unknown[], key?: unknown, first?: unknown): unknown;
@@ -99,6 +99,47 @@ export declare function max<Item extends PlainObject>(items: Item[], key: keyof
99
99
  * @returns Maximum value, or `Number.NaN` if no maximum can be found
100
100
  */
101
101
  export declare function max(values: number[]): number;
102
+ /**
103
+ * Get the minimum value from a list of items
104
+ *
105
+ * @param items List of items
106
+ * @param callback Callback to get an item's value
107
+ * @param first Return only the first item with the minimum value
108
+ * @returns First item with the minimum value, or `undefined` if no minimum can be found
109
+ */
110
+ export declare function min<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number, first: true): Item | undefined;
111
+ /**
112
+ * Get the minimum value from a list of items
113
+ *
114
+ * @param items List of items
115
+ * @param key Key to use for value
116
+ * @param first Return only the first item with the minimum value
117
+ * @returns First item with the minimum value, or `undefined` if no minimum can be found
118
+ */
119
+ export declare function min<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>, first: true): Item | undefined;
120
+ /**
121
+ * Get the minimum value from a list of items
122
+ *
123
+ * @param items List of items
124
+ * @param callback Callback to get an item's value
125
+ * @returns Items with the minimum value
126
+ */
127
+ export declare function min<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number): Item[];
128
+ /**
129
+ * Get the minimum value from a list of items
130
+ *
131
+ * @param items List of items
132
+ * @param key Key to use for value
133
+ * @returns Items with the minimum value
134
+ */
135
+ export declare function min<Item extends PlainObject>(items: Item[], key: keyof NumericalValues<Item>): Item[];
136
+ /**
137
+ * Get the minimum value from a list of numbers
138
+ *
139
+ * @param values List of numbers
140
+ * @returns Minimum value, or `Number.NaN` if no minimum can be found
141
+ */
142
+ export declare function min(values: number[]): number;
102
143
  export declare const AGGREGATION_AVERAGE: AggregationType;
103
144
  export declare const AGGREGATION_MAX = "max";
104
145
  export declare const AGGREGATION_MIN = "min";
@@ -59,6 +59,9 @@ function isMinValue(aggregation, value) {
59
59
  function max(array, key, first) {
60
60
  return getAggregated("max", array, key, first);
61
61
  }
62
+ function min(array, key, first) {
63
+ return getAggregated("min", array, key, first);
64
+ }
62
65
  const AGGREGATION_AVERAGE = "average";
63
66
  const AGGREGATION_MAX = "max";
64
67
  const AGGREGATION_MIN = "min";
@@ -70,4 +73,4 @@ const aggregators = {
70
73
  sum: calculateSum
71
74
  };
72
75
  //#endregion
73
- export { AGGREGATION_AVERAGE, AGGREGATION_MAX, AGGREGATION_MIN, AGGREGATION_SUM, aggregate, getAggregateCallback, getAggregated, max };
76
+ export { AGGREGATION_AVERAGE, AGGREGATION_MAX, AGGREGATION_MIN, AGGREGATION_SUM, aggregate, getAggregateCallback, getAggregated, max, min };
@@ -1,4 +1,12 @@
1
1
  //#region src/internal/math/misc.d.ts
2
+ /**
3
+ * Round a number down
4
+ *
5
+ * @param value Number to round down
6
+ * @param decimals Number of decimal places to round to _(defaults to `0`)_
7
+ * @returns Rounded number, or `Number.NaN` if the value if unable to be rounded
8
+ */
9
+ export declare function floor(value: number, decimals?: number): number;
2
10
  /**
3
11
  * Round a number
4
12
  *
@@ -1,5 +1,15 @@
1
1
  //#region src/internal/math/misc.ts
2
2
  /**
3
+ * Round a number down
4
+ *
5
+ * @param value Number to round down
6
+ * @param decimals Number of decimal places to round to _(defaults to `0`)_
7
+ * @returns Rounded number, or `Number.NaN` if the value if unable to be rounded
8
+ */
9
+ function floor(value, decimals) {
10
+ return roundNumber(Math.floor, value, decimals);
11
+ }
12
+ /**
3
13
  * Round a number
4
14
  *
5
15
  * @param value Number to round
@@ -16,4 +26,4 @@ function roundNumber(callback, value, decimals) {
16
26
  return callback((value + Number.EPSILON) * mod) / mod;
17
27
  }
18
28
  //#endregion
19
- export { round, roundNumber };
29
+ export { floor, round, roundNumber };
@@ -1,4 +1,5 @@
1
1
  import { isNumber } from "./is.mjs";
2
+ import { floor } from "./math/misc.mjs";
2
3
  //#region src/internal/random.ts
3
4
  function getRandomFloatingNumberValue(inclusive, minimum, maximum) {
4
5
  let maxFloat = isNumber(maximum) && maximum <= Number.MAX_SAFE_INTEGER ? maximum : Number.MAX_SAFE_INTEGER;
@@ -25,7 +26,7 @@ function getRandomFloatingNumber(minimum, maximum) {
25
26
  * @returns Random integer
26
27
  */
27
28
  function getRandomInteger(minimum, maximum) {
28
- return Math.floor(getRandomFloatingNumberValue(true, minimum, maximum));
29
+ return floor(getRandomFloatingNumberValue(true, minimum, maximum));
29
30
  }
30
31
  //#endregion
31
32
  export { getRandomFloatingNumber, getRandomInteger };
@@ -0,0 +1,59 @@
1
+ import { ExtendedResult, Result } from "./models.mjs";
2
+ //#region src/internal/result/attempt.d.ts
3
+ /**
4
+ * Executes a _Promise_, catching any errors, and returns a result
5
+ *
6
+ * _Available as `asyncAttempt` and `attempt.async`_
7
+ *
8
+ * @param promise _Promise_ to execute
9
+ * @param error Error value
10
+ * @returns Callback result
11
+ */
12
+ export declare function asyncAttempt<Value, E>(promise: Promise<Value>, error: E): Promise<ExtendedResult<Awaited<Value>, E>>;
13
+ /**
14
+ * Executes a callback asynchronously, catching any errors, and returns a result
15
+ *
16
+ * _Available as `asyncAttempt` and `attempt.async`_
17
+ *
18
+ * @param callback Callback to execute
19
+ * @param error Error value
20
+ * @returns Callback result
21
+ */
22
+ export declare function asyncAttempt<Value, E>(callback: () => Promise<Value>, error: E): Promise<ExtendedResult<Awaited<Value>, E>>;
23
+ /**
24
+ * Executes a _Promise_, catching any errors, and returns a result
25
+ *
26
+ * _Available as `asyncAttempt` and `attempt.async`_
27
+ *
28
+ * @param promise _Promise_ to execute
29
+ * @returns Callback result
30
+ */
31
+ export declare function asyncAttempt<Value>(promise: Promise<Value>): Promise<Result<Awaited<Value>>>;
32
+ /**
33
+ * Executes a callback asynchronously, catching any errors, and returns a result
34
+ *
35
+ * _Available as `asyncAttempt` and `attempt.async`_
36
+ *
37
+ * @param callback Callback to execute
38
+ * @returns Callback result
39
+ */
40
+ export declare function asyncAttempt<Value>(callback: () => Promise<Value>): Promise<Result<Awaited<Value>>>;
41
+ /**
42
+ * Executes a callback, catching any errors, and returns a result
43
+ *
44
+ * @param callback Callback to execute
45
+ * @param error Error value
46
+ * @returns Callback result
47
+ */
48
+ export declare function attempt<Value, E>(callback: () => Value, error: E): ExtendedResult<Value, E>;
49
+ /**
50
+ * Executes a callback, catching any errors, and returns a result
51
+ *
52
+ * @param callback Callback to execute
53
+ * @returns Callback result
54
+ */
55
+ export declare function attempt<Value>(callback: () => Value): Result<Value, Error>;
56
+ export declare namespace attempt {
57
+ export var async: typeof asyncAttempt;
58
+ }
59
+ //#endregion
@@ -0,0 +1,24 @@
1
+ import { getError, ok } from "./misc.mjs";
2
+ //#region src/internal/result/attempt.ts
3
+ async function asyncAttempt(value, err) {
4
+ try {
5
+ let result;
6
+ if (typeof value === "function") result = value();
7
+ else result = value;
8
+ if (result instanceof Promise) result = await result;
9
+ return ok(result);
10
+ } catch (thrown) {
11
+ return getError(err ?? thrown, err == null ? void 0 : thrown);
12
+ }
13
+ }
14
+ function attempt(callback, err) {
15
+ try {
16
+ const value = callback();
17
+ return ok(value);
18
+ } catch (thrown) {
19
+ return getError(err ?? thrown, err == null ? void 0 : thrown);
20
+ }
21
+ }
22
+ attempt.async = asyncAttempt;
23
+ //#endregion
24
+ export { asyncAttempt, attempt };
@@ -15,9 +15,6 @@ export declare function error<E>(value: E, original: Error): ExtendedErr<E>;
15
15
  * @returns Error result
16
16
  */
17
17
  export declare function error<E>(value: E): Err<E>;
18
- export declare namespace error {
19
- var is: typeof isError;
20
- }
21
18
  export declare function getError<E>(value: E, original?: Error): Err<E> | ExtendedErr<E>;
22
19
  /**
23
20
  * Is the _Result_ an extended error?
@@ -78,9 +75,6 @@ export declare function isResult(value: unknown): value is ExtendedErr<unknown>
78
75
  * @returns Ok result
79
76
  */
80
77
  export declare function ok<Value>(value: Value): Ok<Value>;
81
- export declare namespace ok {
82
- var is: typeof isOk;
83
- }
84
78
  /**
85
79
  * Gets the value of an ok result _(or a default value)_
86
80
  *
@@ -97,4 +91,10 @@ export declare function unwrap<Value, E = Error>(value: Result<Value, E>, defaul
97
91
  * @returns Value of the _Result_ _(or the default value)_
98
92
  */
99
93
  export declare function unwrap(value: unknown, defaultValue: unknown): unknown;
94
+ export declare namespace error {
95
+ export var is: typeof isError;
96
+ }
97
+ export declare namespace ok {
98
+ export var is: typeof isOk;
99
+ }
100
100
  //#endregion
@@ -39,10 +39,10 @@ export declare class SizedMap<SizedKey = unknown, SizedValue = unknown> extends
39
39
  /**
40
40
  * @inheritdoc
41
41
  */
42
- get(key: SizedKey): SizedValue | undefined;
42
+ override get(key: SizedKey): SizedValue | undefined;
43
43
  /**
44
44
  * @inheritdoc
45
45
  */
46
- set(key: SizedKey, value: SizedValue): this;
46
+ override set(key: SizedKey, value: SizedValue): this;
47
47
  }
48
48
  //#endregion
@@ -1,3 +1,6 @@
1
+ import { unwrap } from "../result/misc.mjs";
2
+ import "../../result/misc.mjs";
3
+ import { attempt } from "../result/attempt.mjs";
1
4
  //#region src/internal/string/misc.ts
2
5
  /**
3
6
  * Get the string value from any value
@@ -57,11 +60,7 @@ function join(array, delimiter) {
57
60
  return values.join(typeof delimiter === "string" ? delimiter : "");
58
61
  }
59
62
  function tryCallback(value, callback) {
60
- try {
61
- return callback(value);
62
- } catch {
63
- return value;
64
- }
63
+ return unwrap(attempt(() => callback(value)), value);
65
64
  }
66
65
  function tryDecode(value) {
67
66
  return tryCallback(value, decodeURIComponent);
@@ -1,4 +1,5 @@
1
- import { Constructor, GenericCallback } from "../../models.mjs";
1
+ import { Constructor } from "../../models.mjs";
2
+ import { CompareHandler } from "./handlers.mjs";
2
3
  //#region src/internal/value/compare.d.ts
3
4
  type Comparator<Value = any> = (first: Value, second: Value) => number;
4
5
  /**
@@ -9,24 +10,6 @@ type Comparator<Value = any> = (first: Value, second: Value) => number;
9
10
  * @returns `0` if equal; `-1` first comes before second; `1` first comes after second
10
11
  */
11
12
  export declare function compare(first: unknown, second: unknown): number;
12
- export declare namespace compare {
13
- var deregister: typeof deregisterComparator;
14
- var handler: {
15
- base: {
16
- handlers: WeakMap<WeakKey, string | GenericCallback>;
17
- options: {
18
- callback: GenericCallback;
19
- method?: string;
20
- };
21
- owner: GenericCallback;
22
- deregister: (constructor: Constructor) => void;
23
- get: (first: unknown, second: unknown) => string | GenericCallback | undefined;
24
- register: (constructor: Constructor, handler?: string | GenericCallback) => void;
25
- };
26
- handle(first: unknown, second: unknown, ...parameters: unknown[]): number;
27
- };
28
- var register: typeof registerComparator;
29
- }
30
13
  /**
31
14
  * Deregister a custom comparison handler for a class
32
15
  *
@@ -44,4 +27,9 @@ export declare function deregisterComparator<Instance>(constructor: Constructor<
44
27
  * @param handler Method name or comparison function _(defaults to `compare`)_
45
28
  */
46
29
  export declare function registerComparator<Instance>(constructor: Constructor<Instance>, handler?: string | Comparator<Instance>): void;
30
+ export declare namespace compare {
31
+ export var deregister: typeof deregisterComparator;
32
+ export var handler: CompareHandler<number>;
33
+ export var register: typeof registerComparator;
34
+ }
47
35
  //#endregion
@@ -1,4 +1,5 @@
1
- import { Constructor, GenericCallback } from "../../models.mjs";
1
+ import { Constructor } from "../../models.mjs";
2
+ import { CompareHandler } from "./handlers.mjs";
2
3
  //#region src/internal/value/equal.d.ts
3
4
  /**
4
5
  * Options for value equality comparison
@@ -54,17 +55,6 @@ type Equalizer = {
54
55
  */
55
56
  register: <Instance>(constructor: Constructor<Instance>, handler: (first: Instance, second: Instance) => boolean) => void;
56
57
  };
57
- type Options = {
58
- ignoreCase: boolean;
59
- ignoreExpressions: OptionsKeys<RegExp[]>;
60
- ignoreKeys: OptionsKeys<Set<string>>;
61
- relaxedNullish: boolean;
62
- };
63
- type OptionsKeys<Values> = {
64
- enabled: boolean;
65
- values: Values;
66
- };
67
- declare function Equalizer(this: any, options: Options): void;
68
58
  /**
69
59
  * Deregister a equality comparison handler for a specific class
70
60
  *
@@ -91,25 +81,6 @@ export declare function equal(first: string, second: string, ignoreCase?: boolea
91
81
  * @returns `true` if the values are equal, otherwise `false`
92
82
  */
93
83
  export declare function equal(first: unknown, second: unknown, options?: EqualOptions): boolean;
94
- export declare namespace equal {
95
- var deregister: typeof deregisterEqualizer;
96
- var handler: {
97
- base: {
98
- handlers: WeakMap<WeakKey, string | GenericCallback>;
99
- options: {
100
- callback: GenericCallback;
101
- method?: string;
102
- };
103
- owner: GenericCallback;
104
- deregister: (constructor: Constructor) => void;
105
- get: (first: unknown, second: unknown) => string | GenericCallback | undefined;
106
- register: (constructor: Constructor, handler?: string | GenericCallback) => void;
107
- };
108
- handle(first: unknown, second: unknown, ...parameters: unknown[]): boolean;
109
- };
110
- var initialize: typeof initializeEqualizer;
111
- var register: typeof registerEqualizer;
112
- }
113
84
  /**
114
85
  * Create an equalizer with predefined options
115
86
  *
@@ -128,4 +99,10 @@ export declare function initializeEqualizer(options?: EqualOptions): Equalizer;
128
99
  * @param handler Comparison function
129
100
  */
130
101
  export declare function registerEqualizer<Instance>(constructor: Constructor<Instance>, handler: (first: Instance, second: Instance) => boolean): void;
102
+ export declare namespace equal {
103
+ export var deregister: typeof deregisterEqualizer;
104
+ export var handler: CompareHandler<boolean>;
105
+ export var initialize: typeof initializeEqualizer;
106
+ export var register: typeof registerEqualizer;
107
+ }
131
108
  //#endregion
@@ -1,4 +1,5 @@
1
1
  import { isNonPlainObject, isPlainObject, isPrimitive, isTypedArray } from "../is.mjs";
2
+ import { round } from "../math/misc.mjs";
2
3
  import { createCompareHandler } from "./handlers.mjs";
3
4
  //#region src/internal/value/equal.ts
4
5
  function Equalizer(options) {
@@ -60,7 +61,7 @@ function equalArray(first, second, options) {
60
61
  if (length !== second.length) return false;
61
62
  let offset = 0;
62
63
  if (length >= EQUAL_ARRAY_THRESHOLD) {
63
- offset = Math.round(length / EQUAL_ARRAY_PEEK_PERCENTAGE);
64
+ offset = round(length / EQUAL_ARRAY_PEEK_PERCENTAGE);
64
65
  offset = offset > EQUAL_ARRAY_THRESHOLD ? EQUAL_ARRAY_THRESHOLD : offset;
65
66
  for (let index = 0; index < offset; index += 1) if (!(equalValue(first[index], second[index], options) && equalValue(first[length - index - 1], second[length - index - 1], options))) return false;
66
67
  }
@@ -12,17 +12,14 @@ type BaseHandlerOptions = {
12
12
  callback: GenericCallback;
13
13
  method?: string;
14
14
  };
15
- type CompareHandler<Value> = {
15
+ export type CompareHandler<Value> = {
16
16
  base: BaseHandler;
17
17
  handle(first: unknown, second: unknown, ...parameters: unknown[]): Value;
18
18
  };
19
- type ValueHandler = {
19
+ export type ValueHandler = {
20
20
  base: BaseHandler;
21
21
  handle(value: unknown, ...parameters: unknown[]): unknown;
22
22
  };
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
23
  export declare function createCompareHandler<Value>(owner: GenericCallback, options: BaseHandlerOptions): CompareHandler<Value>;
27
24
  export declare function createValueHandler(owner: GenericCallback, options: BaseHandlerOptions): ValueHandler;
28
25
  //#endregion
@@ -38,9 +38,6 @@ export declare function hasValue<Data extends PlainObject, Path extends NestedKe
38
38
  * ```
39
39
  */
40
40
  export declare function hasValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): boolean;
41
- export declare namespace hasValue {
42
- var get: typeof hasValueResult;
43
- }
44
41
  /**
45
42
  * Check if a property is defined in an object, and get its value if it is
46
43
  *
@@ -83,4 +80,7 @@ export declare function hasValueResult<Data extends PlainObject, Path extends Ne
83
80
  * ```
84
81
  */
85
82
  export declare function hasValueResult<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): Result<unknown, string>;
83
+ export declare namespace hasValue {
84
+ export var get: typeof hasValueResult;
85
+ }
86
86
  //#endregion
@@ -1,5 +1,5 @@
1
- import { ignoreKey } from "../string/misc.mjs";
2
1
  import { error, ok } from "../result/misc.mjs";
2
+ import { ignoreKey } from "../string/misc.mjs";
3
3
  //#region src/internal/value/misc.ts
4
4
  function findKey(needle, haystack) {
5
5
  const keys = Object.keys(haystack);