@oscarpalmer/atoms 0.179.0 → 0.180.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 (100) hide show
  1. package/dist/array/filter.d.mts +36 -28
  2. package/dist/array/filter.mjs +5 -5
  3. package/dist/array/first.d.mts +13 -5
  4. package/dist/array/first.mjs +1 -1
  5. package/dist/array/group-by.d.mts +13 -1
  6. package/dist/array/group-by.mjs +1 -1
  7. package/dist/array/last.d.mts +9 -1
  8. package/dist/array/last.mjs +1 -1
  9. package/dist/array/move.d.mts +9 -1
  10. package/dist/array/move.mjs +3 -1
  11. package/dist/array/reverse.d.mts +5 -0
  12. package/dist/array/reverse.mjs +5 -0
  13. package/dist/array/select.d.mts +2 -2
  14. package/dist/array/sort.d.mts +23 -9
  15. package/dist/array/sort.mjs +22 -22
  16. package/dist/array/swap.d.mts +2 -0
  17. package/dist/array/swap.mjs +2 -0
  18. package/dist/array/to-map.d.mts +13 -1
  19. package/dist/array/to-map.mjs +1 -1
  20. package/dist/array/to-record.d.mts +13 -1
  21. package/dist/array/to-record.mjs +1 -1
  22. package/dist/function/assert.d.mts +9 -1
  23. package/dist/function/assert.mjs +9 -1
  24. package/dist/function/limit.d.mts +5 -1
  25. package/dist/function/limit.mjs +5 -1
  26. package/dist/function/once.d.mts +3 -1
  27. package/dist/function/once.mjs +3 -1
  28. package/dist/function/retry.d.mts +4 -0
  29. package/dist/function/retry.mjs +2 -0
  30. package/dist/function/work.d.mts +49 -1
  31. package/dist/function/work.mjs +1 -1
  32. package/dist/index.d.mts +415 -242
  33. package/dist/index.mjs +224 -167
  34. package/dist/internal/array/index-of.mjs +1 -1
  35. package/dist/internal/function/timer.mjs +3 -1
  36. package/dist/internal/value/compare.d.mts +13 -9
  37. package/dist/internal/value/compare.mjs +13 -9
  38. package/dist/internal/value/equal.d.mts +29 -15
  39. package/dist/internal/value/equal.mjs +41 -35
  40. package/dist/internal/value/handlers.d.mts +4 -4
  41. package/dist/internal/value/handlers.mjs +19 -11
  42. package/dist/internal/value/has.d.mts +9 -8
  43. package/dist/internal/value/has.mjs +3 -3
  44. package/dist/internal/value/misc.d.mts +4 -8
  45. package/dist/internal/value/misc.mjs +6 -17
  46. package/dist/promise/index.d.mts +11 -1
  47. package/dist/promise/index.mjs +1 -1
  48. package/dist/result/index.d.mts +9 -1
  49. package/dist/result/index.mjs +1 -1
  50. package/dist/result/match.d.mts +5 -1
  51. package/dist/result/match.mjs +1 -1
  52. package/dist/result/misc.d.mts +3 -3
  53. package/dist/result/work/flow.d.mts +49 -1
  54. package/dist/result/work/flow.mjs +1 -1
  55. package/dist/result/work/pipe.d.mts +67 -155
  56. package/dist/result/work/pipe.mjs +3 -3
  57. package/dist/string/fuzzy.d.mts +11 -1
  58. package/dist/string/fuzzy.mjs +22 -6
  59. package/dist/string/template.d.mts +3 -1
  60. package/dist/string/template.mjs +3 -1
  61. package/dist/value/clone.d.mts +13 -9
  62. package/dist/value/clone.mjs +21 -17
  63. package/dist/value/merge.d.mts +9 -7
  64. package/dist/value/merge.mjs +7 -5
  65. package/package.json +3 -3
  66. package/plugin/helpers.js +2 -2
  67. package/src/array/filter.ts +44 -36
  68. package/src/array/first.ts +18 -9
  69. package/src/array/group-by.ts +22 -10
  70. package/src/array/last.ts +17 -5
  71. package/src/array/move.ts +18 -5
  72. package/src/array/reverse.ts +5 -0
  73. package/src/array/select.ts +2 -2
  74. package/src/array/sort.ts +110 -86
  75. package/src/array/swap.ts +2 -0
  76. package/src/array/to-map.ts +22 -10
  77. package/src/array/to-record.ts +22 -10
  78. package/src/function/assert.ts +12 -4
  79. package/src/function/limit.ts +6 -2
  80. package/src/function/once.ts +3 -1
  81. package/src/function/retry.ts +8 -2
  82. package/src/function/work.ts +92 -26
  83. package/src/internal/array/index-of.ts +1 -1
  84. package/src/internal/function/timer.ts +4 -2
  85. package/src/internal/string.ts +2 -0
  86. package/src/internal/value/compare.ts +14 -11
  87. package/src/internal/value/equal.ts +79 -67
  88. package/src/internal/value/handlers.ts +19 -11
  89. package/src/internal/value/has.ts +16 -16
  90. package/src/internal/value/misc.ts +10 -8
  91. package/src/promise/index.ts +14 -4
  92. package/src/result/index.ts +15 -5
  93. package/src/result/match.ts +7 -3
  94. package/src/result/misc.ts +3 -3
  95. package/src/result/work/flow.ts +68 -13
  96. package/src/result/work/pipe.ts +97 -392
  97. package/src/string/fuzzy.ts +34 -8
  98. package/src/string/template.ts +3 -1
  99. package/src/value/clone.ts +25 -22
  100. package/src/value/merge.ts +14 -12
