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