es-toolkit 1.4.0-dev.98 → 1.5.0-dev.102

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 (54) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/array/index.d.mts +2 -1
  3. package/dist/array/index.d.ts +2 -1
  4. package/dist/array/index.js +32 -19
  5. package/dist/array/index.js.map +1 -1
  6. package/dist/array/index.mjs +31 -27
  7. package/dist/{math → array}/maxBy.js +1 -1
  8. package/dist/array/maxBy.js.map +1 -0
  9. package/dist/{math → array}/maxBy.mjs +1 -1
  10. package/dist/{math → array}/minBy.js +1 -1
  11. package/dist/array/minBy.js.map +1 -0
  12. package/dist/{math → array}/minBy.mjs +1 -1
  13. package/dist/chunk-7LAMAYGN.mjs +1 -0
  14. package/dist/{chunk-SAL5OUAE.mjs → chunk-H23GUJVF.mjs} +2 -2
  15. package/dist/chunk-H23GUJVF.mjs.map +1 -0
  16. package/dist/{chunk-6E6PBLFK.mjs → chunk-KZ5JY7J3.mjs} +2 -2
  17. package/dist/chunk-KZ5JY7J3.mjs.map +1 -0
  18. package/dist/chunk-NP6PDX3E.mjs +1 -0
  19. package/dist/chunk-UNLX2F2Y.mjs +24 -0
  20. package/dist/chunk-UNLX2F2Y.mjs.map +1 -0
  21. package/dist/index.d.mts +3 -3
  22. package/dist/index.d.ts +3 -3
  23. package/dist/index.js +44 -41
  24. package/dist/index.js.map +1 -1
  25. package/dist/index.mjs +50 -50
  26. package/dist/math/index.d.mts +1 -2
  27. package/dist/math/index.d.ts +1 -2
  28. package/dist/math/index.js +18 -28
  29. package/dist/math/index.js.map +1 -1
  30. package/dist/math/index.mjs +8 -12
  31. package/dist/{array → math}/range.d.mts +7 -6
  32. package/dist/{array → math}/range.d.ts +7 -6
  33. package/dist/{array → math}/range.js +8 -5
  34. package/dist/math/range.js.map +1 -0
  35. package/dist/{array → math}/range.mjs +1 -1
  36. package/package.json +1 -1
  37. package/dist/array/range.js.map +0 -1
  38. package/dist/chunk-6E6PBLFK.mjs.map +0 -1
  39. package/dist/chunk-AJG36DJN.mjs +0 -21
  40. package/dist/chunk-AJG36DJN.mjs.map +0 -1
  41. package/dist/chunk-QVHWSUEH.mjs +0 -1
  42. package/dist/chunk-SAL5OUAE.mjs.map +0 -1
  43. package/dist/chunk-SDBGIIDD.mjs +0 -1
  44. package/dist/math/maxBy.js.map +0 -1
  45. package/dist/math/minBy.js.map +0 -1
  46. /package/dist/{math → array}/maxBy.d.mts +0 -0
  47. /package/dist/{math → array}/maxBy.d.ts +0 -0
  48. /package/dist/{math → array}/maxBy.mjs.map +0 -0
  49. /package/dist/{math → array}/minBy.d.mts +0 -0
  50. /package/dist/{math → array}/minBy.d.ts +0 -0
  51. /package/dist/{math → array}/minBy.mjs.map +0 -0
  52. /package/dist/{array/range.mjs.map → chunk-7LAMAYGN.mjs.map} +0 -0
  53. /package/dist/{chunk-QVHWSUEH.mjs.map → chunk-NP6PDX3E.mjs.map} +0 -0
  54. /package/dist/{chunk-SDBGIIDD.mjs.map → math/range.mjs.map} +0 -0
@@ -21,10 +21,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var math_exports = {};
22
22
  __export(math_exports, {
23
23
  clamp: () => clamp,
24
- maxBy: () => maxBy,
25
- minBy: () => minBy,
26
24
  random: () => random,
27
25
  randomInt: () => randomInt,
26
+ range: () => range,
28
27
  round: () => round,
29
28
  sum: () => sum
30
29
  });
@@ -69,40 +68,31 @@ function sum(nums) {
69
68
  return result;
70
69
  }
71
70
 