@@ -61,7 +61,7 @@ class Fuzzy<Item> {
61
61
  this.#state.items,
62
62
  this.#state.strings,
63
63
  value,
64
- options == null ? this.#state : getOptions(options, this.#state),
64
+ options == null ? this.#state : getFuzzyOptions(options, this.#state),
65
65
  );
66
66
  }
67
67
  }
@@ -165,7 +165,7 @@ function getItems<Item>(items: FuzzyItem<Item>[]): Item[] {
165
165
  .map(({item}) => item);
166
166
  }
167
167
 
168
- function getOptions<Item>(
168
+ function getFuzzyOptions<Item>(
169
169
  input: unknown,
170
170
  state?: FuzzyState<Item>,
171
171
  ): RequiredKeys<FuzzyOptions, 'tolerance'> {
@@ -179,18 +179,14 @@ function getOptions<Item>(
179
179
  options.limit = state?.limit;
180
180
  }
181
181
 
182
- if (typeof options.tolerance === 'number' && !Number.isNaN(options.tolerance)) {
183
- options.tolerance = options.tolerance < 0 ? 0 : Math.floor(options.tolerance);
184
- } else {
185
- options.tolerance = state?.tolerance ?? PROXIMITY_THRESHOLD;
186
- }
182
+ options.tolerance = getTolerance(options.tolerance, state);
187
183
 
188
184
  return options as RequiredKeys<FuzzyOptions, 'tolerance'>;
189
185
  }
190
186
 
191
187
  function getState<Item>(items: Item[], input: unknown): FuzzyState<Item> {
192
188
  const handler = getHandler(input);
193
- const options = getOptions(input);
189
+ const options = getFuzzyOptions(input);
194
190
 
195
191
  return {
196
192
  handler,
@@ -201,6 +197,14 @@ function getState<Item>(items: Item[], input: unknown): FuzzyState<Item> {
201
197
  };
202
198
  }
203
199
 
200
+ function getTolerance<Item>(input: unknown, state?: FuzzyState<Item>): number {
201
+ if (typeof input === 'number' && !Number.isNaN(input)) {
202
+ return input < 0 ? 0 : Math.floor(input);
203
+ }
204
+
205
+ return state?.tolerance ?? PROXIMITY_THRESHOLD;
206
+ }
207
+
204
208
  /**
205
209
  * Create a fuzzy searcher for an array of items
206
210
  * @param items Items to search through
@@ -236,6 +240,28 @@ export function fuzzy(items: unknown[], configuration?: unknown): Fuzzy<unknown>
236
240
  return new Fuzzy(getState(items, configuration));
237
241
  }
238
242
 
243
+ fuzzy.match = fuzzyMatch;
244
+
245
+ /**
246
+ * Does the needle match the haystack in a fuzzy way?
247
+ * @param haystack Haystack to search through
248
+ * @param needle Needle to search for
249
+ * @returns `true` if the needle matches the haystack in a fuzzy way, `false` otherwise
250
+ */
251
+ export function fuzzyMatch(haystack: string, needle: string): boolean {
252
+ if (typeof haystack !== 'string' || typeof needle !== 'string') {
253
+ return false;
254
+ }
255
+
256
+ const trimmed = needle.trim();
257
+
258
+ if (includes(haystack, trimmed, true)) {
259
+ return true;
260
+ }
261
+
262
+ return getScore(haystack, trimmed) > -1;
263
+ }
264
+
239
265
  function isSubsequence(haystack: string, needle: string): boolean {
240
266
  const lowerCaseHaystack = lowerCase(haystack);
241
267
  const lowerCaseNeedle = lowerCase(needle);
@@ -69,10 +69,12 @@ function handleTemplate(
69
69
 
70
70
  /**
71
71
  * Create a templater with predefined options
72
+ *
73
+ * Available as `initializeTemplater` and `template.initialize`
72
74
  * @param options Templating options
73
75
  * @returns Templater function
74
76
  */
75
- function initializeTemplater(options?: Partial<TemplateOptions>): Templater {
77
+ export function initializeTemplater(options?: Partial<TemplateOptions>): Templater {
76
78
  const {ignoreCase, pattern} = getTemplateOptions(options);
77
79
 
78
80
  return (value: string, variables?: PlainObject): string => {
@@ -26,30 +26,9 @@ clone.handlers = getSelfHandlers(clone, {
26
26
  method: CLONE_NAME,
27
27
  });
28
28
 
29
+ clone.deregister = deregisterCloner;
29
30
  clone.register = registerCloner;
30
31
 
31
- clone.unregister = unregisterCloner;
32
-
33
- /**
34
- * Register a clone handler for a specific class
35
- * @param constructor Class constructor
36
- * @param handler Method name or clone function _(defaults to `clone`)_
37
- */
38
- function registerCloner<Instance>(
39
- constructor: Constructor<Instance>,
40
- handler?: string | ((value: Instance) => Instance),
41
- ): void {
42
- clone.handlers.register(constructor, handler);
43
- }
44
-
45
- /**
46
- * Unregister a clone handler for a specific class
47
- * @param constructor Class constructor
48
- */
49
- function unregisterCloner<Instance>(constructor: Constructor<Instance>): void {
50
- clone.handlers.unregister(constructor);
51
- }
52
-
53
32
  function cloneArrayBuffer(
54
33
  value: ArrayBuffer,
55
34
  depth?: number,
@@ -260,6 +239,30 @@ function cloneValue(value: unknown, depth: number, references: WeakMap<WeakKey,
260
239
  }
261
240
  }
262
241
 
242
+ /**
243
+ * Deregister a clone handler for a specific class
244
+ *
245
+ * Available as `deregisterCloner` and `template.deregister`
246
+ * @param constructor Class constructor
247
+ */
248
+ export function deregisterCloner<Instance>(constructor: Constructor<Instance>): void {
249
+ clone.handlers.deregister(constructor);
250
+ }
251
+
252
+ /**
253
+ * Register a clone handler for a specific class
254
+ *
255
+ * Available as `registerCloner` and `template.register`
256
+ * @param constructor Class constructor
257
+ * @param handler Method name or clone function _(defaults to `clone`)_
258
+ */
259
+ export function registerCloner<Instance>(
260
+ constructor: Constructor<Instance>,
261
+ handler?: string | ((value: Instance) => Instance),
262
+ ): void {
263
+ clone.handlers.register(constructor, handler);
264
+ }
265
+
263
266
  function tryStructuredClone(
264
267
  value: object,
265
268
  depth: number,
@@ -80,6 +80,20 @@ function handleMerge(values: ArrayOrPlainObject[], options: Options): ArrayOrPla
80
80
  return !Array.isArray(values) || values.length === 0 ? {} : mergeValues(values, options, true);
81
81
  }
82
82
 
83
+ /**
84
+ * Create a merger with predefined options
85
+ *
86
+ * Available as `initializeMerger` and `merge.initialize`
87
+ * @param options Merging options
88
+ * @returns Merger function
89
+ */
90
+ export function initializeMerger(options?: MergeOptions): Merger {
91
+ const actual = getMergeOptions(options);
92
+
93
+ return <Model extends ArrayOrPlainObject>(values: NestedPartial<Model>[]): Model =>
94
+ handleMerge(values, actual) as Model;
95
+ }
96
+
83
97
  /**
84
98
  * Merge multiple arrays or objects into a single one
85
99
  * @param values Values to merge
@@ -111,18 +125,6 @@ export function merge(
111
125
 
112
126
  merge.initialize = initializeMerger;
113
127
 
114
- /**
115
- * Create a merger with predefined options
116
- * @param options Merging options
117
- * @returns Merger function
118
- */
119
- function initializeMerger(options?: MergeOptions): Merger {
120
- const actual = getMergeOptions(options);
121
-
122
- return <Model extends ArrayOrPlainObject>(values: NestedPartial<Model>[]): Model =>
123
- handleMerge(values, actual) as Model;
124
- }
125
-
126
128
  function mergeObjects(
127
129
  values: ArrayOrPlainObject[],
128
130
  options: Options,