atomic-fns 1.1.7 → 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 +405 -60
  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 -249
  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 -642
  27. package/dist/collections/index.js +14 -716
  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 -145
  55. package/dist/decimal/index.js +10 -374
  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 -50
  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 -226
  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 +101 -58
  171. package/dist/collections/BTree.d.ts +0 -229
  172. package/dist/collections/BTree.js +0 -1165
  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 -80
  176. package/dist/collections/LRUCache.js +0 -126
  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 -141
  183. package/dist/collections/SplayTree.d.ts +0 -105
  184. package/dist/collections/SplayTree.js +0 -338
  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 -30
  191. package/dist/functools/index.d.ts +0 -130
  192. package/dist/functools/index.js +0 -235
  193. package/dist/globals/index.d.ts +0 -285
  194. package/dist/globals/index.js +0 -463
  195. package/dist/i18n/currency.d.ts +0 -24
  196. package/dist/i18n/currency.js +0 -85
  197. package/dist/i18n/date/index.d.ts +0 -203
  198. package/dist/i18n/date/index.js +0 -531
  199. package/dist/i18n/date/utils.d.ts +0 -65
  200. package/dist/i18n/date/utils.js +0 -290
  201. package/dist/i18n/duration.d.ts +0 -112
  202. package/dist/i18n/duration.js +0 -392
  203. package/dist/i18n/format.d.ts +0 -31
  204. package/dist/i18n/format.js +0 -506
  205. package/dist/i18n/index.d.ts +0 -8
  206. package/dist/i18n/index.js +0 -8
  207. package/dist/itertools/index.d.ts +0 -255
  208. package/dist/itertools/index.js +0 -437
  209. package/dist/math/index.d.ts +0 -96
  210. package/dist/math/index.js +0 -98
  211. package/dist/mod.d.ts +0 -1
  212. package/dist/mod.js +0 -1
  213. package/dist/operators/index.d.ts +0 -152
  214. package/dist/operators/index.js +0 -305
  215. package/dist/types.d.ts +0 -19
  216. package/dist/types.js +0 -1
