es-toolkit 1.31.0 → 1.32.0-dev.1003

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 (70) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/_chunk/AbortError-Cg4ZQ1.js +10 -0
  3. package/dist/_chunk/delay-_VMfFa.js +25 -0
  4. package/dist/_chunk/isPlainObject-Xaozpc.js +93 -0
  5. package/dist/_chunk/{isWeakSet-CvIdTA.js → isWeakSet-DoHqUM.js} +42 -71
  6. package/dist/_chunk/{toMerged-DGFrN7.js → toMerged-BQTfB8.js} +39 -8
  7. package/dist/_chunk/{unary-B6qG7C.js → unary-c1NFA5.js} +35 -0
  8. package/dist/array/at.d.mts +1 -1
  9. package/dist/array/at.d.ts +1 -1
  10. package/dist/array/pullAt.d.mts +1 -1
  11. package/dist/array/pullAt.d.ts +1 -1
  12. package/dist/browser.global.js +1 -1
  13. package/dist/browser.global.js.map +1 -1
  14. package/dist/compat/array/find.d.mts +16 -8
  15. package/dist/compat/array/find.d.ts +16 -8
  16. package/dist/compat/array/find.mjs +5 -2
  17. package/dist/compat/array/sortedIndex.d.mts +16 -0
  18. package/dist/compat/array/sortedIndex.d.ts +16 -0
  19. package/dist/compat/array/sortedIndex.mjs +30 -0
  20. package/dist/compat/array/sortedIndexBy.d.mts +19 -0
  21. package/dist/compat/array/sortedIndexBy.d.ts +19 -0
  22. package/dist/compat/array/sortedIndexBy.mjs +56 -0
  23. package/dist/compat/index.d.mts +7 -0
  24. package/dist/compat/index.d.ts +7 -0
  25. package/dist/compat/index.js +145 -43
  26. package/dist/compat/index.mjs +7 -0
  27. package/dist/compat/math/multiply.d.mts +18 -0
  28. package/dist/compat/math/multiply.d.ts +18 -0
  29. package/dist/compat/math/multiply.mjs +21 -0
  30. package/dist/error/index.d.mts +2 -0
  31. package/dist/error/index.d.ts +2 -0
  32. package/dist/error/index.js +15 -0
  33. package/dist/error/index.mjs +2 -0
  34. package/dist/function/index.d.mts +1 -0
  35. package/dist/function/index.d.ts +1 -0
  36. package/dist/function/index.js +2 -1
  37. package/dist/function/index.mjs +1 -0
  38. package/dist/function/retry.d.mts +59 -0
  39. package/dist/function/retry.d.ts +59 -0
  40. package/dist/function/retry.mjs +35 -0
  41. package/dist/index.d.mts +4 -0
  42. package/dist/index.d.ts +4 -0
  43. package/dist/index.js +18 -11
  44. package/dist/index.mjs +4 -0
  45. package/dist/object/cloneDeepWith.mjs +34 -1
  46. package/dist/object/flattenObject.d.mts +10 -2
  47. package/dist/object/flattenObject.d.ts +10 -2
  48. package/dist/object/flattenObject.mjs +6 -6
  49. package/dist/object/index.js +1 -1
  50. package/dist/predicate/index.d.mts +1 -0
  51. package/dist/predicate/index.d.ts +1 -0
  52. package/dist/predicate/index.js +4 -7
  53. package/dist/predicate/index.mjs +1 -0
  54. package/dist/predicate/isPromise.d.mts +20 -0
  55. package/dist/predicate/isPromise.d.ts +20 -0
  56. package/dist/predicate/isPromise.mjs +5 -0
  57. package/dist/promise/index.d.mts +2 -0
  58. package/dist/promise/index.d.ts +2 -0
  59. package/dist/promise/index.js +56 -4
  60. package/dist/promise/index.mjs +2 -0
  61. package/dist/promise/mutex.d.mts +64 -0
  62. package/dist/promise/mutex.d.ts +64 -0
  63. package/dist/promise/mutex.mjs +16 -0
  64. package/dist/promise/semaphore.d.mts +81 -0
  65. package/dist/promise/semaphore.d.ts +81 -0
  66. package/dist/promise/semaphore.mjs +30 -0
  67. package/error.d.ts +1 -0
  68. package/package.json +42 -21
  69. package/dist/_chunk/index-BGZDR9.js +0 -50
  70. package/dist/_chunk/isPlainObject-octpoD.js +0 -32
