es-toolkit
Advanced tools
Comparing version 1.4.0 to 1.5.0-dev.102
# es-toolkit Changelog | ||
## Version v1.5.0 | ||
Released on June 28th, 2024. | ||
### Features | ||
- Add support for [range](https://es-toolkit.slash.page/reference/math/range.html). (https://github.com/toss/es-toolkit/pull/77, [2db11d8](https://github.com/toss/es-toolkit/commit/2db11d8882f6d7b0b53271c76f4c5007b6a9181e)). | ||
- Add support for [minBy](https://es-toolkit.slash.page/reference/math/minBy.html) (https://github.com/toss/es-toolkit/pull/71). | ||
- Add support for [maxBy](https://es-toolkit.slash.page/reference/math/maxBy.html) (https://github.com/toss/es-toolkit/pull/64). | ||
### Bug fixes | ||
- Enforce stricter argument types in `pickBy` and `omitBy`. (https://github.com/toss/es-toolkit/pull/60) | ||
- Fix a bug in `difference` where one array parameter was not readonly. (https://github.com/toss/es-toolkit/pull/83) | ||
- Fix a bug in `round` where it incorrectly accepts floating-point numbers as `precision`. (https://github.com/toss/es-toolkit/pull/79) | ||
## Version v1.4.0 | ||
@@ -4,0 +20,0 @@ |
@@ -23,4 +23,4 @@ /** | ||
*/ | ||
declare function difference<T>(firstArr: readonly T[], secondArr: T[]): T[]; | ||
declare function difference<T>(firstArr: readonly T[], secondArr: readonly T[]): T[]; | ||
export { difference }; |
@@ -13,2 +13,4 @@ export { chunk } from './chunk.js'; | ||
export { intersectionWith } from './intersectionWith.js'; | ||
export { maxBy } from './maxBy.js'; | ||
export { minBy } from './minBy.js'; | ||
export { partition } from './partition.js'; | ||
@@ -15,0 +17,0 @@ export { sample } from './sample.js'; |
@@ -35,2 +35,4 @@ "use strict"; | ||
intersectionWith: () => intersectionWith, | ||
maxBy: () => maxBy, | ||
minBy: () => minBy, | ||
partition: () => partition, | ||
@@ -157,2 +159,30 @@ sample: () => sample, | ||
// src/array/maxBy.ts | ||
function maxBy(elements, selector) { | ||
let maxElement = elements[0]; | ||
let max = -Infinity; | ||
for (const element of elements) { | ||
const value = selector(element); | ||
if (value > max) { | ||
max = value; | ||
maxElement = element; | ||
} | ||
} | ||
return maxElement; | ||
} | ||
// src/array/minBy.ts | ||
function minBy(elements, selector) { | ||
let minElement = elements[0]; | ||
let min = Infinity; | ||
for (const element of elements) { | ||
const value = selector(element); | ||
if (value < min) { | ||
min = value; | ||
minElement = element; | ||
} | ||
} | ||
return minElement; | ||
} | ||
// src/array/partition.ts | ||
@@ -333,2 +363,4 @@ function partition(arr, isInTruthy) { | ||
intersectionWith, | ||
maxBy, | ||
minBy, | ||
partition, | ||
@@ -335,0 +367,0 @@ sample, |
@@ -13,2 +13,4 @@ export { chunk } from './array/chunk.js'; | ||
export { intersectionWith } from './array/intersectionWith.js'; | ||
export { maxBy } from './array/maxBy.js'; | ||
export { minBy } from './array/minBy.js'; | ||
export { partition } from './array/partition.js'; | ||
@@ -42,2 +44,3 @@ export { sample } from './array/sample.js'; | ||
export { sum } from './math/sum.js'; | ||
export { range } from './math/range.js'; | ||
export { omit } from './object/omit.js'; | ||
@@ -44,0 +47,0 @@ export { omitBy } from './object/omitBy.js'; |
@@ -57,2 +57,4 @@ "use strict"; | ||
isUndefined: () => isUndefined, | ||
maxBy: () => maxBy, | ||
minBy: () => minBy, | ||
noop: () => noop, | ||
@@ -67,2 +69,3 @@ omit: () => omit, | ||
randomInt: () => randomInt, | ||
range: () => range, | ||
round: () => round, | ||
@@ -191,2 +194,30 @@ sample: () => sample, | ||
// src/array/maxBy.ts | ||
function maxBy(elements, selector) { | ||
let maxElement = elements[0]; | ||
let max = -Infinity; | ||
for (const element of elements) { | ||
const value = selector(element); | ||
if (value > max) { | ||
max = value; | ||
maxElement = element; | ||
} | ||
} | ||
return maxElement; | ||
} | ||
// src/array/minBy.ts | ||
function minBy(elements, selector) { | ||
let minElement = elements[0]; | ||
let min = Infinity; | ||
for (const element of elements) { | ||
const value = selector(element); | ||
if (value < min) { | ||
min = value; | ||
minElement = element; | ||
} | ||
} | ||
return minElement; | ||
} | ||
// src/array/partition.ts | ||
@@ -445,2 +476,5 @@ function partition(arr, isInTruthy) { | ||
function round(value, precision = 0) { | ||
if (!Number.isInteger(precision)) { | ||
throw new Error("Precision must be an integer."); | ||
} | ||
const multiplier = Math.pow(10, precision); | ||
@@ -459,2 +493,22 @@ return Math.round(value * multiplier) / multiplier; | ||
// src/math/range.ts | ||
function range(start, end, step) { | ||
if (end == null) { | ||
end = start; | ||
start = 0; | ||
} | ||
if (step == null) { | ||
step = 1; | ||
} | ||
if (!Number.isInteger(step) || step === 0) { | ||
throw new Error(`The step value must be a non-zero integer.`); | ||
} | ||
const length = Math.max(Math.ceil((end - start) / step), 0); | ||
const result = new Array(length); | ||
for (let i = 0; i < length; i++) { | ||
result[i] = start + i * step; | ||
} | ||
return result; | ||
} | ||
// src/object/omit.ts | ||
@@ -561,2 +615,4 @@ function omit(obj, keys) { | ||
isUndefined, | ||
maxBy, | ||
minBy, | ||
noop, | ||
@@ -571,2 +627,3 @@ omit, | ||
randomInt, | ||
range, | ||
round, | ||
@@ -573,0 +630,0 @@ sample, |
@@ -6,1 +6,2 @@ export { clamp } from './clamp.js'; | ||
export { sum } from './sum.js'; | ||
export { range } from './range.js'; |
@@ -26,2 +26,3 @@ "use strict"; | ||
randomInt: () => randomInt, | ||
range: () => range, | ||
round: () => round, | ||
@@ -55,2 +56,5 @@ sum: () => sum | ||
function round(value, precision = 0) { | ||
if (!Number.isInteger(precision)) { | ||
throw new Error("Precision must be an integer."); | ||
} | ||
const multiplier = Math.pow(10, precision); | ||
@@ -68,2 +72,22 @@ return Math.round(value * multiplier) / multiplier; | ||
} | ||
// src/math/range.ts | ||
function range(start, end, step) { | ||
if (end == null) { | ||
end = start; | ||
start = 0; | ||
} | ||
if (step == null) { | ||
step = 1; | ||
} | ||
if (!Number.isInteger(step) || step === 0) { | ||
throw new Error(`The step value must be a non-zero integer.`); | ||
} | ||
const length = Math.max(Math.ceil((end - start) / step), 0); | ||
const result = new Array(length); | ||
for (let i = 0; i < length; i++) { | ||
result[i] = start + i * step; | ||
} | ||
return result; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -74,2 +98,3 @@ 0 && (module.exports = { | ||
randomInt, | ||
range, | ||
round, | ||
@@ -76,0 +101,0 @@ sum |
/** | ||
* Generates a random floating-point number between minimum (inclusive) and maximum (exclusive). | ||
* If min is greater than maximum, the values are swapped. | ||
* Generate a random number within the given range. | ||
* | ||
* @param {number} minimum - The lower bound (inclusive). | ||
* @param {number} maximum - The upper bound (exclusive). | ||
* @returns {number} A random integer between minimum (inclusive) and maximum (exclusive). | ||
* @returns {number} A random number between minimum (inclusive) and maximum (exclusive). The number can be an integer or a decimal. | ||
* @throws {Error} Throws an error if `maximum` is not greater than `minimum`. | ||
* | ||
* @example | ||
* const result = random(0, 5); // result will be a random floating-point number between 0 (inclusive) and 5 (exclusive) | ||
* const result2 = random(5, 0); // This will throw an error | ||
* const result1 = random(0, 5); // Returns a random number between 0 and 5. | ||
* const result2 = random(5, 0); // If the minimum is greater than the maximum, an error is thrown | ||
* const result3 = random(5, 5); // If the minimum is equal to the maximum, an error is thrown. | ||
*/ | ||
@@ -14,0 +14,0 @@ declare function random(minimum: number, maximum: number): number; |
@@ -10,2 +10,3 @@ /** | ||
* @returns {number} The rounded number. | ||
* @throws {Error} Throws an error if `Precision` is not integer. | ||
* | ||
@@ -16,2 +17,3 @@ * @example | ||
* const result3 = round(1.2345, 3); // result3 will be 1.235 | ||
* const result4 = round(1.2345, 3.1); // This will throw an error | ||
*/ | ||
@@ -18,0 +20,0 @@ declare function round(value: number, precision?: number): number; |
@@ -27,2 +27,5 @@ "use strict"; | ||
function round(value, precision = 0) { | ||
if (!Number.isInteger(precision)) { | ||
throw new Error("Precision must be an integer."); | ||
} | ||
const multiplier = Math.pow(10, precision); | ||
@@ -29,0 +32,0 @@ return Math.round(value * multiplier) / multiplier; |
@@ -8,3 +8,3 @@ /** | ||
* @param {T} obj - The object to omit properties from. | ||
* @param {(value: T[string], key: string) => boolean} shouldOmit - A predicate function that determines | ||
* @param {(value: T[string], key: keyof T) => boolean} shouldOmit - A predicate function that determines | ||
* whether a property should be omitted. It takes the property's key and value as arguments and returns `true` | ||
@@ -20,4 +20,4 @@ * if the property should be omitted, and `false` otherwise. | ||
*/ | ||
declare function omitBy<T extends Record<string, any>>(obj: T, shouldOmit: (value: T[keyof T], key: string) => boolean): Partial<T>; | ||
declare function omitBy<T extends Record<string, any>>(obj: T, shouldOmit: (value: T[keyof T], key: keyof T) => boolean): Partial<T>; | ||
export { omitBy }; |
@@ -8,3 +8,3 @@ /** | ||
* @param {T} obj - The object to pick properties from. | ||
* @param {(value: T[keyof T], key: string) => boolean} shouldPick - A predicate function that determines | ||
* @param {(value: T[keyof T], key: keyof T) => boolean} shouldPick - A predicate function that determines | ||
* whether a property should be picked. It takes the property's key and value as arguments and returns `true` | ||
@@ -20,4 +20,4 @@ * if the property should be picked, and `false` otherwise. | ||
*/ | ||
declare function pickBy<T extends Record<string, any>>(obj: T, shouldPick: (value: T[keyof T], key: string) => boolean): Partial<T>; | ||
declare function pickBy<T extends Record<string, any>>(obj: T, shouldPick: (value: T[keyof T], key: keyof T) => boolean): Partial<T>; | ||
export { pickBy }; |
{ | ||
"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.4.0", | ||
"version": "1.5.0-dev.102+8ae92e43", | ||
"workspaces": [ | ||
@@ -171,2 +171,3 @@ "docs" | ||
"@changesets/cli": "^2.27.1", | ||
"@codspeed/vitest-plugin": "^3.1.0", | ||
"@types/babel__core": "^7", | ||
@@ -194,2 +195,3 @@ "@types/babel__preset-env": "^7", | ||
"test": "vitest run --coverage --typecheck", | ||
"bench": "vitest bench", | ||
"lint": "eslint ./src --ext .ts", | ||
@@ -196,0 +198,0 @@ "format": "prettier --write ." |
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
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
555925
490
6005
22
2
1