es-toolkit
Advanced tools
Comparing version 1.3.1 to 1.4.0-dev.82
# es-toolkit Changelog | ||
## Version v1.4.0 | ||
Released on June 15th, 2024. | ||
### Features | ||
- Add support for [random](https://es-toolkit.slash.page/reference/math/random.html). (https://github.com/toss/es-toolkit/pull/53) | ||
- Add support for [randomInt](https://es-toolkit.slash.page/reference/math/randomInt.html). ([99a34e4](https://github.com/toss/es-toolkit/commit/99a34e4e9944c1b843e9d97dff0b5ff4e5eec260)) | ||
- Add support for using AbortSignals to cancel the `Promise` returned by `delay`. (https://github.com/toss/es-toolkit/pull/52) | ||
### Performance Optimizations | ||
- Optimized `uniqBy`. ([60e7974](https://github.com/toss/es-toolkit/commit/60e79741271e645bfa551f708466e43b136f69b1)) | ||
## Version v1.3.1 | ||
@@ -4,0 +18,0 @@ |
@@ -240,3 +240,3 @@ "use strict"; | ||
} | ||
return [...map.values()]; | ||
return Array.from(map.values()); | ||
} | ||
@@ -263,3 +263,10 @@ | ||
function uniqBy(arr, mapper) { | ||
return uniqWith(arr, (item1, item2) => mapper(item1) === mapper(item2)); | ||
const map = /* @__PURE__ */ new Map(); | ||
for (const item of arr) { | ||
const key = mapper(item); | ||
if (!map.has(key)) { | ||
map.set(key, item); | ||
} | ||
} | ||
return Array.from(map.values()); | ||
} | ||
@@ -266,0 +273,0 @@ |
@@ -34,3 +34,3 @@ "use strict"; | ||
} | ||
return [...map.values()]; | ||
return Array.from(map.values()); | ||
} | ||
@@ -37,0 +37,0 @@ // Annotate the CommonJS export names for ESM import in node: |
@@ -26,19 +26,12 @@ "use strict"; | ||
module.exports = __toCommonJS(uniqBy_exports); | ||
// src/array/uniqWith.ts | ||
function uniqWith(arr, areItemsEqual) { | ||
const result = []; | ||
function uniqBy(arr, mapper) { | ||
const map = /* @__PURE__ */ new Map(); | ||
for (const item of arr) { | ||
const isUniq = result.every((v) => !areItemsEqual(v, item)); | ||
if (isUniq) { | ||
result.push(item); | ||
const key = mapper(item); | ||
if (!map.has(key)) { | ||
map.set(key, item); | ||
} | ||
} | ||
return result; | ||
return Array.from(map.values()); | ||
} | ||
// src/array/uniqBy.ts | ||
function uniqBy(arr, mapper) { | ||
return uniqWith(arr, (item1, item2) => mapper(item1) === mapper(item2)); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -45,0 +38,0 @@ 0 && (module.exports = { |
@@ -50,3 +50,3 @@ "use strict"; | ||
} | ||
return [...map.values()]; | ||
return Array.from(map.values()); | ||
} | ||
@@ -53,0 +53,0 @@ |
@@ -48,5 +48,4 @@ "use strict"; | ||
} | ||
signal == null ? void 0 : signal.removeEventListener("abort", onAbort); | ||
}; | ||
signal == null ? void 0 : signal.addEventListener("abort", onAbort); | ||
signal == null ? void 0 : signal.addEventListener("abort", onAbort, { once: true }); | ||
return debounced; | ||
@@ -53,0 +52,0 @@ } |
@@ -53,5 +53,4 @@ "use strict"; | ||
} | ||
signal == null ? void 0 : signal.removeEventListener("abort", onAbort); | ||
}; | ||
signal == null ? void 0 : signal.addEventListener("abort", onAbort); | ||
signal == null ? void 0 : signal.addEventListener("abort", onAbort, { once: true }); | ||
return debounced; | ||
@@ -58,0 +57,0 @@ } |
@@ -31,2 +31,3 @@ export { chunk } from './array/chunk.js'; | ||
export { zipWith } from './array/zipWith.js'; | ||
export { AbortError } from './error/AbortError.js'; | ||
export { debounce } from './function/debounce.js'; | ||
@@ -37,2 +38,4 @@ export { noop } from './function/noop.js'; | ||
export { clamp } from './math/clamp.js'; | ||
export { random } from './math/random.js'; | ||
export { randomInt } from './math/randomInt.js'; | ||
export { round } from './math/round.js'; | ||
@@ -39,0 +42,0 @@ export { sum } from './math/sum.js'; |
@@ -37,2 +37,3 @@ "use strict"; | ||
__export(src_exports, { | ||
AbortError: () => AbortError, | ||
chunk: () => chunk, | ||
@@ -64,2 +65,4 @@ clamp: () => clamp, | ||
pickBy: () => pickBy, | ||
random: () => random, | ||
randomInt: () => randomInt, | ||
round: () => round, | ||
@@ -272,3 +275,3 @@ sample: () => sample, | ||
} | ||
return [...map.values()]; | ||
return Array.from(map.values()); | ||
} | ||
@@ -295,3 +298,10 @@ | ||
function uniqBy(arr, mapper) { | ||
return uniqWith(arr, (item1, item2) => mapper(item1) === mapper(item2)); | ||
const map = /* @__PURE__ */ new Map(); | ||
for (const item of arr) { | ||
const key = mapper(item); | ||
if (!map.has(key)) { | ||
map.set(key, item); | ||
} | ||
} | ||
return Array.from(map.values()); | ||
} | ||
@@ -345,2 +355,10 @@ | ||
// src/error/AbortError.ts | ||
var AbortError = class extends Error { | ||
constructor(message = "The operation was aborted") { | ||
super(message); | ||
this.name = "AbortError"; | ||
} | ||
}; | ||
// src/function/debounce.ts | ||
@@ -369,5 +387,4 @@ function debounce(func, debounceMs, { signal } = {}) { | ||
} | ||
signal == null ? void 0 : signal.removeEventListener("abort", onAbort); | ||
}; | ||
signal == null ? void 0 : signal.addEventListener("abort", onAbort); | ||
signal == null ? void 0 : signal.addEventListener("abort", onAbort, { once: true }); | ||
return debounced; | ||
@@ -416,2 +433,15 @@ } | ||
// src/math/random.ts | ||
function random(minimum, maximum) { | ||
if (minimum >= maximum) { | ||
throw new Error("Invalid input: The maximum value must be greater than the minimum value."); | ||
} | ||
return Math.random() * (maximum - minimum) + minimum; | ||
} | ||
// src/math/randomInt.ts | ||
function randomInt(minimum, maximum) { | ||
return Math.floor(random(minimum, maximum)); | ||
} | ||
// src/math/round.ts | ||
@@ -495,5 +525,16 @@ function round(value, precision = 0) { | ||
// src/promise/delay.ts | ||
function delay(ms) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
function delay(ms, { signal } = {}) { | ||
return new Promise((resolve, reject) => { | ||
const abortError = () => { | ||
reject(new AbortError()); | ||
}; | ||
const abortHandler = () => { | ||
clearTimeout(timeoutId); | ||
abortError(); | ||
}; | ||
if (signal == null ? void 0 : signal.aborted) { | ||
return abortError(); | ||
} | ||
const timeoutId = setTimeout(resolve, ms); | ||
signal == null ? void 0 : signal.addEventListener("abort", abortHandler, { once: true }); | ||
}); | ||
@@ -503,2 +544,3 @@ } | ||
0 && (module.exports = { | ||
AbortError, | ||
chunk, | ||
@@ -530,2 +572,4 @@ clamp, | ||
pickBy, | ||
random, | ||
randomInt, | ||
round, | ||
@@ -532,0 +576,0 @@ sample, |
export { clamp } from './clamp.js'; | ||
export { random } from './random.js'; | ||
export { randomInt } from './randomInt.js'; | ||
export { round } from './round.js'; | ||
export { sum } from './sum.js'; |
@@ -24,2 +24,4 @@ "use strict"; | ||
clamp: () => clamp, | ||
random: () => random, | ||
randomInt: () => randomInt, | ||
round: () => round, | ||
@@ -38,2 +40,15 @@ sum: () => sum | ||
// src/math/random.ts | ||
function random(minimum, maximum) { | ||
if (minimum >= maximum) { | ||
throw new Error("Invalid input: The maximum value must be greater than the minimum value."); | ||
} | ||
return Math.random() * (maximum - minimum) + minimum; | ||
} | ||
// src/math/randomInt.ts | ||
function randomInt(minimum, maximum) { | ||
return Math.floor(random(minimum, maximum)); | ||
} | ||
// src/math/round.ts | ||
@@ -56,2 +71,4 @@ function round(value, precision = 0) { | ||
clamp, | ||
random, | ||
randomInt, | ||
round, | ||
@@ -58,0 +75,0 @@ sum |
@@ -0,1 +1,4 @@ | ||
interface DelayOptions { | ||
signal?: AbortSignal; | ||
} | ||
/** | ||
@@ -8,2 +11,4 @@ * Delays the execution of code for a specified number of milliseconds. | ||
* @param {number} ms - The number of milliseconds to delay. | ||
* @param {DelayOptions} options - The options object. | ||
* @param {AbortSignal} options.signal - An optional AbortSignal to cancel the delay. | ||
* @returns {Promise<void>} A Promise that resolves after the specified delay. | ||
@@ -19,5 +24,17 @@ * | ||
* foo(); | ||
* | ||
* // With AbortSignal | ||
* const controller = new AbortController(); | ||
* const { signal } = controller; | ||
* | ||
* setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms | ||
* try { | ||
* await delay(100, { signal }); | ||
* } catch (error) { | ||
* console.error(error); // Will log 'AbortError' | ||
* } | ||
* } | ||
*/ | ||
declare function delay(ms: number): Promise<void>; | ||
declare function delay(ms: number, { signal }?: DelayOptions): Promise<void>; | ||
export { delay }; |
@@ -26,5 +26,26 @@ "use strict"; | ||
module.exports = __toCommonJS(delay_exports); | ||
function delay(ms) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
// src/error/AbortError.ts | ||
var AbortError = class extends Error { | ||
constructor(message = "The operation was aborted") { | ||
super(message); | ||
this.name = "AbortError"; | ||
} | ||
}; | ||
// src/promise/delay.ts | ||
function delay(ms, { signal } = {}) { | ||
return new Promise((resolve, reject) => { | ||
const abortError = () => { | ||
reject(new AbortError()); | ||
}; | ||
const abortHandler = () => { | ||
clearTimeout(timeoutId); | ||
abortError(); | ||
}; | ||
if (signal == null ? void 0 : signal.aborted) { | ||
return abortError(); | ||
} | ||
const timeoutId = setTimeout(resolve, ms); | ||
signal == null ? void 0 : signal.addEventListener("abort", abortHandler, { once: true }); | ||
}); | ||
@@ -31,0 +52,0 @@ } |
@@ -27,6 +27,25 @@ "use strict"; | ||
// src/error/AbortError.ts | ||
var AbortError = class extends Error { | ||
constructor(message = "The operation was aborted") { | ||
super(message); | ||
this.name = "AbortError"; | ||
} | ||
}; | ||
// src/promise/delay.ts | ||
function delay(ms) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
function delay(ms, { signal } = {}) { | ||
return new Promise((resolve, reject) => { | ||
const abortError = () => { | ||
reject(new AbortError()); | ||
}; | ||
const abortHandler = () => { | ||
clearTimeout(timeoutId); | ||
abortError(); | ||
}; | ||
if (signal == null ? void 0 : signal.aborted) { | ||
return abortError(); | ||
} | ||
const timeoutId = setTimeout(resolve, ms); | ||
signal == null ? void 0 : signal.addEventListener("abort", abortHandler, { once: true }); | ||
}); | ||
@@ -33,0 +52,0 @@ } |
{ | ||
"name": "es-toolkit", | ||
"description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.", | ||
"version": "1.3.1", | ||
"version": "1.4.0-dev.82+8f1ffc75", | ||
"workspaces": [ | ||
@@ -6,0 +6,0 @@ "docs" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
521594
466
5595
2
1