@@ -1,463 +0,0 @@
1
- /**
2
- * This module includes global functions and constants.
3
- *
4
- * @module globals
5
- */
6
- /** This is just a noop function. */
7
- export function noop() { }
8
- /** A function that always returns `true`. */
9
- export const True = () => true;
10
- /** A function that always returns `false`. */
11
- export const False = () => false;
12
- /**
13
- * A custom error class that inherits from the Error object.
14
- * @class CustomError
15
- * @extends {Error}
16
- */
17
- export class CustomError extends Error {
18
- /**
19
- * Returns a new CustomError with the specified message.
20
- * @param {string} message
21
- * @constructor
22
- */
23
- constructor(message) {
24
- super(message);
25
- this.name = this.constructor.name;
26
- if (typeof Error.captureStackTrace === 'function') {
27
- Error.captureStackTrace(this, this.constructor);
28
- }
29
- else {
30
- this.stack = new Error(message).stack;
31
- }
32
- }
33
- }
34
- /**
35
- * Custom error to signal an invalid index access.
36
- * @extends {CustomError}
37
- */
38
- export class IndexError extends CustomError {
39
- }
40
- /**
41
- * Raised when a system function returns an I/O failure such as "file not found"
42
- * or "disk full".
43
- * @extends {CustomError}
44
- */
45
- export class IOError extends CustomError {
46
- }
47
- /**
48
- * Custom error to signal an invalid key access.
49
- * @extends {CustomError}
50
- */
51
- export class KeyError extends CustomError {
52
- }
53
- /**
54
- * Custom error to signal an invalid operation.
55
- * @extends {CustomError}
56
- */
57
- export class NotImplementedError extends CustomError {
58
- }
59
- /**
60
- * Custom error to signal an invalid value.
61
- * @extends {CustomError}
62
- */
63
- export class ValueError extends CustomError {
64
- }
65
- /**
66
- * Raised when the second argument of a division or modulo operation is zero.
67
- * @extends {CustomError}
68
- */
69
- export class ZeroDivisionError extends CustomError {
70
- }
71
- /**
72
- * Returns the true type of any value with correct detection for `null`, `Array`,
73
- * `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.
74
- * @param value
75
- * @returns The value type.
76
- */
77
- export function type(value) {
78
- const result = typeof value;
79
- if (result === 'object') {
80
- if (value) {
81
- if (value instanceof String)
82
- return 'string';
83
- if (value instanceof Boolean)
84
- return 'boolean';
85
- if (value instanceof Function)
86
- return 'function';
87
- if (value instanceof Array)
88
- return 'array';
89
- if (value instanceof Error)
90
- return 'error';
91
- if (value[Symbol.toStringTag])
92
- return value[Symbol.toStringTag];
93
- const className = str(value);
94
- if (className === '[object Window]')
95
- return 'object';
96
- if (className.endsWith('Array]') || Array.isArray(value)) {
97
- return 'array';
98
- }
99
- if (className.endsWith('Function]') || typeof value.call === 'function') {
100
- return 'function';
101
- }
102
- // Other primitive types
103
- if (className === '[object Promise]' || value instanceof Promise) {
104
- return 'Promise';
105
- }
106
- }
107
- else {
108
- return 'null';
109
- }
110
- }
111
- else if (result === 'function' && typeof value.call === 'undefined') {
112
- return 'object';
113
- }
114
- else if (Number.isNaN(value))
115
- return 'NaN';
116
- return result;
117
- }
118
- /**
119
- * Returns a string representing the object.
120
- * @param obj
121
- * @returns `obj.toString()` or `''` if `obj` is `null` or `undefined`.
122
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString | Object.toString }
123
- */
124
- export function str(obj) {
125
- return obj?.toString() ?? '';
126
- }
127
- /** Check if value is a boolean type. */
128
- export function isBool(x) {
129
- return type(x) === 'boolean';
130
- }
131
- /** Check if value is an `Error` type. */
132
- export function isError(x) {
133
- return x instanceof Error;
134
- }
135
- /** Check if value is an iterable type. */
136
- export function isIterable(x) {
137
- if (x == null)
138
- return false;
139
- return typeof x[Symbol.iterator] === 'function';
140
- }
141
- /** Check if value is a native `Date` type. */
142
- export function isDate(x) {
143
- return x instanceof Date;
144
- }
145
- /** Check if value is an object type. */
146
- export function isObject(x) {
147
- return type(x) === 'object';
148
- }
149
- /** Check if value is a string type. */
150
- export function isString(x) {
151
- return type(x) === 'string';
152
- }
153
- /** Check if value is an Array type. */
154
- export function isArray(x) {
155
- return Array.isArray(x);
156
- }
157
- /**
158
- * Check if value is Array-like type.
159
- * A value is considered array-like if it's not a function and has a `.length` number property.
160
- */
161
- export function isArrayLike(x) {
162
- const T = type(x);
163
- return T === 'array' || (T === 'object' && isNumber(x.length));
164
- }
165
- /** Check if value is a number type. */
166
- export function isNumber(x) {
167
- return type(x) === 'number';
168
- }
169
- /** Check if value is an integer number type. */
170
- export function isInteger(x) {
171
- return type(x) === 'number' && Math.trunc(x) === x;
172
- }
173
- /** Check if value is a bigint type. */
174
- export function isBigint(x) {
175
- return type(x) === 'bigint';
176
- }
177
- /** Check if value is a Promise type. */
178
- export function isPromise(x) {
179
- return type(x) === 'Promise';
180
- }
181
- /** Check if value is a function type. */
182
- export function isFunction(x) {
183
- return x?.constructor === Function;
184
- }
185
- /** Check if value is an async function type. */
186
- export function isAsyncFunction(x) {
187
- return x?.constructor.name === 'AsyncFunction';
188
- }
189
- /** Check if value is a generator function type. */
190
- export function isGenerator(x) {
191
- return x?.constructor.constructor?.name === 'GeneratorFunction';
192
- }
193
- /** Check if value is `null` or `undefined`. */
194
- export function isNull(x) {
195
- return x == null;
196
- }
197
- /** Check if value === `undefined`. */
198
- export function isUndefined(x) {
199
- return x === undefined;
200
- }
201
- /** Check if value is not `null` or `undefined`. */
202
- export function notNull(x) {
203
- return x != null;
204
- }
205
- /** Returns `true` for objects without length or falsy values. */
206
- export function isEmpty(x) {
207
- return len(x) === 0 ? true : !x;
208
- }
209
- /** Check if value is a `RegExp` type */
210
- export function isRegExp(x) {
211
- return x instanceof RegExp;
212
- }
213
- /** Check if value is a `Symbol` type */
214
- export function isSymbol(x) {
215
- return type(x) === 'symbol';
216
- }
217
- /** Check if value is a `Set` type. */
218
- export function isSet(x) {
219
- return x instanceof Set;
220
- }
221
- /** Check if value is a `Map` type. */
222
- export function isMap(x) {
223
- return x instanceof Map;
224
- }
225
- /** Check if value is a `WeakSet` type. */
226
- export function isWeakSet(x) {
227
- return x instanceof WeakSet;
228
- }
229
- /** Check if value is a `WeakMap` type. */
230
- export function isWeakMap(x) {
231
- return x instanceof WeakMap;
232
- }
233
- /**
234
- * Returns the number of elements in a collection type.
235
- *
236
- * If `value.length` or `value.size()` is defined, this will be returned.
237
- * If `value` is an `Object`, it returns the number of keys.
238
- * @param value
239
- * @returns The number of elements in the collection or `undefined`.
240
- */
241
- export function len(value) {
242
- if (value == null)
243
- return;
244
- if (isNumber(value.length)) {
245
- return value.length;
246
- }
247
- if (isNumber(value.size)) {
248
- return value.size;
249
- }
250
- if (isFunction(value.size)) {
251
- return value.size();
252
- }
253
- if (isObject(value)) {
254
- return Object.keys(value).length;
255
- }
256
- }
257
- /**
258
- * Generates a unique ID using random numbers. If a prefix is given, the ID is appended to it and returned as a string.
259
- * @param {string} [pre=''] The id prefix.
260
- * @returns {number|string} Returns the numeric or string ID.
261
- * @example
262
- ```js
263
- uniqueId()
264
- // 3445556877
265
-
266
- uniqueId('user_')
267
- // 'user_1033763188'
268
- ```
269
- */
270
- export function uniqueId(pre = '') {
271
- // @ts-expect-error
272
- return (pre || 0) + ((Math.random() * 1e10) >>> 0);
273
- }
274
- /**
275
- * 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).
276
- * @returns Returns a GUID formatted string.
277
- * @example
278
- ```js
279
- uuid()
280
- // '183f8ef3-b8f0-4000-81f5-89234c836d00'
281
- ```
282
- */
283
- export function uuid() {
284
- const u = Date.now().toString(16) + Math.random().toString(16) + '0'.repeat(16);
285
- return [u.substr(0, 8), u.substr(8, 4), '4000-8' + u.substr(13, 3), u.substr(16, 12)].join('-');
286
- }
287
- /**
288
- * Checks if `obj.key` is a function, and calls it with any given `args`.
289
- * @param {*} obj
290
- * @param {PropertyKey} key
291
- * @param {...any[]} args
292
- * @return `obj.key(...args)` or `undefined`.
293
- */
294
- export function call(obj, key, ...args) {
295
- if (isFunction(get(key, obj))) {
296
- return obj[key](...args);
297
- }
298
- }
299
- /** Convert number to unicode character */
300
- export function chr(x) {
301
- return String.fromCodePoint(x);
302
- }
303
- /** Convert character to Unicode code point */
304
- export function ord(x) {
305
- return x.charCodeAt(0);
306
- }
307
- /**
308
- * Returns an array of the own enumerable property names of `object`.
309
- * @param object The given object.
310
- * @returns The array of object keys.
311
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys Object.keys()}
312
- */
313
- export function keys(object) {
314
- return Object.keys(object);
315
- }
316
- /**
317
- * Returns an array of the own enumerable property values of `object`.
318
- * @param object The given object.
319
- * @returns The array of object keys.
320
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values Object.values()}
321
- */
322
- export function values(object) {
323
- return Object.values(object);
324
- }
325
- /**
326
- * Check if a given property is present in a given object.
327
- * @param obj The object to check.
328
- * @param attr A property key name.
329
- * @returns `true` if the object has the property name.
330
- */
331
- export function has(obj, attr) {
332
- return obj && attr in obj;
333
- }
334
- export function get(key, obj, value) {
335
- if (obj == null)
336
- return value;
337
- let paths = Array.isArray(key) ? key : [key];
338
- if (typeof key === 'string') {
339
- paths = key.split('.');
340
- }
341
- try {
342
- let result = obj;
343
- for (const k of paths) {
344
- result = result[k];
345
- }
346
- return result === undefined ? value : result;
347
- }
348
- catch (error) {
349
- return value;
350
- }
351
- }
352
- /**
353
- * Deletes a given property key from an object.
354
- * @param attr The property name.
355
- * @param x The given object.
356
- * @returns The modified object.
357
- */
358
- export function del(attr, x) {
359
- if (x != null)
360
- delete x[attr];
361
- return x;
362
- }
363
- /**
364
- * Adds a property to an object, or modifies the existing value.
365
- * @param attr The name or Symbol of the property to be defined or modified.
366
- * @param obj The container object.
367
- * @param value The value associated with the property
368
- * @param {boolean} [writable=false] If `true` the value may be changed later. Defaults to `false`.
369
- * @param {boolean} [enumerable=false] If `true` this property shows up during enumeration of the properties in the object. Defaults to `false`.
370
- * @returns
371
- */
372
- export function set(attr, obj, value, writable = false, enumerable = false) {
373
- return Object.defineProperty(obj, attr, {
374
- value,
375
- enumerable,
376
- writable
377
- });
378
- }
379
- export const HASH_KEY = Symbol.for('HASH_KEY');
380
- export function hash(obj) {
381
- if (typeof obj === 'string') {
382
- return hashCode(obj);
383
- }
384
- if (typeof obj === 'object') {
385
- if (get(HASH_KEY, obj) != null) {
386
- return obj[HASH_KEY];
387
- }
388
- obj[HASH_KEY] = uniqueId();
389
- return obj[HASH_KEY];
390
- }
391
- return obj;
392
- }
393
- /**
394
- * A hash value generator for strings.
395
- * @param {string} str The given string.
396
- * @returns {number} The hash value of the string.
397
- * @see {@link http://isthe.com/chongo/tech/comp/fnv/ FNV hash}
398
- */
399
- export function hashCode(str) {
400
- const FNV1_32A_INIT = 0x811c9dc5;
401
- let hval = FNV1_32A_INIT;
402
- for (let i = 0; i < str.length; ++i) {
403
- hval ^= str.charCodeAt(i);
404
- hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
405
- }
406
- return hval >>> 0;
407
- }
408
- /**
409
- * Converts a number to a binary string.
410
- * @param n The given number.
411
- * @returns The binary string representation of `n`.
412
- */
413
- export function bin(n) {
414
- return n.toString(2);
415
- }
416
- /**
417
- * Converts a number to a hexadecimal string.
418
- * @param n The given number.
419
- * @returns The hexadecimal string representation of `n`.
420
- */
421
- export function hex(n) {
422
- return n.toString(16);
423
- }
424
- /**
425
- * Converts a number to an octal string.
426
- * @param n The given number.
427
- * @returns The octal string representation of `n`.
428
- */
429
- export function oct(n) {
430
- return n.toString(8);
431
- }
432
- /**
433
- * Returns a floating point number constructed from a number or string `x`.
434
- * @param x The number or string.
435
- * @returns A floating point number parsed from the given value or `NaN`.
436
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat parseFloat}
437
- */
438
- export function float(x) {
439
- return parseFloat(x);
440
- }
441
- /**
442
- * Returns an integer number constructed from a number or string `x`.
443
- * @param x The number or string.
444
- * @returns An integer number parsed from the given value or `NaN`.
445
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt parseInt}
446
- */
447
- export function int(x, base = 10) {
448
- return parseInt(x, base);
449
- }
450
- /**
451
- * Converts the value to an array using `Array.from()`.
452
- * @param value
453
- * @returns A new array value.
454
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from Array.from()}
455
- */
456
- export function list(value) {
457
- if (value != null)
458
- return Array.from(value);
459
- return [];
460
- }
461
- export function next(iter) {
462
- return iter.next().value;
463
- }
@@ -1,24 +0,0 @@
1
- import { Decimal } from '../decimal/index.js';
2
- /**
3
- * Currency represents a local currency amount that can be formatted using the `Intl` apis.
4
- */
5
- export declare class Currency extends Decimal {
6
- private readonly locale;
7
- private readonly currency;
8
- private readonly numberFormat;
9
- private readonly options;
10
- constructor(amount: any, currency: string, locale?: string, opts?: Intl.NumberFormatOptions);
11
- clone(n: Currency): Currency;
12
- negated(): Currency;
13
- add(x: Currency | string): Currency;
14
- sub(x: Currency | string): Currency;
15
- div(x: Currency | Decimal | string | number): Currency;
16
- mul(x: Currency | Decimal): Currency;
17
- toDecimal(): Decimal;
18
- toString(): string;
19
- format(opts?: Intl.NumberFormatOptions, locale?: string): string;
20
- displayName(): string;
21
- accounting(): string;
22
- precision(digits?: number): string;
23
- eq(x: any): boolean;
24
- }
@@ -1,85 +0,0 @@
1
- import { defaults } from '../collections/index.js';
2
- import { isObject } from '../globals/index.js';
3
- import { Decimal } from '../decimal/index.js';
4
- /**
5
- * Currency represents a local currency amount that can be formatted using the `Intl` apis.
6
- */
7
- export class Currency extends Decimal {
8
- locale;
9
- currency;
10
- numberFormat;
11
- options = {
12
- style: 'currency',
13
- currencyDisplay: 'symbol',
14
- maximumFractionDigits: 20
15
- };
16
- constructor(amount, currency, locale, opts = {}) {
17
- super(amount);
18
- if (isObject(locale)) {
19
- opts = locale;
20
- locale = undefined;
21
- }
22
- const systemLocale = new Intl.NumberFormat(locale, defaults(opts, { currency }, this.options));
23
- const resolved = systemLocale.resolvedOptions();
24
- this.numberFormat = systemLocale;
25
- this.options.currency = currency;
26
- this.currency = resolved.currency;
27
- this.locale = resolved.locale;
28
- }
29
- // @ts-expect-error overrides
30
- clone(n) {
31
- return new Currency(n, this.currency, this.locale, this.options);
32
- }
33
- // @ts-expect-error overrides
34
- negated() {
35
- const n = super.negated.call(this);
36
- return this.clone(n);
37
- }
38
- // @ts-expect-error overrides
39
- add(x) {
40
- const n = super.add.call(this.toDecimal(), x);
41
- return this.clone(n);
42
- }
43
- // @ts-expect-error overrides
44
- sub(x) {
45
- const n = super.sub.call(this.toDecimal(), x);
46
- return this.clone(n);
47
- }
48
- // @ts-expect-error overrides
49
- div(x) {
50
- const n = super.div.call(this.toDecimal(), x);
51
- return this.clone(n);
52
- }
53
- // @ts-expect-error overrides
54
- mul(x) {
55
- const n = super.mul.call(this.toDecimal(), x);
56
- return this.clone(n);
57
- }
58
- toDecimal() {
59
- return super.clone.call(this);
60
- }
61
- toString() {
62
- const number = this.toNumber();
63
- return this.numberFormat.format(number);
64
- }
65
- format(opts = {}, locale) {
66
- const number = this.toNumber();
67
- opts = defaults(opts, this.options);
68
- return new Intl.NumberFormat(locale ?? this.locale, opts).format(number);
69
- }
70
- displayName() {
71
- return this.format({ currencyDisplay: 'name' });
72
- }
73
- accounting() {
74
- return this.format({ currencySign: 'accounting' });
75
- }
76
- precision(digits) {
77
- return this.format({ maximumFractionDigits: digits });
78
- }
79
- eq(x) {
80
- if (x?.currency && x.currency !== this.currency) {
81
- return false;
82
- }
83
- return super.eq.call(this, x);
84
- }
85
- }