@oscarpalmer/atoms 0.144.0 → 0.145.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.
@@ -901,6 +901,64 @@ const DEFAULT_CACHE_SIZE = 1024;
901
901
  function throttle(callback, time) {
902
902
  return getLimiter(callback, true, time);
903
903
  }
904
+ function assert(condition, message, error) {
905
+ if (!condition()) throw new (error ?? Error)(message);
906
+ }
907
+ assert.condition = (condition, message, error) => {
908
+ return (value) => {
909
+ assert(() => condition(value), message, error);
910
+ };
911
+ };
912
+ assert.instanceOf = (constructor, message, error) => {
913
+ return (value) => {
914
+ assert(() => value instanceof constructor, message, error);
915
+ };
916
+ };
917
+ assert.is = (condition, message, error) => {
918
+ return (value) => {
919
+ assert(() => condition(value), message, error);
920
+ };
921
+ };
922
+ function asyncFlow(...fns) {
923
+ assertFlowFunctions(fns);
924
+ return (...args) => asyncWork(args, fns, true);
925
+ }
926
+ function flow(...fns) {
927
+ assertFlowFunctions(fns);
928
+ return (...args) => work(args, fns, true);
929
+ }
930
+ flow.async = asyncFlow;
931
+ async function asyncPipe(value, ...pipes) {
932
+ assertPipeFunctions(pipes);
933
+ return asyncWork(value, pipes, false);
934
+ }
935
+ function pipe(value, ...pipes) {
936
+ assertPipeFunctions(pipes);
937
+ return work(value, pipes, false);
938
+ }
939
+ pipe.async = asyncPipe;
940
+ async function asyncWork(initial, functions, flow) {
941
+ const { length } = functions;
942
+ let transformed = initial;
943
+ for (let index = 0; index < length; index += 1) {
944
+ const fn = functions[index];
945
+ transformed = flow && index === 0 && Array.isArray(initial) ? await fn(...initial) : await fn(transformed);
946
+ }
947
+ return transformed;
948
+ }
949
+ function work(initial, functions, flow) {
950
+ const { length } = functions;
951
+ let transformed = initial;
952
+ for (let index = 0; index < length; index += 1) {
953
+ const fn = functions[index];
954
+ transformed = flow && index === 0 && Array.isArray(initial) ? fn(...initial) : fn(transformed);
955
+ }
956
+ return transformed;
957
+ }
958
+ const MESSAGE_FLOW = "Flow expected to receive an array of functions";
959
+ const MESSAGE_PIPE = "Pipe expected to receive an array of functions";
960
+ const assertFlowFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), MESSAGE_FLOW, TypeError);
961
+ const assertPipeFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), MESSAGE_PIPE, TypeError);
904
962
  function _getRandomFloat(inclusive, minimum, maximum) {
905
963
  let maxFloat = isNumber(maximum) && maximum <= Number.MAX_SAFE_INTEGER ? maximum : Number.MAX_SAFE_INTEGER;
906
964
  let minFloat = isNumber(minimum) && minimum >= Number.MIN_SAFE_INTEGER ? minimum : Number.MIN_SAFE_INTEGER;
@@ -2630,6 +2688,15 @@ function isEmpty(value) {
2630
2688
  return true;
2631
2689
  }
2632
2690
  /**
2691
+ * Is the value an instance of the constructor?
2692
+ * @param constructor Class constructor
2693
+ * @param value Value to check
2694
+ * @returns `true` if the value is an instance of the constructor, otherwise `false`
2695
+ */
2696
+ function isInstanceOf(constructor, value) {
2697
+ return isConstructor(constructor) && value instanceof constructor;
2698
+ }
2699
+ /**
2633
2700
  * Is the value not `undefined` or `null`?
2634
2701
  * @param value Value to check
2635
2702
  * @returns `true` if the value is not `undefined` or `null`, otherwise `false`
@@ -2893,6 +2960,31 @@ var PromiseTimeoutError = class extends Error {
2893
2960
  this.name = ERROR_NAME$1;
2894
2961
  }
2895
2962
  };
2963
+ async function attemptPromise(value, options) {
2964
+ const isFunction = typeof value === "function";
2965
+ if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_ATTEMPT));
2966
+ const { signal, time } = getPromiseOptions(options);
2967
+ if (signal?.aborted ?? false) return Promise.reject(signal.reason);
2968
+ function abort() {
2969
+ rejector(signal.reason);
2970
+ }
2971
+ async function handler(resolve, reject) {
2972
+ try {
2973
+ let result = isFunction ? value() : await value;
2974
+ if (result instanceof Promise) result = await result;
2975
+ settlePromise(abort, resolve, result, signal);
2976
+ } catch (error) {
2977
+ settlePromise(abort, reject, error, signal);
2978
+ }
2979
+ }
2980
+ let rejector;
2981
+ signal?.addEventListener(EVENT_NAME$1, abort, ABORT_OPTIONS);
2982
+ const promise = new Promise((resolve, reject) => {
2983
+ rejector = reject;
2984
+ handler(resolve, reject);
2985
+ });
2986
+ return time > 0 ? getTimed(promise, time, signal) : promise;
2987
+ }
2896
2988
  /**
2897
2989
  * Create a cancelable promise
2898
2990
  * @param executor Executor function for the promise
@@ -3053,31 +3145,6 @@ async function timed(promise, options) {
3053
3145
  if (signal?.aborted ?? false) return Promise.reject(signal.reason);
3054
3146
  return time > 0 ? getTimed(promise, time, signal) : promise;
3055
3147
  }
3056
- async function attemptPromise(value, options) {
3057
- const isFunction = typeof value === "function";
3058
- if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_ATTEMPT));
3059
- const { signal, time } = getPromiseOptions(options);
3060
- if (signal?.aborted ?? false) return Promise.reject(signal.reason);
3061
- function abort() {
3062
- rejector(signal.reason);
3063
- }
3064
- async function handler(resolve, reject) {
3065
- try {
3066
- let result = isFunction ? value() : await value;
3067
- if (result instanceof Promise) result = await result;
3068
- settlePromise(abort, resolve, result, signal);
3069
- } catch (error) {
3070
- settlePromise(abort, reject, error, signal);
3071
- }
3072
- }
3073
- let rejector;
3074
- signal?.addEventListener(EVENT_NAME$1, abort, ABORT_OPTIONS);
3075
- const promise = new Promise((resolve, reject) => {
3076
- rejector = reject;
3077
- handler(resolve, reject);
3078
- });
3079
- return time > 0 ? getTimed(promise, time, signal) : promise;
3080
- }
3081
3148
  const ABORT_OPTIONS = { once: true };
3082
3149
  const DEFAULT_STRATEGY = "complete";
3083
3150
  const ERROR_NAME$1 = "PromiseTimeoutError";
@@ -3420,6 +3487,24 @@ function _isResult(value, okValue) {
3420
3487
  if (!isPlainObject(value)) return false;
3421
3488
  return value.ok === okValue && (okValue ? "value" : "error") in value;
3422
3489
  }
3490
+ async function asyncAttempt(value, err) {
3491
+ try {
3492
+ let result = typeof value === "function" ? value() : await value;
3493
+ if (result instanceof Promise) result = await result;
3494
+ return ok(result);
3495
+ } catch (thrown) {
3496
+ return getError(err ?? thrown, err == null ? void 0 : thrown);
3497
+ }
3498
+ }
3499
+ function attempt(callback, err) {
3500
+ try {
3501
+ return ok(callback());
3502
+ } catch (thrown) {
3503
+ return getError(err ?? thrown, err == null ? void 0 : thrown);
3504
+ }
3505
+ }
3506
+ attempt.async = asyncAttempt;
3507
+ attempt.promise = attemptPromise;
3423
3508
  function error(value, original) {
3424
3509
  return getError(value, original);
3425
3510
  }
@@ -3461,22 +3546,6 @@ function ok(value) {
3461
3546
  value
3462
3547
  };
3463
3548
  }
3464
- function attempt(callback, err) {
3465
- try {
3466
- return ok(callback());
3467
- } catch (thrown) {
3468
- return getError(err ?? thrown, err == null ? void 0 : thrown);
3469
- }
3470
- }
3471
- attempt.async = asyncAttempt;
3472
- attempt.promise = attemptPromise;
3473
- async function asyncAttempt(value, err) {
3474
- try {
3475
- return ok(await (typeof value === "function" ? value() : value));
3476
- } catch (thrown) {
3477
- return getError(err ?? thrown, err == null ? void 0 : thrown);
3478
- }
3479
- }
3480
3549
  function unwrap(value, defaultValue) {
3481
3550
  return isOk(value) ? value.value : defaultValue;
3482
3551
  }
@@ -3532,4 +3601,4 @@ var SizedSet = class extends Set {
3532
3601
  }
3533
3602
  }
3534
3603
  };
3535
- export { CancelablePromise, frame_rate_default as FRAME_RATE_MS, PromiseTimeoutError, QueueError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, floor, fromQuery, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, promises, push, queue, range, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, titleCase, toMap, toQuery, toRecord, toSet, toggle, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, update, upperCase, words };
3604
+ export { CancelablePromise, frame_rate_default as FRAME_RATE_MS, PromiseTimeoutError, QueueError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, floor, flow, fromQuery, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, titleCase, toMap, toQuery, toRecord, toSet, toggle, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, update, upperCase, words };
@@ -0,0 +1,19 @@
1
+ function assert(condition, message, error) {
2
+ if (!condition()) throw new (error ?? Error)(message);
3
+ }
4
+ assert.condition = (condition, message, error) => {
5
+ return (value) => {
6
+ assert(() => condition(value), message, error);
7
+ };
8
+ };
9
+ assert.instanceOf = (constructor, message, error) => {
10
+ return (value) => {
11
+ assert(() => value instanceof constructor, message, error);
12
+ };
13
+ };
14
+ assert.is = (condition, message, error) => {
15
+ return (value) => {
16
+ assert(() => condition(value), message, error);
17
+ };
18
+ };
19
+ export { assert };
@@ -0,0 +1,42 @@
1
+ import { assert } from "./assert.js";
2
+ function asyncFlow(...fns) {
3
+ assertFlowFunctions(fns);
4
+ return (...args) => asyncWork(args, fns, true);
5
+ }
6
+ function flow(...fns) {
7
+ assertFlowFunctions(fns);
8
+ return (...args) => work(args, fns, true);
9
+ }
10
+ flow.async = asyncFlow;
11
+ async function asyncPipe(value, ...pipes) {
12
+ assertPipeFunctions(pipes);
13
+ return asyncWork(value, pipes, false);
14
+ }
15
+ function pipe(value, ...pipes) {
16
+ assertPipeFunctions(pipes);
17
+ return work(value, pipes, false);
18
+ }
19
+ pipe.async = asyncPipe;
20
+ async function asyncWork(initial, functions, flow) {
21
+ const { length } = functions;
22
+ let transformed = initial;
23
+ for (let index = 0; index < length; index += 1) {
24
+ const fn = functions[index];
25
+ transformed = flow && index === 0 && Array.isArray(initial) ? await fn(...initial) : await fn(transformed);
26
+ }
27
+ return transformed;
28
+ }
29
+ function work(initial, functions, flow) {
30
+ const { length } = functions;
31
+ let transformed = initial;
32
+ for (let index = 0; index < length; index += 1) {
33
+ const fn = functions[index];
34
+ transformed = flow && index === 0 && Array.isArray(initial) ? fn(...initial) : fn(transformed);
35
+ }
36
+ return transformed;
37
+ }
38
+ var MESSAGE_FLOW = "Flow expected to receive an array of functions";
39
+ var MESSAGE_PIPE = "Pipe expected to receive an array of functions";
40
+ var assertFlowFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), MESSAGE_FLOW, TypeError);
41
+ var assertPipeFunctions = assert.condition((value) => Array.isArray(value) && value.every((item) => typeof item === "function"), MESSAGE_PIPE, TypeError);
42
+ export { flow, pipe };
package/dist/index.js CHANGED
@@ -38,6 +38,7 @@ import { debounce } from "./function/debounce.js";
38
38
  import { SizedMap } from "./sized/map.js";
39
39
  import { memoize } from "./function/memoize.js";
40
40
  import { throttle } from "./function/throttle.js";
41
+ import { flow, pipe } from "./function/work.js";
41
42
  import { getRandomFloat, getRandomInteger } from "./internal/random.js";
42
43
  import { shuffle } from "./internal/array/shuffle.js";
43
44
  import { equal } from "./internal/value/equal.js";
@@ -54,7 +55,7 @@ import { omit } from "./value/omit.js";
54
55
  import { pick } from "./value/pick.js";
55
56
  import { smush } from "./value/smush.js";
56
57
  import { unsmush } from "./value/unsmush.js";
57
- import { isEmpty, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumerical, isObject, isPrimitive } from "./is.js";
58
+ import { isEmpty, isInstanceOf, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumerical, isObject, isPrimitive } from "./is.js";
58
59
  import { logger } from "./logger.js";
59
60
  import { average, ceil, count, floor, median, min, round, sum } from "./math.js";
60
61
  import { CancelablePromise, PromiseTimeoutError, attemptPromise, cancelable, delay, isFulfilled, isRejected, promises, timed } from "./promise.js";
@@ -63,4 +64,4 @@ import { QueueError, queue } from "./queue.js";
63
64
  import { getRandomBoolean, getRandomCharacters, getRandomColor, getRandomHex, getRandomItem, getRandomItems } from "./random.js";
64
65
  import { attempt, error, isError, isOk, isResult, ok, unwrap } from "./result.js";
65
66
  import { SizedSet } from "./sized/set.js";
66
- export { CancelablePromise, frame_rate_default as FRAME_RATE_MS, PromiseTimeoutError, QueueError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, floor, fromQuery, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, promises, push, queue, range, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, titleCase, toMap, toQuery, toRecord, toSet, toggle, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, update, upperCase, words };
67
+ export { CancelablePromise, frame_rate_default as FRAME_RATE_MS, PromiseTimeoutError, QueueError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, floor, flow, fromQuery, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, titleCase, toMap, toQuery, toRecord, toSet, toggle, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, update, upperCase, words };
package/dist/is.js CHANGED
@@ -15,6 +15,15 @@ function isEmpty(value) {
15
15
  return true;
16
16
  }
17
17
  /**
18
+ * Is the value an instance of the constructor?
19
+ * @param constructor Class constructor
20
+ * @param value Value to check
21
+ * @returns `true` if the value is an instance of the constructor, otherwise `false`
22
+ */
23
+ function isInstanceOf(constructor, value) {
24
+ return isConstructor(constructor) && value instanceof constructor;
25
+ }
26
+ /**
18
27
  * Is the value not `undefined` or `null`?
19
28
  * @param value Value to check
20
29
  * @returns `true` if the value is not `undefined` or `null`, otherwise `false`
@@ -72,4 +81,4 @@ function isPrimitive(value) {
72
81
  }
73
82
  var EXPRESSION_PRIMITIVE = /^(bigint|boolean|number|string|symbol)$/;
74
83
  var EXPRESSION_WHITESPACE = /^\s*$/;
75
- export { isArrayOrPlainObject, isConstructor, isEmpty, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isPlainObject, isPrimitive, isTypedArray };
84
+ export { isArrayOrPlainObject, isConstructor, isEmpty, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isPlainObject, isPrimitive, isTypedArray };
package/dist/promise.js CHANGED
@@ -22,6 +22,31 @@ var PromiseTimeoutError = class extends Error {
22
22
  this.name = ERROR_NAME;
23
23
  }
24
24
  };
25
+ async function attemptPromise(value, options) {
26
+ const isFunction = typeof value === "function";
27
+ if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_ATTEMPT));
28
+ const { signal, time } = getPromiseOptions(options);
29
+ if (signal?.aborted ?? false) return Promise.reject(signal.reason);
30
+ function abort() {
31
+ rejector(signal.reason);
32
+ }
33
+ async function handler(resolve, reject) {
34
+ try {
35
+ let result = isFunction ? value() : await value;
36
+ if (result instanceof Promise) result = await result;
37
+ settlePromise(abort, resolve, result, signal);
38
+ } catch (error) {
39
+ settlePromise(abort, reject, error, signal);
40
+ }
41
+ }
42
+ let rejector;
43
+ signal?.addEventListener(EVENT_NAME, abort, ABORT_OPTIONS);
44
+ const promise = new Promise((resolve, reject) => {
45
+ rejector = reject;
46
+ handler(resolve, reject);
47
+ });
48
+ return time > 0 ? getTimed(promise, time, signal) : promise;
49
+ }
25
50
  /**
26
51
  * Create a cancelable promise
27
52
  * @param executor Executor function for the promise
@@ -182,31 +207,6 @@ async function timed(promise, options) {
182
207
  if (signal?.aborted ?? false) return Promise.reject(signal.reason);
183
208
  return time > 0 ? getTimed(promise, time, signal) : promise;
184
209
  }
185
- async function attemptPromise(value, options) {
186
- const isFunction = typeof value === "function";
187
- if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_ATTEMPT));
188
- const { signal, time } = getPromiseOptions(options);
189
- if (signal?.aborted ?? false) return Promise.reject(signal.reason);
190
- function abort() {
191
- rejector(signal.reason);
192
- }
193
- async function handler(resolve, reject) {
194
- try {
195
- let result = isFunction ? value() : await value;
196
- if (result instanceof Promise) result = await result;
197
- settlePromise(abort, resolve, result, signal);
198
- } catch (error) {
199
- settlePromise(abort, reject, error, signal);
200
- }
201
- }
202
- let rejector;
203
- signal?.addEventListener(EVENT_NAME, abort, ABORT_OPTIONS);
204
- const promise = new Promise((resolve, reject) => {
205
- rejector = reject;
206
- handler(resolve, reject);
207
- });
208
- return time > 0 ? getTimed(promise, time, signal) : promise;
209
- }
210
210
  var ABORT_OPTIONS = { once: true };
211
211
  var DEFAULT_STRATEGY = "complete";
212
212
  var ERROR_NAME = "PromiseTimeoutError";
package/dist/result.js CHANGED
@@ -4,6 +4,24 @@ function _isResult(value, okValue) {
4
4
  if (!isPlainObject(value)) return false;
5
5
  return value.ok === okValue && (okValue ? "value" : "error") in value;
6
6
  }
7
+ async function asyncAttempt(value, err) {
8
+ try {
9
+ let result = typeof value === "function" ? value() : await value;
10
+ if (result instanceof Promise) result = await result;
11
+ return ok(result);
12
+ } catch (thrown) {
13
+ return getError(err ?? thrown, err == null ? void 0 : thrown);
14
+ }
15
+ }
16
+ function attempt(callback, err) {
17
+ try {
18
+ return ok(callback());
19
+ } catch (thrown) {
20
+ return getError(err ?? thrown, err == null ? void 0 : thrown);
21
+ }
22
+ }
23
+ attempt.async = asyncAttempt;
24
+ attempt.promise = attemptPromise;
7
25
  function error(value, original) {
8
26
  return getError(value, original);
9
27
  }
@@ -45,22 +63,6 @@ function ok(value) {
45
63
  value
46
64
  };
47
65
  }
48
- function attempt(callback, err) {
49
- try {
50
- return ok(callback());
51
- } catch (thrown) {
52
- return getError(err ?? thrown, err == null ? void 0 : thrown);
53
- }
54
- }
55
- attempt.async = asyncAttempt;
56
- attempt.promise = attemptPromise;
57
- async function asyncAttempt(value, err) {
58
- try {
59
- return ok(await (typeof value === "function" ? value() : value));
60
- } catch (thrown) {
61
- return getError(err ?? thrown, err == null ? void 0 : thrown);
62
- }
63
- }
64
66
  function unwrap(value, defaultValue) {
65
67
  return isOk(value) ? value.value : defaultValue;
66
68
  }
package/package.json CHANGED
@@ -138,6 +138,10 @@
138
138
  "types": "./types/function/throttle.d.ts",
139
139
  "default": "./dist/function/throttle.js"
140
140
  },
141
+ "./function/work": {
142
+ "types": "./types/function/work.d.ts",
143
+ "default": "./dist/function/work.js"
144
+ },
141
145
  "./is": {
142
146
  "types": "./types/is.d.ts",
143
147
  "default": "./dist/is.js"
@@ -256,5 +260,5 @@
256
260
  },
257
261
  "type": "module",
258
262
  "types": "./types/index.d.ts",
259
- "version": "0.144.0"
263
+ "version": "0.145.0"
260
264
  }
@@ -0,0 +1,43 @@
1
+ import type {Constructor} from '../models';
2
+
3
+ export type Asserter<Value> = (value: unknown) => asserts value is Value;
4
+
5
+ export function assert<Condition extends () => boolean>(
6
+ condition: Condition,
7
+ message: string,
8
+ error?: ErrorConstructor,
9
+ ): asserts condition {
10
+ if (!condition()) {
11
+ throw new (error ?? Error)(message);
12
+ }
13
+ }
14
+
15
+ assert.condition = <Value>(
16
+ condition: (value: unknown) => boolean,
17
+ message: string,
18
+ error?: ErrorConstructor,
19
+ ): Asserter<Value> => {
20
+ return value => {
21
+ assert(() => condition(value), message, error);
22
+ };
23
+ };
24
+
25
+ assert.instanceOf = <Value>(
26
+ constructor: Constructor<Value>,
27
+ message: string,
28
+ error?: ErrorConstructor,
29
+ ): Asserter<Value> => {
30
+ return value => {
31
+ assert(() => value instanceof constructor, message, error);
32
+ };
33
+ };
34
+
35
+ assert.is = <Value>(
36
+ condition: (value: unknown) => value is Value,
37
+ message: string,
38
+ error?: ErrorConstructor,
39
+ ): Asserter<Value> => {
40
+ return value => {
41
+ assert(() => condition(value), message, error);
42
+ };
43
+ };