72
- // src/math/maxBy.ts
73
- function maxBy(elements, selector) {
74
- let maxElement = elements[0];
75
- let max = -Infinity;
76
- for (const element of elements) {
77
- const value = selector(element);
78
- if (value > max) {
79
- max = value;
80
- maxElement = element;
81
- }
71
+ // src/math/range.ts
72
+ function range(start, end, step) {
73
+ if (end == null) {
74
+ end = start;
75
+ start = 0;
82
76
  }
83
- return maxElement;
84
- }
85
-
86
- // src/math/minBy.ts
87
- function minBy(elements, selector) {
88
- let minElement = elements[0];
89
- let min = Infinity;
90
- for (const element of elements) {
91
- const value = selector(element);
92
- if (value < min) {
93
- min = value;
94
- minElement = element;
95
- }
77
+ if (step == null) {
78
+ step = 1;
79
+ }
80
+ if (!Number.isInteger(step) || step === 0) {
81
+ throw new Error(`The step value must be a non-zero integer.`);
96
82
  }
97
- return minElement;
83
+ const length = Math.max(Math.ceil((end - start) / step), 0);
84
+ const result = new Array(length);
85
+ for (let i = 0; i < length; i++) {
86
+ result[i] = start + i * step;
87
+ }
88
+ return result;
98
89
  }
99
90
  // Annotate the CommonJS export names for ESM import in node:
100
91
  0 && (module.exports = {
101
92
  clamp,
102
- maxBy,
103
- minBy,
104
93
  random,
105
94
  randomInt,
95
+ range,
106
96
  round,
107
97
  sum
108
98
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/math/index.ts","../../src/math/clamp.ts","../../src/math/random.ts","../../src/math/randomInt.ts","../../src/math/round.ts","../../src/math/sum.ts","../../src/math/maxBy.ts","../../src/math/minBy.ts"],"sourcesContent":["export { clamp } from './clamp';\nexport { random } from './random';\nexport { randomInt } from './randomInt';\nexport { round } from './round';\nexport { sum } from './sum';\nexport { maxBy } from './maxBy';\nexport { minBy } from './minBy';\n","/**\n * Clamps a number within the inclusive lower and upper bounds.\n *\n * This function takes a number and two bounds, and returns the number clamped within the specified bounds.\n * If only one bound is provided, it returns the minimum of the value and the bound.\n *\n * @param {number} value - The number to clamp.\n * @param {number} minimum - The minimum bound to clamp the number.\n * @param {number} maximum - The maximum bound to clamp the number.\n * @returns {number} The clamped number within the specified bounds.\n *\n * @example\n * const result1 = clamp(10, 5); // result1 will be 5, as 10 is clamped to the bound 5\n * const result2 = clamp(10, 5, 15); // result2 will be 10, as it is within the bounds 5 and 15\n * const result3 = clamp(2, 5, 15); // result3 will be 5, as 2 is clamped to the lower bound 5\n * const result4 = clamp(20, 5, 15); // result4 will be 15, as 20 is clamped to the upper bound 15\n */\nexport function clamp(value: number, maximum: number): number;\nexport function clamp(value: number, minimum: number, maximum: number): number;\nexport function clamp(value: number, bound1: number, bound2?: number): number {\n if (bound2 == null) {\n return Math.min(value, bound1);\n }\n\n return Math.min(Math.max(value, bound1), bound2);\n}\n","/**\n * Generate a random number within the given range.\n *\n * @param {number} minimum - The lower bound (inclusive).\n * @param {number} maximum - The upper bound (exclusive).\n * @returns {number} A random number between minimum (inclusive) and maximum (exclusive). The number can be an integer or a decimal.\n * @throws {Error} Throws an error if `maximum` is not greater than `minimum`.\n *\n * @example\n * const result1 = random(0, 5); // Returns a random number between 0 and 5.\n * const result2 = random(5, 0); // If the minimum is greater than the maximum, an error is thrown\n * const result3 = random(5, 5); // If the minimum is equal to the maximum, an error is thrown.\n */\nexport function random(minimum: number, maximum: number): number {\n if (minimum >= maximum) {\n throw new Error('Invalid input: The maximum value must be greater than the minimum value.');\n }\n\n return Math.random() * (maximum - minimum) + minimum;\n}\n","import { random } from './random';\n\n/**\n * Generates a random integer between minimum (inclusive) and maximum (exclusive).\n *\n * @param {number} minimum - The lower bound (inclusive).\n * @param {number} maximum - The upper bound (exclusive).\n * @returns {number} A random integer between minimum (inclusive) and maximum (exclusive).\n * @throws {Error} Throws an error if `maximum` is not greater than `minimum`.\n *\n * @example\n * const result = randomInt(0, 5); // result will be a random integer between 0 (inclusive) and 5 (exclusive)\n * const result2 = randomInt(5, 0); // This will throw an error\n */\nexport function randomInt(minimum: number, maximum: number): number {\n return Math.floor(random(minimum, maximum));\n}\n","/**\n * Rounds a number to a specified precision.\n *\n * This function takes a number and an optional precision value, and returns the number rounded\n * to the specified number of decimal places.\n *\n * @param {number} value - The number to round.\n * @param {number} [precision=0] - The number of decimal places to round to. Defaults to 0.\n * @returns {number} The rounded number.\n * @throws {Error} Throws an error if `Precision` is not integer.\n *\n * @example\n * const result1 = round(1.2345); // result1 will be 1\n * const result2 = round(1.2345, 2); // result2 will be 1.23\n * const result3 = round(1.2345, 3); // result3 will be 1.235\n * const result4 = round(1.2345, 3.1); // This will throw an error\n */\nexport function round(value: number, precision = 0): number {\n if (!Number.isInteger(precision)) {\n throw new Error('Precision must be an integer.');\n }\n const multiplier = Math.pow(10, precision);\n return Math.round(value * multiplier) / multiplier;\n}\n","/**\n * Calculates the sum of an array of numbers.\n *\n * This function takes an array of numbers and returns the sum of all the elements in the array.\n *\n * @param {number[]} nums - An array of numbers to be summed.\n * @returns {number} The sum of all the numbers in the array.\n *\n * @example\n * const numbers = [1, 2, 3, 4, 5];\n * const result = sum(numbers);\n * // result will be 15\n */\nexport function sum(nums: number[]): number {\n let result = 0;\n\n for (const num of nums) {\n result += num;\n }\n\n return result;\n}\n","/**\n * Returns the element of the specified array that has the maximum value, according to the specified selector.\n *\n * This function takes an array of elements and a selector function, and returns the element with the maximum value according to the selector.\n *\n * @param {T[]} elements - An array of elements to be compared.\n * @param {(element: T) => number} selector - A function that takes an element and returns a number.\n * @returns {T} The element with the maximum value according to the selector.\n *\n * @example\n * const people = [\n * { name: 'Mark', age: 25 },\n * { name: 'Nunu', age: 30 },\n * { name: 'Overmars', age: 20 }\n * ];\n * const result = maxBy(people, person => person.age);\n * // result will be { name: 'Nunu', age: 30 }\n */\nexport function maxBy<T>(elements: T[], selector: (element: T) => number): T {\n let maxElement = elements[0];\n let max = -Infinity;\n\n for (const element of elements) {\n const value = selector(element);\n if (value > max) {\n max = value;\n maxElement = element;\n }\n }\n\n return maxElement;\n}\n","/**\n * Returns the minimum value in an array of numbers.\n *\n * This function takes an array of elements and a selector function, and returns the element with the minimum value according to the selector.\n *\n * If the array is empty, the function returns `undefined`.\n *\n * @param elements An array of elements.\n * @param selector A function that selects a number from an element.\n */\nexport function minBy<T>(elements: T[], selector: (element: T) => number): T {\n let minElement = elements[0];\n let min = Infinity;\n\n for (const element of elements) {\n const value = selector(element);\n if (value < min) {\n min = value;\n minElement = element;\n }\n }\n\n return minElement;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACmBO,SAAS,MAAM,OAAe,QAAgB,QAAyB;AAC5E,MAAI,UAAU,MAAM;AAClB,WAAO,KAAK,IAAI,OAAO,MAAM;AAAA,EAC/B;AAEA,SAAO,KAAK,IAAI,KAAK,IAAI,OAAO,MAAM,GAAG,MAAM;AACjD;;;ACZO,SAAS,OAAO,SAAiB,SAAyB;AAC/D,MAAI,WAAW,SAAS;AACtB,UAAM,IAAI,MAAM,0EAA0E;AAAA,EAC5F;AAEA,SAAO,KAAK,OAAO,KAAK,UAAU,WAAW;AAC/C;;;ACLO,SAAS,UAAU,SAAiB,SAAyB;AAClE,SAAO,KAAK,MAAM,OAAO,SAAS,OAAO,CAAC;AAC5C;;;ACCO,SAAS,MAAM,OAAe,YAAY,GAAW;AAC1D,MAAI,CAAC,OAAO,UAAU,SAAS,GAAG;AAChC,UAAM,IAAI,MAAM,+BAA+B;AAAA,EACjD;AACA,QAAM,aAAa,KAAK,IAAI,IAAI,SAAS;AACzC,SAAO,KAAK,MAAM,QAAQ,UAAU,IAAI;AAC1C;;;ACVO,SAAS,IAAI,MAAwB;AAC1C,MAAI,SAAS;AAEb,aAAW,OAAO,MAAM;AACtB,cAAU;AAAA,EACZ;AAEA,SAAO;AACT;;;ACHO,SAAS,MAAS,UAAe,UAAqC;AAC3E,MAAI,aAAa,SAAS,CAAC;AAC3B,MAAI,MAAM;AAEV,aAAW,WAAW,UAAU;AAC9B,UAAM,QAAQ,SAAS,OAAO;AAC9B,QAAI,QAAQ,KAAK;AACf,YAAM;AACN,mBAAa;AAAA,IACf;AAAA,EACF;AAEA,SAAO;AACT;;;ACrBO,SAAS,MAAS,UAAe,UAAqC;AAC3E,MAAI,aAAa,SAAS,CAAC;AAC3B,MAAI,MAAM;AAEV,aAAW,WAAW,UAAU;AAC9B,UAAM,QAAQ,SAAS,OAAO;AAC9B,QAAI,QAAQ,KAAK;AACf,YAAM;AACN,mBAAa;AAAA,IACf;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../src/math/index.ts","../../src/math/clamp.ts","../../src/math/random.ts","../../src/math/randomInt.ts","../../src/math/round.ts","../../src/math/sum.ts","../../src/math/range.ts"],"sourcesContent":["export { clamp } from './clamp';\nexport { random } from './random';\nexport { randomInt } from './randomInt';\nexport { round } from './round';\nexport { sum } from './sum';\nexport { range } from './range';\n","/**\n * Clamps a number within the inclusive lower and upper bounds.\n *\n * This function takes a number and two bounds, and returns the number clamped within the specified bounds.\n * If only one bound is provided, it returns the minimum of the value and the bound.\n *\n * @param {number} value - The number to clamp.\n * @param {number} minimum - The minimum bound to clamp the number.\n * @param {number} maximum - The maximum bound to clamp the number.\n * @returns {number} The clamped number within the specified bounds.\n *\n * @example\n * const result1 = clamp(10, 5); // result1 will be 5, as 10 is clamped to the bound 5\n * const result2 = clamp(10, 5, 15); // result2 will be 10, as it is within the bounds 5 and 15\n * const result3 = clamp(2, 5, 15); // result3 will be 5, as 2 is clamped to the lower bound 5\n * const result4 = clamp(20, 5, 15); // result4 will be 15, as 20 is clamped to the upper bound 15\n */\nexport function clamp(value: number, maximum: number): number;\nexport function clamp(value: number, minimum: number, maximum: number): number;\nexport function clamp(value: number, bound1: number, bound2?: number): number {\n if (bound2 == null) {\n return Math.min(value, bound1);\n }\n\n return Math.min(Math.max(value, bound1), bound2);\n}\n","/**\n * Generate a random number within the given range.\n *\n * @param {number} minimum - The lower bound (inclusive).\n * @param {number} maximum - The upper bound (exclusive).\n * @returns {number} A random number between minimum (inclusive) and maximum (exclusive). The number can be an integer or a decimal.\n * @throws {Error} Throws an error if `maximum` is not greater than `minimum`.\n *\n * @example\n * const result1 = random(0, 5); // Returns a random number between 0 and 5.\n * const result2 = random(5, 0); // If the minimum is greater than the maximum, an error is thrown\n * const result3 = random(5, 5); // If the minimum is equal to the maximum, an error is thrown.\n */\nexport function random(minimum: number, maximum: number): number {\n if (minimum >= maximum) {\n throw new Error('Invalid input: The maximum value must be greater than the minimum value.');\n }\n\n return Math.random() * (maximum - minimum) + minimum;\n}\n","import { random } from './random';\n\n/**\n * Generates a random integer between minimum (inclusive) and maximum (exclusive).\n *\n * @param {number} minimum - The lower bound (inclusive).\n * @param {number} maximum - The upper bound (exclusive).\n * @returns {number} A random integer between minimum (inclusive) and maximum (exclusive).\n * @throws {Error} Throws an error if `maximum` is not greater than `minimum`.\n *\n * @example\n * const result = randomInt(0, 5); // result will be a random integer between 0 (inclusive) and 5 (exclusive)\n * const result2 = randomInt(5, 0); // This will throw an error\n */\nexport function randomInt(minimum: number, maximum: number): number {\n return Math.floor(random(minimum, maximum));\n}\n","/**\n * Rounds a number to a specified precision.\n *\n * This function takes a number and an optional precision value, and returns the number rounded\n * to the specified number of decimal places.\n *\n * @param {number} value - The number to round.\n * @param {number} [precision=0] - The number of decimal places to round to. Defaults to 0.\n * @returns {number} The rounded number.\n * @throws {Error} Throws an error if `Precision` is not integer.\n *\n * @example\n * const result1 = round(1.2345); // result1 will be 1\n * const result2 = round(1.2345, 2); // result2 will be 1.23\n * const result3 = round(1.2345, 3); // result3 will be 1.235\n * const result4 = round(1.2345, 3.1); // This will throw an error\n */\nexport function round(value: number, precision = 0): number {\n if (!Number.isInteger(precision)) {\n throw new Error('Precision must be an integer.');\n }\n const multiplier = Math.pow(10, precision);\n return Math.round(value * multiplier) / multiplier;\n}\n","/**\n * Calculates the sum of an array of numbers.\n *\n * This function takes an array of numbers and returns the sum of all the elements in the array.\n *\n * @param {number[]} nums - An array of numbers to be summed.\n * @returns {number} The sum of all the numbers in the array.\n *\n * @example\n * const numbers = [1, 2, 3, 4, 5];\n * const result = sum(numbers);\n * // result will be 15\n */\nexport function sum(nums: number[]): number {\n let result = 0;\n\n for (const num of nums) {\n result += num;\n }\n\n return result;\n}\n","/**\n * Returns an array of numbers from `start` to `end`, incrementing by `step`.\n *\n * If `step` is not provided, it defaults to `1` for an\n * ascending range and `-1` for a descending range.\n *\n * @param {number} start - The starting number of the range.\n * @param {number} [end] - The end number of the range.\n * @param {number} [step] - The step value for the range.\n * @returns {number[]} An array of numbers from `start` to `end` with the specified `step`.\n *\n * @example\n * // Returns [0, 1, 2, 3]\n * range(4);\n *\n * @example\n * // Returns [0, 5, 10, 15]\n * range(0, 20, 5);\n *\n * @example\n * // Returns []\n * range(0, -4, -1);\n *\n * @example\n * // Throws an error: The step value must be a non-zero integer.\n * range(1, 4, 0);\n */\nexport function range(end: number): number[];\nexport function range(start: number, end: number): number[];\nexport function range(start: number, end: number, step: number): number[];\nexport function range(start: number, end?: number, step?: number): number[] {\n if (end == null) {\n end = start;\n start = 0;\n }\n\n if (step == null) {\n step = 1;\n }\n\n if (!Number.isInteger(step) || step === 0) {\n throw new Error(`The step value must be a non-zero integer.`);\n }\n\n const length = Math.max(Math.ceil((end - start) / step), 0);\n const result = new Array(length);\n\n for (let i = 0; i < length; i++) {\n result[i] = start + i * step;\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACmBO,SAAS,MAAM,OAAe,QAAgB,QAAyB;AAC5E,MAAI,UAAU,MAAM;AAClB,WAAO,KAAK,IAAI,OAAO,MAAM;AAAA,EAC/B;AAEA,SAAO,KAAK,IAAI,KAAK,IAAI,OAAO,MAAM,GAAG,MAAM;AACjD;;;ACZO,SAAS,OAAO,SAAiB,SAAyB;AAC/D,MAAI,WAAW,SAAS;AACtB,UAAM,IAAI,MAAM,0EAA0E;AAAA,EAC5F;AAEA,SAAO,KAAK,OAAO,KAAK,UAAU,WAAW;AAC/C;;;ACLO,SAAS,UAAU,SAAiB,SAAyB;AAClE,SAAO,KAAK,MAAM,OAAO,SAAS,OAAO,CAAC;AAC5C;;;ACCO,SAAS,MAAM,OAAe,YAAY,GAAW;AAC1D,MAAI,CAAC,OAAO,UAAU,SAAS,GAAG;AAChC,UAAM,IAAI,MAAM,+BAA+B;AAAA,EACjD;AACA,QAAM,aAAa,KAAK,IAAI,IAAI,SAAS;AACzC,SAAO,KAAK,MAAM,QAAQ,UAAU,IAAI;AAC1C;;;ACVO,SAAS,IAAI,MAAwB;AAC1C,MAAI,SAAS;AAEb,aAAW,OAAO,MAAM;AACtB,cAAU;AAAA,EACZ;AAEA,SAAO;AACT;;;ACSO,SAAS,MAAM,OAAe,KAAc,MAAyB;AAC1E,MAAI,OAAO,MAAM;AACf,UAAM;AACN,YAAQ;AAAA,EACV;AAEA,MAAI,QAAQ,MAAM;AAChB,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,OAAO,UAAU,IAAI,KAAK,SAAS,GAAG;AACzC,UAAM,IAAI,MAAM,4CAA4C;AAAA,EAC9D;AAEA,QAAM,SAAS,KAAK,IAAI,KAAK,MAAM,MAAM,SAAS,IAAI,GAAG,CAAC;AAC1D,QAAM,SAAS,IAAI,MAAM,MAAM;AAE/B,WAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,WAAO,CAAC,IAAI,QAAQ,IAAI;AAAA,EAC1B;AAEA,SAAO;AACT;","names":[]}
@@ -1,32 +1,28 @@
1
- import "../chunk-QVHWSUEH.mjs";
1
+ import "../chunk-7LAMAYGN.mjs";
2
+ import {
3
+ sum
4
+ } from "../chunk-7OU22ENP.mjs";
2
5
  import {
3
6
  clamp
4
7
  } from "../chunk-MLCL773E.mjs";
5
- import {
6
- maxBy
7
- } from "../chunk-6E6PBLFK.mjs";
8
- import {
9
- minBy
10
- } from "../chunk-SAL5OUAE.mjs";
11
8
  import {
12
9
  randomInt
13
10
  } from "../chunk-5UT77WON.mjs";
14
11
  import {
15
12
  random
16
13
  } from "../chunk-LCP6FOCX.mjs";
14
+ import {
15
+ range
16
+ } from "../chunk-UNLX2F2Y.mjs";
17
17
  import {
18
18
  round
19
19
  } from "../chunk-5PMKOZ2F.mjs";
20
- import {
21
- sum
22
- } from "../chunk-7OU22ENP.mjs";
23
20
  import "../chunk-2MM5EJJX.mjs";
24
21
  export {
25
22
  clamp,
26
- maxBy,
27
- minBy,
28
23
  random,
29
24
  randomInt,
25
+ range,
30
26
  round,
31
27
  sum
32
28
  };
@@ -1,8 +1,7 @@
1
1
  /**
2
- * Returns an array of numbers from `start` to `end` with the specified `step` value.
2
+ * Returns an array of numbers from `start` to `end`, incrementing by `step`.
3
3
  *
4
- * This function generates a range of numbers, starting from `start`, ending before `end`,
5
- * and incrementing by `step`. If `step` is not provided, it defaults to `1` for an
4
+ * If `step` is not provided, it defaults to `1` for an
6
5
  * ascending range and `-1` for a descending range.
7
6
  *
8
7
  * @param {number} start - The starting number of the range.
@@ -19,13 +18,15 @@
19
18
  * range(0, 20, 5);
20
19
  *
21
20
  * @example
22
- * // Returns [0, -1, -2, -3]
21
+ * // Returns []
23
22
  * range(0, -4, -1);
24
23
  *
25
24
  * @example
26
- * // Returns [1, 1, 1]
25
+ * // Throws an error: The step value must be a non-zero integer.
27
26
  * range(1, 4, 0);
28
27
  */
29
- declare function range(start: number, end?: number, step?: number): number[];
28
+ declare function range(end: number): number[];
29
+ declare function range(start: number, end: number): number[];
30
+ declare function range(start: number, end: number, step: number): number[];
30
31
 
31
32
  export { range };
@@ -1,8 +1,7 @@
1
1
  /**
2
- * Returns an array of numbers from `start` to `end` with the specified `step` value.
2
+ * Returns an array of numbers from `start` to `end`, incrementing by `step`.
3
3
  *
4
- * This function generates a range of numbers, starting from `start`, ending before `end`,
5
- * and incrementing by `step`. If `step` is not provided, it defaults to `1` for an
4
+ * If `step` is not provided, it defaults to `1` for an
6
5
  * ascending range and `-1` for a descending range.
7
6
  *
8
7
  * @param {number} start - The starting number of the range.
@@ -19,13 +18,15 @@
19
18
  * range(0, 20, 5);
20
19
  *
21
20
  * @example
22
- * // Returns [0, -1, -2, -3]
21
+ * // Returns []
23
22
  * range(0, -4, -1);
24
23
  *
25
24
  * @example
26
- * // Returns [1, 1, 1]
25
+ * // Throws an error: The step value must be a non-zero integer.
27
26
  * range(1, 4, 0);
28
27
  */
29
- declare function range(start: number, end?: number, step?: number): number[];
28
+ declare function range(end: number): number[];
29
+ declare function range(start: number, end: number): number[];
30
+ declare function range(start: number, end: number, step: number): number[];
30
31
 
31
32
  export { range };
@@ -17,21 +17,24 @@ var __copyProps = (to, from, except, desc) => {
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
- // src/array/range.ts
20
+ // src/math/range.ts
21
21
  var range_exports = {};
22
22
  __export(range_exports, {
23
23
  range: () => range
24
24
  });
25
25
  module.exports = __toCommonJS(range_exports);
26
26
  function range(start, end, step) {
27
- if (end === void 0) {
27
+ if (end == null) {
28
28
  end = start;
29
29
  start = 0;
30
30
  }
31
- if (step === void 0) {
32
- step = start <= end ? 1 : -1;
31
+ if (step == null) {
32
+ step = 1;
33
33
  }
34
- const length = Math.max(Math.ceil((end - start) / (step || 1)), 0);
34
+ if (!Number.isInteger(step) || step === 0) {
35
+ throw new Error(`The step value must be a non-zero integer.`);
36
+ }
37
+ const length = Math.max(Math.ceil((end - start) / step), 0);
35
38
  const result = new Array(length);
36
39
  for (let i = 0; i < length; i++) {
37
40
  result[i] = start + i * step;
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/math/range.ts"],"sourcesContent":["/**\n * Returns an array of numbers from `start` to `end`, incrementing by `step`.\n *\n * If `step` is not provided, it defaults to `1` for an\n * ascending range and `-1` for a descending range.\n *\n * @param {number} start - The starting number of the range.\n * @param {number} [end] - The end number of the range.\n * @param {number} [step] - The step value for the range.\n * @returns {number[]} An array of numbers from `start` to `end` with the specified `step`.\n *\n * @example\n * // Returns [0, 1, 2, 3]\n * range(4);\n *\n * @example\n * // Returns [0, 5, 10, 15]\n * range(0, 20, 5);\n *\n * @example\n * // Returns []\n * range(0, -4, -1);\n *\n * @example\n * // Throws an error: The step value must be a non-zero integer.\n * range(1, 4, 0);\n */\nexport function range(end: number): number[];\nexport function range(start: number, end: number): number[];\nexport function range(start: number, end: number, step: number): number[];\nexport function range(start: number, end?: number, step?: number): number[] {\n if (end == null) {\n end = start;\n start = 0;\n }\n\n if (step == null) {\n step = 1;\n }\n\n if (!Number.isInteger(step) || step === 0) {\n throw new Error(`The step value must be a non-zero integer.`);\n }\n\n const length = Math.max(Math.ceil((end - start) / step), 0);\n const result = new Array(length);\n\n for (let i = 0; i < length; i++) {\n result[i] = start + i * step;\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA8BO,SAAS,MAAM,OAAe,KAAc,MAAyB;AAC1E,MAAI,OAAO,MAAM;AACf,UAAM;AACN,YAAQ;AAAA,EACV;AAEA,MAAI,QAAQ,MAAM;AAChB,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,OAAO,UAAU,IAAI,KAAK,SAAS,GAAG;AACzC,UAAM,IAAI,MAAM,4CAA4C;AAAA,EAC9D;AAEA,QAAM,SAAS,KAAK,IAAI,KAAK,MAAM,MAAM,SAAS,IAAI,GAAG,CAAC;AAC1D,QAAM,SAAS,IAAI,MAAM,MAAM;AAE/B,WAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,WAAO,CAAC,IAAI,QAAQ,IAAI;AAAA,EAC1B;AAEA,SAAO;AACT;","names":[]}
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  range
3
- } from "../chunk-AJG36DJN.mjs";
3
+ } from "../chunk-UNLX2F2Y.mjs";
4
4
  import "../chunk-2MM5EJJX.mjs";
5
5
  export {
6
6
  range
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.4.0-dev.98+41a30089",
4
+ "version": "1.5.0-dev.102+8ae92e43",
5
5
  "workspaces": [
6
6
  "docs"
7
7
  ],
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/array/range.ts"],"sourcesContent":["/**\n * Returns an array of numbers from `start` to `end` with the specified `step` value.\n *\n * This function generates a range of numbers, starting from `start`, ending before `end`,\n * and incrementing by `step`. If `step` is not provided, it defaults to `1` for an\n * ascending range and `-1` for a descending range.\n *\n * @param {number} start - The starting number of the range.\n * @param {number} [end] - The end number of the range.\n * @param {number} [step] - The step value for the range.\n * @returns {number[]} An array of numbers from `start` to `end` with the specified `step`.\n *\n * @example\n * // Returns [0, 1, 2, 3]\n * range(4);\n *\n * @example\n * // Returns [0, 5, 10, 15]\n * range(0, 20, 5);\n *\n * @example\n * // Returns [0, -1, -2, -3]\n * range(0, -4, -1);\n *\n * @example\n * // Returns [1, 1, 1]\n * range(1, 4, 0);\n */\n\nexport function range(start: number, end?: number, step?: number): number[] {\n if (end === undefined) {\n end = start;\n start = 0;\n }\n\n if (step === undefined) {\n step = start <= end ? 1 : -1;\n }\n\n const length = Math.max(Math.ceil((end - start) / (step || 1)), 0);\n const result = new Array(length);\n\n for (let i = 0; i < length; i++) {\n result[i] = start + i * step;\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA6BO,SAAS,MAAM,OAAe,KAAc,MAAyB;AAC1E,MAAI,QAAQ,QAAW;AACrB,UAAM;AACN,YAAQ;AAAA,EACV;AAEA,MAAI,SAAS,QAAW;AACtB,WAAO,SAAS,MAAM,IAAI;AAAA,EAC5B;AAEA,QAAM,SAAS,KAAK,IAAI,KAAK,MAAM,MAAM,UAAU,QAAQ,EAAE,GAAG,CAAC;AACjE,QAAM,SAAS,IAAI,MAAM,MAAM;AAE/B,WAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,WAAO,CAAC,IAAI,QAAQ,IAAI;AAAA,EAC1B;AAEA,SAAO;AACT;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/math/maxBy.ts"],"sourcesContent":["/**\n * Returns the element of the specified array that has the maximum value, according to the specified selector.\n *\n * This function takes an array of elements and a selector function, and returns the element with the maximum value according to the selector.\n *\n * @param {T[]} elements - An array of elements to be compared.\n * @param {(element: T) => number} selector - A function that takes an element and returns a number.\n * @returns {T} The element with the maximum value according to the selector.\n *\n * @example\n * const people = [\n * { name: 'Mark', age: 25 },\n * { name: 'Nunu', age: 30 },\n * { name: 'Overmars', age: 20 }\n * ];\n * const result = maxBy(people, person => person.age);\n * // result will be { name: 'Nunu', age: 30 }\n */\nexport function maxBy<T>(elements: T[], selector: (element: T) => number): T {\n let maxElement = elements[0];\n let max = -Infinity;\n\n for (const element of elements) {\n const value = selector(element);\n if (value > max) {\n max = value;\n maxElement = element;\n }\n }\n\n return maxElement;\n}\n"],"mappings":";AAkBO,SAAS,MAAS,UAAe,UAAqC;AAC3E,MAAI,aAAa,SAAS,CAAC;AAC3B,MAAI,MAAM;AAEV,aAAW,WAAW,UAAU;AAC9B,UAAM,QAAQ,SAAS,OAAO;AAC9B,QAAI,QAAQ,KAAK;AACf,YAAM;AACN,mBAAa;AAAA,IACf;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
@@ -1,21 +0,0 @@
1
- // src/array/range.ts
2
- function range(start, end, step) {
3
- if (end === void 0) {
4
- end = start;
5
- start = 0;
6
- }
7
- if (step === void 0) {
8
- step = start <= end ? 1 : -1;
9
- }
10
- const length = Math.max(Math.ceil((end - start) / (step || 1)), 0);
11
- const result = new Array(length);
12
- for (let i = 0; i < length; i++) {
13
- result[i] = start + i * step;
14
- }
15
- return result;
16
- }
17
-
18
- export {
19
- range
20
- };
21
- //# sourceMappingURL=chunk-AJG36DJN.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/array/range.ts"],"sourcesContent":["/**\n * Returns an array of numbers from `start` to `end` with the specified `step` value.\n *\n * This function generates a range of numbers, starting from `start`, ending before `end`,\n * and incrementing by `step`. If `step` is not provided, it defaults to `1` for an\n * ascending range and `-1` for a descending range.\n *\n * @param {number} start - The starting number of the range.\n * @param {number} [end] - The end number of the range.\n * @param {number} [step] - The step value for the range.\n * @returns {number[]} An array of numbers from `start` to `end` with the specified `step`.\n *\n * @example\n * // Returns [0, 1, 2, 3]\n * range(4);\n *\n * @example\n * // Returns [0, 5, 10, 15]\n * range(0, 20, 5);\n *\n * @example\n * // Returns [0, -1, -2, -3]\n * range(0, -4, -1);\n *\n * @example\n * // Returns [1, 1, 1]\n * range(1, 4, 0);\n */\n\nexport function range(start: number, end?: number, step?: number): number[] {\n if (end === undefined) {\n end = start;\n start = 0;\n }\n\n if (step === undefined) {\n step = start <= end ? 1 : -1;\n }\n\n const length = Math.max(Math.ceil((end - start) / (step || 1)), 0);\n const result = new Array(length);\n\n for (let i = 0; i < length; i++) {\n result[i] = start + i * step;\n }\n\n return result;\n}\n"],"mappings":";AA6BO,SAAS,MAAM,OAAe,KAAc,MAAyB;AAC1E,MAAI,QAAQ,QAAW;AACrB,UAAM;AACN,YAAQ;AAAA,EACV;AAEA,MAAI,SAAS,QAAW;AACtB,WAAO,SAAS,MAAM,IAAI;AAAA,EAC5B;AAEA,QAAM,SAAS,KAAK,IAAI,KAAK,MAAM,MAAM,UAAU,QAAQ,EAAE,GAAG,CAAC;AACjE,QAAM,SAAS,IAAI,MAAM,MAAM;AAE/B,WAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAC/B,WAAO,CAAC,IAAI,QAAQ,IAAI;AAAA,EAC1B;AAEA,SAAO;AACT;","names":[]}
@@ -1 +0,0 @@
1
- //# sourceMappingURL=chunk-QVHWSUEH.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/math/minBy.ts"],"sourcesContent":["/**\n * Returns the minimum value in an array of numbers.\n *\n * This function takes an array of elements and a selector function, and returns the element with the minimum value according to the selector.\n *\n * If the array is empty, the function returns `undefined`.\n *\n * @param elements An array of elements.\n * @param selector A function that selects a number from an element.\n */\nexport function minBy<T>(elements: T[], selector: (element: T) => number): T {\n let minElement = elements[0];\n let min = Infinity;\n\n for (const element of elements) {\n const value = selector(element);\n if (value < min) {\n min = value;\n minElement = element;\n }\n }\n\n return minElement;\n}\n"],"mappings":";AAUO,SAAS,MAAS,UAAe,UAAqC;AAC3E,MAAI,aAAa,SAAS,CAAC;AAC3B,MAAI,MAAM;AAEV,aAAW,WAAW,UAAU;AAC9B,UAAM,QAAQ,SAAS,OAAO;AAC9B,QAAI,QAAQ,KAAK;AACf,YAAM;AACN,mBAAa;AAAA,IACf;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
@@ -1 +0,0 @@
1
- //# sourceMappingURL=chunk-SDBGIIDD.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/math/maxBy.ts"],"sourcesContent":["/**\n * Returns the element of the specified array that has the maximum value, according to the specified selector.\n *\n * This function takes an array of elements and a selector function, and returns the element with the maximum value according to the selector.\n *\n * @param {T[]} elements - An array of elements to be compared.\n * @param {(element: T) => number} selector - A function that takes an element and returns a number.\n * @returns {T} The element with the maximum value according to the selector.\n *\n * @example\n * const people = [\n * { name: 'Mark', age: 25 },\n * { name: 'Nunu', age: 30 },\n * { name: 'Overmars', age: 20 }\n * ];\n * const result = maxBy(people, person => person.age);\n * // result will be { name: 'Nunu', age: 30 }\n */\nexport function maxBy<T>(elements: T[], selector: (element: T) => number): T {\n let maxElement = elements[0];\n let max = -Infinity;\n\n for (const element of elements) {\n const value = selector(element);\n if (value > max) {\n max = value;\n maxElement = element;\n }\n }\n\n return maxElement;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBO,SAAS,MAAS,UAAe,UAAqC;AAC3E,MAAI,aAAa,SAAS,CAAC;AAC3B,MAAI,MAAM;AAEV,aAAW,WAAW,UAAU;AAC9B,UAAM,QAAQ,SAAS,OAAO;AAC9B,QAAI,QAAQ,KAAK;AACf,YAAM;AACN,mBAAa;AAAA,IACf;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/math/minBy.ts"],"sourcesContent":["/**\n * Returns the minimum value in an array of numbers.\n *\n * This function takes an array of elements and a selector function, and returns the element with the minimum value according to the selector.\n *\n * If the array is empty, the function returns `undefined`.\n *\n * @param elements An array of elements.\n * @param selector A function that selects a number from an element.\n */\nexport function minBy<T>(elements: T[], selector: (element: T) => number): T {\n let minElement = elements[0];\n let min = Infinity;\n\n for (const element of elements) {\n const value = selector(element);\n if (value < min) {\n min = value;\n minElement = element;\n }\n }\n\n return minElement;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUO,SAAS,MAAS,UAAe,UAAqC;AAC3E,MAAI,aAAa,SAAS,CAAC;AAC3B,MAAI,MAAM;AAEV,aAAW,WAAW,UAAU;AAC9B,UAAM,QAAQ,SAAS,OAAO;AAC9B,QAAI,QAAQ,KAAK;AACf,YAAM;AACN,mBAAa;AAAA,IACf;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes