@oscarpalmer/atoms 0.205.0 → 0.207.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 (58) hide show
  1. package/dist/array/reverse.mjs +2 -1
  2. package/dist/array/single.d.mts +3 -3
  3. package/dist/array/sort.mjs +3 -2
  4. package/dist/color/misc/alpha.mjs +1 -1
  5. package/dist/color/misc/get.mjs +1 -1
  6. package/dist/color/space/hsl.mjs +7 -2
  7. package/dist/color/space/hwb.mjs +2 -1
  8. package/dist/color/space/rgb.mjs +22 -13
  9. package/dist/function/retry.mjs +12 -14
  10. package/dist/index.d.mts +105 -55
  11. package/dist/index.mjs +323 -262
  12. package/dist/internal/array/insert.mjs +2 -1
  13. package/dist/internal/function/limit.mjs +9 -9
  14. package/dist/internal/function/work.d.mts +1 -1
  15. package/dist/internal/math/aggregate.d.mts +41 -0
  16. package/dist/internal/math/aggregate.mjs +4 -1
  17. package/dist/internal/math/misc.d.mts +8 -0
  18. package/dist/internal/math/misc.mjs +11 -1
  19. package/dist/internal/models/plan.model.d.mts +23 -0
  20. package/dist/internal/models/plan.model.mjs +7 -0
  21. package/dist/internal/{result/models.d.mts → models/result.model.d.mts} +1 -1
  22. package/dist/internal/random.mjs +2 -1
  23. package/dist/internal/result/attempt.d.mts +59 -0
  24. package/dist/internal/result/attempt.mjs +24 -0
  25. package/dist/internal/result/misc.d.mts +1 -1
  26. package/dist/internal/string/misc.mjs +4 -5
  27. package/dist/internal/value/equal.mjs +2 -1
  28. package/dist/internal/value/has.d.mts +1 -1
  29. package/dist/internal/value/misc.d.mts +1 -1
  30. package/dist/internal/value/misc.mjs +1 -1
  31. package/dist/math.d.mts +3 -52
  32. package/dist/math.mjs +3 -16
  33. package/dist/plan/index.d.mts +38 -0
  34. package/dist/plan/index.mjs +61 -0
  35. package/dist/plan/models.d.mts +2 -0
  36. package/dist/plan/models.mjs +1 -0
  37. package/dist/promise/constants.d.mts +0 -1
  38. package/dist/promise/constants.mjs +1 -2
  39. package/dist/promise/helpers.mjs +1 -1
  40. package/dist/promise/index.mjs +9 -9
  41. package/dist/promise/misc.d.mts +1 -1
  42. package/dist/promise/misc.mjs +1 -1
  43. package/dist/promise/models.d.mts +1 -1
  44. package/dist/queue.mjs +11 -6
  45. package/dist/result/index.d.mts +2 -59
  46. package/dist/result/index.mjs +1 -23
  47. package/dist/result/match.d.mts +1 -1
  48. package/dist/result/misc.d.mts +1 -1
  49. package/dist/result/work/flow.d.mts +1 -1
  50. package/dist/result/work/flow.mjs +2 -1
  51. package/dist/result/work/pipe.d.mts +1 -1
  52. package/dist/result/work/pipe.mjs +2 -1
  53. package/dist/string/fuzzy.mjs +7 -5
  54. package/dist/string/index.mjs +2 -1
  55. package/dist/value/clone.mjs +5 -8
  56. package/dist/value/diff.mjs +3 -2
  57. package/package.json +9 -4
  58. /package/dist/internal/{result/models.mjs → models/result.model.mjs} +0 -0
@@ -1,6 +1,7 @@
1
1
  import { isArrayOrPlainObject } from "../internal/is.mjs";
2
2
  import { createAborter } from "../internal/aborter.mjs";