@@ -0,0 +1,81 @@
1
+ /**
2
+ * A counting semaphore for async functions that manages available permits.
3
+ * Semaphores are mainly used to limit the number of concurrent async tasks.
4
+ *
5
+ * Each `acquire` operation takes a permit or waits until one is available.
6
+ * Each `release` operation adds a permit, potentially allowing a waiting task to proceed.
7
+ *
8
+ * The semaphore ensures fairness by maintaining a FIFO (First In, First Out) order for acquirers.
9
+ *
10
+ * @example
11
+ * const sema = new Semaphore(2);
12
+ *
13
+ * async function task() {
14
+ * await sema.acquire();
15
+ * try {
16
+ * // This code can only be executed by two tasks at the same time
17
+ * } finally {
18
+ * sema.release();
19
+ * }
20
+ * }
21
+ *
22
+ * task();
23
+ * task();
24
+ * task(); // This task will wait until one of the previous tasks releases the semaphore.
25
+ */
26
+ declare class Semaphore {
27
+ /**
28
+ * The maximum number of concurrent operations allowed.
29
+ * @type {number}
30
+ */
31
+ capacity: number;
32
+ /**
33
+ * The number of available permits.
34
+ * @type {number}
35
+ */
36
+ available: number;
37
+ private deferredTasks;
38
+ /**
39
+ * Creates an instance of Semaphore.
40
+ * @param {number} capacity - The maximum number of concurrent operations allowed.
41
+ *
42
+ * @example
43
+ * const sema = new Semaphore(3); // Allows up to 3 concurrent operations.
44
+ */
45
+ constructor(capacity: number);
46
+ /**
47
+ * Acquires a semaphore, blocking if necessary until one is available.
48
+ * @returns {Promise<void>} A promise that resolves when the semaphore is acquired.
49
+ *
50
+ * @example
51
+ * const sema = new Semaphore(1);
52
+ *
53
+ * async function criticalSection() {
54
+ * await sema.acquire();
55
+ * try {
56
+ * // This code section cannot be executed simultaneously
57
+ * } finally {
58
+ * sema.release();
59
+ * }
60
+ * }
61
+ */
62
+ acquire(): Promise<void>;
63
+ /**
64
+ * Releases a semaphore, allowing one more operation to proceed.
65
+ *
66
+ * @example
67
+ * const sema = new Semaphore(1);
68
+ *
69
+ * async function task() {
70
+ * await sema.acquire();
71
+ * try {
72
+ * // This code can only be executed by two tasks at the same time
73
+ * } finally {
74
+ * sema.release(); // Allows another waiting task to proceed.
75
+ * }
76
+ * }
77
+ */
78
+ release(): void;
79
+ }
80
+
81
+ export { Semaphore };
@@ -0,0 +1,30 @@
1
+ class Semaphore {
2
+ capacity;
3
+ available;
4
+ deferredTasks = [];
5
+ constructor(capacity) {
6
+ this.capacity = capacity;
7
+ this.available = capacity;
8
+ }
9
+ async acquire() {
10
+ if (this.available > 0) {
11
+ this.available--;
12
+ return;
13
+ }
14
+ return new Promise(resolve => {
15
+ this.deferredTasks.push(resolve);
16
+ });
17
+ }
18
+ release() {
19
+ const deferredTask = this.deferredTasks.shift();
20
+ if (deferredTask != null) {
21
+ deferredTask();
22
+ return;
23
+ }
24
+ if (this.available < this.capacity) {
25
+ this.available++;
26
+ }
27
+ }
28
+ }
29
+
30
+ export { Semaphore };
package/error.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/error';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
3
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
4
- "version": "1.31.0",
4
+ "version": "1.32.0-dev.1003+2241152e",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {
@@ -35,6 +35,26 @@
35
35
  "default": "./dist/array/index.js"
36
36
  }
37
37
  },
38
+ "./compat": {
39
+ "import": {
40
+ "types": "./dist/compat/index.d.mts",
41
+ "default": "./dist/compat/index.mjs"
42
+ },
43
+ "require": {
44
+ "types": "./dist/compat/index.d.ts",
45
+ "default": "./dist/compat/index.js"
46
+ }
47
+ },
48
+ "./error": {
49
+ "import": {
50
+ "types": "./dist/error/index.d.mts",
51
+ "default": "./dist/error/index.mjs"
52
+ },
53
+ "require": {
54
+ "types": "./dist/error/index.d.ts",
55
+ "default": "./dist/error/index.js"
56
+ }
57
+ },
38
58
  "./function": {
39
59
  "import": {
40
60
  "types": "./dist/function/index.d.mts",
@@ -105,16 +125,6 @@
105
125
  "default": "./dist/util/index.js"
106
126
  }
107
127
  },
108
- "./compat": {
109
- "import": {
110
- "types": "./dist/compat/index.d.mts",
111
- "default": "./dist/compat/index.mjs"
112
- },
113
- "require": {
114
- "types": "./dist/compat/index.d.ts",
115
- "default": "./dist/compat/index.js"
116
- }
117
- },
118
128
  "./package.json": "./package.json"
