atomic-fns 1.2.0 → 2.0.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 (216) hide show
  1. package/README.md +397 -91
  2. package/dist/array/index.d.ts +9 -0
  3. package/dist/array/index.js +9 -0
  4. package/dist/array/list.d.ts +99 -0
  5. package/dist/array/list.js +133 -0
  6. package/dist/array/sets.d.ts +130 -0
  7. package/dist/array/sets.js +213 -0
  8. package/dist/async/index.d.ts +6 -0
  9. package/dist/async/index.js +6 -0
  10. package/dist/async/retry.d.ts +38 -0
  11. package/dist/async/retry.js +56 -0
  12. package/dist/async/sleep.d.ts +42 -0
  13. package/dist/async/sleep.js +76 -0
  14. package/dist/collections/bisect.d.ts +103 -0
  15. package/dist/collections/bisect.js +157 -0
  16. package/dist/collections/deque.d.ts +67 -97
  17. package/dist/collections/deque.js +197 -256
  18. package/dist/collections/entries.d.ts +89 -0
  19. package/dist/collections/entries.js +131 -0
  20. package/dist/collections/frozen-set.d.ts +42 -0
  21. package/dist/collections/frozen-set.js +64 -0
  22. package/dist/collections/group.d.ts +141 -0
  23. package/dist/collections/group.js +241 -0
  24. package/dist/collections/heap.d.ts +64 -0
  25. package/dist/collections/heap.js +205 -0
  26. package/dist/collections/index.d.ts +15 -623
  27. package/dist/collections/index.js +14 -844
  28. package/dist/collections/internal/arrayKeys.d.ts +19 -0
  29. package/dist/collections/internal/arrayKeys.js +83 -0
  30. package/dist/collections/internal/invariants.d.ts +4 -0
  31. package/dist/collections/internal/invariants.js +12 -0
  32. package/dist/collections/internal/iterable.d.ts +25 -0
  33. package/dist/collections/internal/iterable.js +62 -0
  34. package/dist/collections/iterate.d.ts +162 -0
  35. package/dist/collections/iterate.js +188 -0
  36. package/dist/collections/lru-cache.d.ts +58 -0
  37. package/dist/collections/lru-cache.js +164 -0
  38. package/dist/collections/order.d.ts +19 -0
  39. package/dist/collections/order.js +38 -0
  40. package/dist/collections/shape.d.ts +32 -0
  41. package/dist/collections/shape.js +86 -0
  42. package/dist/collections/sorted-map.d.ts +171 -0
  43. package/dist/collections/sorted-map.js +845 -0
  44. package/dist/collections/sorted-set.d.ts +103 -0
  45. package/dist/collections/sorted-set.js +147 -0
  46. package/dist/collections/splay-tree.d.ts +82 -0
  47. package/dist/collections/splay-tree.js +354 -0
  48. package/dist/collections/trie.d.ts +58 -0
  49. package/dist/collections/trie.js +196 -0
  50. package/dist/decimal/currency.d.ts +211 -0
  51. package/dist/decimal/currency.js +321 -0
  52. package/dist/decimal/decimal.d.ts +299 -0
  53. package/dist/decimal/decimal.js +660 -0
  54. package/dist/decimal/index.d.ts +11 -148
  55. package/dist/decimal/index.js +10 -410
  56. package/dist/decimal/internal/bits.d.ts +2 -0
  57. package/dist/decimal/internal/bits.js +7 -0
  58. package/dist/decimal/internal/echo.d.ts +4 -0
  59. package/dist/decimal/internal/echo.js +6 -0
  60. package/dist/decimal/internal/isqrt.d.ts +9 -0
  61. package/dist/decimal/internal/isqrt.js +34 -0
  62. package/dist/decimal/internal/locale.d.ts +7 -0
  63. package/dist/decimal/internal/locale.js +24 -0
  64. package/dist/decimal/internal/minor-units.d.ts +2 -0
  65. package/dist/decimal/internal/minor-units.js +33 -0
  66. package/dist/decimal/internal/pow10.d.ts +24 -0
  67. package/dist/decimal/internal/pow10.js +96 -0
  68. package/dist/encoding/base64.d.ts +39 -0
  69. package/dist/encoding/base64.js +53 -0
  70. package/dist/encoding/hex.d.ts +23 -0
  71. package/dist/encoding/hex.js +34 -0
  72. package/dist/encoding/index.d.ts +6 -0
  73. package/dist/encoding/index.js +6 -0
  74. package/dist/function/debounce.d.ts +68 -0
  75. package/dist/function/debounce.js +203 -0
  76. package/dist/function/index.d.ts +6 -0
  77. package/dist/function/index.js +6 -0
  78. package/dist/function/shared.d.ts +2 -0
  79. package/dist/function/shared.js +5 -0
  80. package/dist/function/wrap.d.ts +130 -0
  81. package/dist/function/wrap.js +113 -0
  82. package/dist/hash/digest.d.ts +18 -0
  83. package/dist/hash/digest.js +26 -0
  84. package/dist/hash/hash.d.ts +22 -0
  85. package/dist/hash/hash.js +252 -0
  86. package/dist/hash/hashString.d.ts +13 -0
  87. package/dist/hash/hashString.js +53 -0
  88. package/dist/hash/index.d.ts +8 -0
  89. package/dist/hash/index.js +8 -0
  90. package/dist/hash/stableStringify.d.ts +19 -0
  91. package/dist/hash/stableStringify.js +102 -0
  92. package/dist/id/index.d.ts +7 -0
  93. package/dist/id/index.js +7 -0
  94. package/dist/id/nanoid.d.ts +16 -0
  95. package/dist/id/nanoid.js +65 -0
  96. package/dist/id/uniqueId.d.ts +14 -0
  97. package/dist/id/uniqueId.js +20 -0
  98. package/dist/id/uuid.d.ts +25 -0
  99. package/dist/id/uuid.js +64 -0
  100. package/dist/index.d.ts +15 -8
  101. package/dist/index.js +15 -8
  102. package/dist/internal/bytes.d.ts +11 -0
  103. package/dist/internal/bytes.js +33 -0
  104. package/dist/internal/codec.d.ts +11 -0
  105. package/dist/internal/codec.js +84 -0
  106. package/dist/internal/compare.d.ts +10 -0
  107. package/dist/internal/compare.js +26 -0
  108. package/dist/internal/crypto.d.ts +5 -0
  109. package/dist/internal/crypto.js +11 -0
  110. package/dist/internal/keys.d.ts +12 -0
  111. package/dist/internal/keys.js +23 -0
  112. package/dist/internal/kind.d.ts +44 -0
  113. package/dist/internal/kind.js +139 -0
  114. package/dist/internal/sparse.d.ts +51 -0
  115. package/dist/internal/sparse.js +104 -0
  116. package/dist/internal/text.d.ts +7 -0
  117. package/dist/internal/text.js +15 -0
  118. package/dist/internal/timer.d.ts +10 -0
  119. package/dist/internal/timer.js +17 -0
  120. package/dist/internal/truncate.d.ts +10 -0
  121. package/dist/internal/truncate.js +22 -0
  122. package/dist/internal/walk.d.ts +47 -0
  123. package/dist/internal/walk.js +100 -0
  124. package/dist/is/index.d.ts +8 -0
  125. package/dist/is/index.js +8 -0
  126. package/dist/is/isArrayLike.d.ts +18 -0
  127. package/dist/is/isArrayLike.js +25 -0
  128. package/dist/is/isError.d.ts +13 -0
  129. package/dist/is/isError.js +18 -0
  130. package/dist/is/isNil.d.ts +12 -0
  131. package/dist/is/isNil.js +14 -0
  132. package/dist/is/isPlainObject.d.ts +15 -0
  133. package/dist/is/isPlainObject.js +34 -0
  134. package/dist/number/index.d.ts +7 -0
  135. package/dist/number/index.js +7 -0
  136. package/dist/number/round.d.ts +86 -0
  137. package/dist/number/round.js +130 -0
  138. package/dist/number/stats.d.ts +52 -0
  139. package/dist/number/stats.js +115 -0
  140. package/dist/object/cloneDeep.d.ts +28 -0
  141. package/dist/object/cloneDeep.js +200 -0
  142. package/dist/object/index.d.ts +14 -0
  143. package/dist/object/index.js +13 -0
  144. package/dist/object/isEqual.d.ts +64 -0
  145. package/dist/object/isEqual.js +289 -0
  146. package/dist/object/matches.d.ts +40 -0
  147. package/dist/object/matches.js +284 -0
  148. package/dist/object/merge.d.ts +66 -0
  149. package/dist/object/merge.js +272 -0
  150. package/dist/object/parsePath.d.ts +22 -0
  151. package/dist/object/parsePath.js +111 -0
  152. package/dist/object/path.d.ts +85 -0
  153. package/dist/object/path.js +208 -0
  154. package/dist/object/pick.d.ts +91 -0
  155. package/dist/object/pick.js +180 -0
  156. package/dist/object/reshape.d.ts +73 -0
  157. package/dist/object/reshape.js +144 -0
  158. package/dist/random/index.d.ts +96 -32
  159. package/dist/random/index.js +215 -53
  160. package/dist/string/case.d.ts +71 -0
  161. package/dist/string/case.js +152 -0
  162. package/dist/string/fold.d.ts +35 -0
  163. package/dist/string/fold.js +100 -0
  164. package/dist/string/index.d.ts +6 -187
  165. package/dist/string/index.js +6 -224
  166. package/dist/string/truncate.d.ts +25 -0
  167. package/dist/string/truncate.js +44 -0
  168. package/dist/string/words.d.ts +27 -0
  169. package/dist/string/words.js +241 -0
  170. package/package.json +99 -51
  171. package/dist/collections/BTree.d.ts +0 -230
  172. package/dist/collections/BTree.js +0 -1143
  173. package/dist/collections/Heap.d.ts +0 -69
  174. package/dist/collections/Heap.js +0 -181
  175. package/dist/collections/LRUCache.d.ts +0 -83
  176. package/dist/collections/LRUCache.js +0 -139
  177. package/dist/collections/SortedMap.d.ts +0 -92
  178. package/dist/collections/SortedMap.js +0 -131
  179. package/dist/collections/SortedSet.d.ts +0 -69
  180. package/dist/collections/SortedSet.js +0 -99
  181. package/dist/collections/SortedTree.d.ts +0 -103
  182. package/dist/collections/SortedTree.js +0 -142
  183. package/dist/collections/SplayTree.d.ts +0 -107
  184. package/dist/collections/SplayTree.js +0 -351
  185. package/dist/collections/Trie.d.ts +0 -42
  186. package/dist/collections/Trie.js +0 -104
  187. package/dist/collections/abc.d.ts +0 -152
  188. package/dist/collections/abc.js +0 -39
  189. package/dist/collections/frozenset.d.ts +0 -19
  190. package/dist/collections/frozenset.js +0 -33
  191. package/dist/functools/index.d.ts +0 -106
  192. package/dist/functools/index.js +0 -203
  193. package/dist/globals/index.d.ts +0 -268
  194. package/dist/globals/index.js +0 -447
  195. package/dist/i18n/currency.d.ts +0 -27
  196. package/dist/i18n/currency.js +0 -100
  197. package/dist/i18n/date/index.d.ts +0 -210
  198. package/dist/i18n/date/index.js +0 -552
  199. package/dist/i18n/date/utils.d.ts +0 -64
  200. package/dist/i18n/date/utils.js +0 -192
  201. package/dist/i18n/duration.d.ts +0 -113
  202. package/dist/i18n/duration.js +0 -398
  203. package/dist/i18n/format.d.ts +0 -34
  204. package/dist/i18n/format.js +0 -378
  205. package/dist/i18n/index.d.ts +0 -9
  206. package/dist/i18n/index.js +0 -8
  207. package/dist/itertools/index.d.ts +0 -251
  208. package/dist/itertools/index.js +0 -434
  209. package/dist/math/index.d.ts +0 -79
  210. package/dist/math/index.js +0 -81
  211. package/dist/mod.d.ts +0 -1
  212. package/dist/mod.js +0 -1
  213. package/dist/operators/index.d.ts +0 -148
  214. package/dist/operators/index.js +0 -325
  215. package/dist/types.d.ts +0 -19
  216. package/dist/types.js +0 -1