3
- import { PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_ITEMS_EMPTY, PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./constants.mjs";
3
+ import { asyncAttempt } from "../internal/result/attempt.mjs";
4
+ import { PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./constants.mjs";
4
5
  import { createPromiseOptions, createPromisesOptions, getResultsFromPromises } from "./helpers.mjs";
5
6
  import { handleResult, settlePromise } from "./misc.mjs";
6
7
  import { getTimedPromise } from "./timed.mjs";
@@ -14,13 +15,13 @@ async function attemptPromise(value, options) {
14
15
  rejector(signal?.reason);
15
16
  });
16
17
  async function handler(resolve, reject) {
17
- try {
18
+ const result = await asyncAttempt(async () => {
18
19
  let result = isFunction ? value() : await value;
19
20
  if (result instanceof Promise) result = await result;
20
- settlePromise(resolve, result, aborter);
21
- } catch (error) {
22
- settlePromise(reject, error, aborter);
23
- }
21
+ return result;
22
+ });
23
+ if (result.ok) settlePromise(resolve, result.value, aborter);
24
+ else settlePromise(reject, result.error, aborter);
24
25
  }
25
26
  let rejector;
26
27
  const promise = new Promise((resolve, reject) => {
@@ -35,12 +36,11 @@ async function promises(items, options) {
35
36
  if (!isArrayOrPlainObject(items)) return Promise.reject(PROMISE_MESSAGE_EXPECTATION_ITEMS_TYPE);
36
37
  const isArray = Array.isArray(items);
37
38
  const actual = (isArray ? items.map((item, index) => [index, item]) : Object.entries(items)).map(([key, value]) => [key, typeof value === "function" ? value() : value]).filter(([, value]) => value instanceof Promise);
38
- const { length } = actual;
39
- if (length === 0) return Promise.reject(PROMISE_MESSAGE_EXPECTATION_ITEMS_EMPTY);
40
- const complete = strategy === PROMISE_STRATEGY_DEFAULT;
41
39
  const aborter = createAborter(signal, () => {
42
40
  handlers.reject(signal?.reason);
43
41
  });
42
+ const complete = strategy === PROMISE_STRATEGY_DEFAULT;
43
+ const { length } = actual;
44
44
  const data = {
45
45
  last: length - 1,
46
46
  result: isArray ? [] : {}
@@ -1,4 +1,4 @@
1
- import { Result } from "../internal/result/models.mjs";
1
+ import { Result } from "../internal/models/result.model.mjs";
2
2
  import { Aborter } from "../internal/aborter.mjs";
3
3
  import { CancelablePromise, PromiseParameters } from "./models.mjs";
4
4
  import { toPromise } from "../result/misc.mjs";
@@ -1,8 +1,8 @@
1
1
  import { error, ok } from "../internal/result/misc.mjs";
2
+ import { toPromise } from "../result/misc.mjs";
2
3
  import { PROMISE_MESSAGE_EXPECTATION_RESULT } from "./constants.mjs";
3
4
  import { CancelablePromise } from "./models.mjs";
4
5
  import { isFulfilled, isRejected } from "./helpers.mjs";
5
- import { toPromise } from "../result/misc.mjs";
6
6
  //#region src/promise/misc.ts
7
7
  /**
8
8
  * Create a cancelable _Promise_
@@ -1,5 +1,5 @@
1
1
  import { ArrayOrPlainObject, GenericCallback, Key } from "../models.mjs";
2
- import { Result } from "../internal/result/models.mjs";
2
+ import { Result } from "../internal/models/result.model.mjs";
3
3
  import { Aborter } from "../internal/aborter.mjs";
4
4
  import { PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./constants.mjs";
5
5
  //#region src/promise/models.d.ts
package/dist/queue.mjs CHANGED
@@ -1,4 +1,6 @@
1
1
  import { createAborter } from "./internal/aborter.mjs";
2
+ import { min } from "./internal/math/aggregate.mjs";
3
+ import { asyncAttempt } from "./internal/result/attempt.mjs";
2
4
  import { getBooleanOrDefault, getNumberOrDefault } from "./internal/defaults.mjs";
3
5
  //#region src/queue.ts
4
6
  const QUEUE_NAME_KEYED = "keyedQueue";
@@ -333,7 +335,7 @@ function resumeQueue() {
333
335
  for (let index = 0; index < length; index += 1) handled[index]();
334
336
  }
335
337
  state.paused = false;
336
- const length = Math.min(state.options.concurrency, state.items.length);
338
+ const length = min([state.options.concurrency, state.items.length]);
337
339
  for (let index = 0; index < length; index += 1) run(state);
338
340
  }
339
341
  function resumeQueues(key) {
@@ -346,14 +348,17 @@ async function run(state) {
346
348
  while (item != null) {
347
349
  let error = false;
348
350
  let result;
349
- try {
350
- if (!(item.aborter?.signal?.aborted ?? false)) {
351
+ const attempted = await asyncAttempt(async () => {
352
+ if (item != null && !(item.aborter?.signal?.aborted ?? false)) {
351
353
  const parameters = item.key == null ? item.parameters : [item.key, ...item.parameters];
352
- result = await state.callback(...parameters);
354
+ return await state.callback(...parameters);
353
355
  }
354
- } catch (thrown) {
356
+ throw new Error();
357
+ });
358
+ if (attempted.ok) result = attempted.value;
359
+ else {
355
360
  error = true;
356
- result = thrown;
361
+ result = attempted.error;
357
362
  }
358
363
  if (state.paused) {
359
364
  const paused = item;
@@ -1,59 +1,2 @@
1
- import { ExtendedResult, Result } from "../internal/result/models.mjs";
2
- //#region src/result/index.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
1
+ import { asyncAttempt, attempt } from "../internal/result/attempt.mjs";
2
+ export { asyncAttempt, attempt };
@@ -1,24 +1,2 @@
1
- import { getError, ok } from "../internal/result/misc.mjs";
2
- //#region src/result/index.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
1
+ import { asyncAttempt, attempt } from "../internal/result/attempt.mjs";
24
2
  export { asyncAttempt, attempt };
@@ -1,4 +1,4 @@
1
- import { AnyResult, ResultMatch } from "../internal/result/models.mjs";
1
+ import { AnyResult, ResultMatch } from "../internal/models/result.model.mjs";
2
2
  //#region src/result/match.d.ts
3
3
  /**
4
4
  * Handles a _Result_ with match callbacks
@@ -1,4 +1,4 @@
1
- import { AnyResult } from "../internal/result/models.mjs";
1
+ import { AnyResult } from "../internal/models/result.model.mjs";
2
2
  import { error, isError, isOk, isResult, ok, unwrap } from "../internal/result/misc.mjs";
3
3
  //#region src/result/misc.d.ts
4
4
  /**
@@ -1,5 +1,5 @@
1
1
  import { GenericCallback } from "../../models.mjs";
2
- import { Result, UnwrapValue } from "../../internal/result/models.mjs";
2
+ import { Result, UnwrapValue } from "../../internal/models/result.model.mjs";
3
3
  //#region src/result/work/flow.d.ts
4
4
  /**
5
5
  * A synchronous _Flow_, a function that attempts to pipe values through a series of functions
@@ -1,6 +1,7 @@
1
1
  import { isError, isOk } from "../../internal/result/misc.mjs";
2
+ import { asyncAttempt, attempt } from "../../internal/result/attempt.mjs";
2
3
  import { asyncFlow, flow } from "../../internal/function/work.mjs";
3
- import { asyncAttempt, attempt } from "../index.mjs";
4
+ import "../index.mjs";
4
5
  //#region src/result/work/flow.ts
5
6
  function attemptAsyncFlow(...fns) {
6
7
  let Flow;
@@ -1,5 +1,5 @@
1
1
  import { GenericCallback } from "../../models.mjs";
2
- import { Result, UnwrapValue } from "../../internal/result/models.mjs";
2
+ import { Result, UnwrapValue } from "../../internal/models/result.model.mjs";
3
3
  //#region src/result/work/pipe.d.ts
4
4
  type WrapValue<Value> = Result<UnwrapValue<Value>>;
5
5
  /**
@@ -1,6 +1,7 @@
1
1
  import { isError, isOk } from "../../internal/result/misc.mjs";
2
+ import { asyncAttempt, attempt } from "../../internal/result/attempt.mjs";
2
3
  import { asyncPipe, pipe } from "../../internal/function/work.mjs";
3
- import { asyncAttempt, attempt } from "../index.mjs";
4
+ import "../index.mjs";
4
5
  //#region src/result/work/pipe.ts
5
6
  async function attemptAsyncPipe(initial, first, ...seconds) {
6
7
  return asyncAttempt(() => {
@@ -1,4 +1,6 @@
1
1
  import { isPlainObject } from "../internal/is.mjs";
2
+ import { floor } from "../internal/math/misc.mjs";
3
+ import { max } from "../internal/math/aggregate.mjs";
2
4
  import { getString } from "../internal/string/misc.mjs";
3
5
  import { lowerCase } from "../internal/string/case.mjs";
4
6
  import { includes } from "./match.mjs";
@@ -27,7 +29,7 @@ function createFuzzyItems(items) {
27
29
  function createFuzzyOptions(input, state) {
28
30
  const options = isPlainObject(input) ? input : {};
29
31
  const limit = typeof input === "number" ? input : options.limit;
30
- if (typeof limit === "number" && !Number.isNaN(limit) && limit >= 1) options.limit = Math.floor(limit);
32
+ if (typeof limit === "number" && !Number.isNaN(limit) && limit >= 1) options.limit = floor(limit);
31
33
  else options.limit = state?.limit;
32
34
  options.tolerance = getTolerance(options.tolerance, state);
33
35
  return options;
@@ -92,18 +94,18 @@ function getScore(haystack, needle) {
92
94
  if (haystackIndex === 0) score += 1;
93
95
  if (previousMatchIndex !== -1) {
94
96
  const gap = haystackIndex - previousMatchIndex - 1;
95
- score += Math.max(0, FUZZY_PROXIMITY_THRESHOLD - gap);
97
+ score += max([0, FUZZY_PROXIMITY_THRESHOLD - gap]);
96
98
  }
97
99
  previousMatchIndex = haystackIndex;
98
100
  needleIndex += 1;
99
101
  }
100
102
  if (needleIndex === needleLength) break;
101
103
  }
102
- score -= Math.floor(lowerCaseHaystack.length / FUZZY_LENGTH_DIVISOR);
103
- return Math.max(0, score);
104
+ score -= floor(lowerCaseHaystack.length / FUZZY_LENGTH_DIVISOR);
105
+ return max([0, score]);
104
106
  }
105
107
  function getTolerance(input, state) {
106
- if (typeof input === "number" && !Number.isNaN(input)) return input < 0 ? 0 : Math.floor(input);
108
+ if (typeof input === "number" && !Number.isNaN(input)) return input < 0 ? 0 : floor(input);
107
109
  return state?.tolerance ?? FUZZY_PROXIMITY_THRESHOLD;
108
110
  }
109
111
  /**
@@ -1,4 +1,5 @@
1
1
  import { isTemplateStringsArray } from "../internal/is.mjs";
2
+ import { min } from "../internal/math/aggregate.mjs";
2
3
  import { getString, interpolate, join, words } from "../internal/string/misc.mjs";
3
4
  //#region src/string/index.ts
4
5
  function dedent(value, ...values) {
@@ -16,7 +17,7 @@ function dedent(value, ...values) {
16
17
  if (indentation != null) lengths.push(indentation.length);
17
18
  }
18
19
  if (lengths.length === 0) return actual.trim();
19
- const minimum = Math.min(...lengths);
20
+ const minimum = min(lengths);
20
21
  const pattern = new RegExp(`^\\s{0,${minimum}}`);
21
22
  let result = "";
22
23
  for (let index = 0; index < length; index += 1) {
@@ -1,4 +1,6 @@
1
1
  import { isArrayOrPlainObject, isTypedArray } from "../internal/is.mjs";
2
+ import { unwrap } from "../internal/result/misc.mjs";
3
+ import { attempt } from "../internal/result/attempt.mjs";
2
4
  import { createValueHandler } from "../internal/value/handlers.mjs";
3
5
  //#region src/value/clone.ts
4
6
  const CLONE_NAME = "clone";
@@ -154,14 +156,9 @@ function registerCloner(constructor, handler) {
154
156
  }
155
157
  function tryStructuredClone(value, parameters, depth) {
156
158
  if (depth >= CLONE_MAX_DEPTH) return value;
157
- try {
158
- const cloned = structuredClone(value);
159
- parameters.references.set(value, cloned);
160
- return cloned;
161
- } catch {
162
- parameters.references.set(value, value);
163
- return value;
164
- }
159
+ const cloned = unwrap(attempt(() => structuredClone(value)), value);
160
+ parameters.references.set(value, cloned);
161
+ return cloned;
165
162
  }
166
163
  const CLONE_COPY_OPTIONS = {
167
164
  copyFunctions: true,
@@ -1,4 +1,5 @@
1
1
  import { isArrayOrPlainObject } from "../internal/is.mjs";
2
+ import { max, min } from "../internal/math/aggregate.mjs";
2
3
  import { join } from "../internal/string/misc.mjs";
3
4
  import { equal } from "../internal/value/equal.mjs";
4
5
  //#region src/value/diff.ts
@@ -70,8 +71,8 @@ function getChanges(changes, first, second, options, prefix) {
70
71
  function getDiffs(first, second, options, prefix) {
71
72
  const changes = [];
72
73
  if (Array.isArray(first) && Array.isArray(second)) {
73
- const maximumLength = Math.max(first.length, second.length);
74
- const minimumLength = Math.min(first.length, second.length);
74
+ const maximumLength = max([first.length, second.length]);
75
+ const minimumLength = min([first.length, second.length]);
75
76
  for (let index = minimumLength; index < maximumLength; index += 1) {
76
77
  const key = join([prefix, index], ".");
77
78
  changes.push({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oscarpalmer/atoms",
3
- "version": "0.205.0",
3
+ "version": "0.207.0",
4
4
  "description": "Atomic utilities for making your JavaScript better.",
5
5
  "keywords": [
6
6
  "array",
@@ -149,6 +149,13 @@
149
149
  "types": "./dist/number.d.mts",
150
150
  "default": "./dist/number.mjs"
151
151
  },
152
+ "./plan": {
153
+ "types": "./dist/plan/index.d.mts",
154
+ "default": "./dist/plan/index.mjs"
155
+ },
156
+ "./plan/models": {
157
+ "default": "./dist/plan/models.mjs"
158
+ },
152
159
  "./promise": {
153
160
  "types": "./dist/promise/index.d.mts",
154
161
  "default": "./dist/promise/index.mjs"
@@ -162,7 +169,6 @@
162
169
  "default": "./dist/promise/misc.mjs"
163
170
  },
164
171
  "./promise/models": {
165
- "types": "./dist/promise/models.d.mts",
166
172
  "default": "./dist/promise/models.mjs"
167
173
  },
168
174
  "./promise/timed": {
@@ -198,8 +204,7 @@
198
204
  "default": "./dist/result/misc.mjs"
199
205
  },
200
206
  "./result/models": {
201
- "types": "./dist/internal/result/models.d.mts",
202
- "default": "./dist/internal/result/models.mjs"
207
+ "default": "./dist/internal/models/result.model.mjs"
203
208
  },
204
209
  "./result/pipe": {
205
210
  "types": "./dist/result/work/pipe.d.mts",