119
129
  },
120
130
  "files": [
@@ -148,6 +158,26 @@
148
158
  "default": "./dist/array/index.js"
149
159
  }
150
160
  },
161
+ "./compat": {
162
+ "import": {
163
+ "types": "./dist/compat/index.d.mts",
164
+ "default": "./dist/compat/index.mjs"
165
+ },
166
+ "require": {
167
+ "types": "./dist/compat/index.d.ts",
168
+ "default": "./dist/compat/index.js"
169
+ }
170
+ },
171
+ "./error": {
172
+ "import": {
173
+ "types": "./dist/error/index.d.mts",
174
+ "default": "./dist/error/index.mjs"
175
+ },
176
+ "require": {
177
+ "types": "./dist/error/index.d.ts",
178
+ "default": "./dist/error/index.js"
179
+ }
180
+ },
151
181
  "./function": {
152
182
  "import": {
153
183
  "types": "./dist/function/index.d.mts",
@@ -218,16 +248,6 @@
218
248
  "default": "./dist/util/index.js"
219
249
  }
220
250
  },
221
- "./compat": {
222
- "import": {
223
- "types": "./dist/compat/index.d.mts",
224
- "default": "./dist/compat/index.mjs"
225
- },
226
- "require": {
227
- "types": "./dist/compat/index.d.ts",
228
- "default": "./dist/compat/index.js"
229
- }
230
- },
231
251
  "./package.json": "./package.json"
232
252
  }
233
253
  },
@@ -254,6 +274,7 @@
254
274
  "eslint-plugin-vue": "^9.28.0",
255
275
  "execa": "^9.3.0",
256
276
  "globals": "^15.9.0",
277
+ "happy-dom": "^16.7.3",
257
278
  "jscodeshift": "^17.0.0",
258
279
  "prettier": "^3.2.5",
259
280
  "prettier-plugin-sort-re-exports": "^0.1.0",
@@ -1,50 +0,0 @@
1
- 'use strict';
2
-
3
- class AbortError extends Error {
4
- constructor(message = 'The operation was aborted') {
5
- super(message);
6
- this.name = 'AbortError';
7
- }
8
- }
9
-
10
- class TimeoutError extends Error {
11
- constructor(message = 'The operation was timed out') {
12
- super(message);
13
- this.name = 'TimeoutError';
14
- }
15
- }
16
-
17
- function delay(ms, { signal } = {}) {
18
- return new Promise((resolve, reject) => {
19
- const abortError = () => {
20
- reject(new AbortError());
21
- };
22
- const abortHandler = () => {
23
- clearTimeout(timeoutId);
24
- abortError();
25
- };
26
- if (signal?.aborted) {
27
- return abortError();
28
- }
29
- const timeoutId = setTimeout(() => {
30
- signal?.removeEventListener('abort', abortHandler);
31
- resolve();
32
- }, ms);
33
- signal?.addEventListener('abort', abortHandler, { once: true });
34
- });
35
- }
36
-
37
- async function timeout(ms) {
38
- await delay(ms);
39
- throw new TimeoutError();
40
- }
41
-
42
- async function withTimeout(run, ms) {
43
- return Promise.race([run(), timeout(ms)]);
44
- }
45
-
46
- exports.AbortError = AbortError;
47
- exports.TimeoutError = TimeoutError;
48
- exports.delay = delay;
49
- exports.timeout = timeout;
50
- exports.withTimeout = withTimeout;
@@ -1,32 +0,0 @@
1
- 'use strict';
2
-
3
- function isPrimitive(value) {
4
- return value == null || (typeof value !== 'object' && typeof value !== 'function');
5
- }
6
-
7
- function isTypedArray(x) {
8
- return ArrayBuffer.isView(x) && !(x instanceof DataView);
9
- }
10
-
11
- function getSymbols(object) {
12
- return Object.getOwnPropertySymbols(object).filter(symbol => Object.prototype.propertyIsEnumerable.call(object, symbol));
13
- }
14
-
15
- function isPlainObject(value) {
16
- if (!value || typeof value !== 'object') {
17
- return false;
18
- }
19
- const proto = Object.getPrototypeOf(value);
20
- const hasObjectPrototype = proto === null ||
21
- proto === Object.prototype ||
22
- Object.getPrototypeOf(proto) === null;
23
- if (!hasObjectPrototype) {
24
- return false;
25
- }
26
- return Object.prototype.toString.call(value) === '[object Object]';
27
- }
28
-
29
- exports.getSymbols = getSymbols;
30
- exports.isPlainObject = isPlainObject;
31
- exports.isPrimitive = isPrimitive;
32
- exports.isTypedArray = isTypedArray;