@@ -1,33 +0,0 @@
1
- /**
2
- * Returns a new immutable Set object with elements from `iterable`. Its contents cannot be altered after it's created.
3
- *
4
- *
5
- * @class FrozenSet
6
- * @extends {Set<T>}
7
- * @template T
8
- */
9
- export class FrozenSet extends Set {
10
- constructor(iterable) {
11
- super();
12
- // The Set constructor would call the no-op add below and drop every item
13
- for (const item of iterable ?? [])
14
- super.add(item);
15
- return this.freeze();
16
- }
17
- /** @private */
18
- add() {
19
- return this;
20
- }
21
- /** @private */
22
- delete() {
23
- return false;
24
- }
25
- /** @private */
26
- clear() {
27
- return this;
28
- }
29
- /** @private */
30
- freeze() {
31
- return Object.freeze(this);
32
- }
33
- }
@@ -1,106 +0,0 @@
1
- /**
2
- * This module includes functions that promote a more functional programming style.
3
- *
4
- * @module functools
5
- */
6
- import type { Function } from '../globals/index.js';
7
- /**
8
- * Creates a function that accepts arguments of `func` and either invokes `func` returning its result, if at least `func.length` arguments have been provided, or returns a function that accepts the remaining `func` arguments, and so on.
9
- * @param {Function} func The function to curry.
10
- * @returns Returns the new curried function.
11
- */
12
- export declare function curry(func: Function): Function<ReturnType<typeof func>>;
13
- /**
14
- * Creates a function that invokes `fn` with partials prepended to the arguments it receives.
15
- * **Note:** The given function will have `this` bound to `undefined`. If using `this` inside `fn`, consider `Function.prototype.bind` instead.
16
- * @param {Function} fn The function to bind.
17
- * @param {...*} [args] The arguments to apply to `fn`.
18
- * @returns {Function} Returns the new partially applied function
19
- */
20
- export declare function partial(fn: Function, ...args: any[]): Function<ReturnType<typeof fn>>;
21
- /**
22
- * Creates a function composition from a given set of functions that will be each applied on the result of the previous one from left to right.
23
- * @param {...*} [args] The set of functions to apply.
24
- * @returns {Function} A new function that applies each given function on the result of the previous one.
25
- */
26
- export declare function flow(...args: Function[]): Function;
27
- /**
28
- * This is just an alias for the {@link flow} function.
29
- * @see {@link flow}
30
- */
31
- export declare const pipe: typeof flow;
32
- /**
33
- * Creates a function composition from a given set of functions that will be each applied on the result of the previous one from right to left.
34
- * @param {...*} [args] The set of functions to apply.
35
- * @returns {Function} A new function that applies each given function on the result of the previous step.
36
- */
37
- export declare function compose(...args: Function[]): Function;
38
- /**
39
- * Creates a function that memoizes the result of `func`. If `resolver` is provided, it determines the cache key for storing the result based on the arguments provided to the memoized function. By default, the first argument provided to the memoized function is used as the map cache key.
40
- *
41
- * @param {Function} func The function to have its output memoized.
42
- * @param {Function} [resolver] The function to resolve the cache key.
43
- * @return {Function} Returns the new memoized function.
44
- */
45
- export declare function memoize<T>(func: Function<T>, resolver?: Function): Function<T>;
46
- /**
47
- * Creates a debounced function that delays invoking `func` until after `wait` milliseconds have elapsed since the last time the debounced function was invoked. Subsequent calls to the debounced function return the result of the last successful invocation. Provide options to set the `maxWait` milliseconds, regardless of when the function was last invoked.
48
- *
49
- * @param {Function} func The function to debounce.
50
- * @param {number} wait The number of milliseconds to delay.
51
- * @param {{ maxWait: number }} [opts] The maximum time `func` is allowed to be delayed before it's invoked.
52
- * @returns Returns the new debounced function.
53
- */
54
- export declare function debounce(func: Function, wait: number, { maxWait }?: {
55
- maxWait?: number;
56
- }): Function;
57
- /**
58
- * Creates a throttled function that only invokes `func` at most once per every `wait` milliseconds. Subsequent calls to the throttled function return the result of the last successful invocation.
59
- *
60
- * @param {Function} func The function to throttle.
61
- * @param {number} wait The number of milliseconds to throttle invocations to.
62
- * @return {Function} Returns the new throttled function.
63
- */
64
- export declare function throttle(func: Function, wait: number): Function;
65
- /**
66
- * Creates a function that is restricted to invoking `func` once. Repeat calls to the function return the value of the first invocation. The `func` is invoked with the arguments of the created function.
67
- *
68
- * @param {Function} func The function to restrict
69
- * @returns {Function} Returns the new restricted function.
70
- * @example
71
- ```js
72
- let func = x => x
73
- let single = once(func)
74
- single(1) // 1
75
- single(2) // 1
76
- single(3) // 1
77
- // ...
78
- // `func` is invoked only once.
79
- ```
80
- */
81
- export declare function once<T>(func: Function<T>): Function<T>;
82
- export type Maybe<T> = T | undefined;
83
- export type Result<T, E = any> = [Maybe<T>, Maybe<E>];
84
- export type Optional<T> = Maybe<T>;
85
- /**
86
- * Wraps a function to catch any exceptions inside and return a {@link Result} tuple with the value and error, if any.
87
- * @template T,E
88
- * @param {Function<T>} fn The function to wrap.
89
- * @returns {Function} A new function that wraps `fn` and returns the result tuple.
90
- */
91
- export declare function result<T = any, E = unknown>(fn: Function<T>): Function<Result<T, E>>;
92
- /**
93
- * Wraps an async function to catch any exceptions inside and return a {@link Result} tuple with the value and error, if any.
94
- * @template T,E
95
- * @param {Function<Promise<T>>} fn The async function to await.
96
- * @returns {Function<Promise<Result<T, E>>>} A new function that awaits the given function and returns the result tuple.
97
- */
98
- export declare function resultAsync<T = any, E = unknown>(fn: Function<Promise<T>>): Function<Promise<Result<T, E>>>;
99
- /**
100
- * Converts a function that expects a node-style callback argument like `(err, result)` to return a `Promise` instead.
101
- * @template T
102
- * @param {Function<T>} fun The given function to convert from callback style to `Promise`.
103
- * @param {*} thisArg
104
- * @returns {Function<Promise<T>>} A function that wraps `fun` and returns a `Promise`.
105
- */
106
- export declare function promisify<T>(fun: Function, thisArg?: any): Function<Promise<T>>;
@@ -1,203 +0,0 @@
1
- /**
2
- * This module includes functions that promote a more functional programming style.
3
- *
4
- * @module functools
5
- */
6
- /**
7
- * Creates a function that accepts arguments of `func` and either invokes `func` returning its result, if at least `func.length` arguments have been provided, or returns a function that accepts the remaining `func` arguments, and so on.
8
- * @param {Function} func The function to curry.
9
- * @returns Returns the new curried function.
10
- */
11
- export function curry(func) {
12
- return function curried(...args) {
13
- if (args.length >= func.length) {
14
- return func.apply(this, args);
15
- }
16
- return (...args2) => {
17
- return curried.apply(this, args.concat(args2));
18
- };
19
- };
20
- }
21
- /**
22
- * Creates a function that invokes `fn` with partials prepended to the arguments it receives.
23
- * **Note:** The given function will have `this` bound to `undefined`. If using `this` inside `fn`, consider `Function.prototype.bind` instead.
24
- * @param {Function} fn The function to bind.
25
- * @param {...*} [args] The arguments to apply to `fn`.
26
- * @returns {Function} Returns the new partially applied function
27
- */
28
- export function partial(fn, ...args) {
29
- return fn.bind(undefined, ...args);
30
- }
31
- /**
32
- * Creates a function composition from a given set of functions that will be each applied on the result of the previous one from left to right.
33
- * @param {...*} [args] The set of functions to apply.
34
- * @returns {Function} A new function that applies each given function on the result of the previous one.
35
- */
36
- export function flow(...args) {
37
- return (x) => {
38
- for (const fn of args) {
39
- x = fn(x);
40
- }
41
- return x;
42
- };
43
- }
44
- /**
45
- * This is just an alias for the {@link flow} function.
46
- * @see {@link flow}
47
- */
48
- export const pipe = flow;
49
- /**
50
- * Creates a function composition from a given set of functions that will be each applied on the result of the previous one from right to left.
51
- * @param {...*} [args] The set of functions to apply.
52
- * @returns {Function} A new function that applies each given function on the result of the previous step.
53
- */
54
- export function compose(...args) {
55
- return flow(...args.reverse());
56
- }
57
- /**
58
- * Creates a function that memoizes the result of `func`. If `resolver` is provided, it determines the cache key for storing the result based on the arguments provided to the memoized function. By default, the first argument provided to the memoized function is used as the map cache key.
59
- *
60
- * @param {Function} func The function to have its output memoized.
61
- * @param {Function} [resolver] The function to resolve the cache key.
62
- * @return {Function} Returns the new memoized function.
63
- */
64
- export function memoize(func, resolver) {
65
- const cache = new Map();
66
- return function (...args) {
67
- const key = resolver ? resolver.apply(this, args) : args[0];
68
- if (cache.has(key))
69
- return cache.get(key);
70
- const value = func.apply(this, args);
71
- cache.set(key, value);
72
- return value;
73
- };
74
- }
75
- /**
76
- * Creates a debounced function that delays invoking `func` until after `wait` milliseconds have elapsed since the last time the debounced function was invoked. Subsequent calls to the debounced function return the result of the last successful invocation. Provide options to set the `maxWait` milliseconds, regardless of when the function was last invoked.
77
- *
78
- * @param {Function} func The function to debounce.
79
- * @param {number} wait The number of milliseconds to delay.
80
- * @param {{ maxWait: number }} [opts] The maximum time `func` is allowed to be delayed before it's invoked.
81
- * @returns Returns the new debounced function.
82
- */
83
- export function debounce(func, wait, { maxWait } = {}) {
84
- let timer, maxTimer, lastResult, latestCall;
85
- return function (...args) {
86
- latestCall = () => func.apply(this, args);
87
- if (timer)
88
- clearTimeout(timer);
89
- timer = setTimeout(() => {
90
- clearTimeout(maxTimer);
91
- maxTimer = null;
92
- lastResult = func.apply(this, args);
93
- }, wait);
94
- if (maxWait && !maxTimer) {
95
- maxTimer = setTimeout(() => {
96
- clearTimeout(timer);
97
- maxTimer = null;
98
- lastResult = latestCall();
99
- }, maxWait);
100
- }
101
- return lastResult;
102
- };
103
- }
104
- /**
105
- * Creates a throttled function that only invokes `func` at most once per every `wait` milliseconds. Subsequent calls to the throttled function return the result of the last successful invocation.
106
- *
107
- * @param {Function} func The function to throttle.
108
- * @param {number} wait The number of milliseconds to throttle invocations to.
109
- * @return {Function} Returns the new throttled function.
110
- */
111
- export function throttle(func, wait) {
112
- let pending = false;
113
- let lastResult;
114
- return function (...args) {
115
- if (pending)
116
- return lastResult;
117
- lastResult = func.apply(this, args);
118
- pending = true;
119
- setTimeout(() => {
120
- pending = false;
121
- }, wait);
122
- return lastResult;
123
- };
124
- }
125
- /**
126
- * Creates a function that is restricted to invoking `func` once. Repeat calls to the function return the value of the first invocation. The `func` is invoked with the arguments of the created function.
127
- *
128
- * @param {Function} func The function to restrict
129
- * @returns {Function} Returns the new restricted function.
130
- * @example
131
- ```js
132
- let func = x => x
133
- let single = once(func)
134
- single(1) // 1
135
- single(2) // 1
136
- single(3) // 1
137
- // ...
138
- // `func` is invoked only once.
139
- ```
140
- */
141
- export function once(func) {
142
- let result, isCalled;
143
- return function (...args) {
144
- if (!isCalled) {
145
- result = func.apply(this, args);
146
- isCalled = true;
147
- }
148
- return result;
149
- };
150
- }
151
- /**
152
- * Wraps a function to catch any exceptions inside and return a {@link Result} tuple with the value and error, if any.
153
- * @template T,E
154
- * @param {Function<T>} fn The function to wrap.
155
- * @returns {Function} A new function that wraps `fn` and returns the result tuple.
156
- */
157
- export function result(fn) {
158
- return function (...args) {
159
- let value, err;
160
- try {
161
- value = fn.apply(this, args);
162
- }
163
- catch (error) {
164
- err = error;
165
- }
166
- return [value, err];
167
- };
168
- }
169
- /**
170
- * Wraps an async function to catch any exceptions inside and return a {@link Result} tuple with the value and error, if any.
171
- * @template T,E
172
- * @param {Function<Promise<T>>} fn The async function to await.
173
- * @returns {Function<Promise<Result<T, E>>>} A new function that awaits the given function and returns the result tuple.
174
- */
175
- export function resultAsync(fn) {
176
- return async function (...args) {
177
- let value, err;
178
- try {
179
- value = await fn.apply(this, args);
180
- }
181
- catch (error) {
182
- err = error;
183
- }
184
- return [value, err];
185
- };
186
- }
187
- /**
188
- * Converts a function that expects a node-style callback argument like `(err, result)` to return a `Promise` instead.
189
- * @template T
190
- * @param {Function<T>} fun The given function to convert from callback style to `Promise`.
191
- * @param {*} thisArg
192
- * @returns {Function<Promise<T>>} A function that wraps `fun` and returns a `Promise`.
193
- */
194
- export function promisify(fun, thisArg) {
195
- const original = thisArg ? fun.bind(thisArg) : fun;
196
- return async (...args) => {
197
- return await new Promise((resolve, reject) => original(...args, (err, result) => {
198
- if (err)
199
- return reject(err);
200
- resolve(result);
201
- }));
202
- };
203
- }
@@ -1,268 +0,0 @@
1
- /**
2
- * This module includes global functions and constants.
3
- *
4
- * @module globals
5
- */
6
- /**
7
- * A generic function type with arbitrary arguments and return.
8
- */
9
- export type Function<TReturn = any> = (...args: any) => TReturn;
10
- /** This is just a noop function. */
11
- export declare function noop(): void;
12
- /** A function that always returns `true`. */
13
- export declare const True: () => boolean;
14
- /** A function that always returns `false`. */
15
- export declare const False: () => boolean;
16
- /**
17
- * A custom error class that inherits from the Error object.
18
- * @class CustomError
19
- * @extends {Error}
20
- */
21
- export declare class CustomError extends Error {
22
- /**
23
- * Returns a new CustomError with the specified message.
24
- * @param {string} message
25
- * @param {ErrorOptions} [options] Standard error options such as `cause`.
26
- * @constructor
27
- */
28
- constructor(message?: string, options?: ErrorOptions);
29
- }
30
- /**
31
- * Custom error to signal an invalid index access.
32
- * @extends {CustomError}
33
- */
34
- export declare class IndexError extends CustomError {
35
- }
36
- /**
37
- * Raised when a system function returns an I/O failure such as "file not found"
38
- * or "disk full".
39
- * @extends {CustomError}
40
- */
41
- export declare class IOError extends CustomError {
42
- }
43
- /**
44
- * Custom error to signal an invalid key access.
45
- * @extends {CustomError}
46
- */
47
- export declare class KeyError extends CustomError {
48
- }
49
- /**
50
- * Custom error to signal an invalid operation.
51
- * @extends {CustomError}
52
- */
53
- export declare class NotImplementedError extends CustomError {
54
- }
55
- /**
56
- * Custom error to signal an invalid value.
57
- * @extends {CustomError}
58
- */
59
- export declare class ValueError extends CustomError {
60
- }
61
- /**
62
- * Raised when the second argument of a division or modulo operation is zero.
63
- * @extends {CustomError}
64
- */
65
- export declare class ZeroDivisionError extends CustomError {
66
- }
67
- /**
68
- * Returns the true type of any value with correct detection for `null`, `Array`,
69
- * `Object`, `Promise`, `Symbol`, and `NaN`. The correct values are inferred from {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag | Symbol.toStringTag} and the value's prototype.
70
- * @param value
71
- * @returns The value type.
72
- */
73
- export declare function type(value: any): string;
74
- /**
75
- * Returns a string representing the object.
76
- * @param obj
77
- * @returns `obj.toString()` or `''` if `obj` is `null` or `undefined`.
78
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString | Object.toString }
79
- */
80
- export declare function str(obj: any): string;
81
- /** Check if value is a boolean type. */
82
- export declare function isBool(x: any): x is boolean;
83
- /** Check if value is an `Error` type. */
84
- export declare function isError(x: any): x is Error;
85
- /** Check if value is an iterable type. */
86
- export declare function isIterable(x: any): x is IterableIterator<any>;
87
- /** Check if value is a native `Date` type. */
88
- export declare function isDate(x: any): x is Date;
89
- /** Check if value is an object type. */
90
- export declare function isObject(x: any): x is Object;
91
- /** Check if value is a string type. */
92
- export declare function isString(x: any): x is string;
93
- /**
94
- * Check if value is Array-like type.
95
- * A value is considered array-like if it's not a function and has a `.length` number property.
96
- */
97
- export declare function isArrayLike(x: any): boolean;
98
- /** Check if value is a number type. */
99
- export declare function isNumber(x: any): x is number;
100
- /** Check if value is a bigint type. */
101
- export declare function isBigint(x: any): x is BigInt;
102
- /** Check if value is a Promise type. */
103
- export declare function isPromise(x: any): x is Promise<any>;
104
- /** Check if value is a function type. */
105
- export declare function isFunction(x: any): x is Function;
106
- /** Check if value is an async function type. */
107
- export declare function isAsyncFunction(x: any): boolean;
108
- /** Check if value is a generator function type. */
109
- export declare function isGenerator(x: any): x is Generator;
110
- /** Check if value is `null` or `undefined`. */
111
- export declare function isNull(x: any): x is null | undefined;
112
- /** Check if value === `undefined`. */
113
- export declare function isUndefined(x: any): x is undefined;
114
- /** Check if value is not `null` or `undefined`. */
115
- export declare function notNull(x: any): boolean;
116
- /** Returns `true` for objects without length or falsy values. */
117
- export declare function isEmpty(x: any): boolean;
118
- /** Check if value is a `RegExp` type */
119
- export declare function isRegExp(x: any): x is RegExp;
120
- /** Check if value is a `Symbol` type */
121
- export declare function isSymbol(x: any): x is symbol;
122
- /** Check if value is a `Set` type. */
123
- export declare function isSet<T>(x: any): x is Set<T>;
124
- /** Check if value is a `Map` type. */
125
- export declare function isMap<K, V>(x: any): x is Map<K, V>;
126
- /** Check if value is a `WeakSet` type. */
127
- export declare function isWeakSet<T extends object>(x: any): x is WeakSet<T>;
128
- /** Check if value is a `WeakMap` type. */
129
- export declare function isWeakMap<K extends object, V>(x: any): x is WeakMap<K, V>;
130
- /**
131
- * Returns the number of elements in a collection type.
132
- *
133
- * If `value.length` or `value.size()` is defined, this will be returned.
134
- * If `value` is an `Object`, it returns the number of keys.
135
- * @param value
136
- * @returns The number of elements in the collection or `undefined`.
137
- */
138
- export declare function len(value: any): number;
139
- /**
140
- * Generates a unique ID using random numbers. If a prefix is given, the ID is appended to it and returned as a string.
141
- * @param {string} [pre=''] The id prefix.
142
- * @returns {number|string} Returns the numeric or string ID.
143
- * @example
144
- ```js
145
- uniqueId()
146
- // 3445556877
147
-
148
- uniqueId('user_')
149
- // 'user_1033763188'
150
- ```
151
- */
152
- export declare function uniqueId(pre?: string): string;
153
- /**
154
- * Generates a monotonically increasing, pseudo-random based uuid-v4 string. These are sortable, url-friendly and are 100% unique if the IDs are generated more than 1 millisecond apart. If two IDs are generated at the same millisecond, the chance of collision is still pretty low (1 in 10^15).
155
- * @returns Returns a GUID formatted string.
156
- * @example
157
- ```js
158
- uuid()
159
- // '183f8ef3-b8f0-4000-81f5-89234c836d00'
160
- ```
161
- */
162
- export declare function uuid(): string;
163
- /**
164
- * Checks if `obj.key` is a function, and calls it with any given `args`.
165
- * @param {*} obj
166
- * @param {PropertyKey} key
167
- * @param {...any[]} args
168
- * @return `obj.key(...args)` or `undefined`.
169
- */
170
- export declare function call<T = any>(obj: any, key: PropertyKey, ...args: any[]): T;
171
- /** Convert number to unicode character */
172
- export declare function chr(x: number): string;
173
- /** Convert character to Unicode code point */
174
- export declare function ord(x: string): number;
175
- /**
176
- * Check if a given property is present in a given object.
177
- * @param obj The object to check.
178
- * @param attr A property key name.
179
- * @returns `true` if the object has the property name.
180
- */
181
- export declare function has(obj: any, attr: PropertyKey): boolean;
182
- /**
183
- * Check if the attribute is present in the object or return a default value instead.
184
- * @param {PropertyKey} key
185
- * @param {*} obj
186
- * @param {*} [value=undefined]
187
- * @return The property value or default value.
188
- */
189
- export declare function get<T>(key: PropertyKey, obj: T, value?: any): any;
190
- export declare function get<T>(key: PropertyKey[], obj: T, value?: any): any;
191
- /**
192
- * Deletes a given property key from an object.
193
- * @param attr The property name.
194
- * @param x The given object.
195
- * @returns The modified object.
196
- */
197
- export declare function del<T>(attr: PropertyKey, x: T): T;
198
- /**
199
- * Adds a property to an object, or modifies the existing value.
200
- * @param attr The name or Symbol of the property to be defined or modified.
201
- * @param obj The container object.
202
- * @param value The value associated with the property
203
- * @param {boolean} [writable=false] If `true` the value may be changed later. Defaults to `false`.
204
- * @param {boolean} [enumerable=false] If `true` this property shows up during enumeration of the properties in the object. Defaults to `false`.
205
- * @returns
206
- */
207
- export declare function set(attr: PropertyKey, obj: any, value: any, writable?: boolean, enumerable?: boolean): any;
208
- export declare const HASH_KEY: unique symbol;
209
- /**
210
- * Return a hash value for a given object. The hash will be the same for subsequent invocations.
211
- * @param obj The given object.
212
- * @returns The hash value.
213
- */
214
- export declare function hash(obj: string): number;
215
- export declare function hash<T>(obj: T): T;
216
- /**
217
- * A hash value generator for strings.
218
- * @param {string} str The given string.
219
- * @returns {number} The hash value of the string.
220
- * @see {@link http://isthe.com/chongo/tech/comp/fnv/ FNV hash}
221
- */
222
- export declare function hashCode(str: string): number;
223
- /**
224
- * Converts a number to a binary string.
225
- * @param n The given number.
226
- * @returns The binary string representation of `n`.
227
- */
228
- export declare function bin(n: number): string;
229
- /**
230
- * Converts a number to a hexadecimal string.
231
- * @param n The given number.
232
- * @returns The hexadecimal string representation of `n`.
233
- */
234
- export declare function hex(n: number): string;
235
- /**
236
- * Converts a number to an octal string.
237
- * @param n The given number.
238
- * @returns The octal string representation of `n`.
239
- */
240
- export declare function oct(n: number): string;
241
- /**
242
- * Returns a floating point number constructed from a number or string `x`.
243
- * @param x The number or string.
244
- * @returns A floating point number parsed from the given value or `NaN`.
245
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat parseFloat}
246
- */
247
- export declare function float(x: string): number;
248
- /**
249
- * Returns an integer number constructed from a number or string `x`.
250
- * @param x The number or string.
251
- * @returns An integer number parsed from the given value or `NaN`.
252
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt parseInt}
253
- */
254
- export declare function int(x: string, base?: number): number;
255
- /**
256
- * Converts the value to an array using `Array.from()`.
257
- * @param value
258
- * @returns A new array value.
259
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from Array.from()}
260
- */
261
- export declare function list<T>(value?: any): T[];
262
- /**
263
- * Retrieve the next item from the iterator by calling its `next()` method.
264
- * @param iter The iterator.
265
- * @returns The next value from the iterator.
266
- */
267
- export declare function next<T>(iter: Iterator<T>): T | undefined;
268
- export declare function next<T>(iter: Generator<any, T>): T | undefined;