es-toolkit 1.10.0-dev.204 → 1.10.0-dev.206

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.
package/dist/index.mjs CHANGED
@@ -1,5 +1,8 @@
1
1
  import "./chunk-NKLIE2OI.mjs";
2
2
  import "./chunk-QKYYOLJ6.mjs";
3
+ import {
4
+ capitalize
5
+ } from "./chunk-U56ZOM7L.mjs";
3
6
  import {
4
7
  kebabCase
5
8
  } from "./chunk-EYYZQFOP.mjs";
@@ -17,10 +20,10 @@ import {
17
20
  camelCase
18
21
  } from "./chunk-XGCYC4YA.mjs";
19
22
  import "./chunk-WVQ7YVRK.mjs";
20
- import {
21
- capitalize
22
- } from "./chunk-U56ZOM7L.mjs";
23
23
  import "./chunk-OSOLEQLZ.mjs";
24
+ import {
25
+ clone
26
+ } from "./chunk-U4JGKZ56.mjs";
24
27
  import {
25
28
  invert
26
29
  } from "./chunk-74WWMNZS.mjs";
@@ -37,6 +40,9 @@ import {
37
40
  pickBy
38
41
  } from "./chunk-6CLH5YZ2.mjs";
39
42
  import "./chunk-ASJF5D6C.mjs";
43
+ import {
44
+ isEqual
45
+ } from "./chunk-JYSV56US.mjs";
40
46
  import {
41
47
  isNil
42
48
  } from "./chunk-WMM7TZLG.mjs";
@@ -49,26 +55,7 @@ import {
49
55
  import {
50
56
  isUndefined
51
57
  } from "./chunk-VH2PV426.mjs";
52
- import {
53
- isEqual
54
- } from "./chunk-JYSV56US.mjs";
55
- import {
56
- clone
57
- } from "./chunk-U4JGKZ56.mjs";
58
- import "./chunk-236H5JZJ.mjs";
59
- import {
60
- negate
61
- } from "./chunk-BOOQBJN2.mjs";
62
- import {
63
- noop
64
- } from "./chunk-LKXCOLNY.mjs";
65
- import {
66
- once
67
- } from "./chunk-CMBC4MXG.mjs";
68
- import {
69
- throttle
70
- } from "./chunk-QOJGMVVR.mjs";
71
- import "./chunk-TLVQSNOE.mjs";
58
+ import "./chunk-FUS75PJ7.mjs";
72
59
  import {
73
60
  meanBy
74
61
  } from "./chunk-IZAD2S7R.mjs";
@@ -81,6 +68,9 @@ import {
81
68
  import {
82
69
  round
83
70
  } from "./chunk-5PMKOZ2F.mjs";
71
+ import {
72
+ sumBy
73
+ } from "./chunk-5L4VWRFX.mjs";
84
74
  import {
85
75
  sum
86
76
  } from "./chunk-2SLTL4KL.mjs";
@@ -90,16 +80,7 @@ import {
90
80
  import {
91
81
  inRange
92
82
  } from "./chunk-BH6HLGBA.mjs";
93
- import {
94
- debounce
95
- } from "./chunk-3IP4JVLL.mjs";
96
83
  import "./chunk-2MRZANXZ.mjs";
97
- import {
98
- without
99
- } from "./chunk-PYEYV7BY.mjs";
100
- import {
101
- xor
102
- } from "./chunk-L7FQX5MN.mjs";
103
84
  import {
104
85
  xorBy
105
86
  } from "./chunk-Q47NGNXT.mjs";
@@ -115,18 +96,9 @@ import {
115
96
  import {
116
97
  zipWith
117
98
  } from "./chunk-4KYXHZ4R.mjs";
118
- import {
119
- union
120
- } from "./chunk-R6QA7PLZ.mjs";
121
- import {
122
- unionBy
123
- } from "./chunk-NFS7RWWE.mjs";
124
99
  import {
125
100
  unionWith
126
101
  } from "./chunk-2ZCITDMK.mjs";
127
- import {
128
- uniq
129
- } from "./chunk-DVK2N4KC.mjs";
130
102
  import {
131
103
  uniqBy
132
104
  } from "./chunk-OXAZEEGP.mjs";
@@ -140,17 +112,11 @@ import {
140
112
  unzipWith
141
113
  } from "./chunk-IFNXROZR.mjs";
142
114
  import {
143
- sampleSize
144
- } from "./chunk-EHSKB6DF.mjs";
145
- import {
146
- randomInt
147
- } from "./chunk-GIWAOOFY.mjs";
148
- import {
149
- random
150
- } from "./chunk-SYWMYQYJ.mjs";
115
+ without
116
+ } from "./chunk-PYEYV7BY.mjs";
151
117
  import {
152
- shuffle
153
- } from "./chunk-G7UQ5PUR.mjs";
118
+ xor
119
+ } from "./chunk-L7FQX5MN.mjs";
154
120
  import {
155
121
  tail
156
122
  } from "./chunk-OF6NM4G7.mjs";
@@ -170,11 +136,14 @@ import {
170
136
  toFilled
171
137
  } from "./chunk-OP4OAKQJ.mjs";
172
138
  import {
173
- intersectionWith
174
- } from "./chunk-XI7KRD2C.mjs";
139
+ union
140
+ } from "./chunk-R6QA7PLZ.mjs";
175
141
  import {
176
- keyBy
177
- } from "./chunk-AJKZMN2V.mjs";
142
+ uniq
143
+ } from "./chunk-DVK2N4KC.mjs";
144
+ import {
145
+ unionBy
146
+ } from "./chunk-NFS7RWWE.mjs";
178
147
  import {
179
148
  last
180
149
  } from "./chunk-KPBQ7OUY.mjs";
@@ -194,11 +163,17 @@ import {
194
163
  sample
195
164
  } from "./chunk-2KNLZB7V.mjs";
196
165
  import {
197
- flattenDeep
198
- } from "./chunk-ML727RRJ.mjs";
166
+ sampleSize
167
+ } from "./chunk-EHSKB6DF.mjs";
199
168
  import {
200
- forEachRight
201
- } from "./chunk-IEA53J7P.mjs";
169
+ randomInt
170
+ } from "./chunk-GIWAOOFY.mjs";
171
+ import {
172
+ random
173
+ } from "./chunk-SYWMYQYJ.mjs";
174
+ import {
175
+ shuffle
176
+ } from "./chunk-G7UQ5PUR.mjs";
202
177
  import {
203
178
  groupBy
204
179
  } from "./chunk-E6E4RZLN.mjs";
@@ -215,11 +190,11 @@ import {
215
190
  intersectionBy
216
191
  } from "./chunk-O6UXZR3U.mjs";
217
192
  import {
218
- differenceBy
219
- } from "./chunk-LVWX5B43.mjs";
193
+ intersectionWith
194
+ } from "./chunk-XI7KRD2C.mjs";
220
195
  import {
221
- differenceWith
222
- } from "./chunk-6NVQAHXP.mjs";
196
+ keyBy
197
+ } from "./chunk-AJKZMN2V.mjs";
223
198
  import {
224
199
  drop
225
200
  } from "./chunk-S3CORED5.mjs";
@@ -235,13 +210,15 @@ import {
235
210
  import {
236
211
  fill
237
212
  } from "./chunk-C4WE4GLC.mjs";
213
+ import {
214
+ flattenDeep
215
+ } from "./chunk-ML727RRJ.mjs";
238
216
  import {
239
217
  flatten
240
218
  } from "./chunk-33MSBDC6.mjs";
241
- import "./chunk-FZEZH22H.mjs";
242
219
  import {
243
- AbortError
244
- } from "./chunk-DXB7EZUQ.mjs";
220
+ forEachRight
221
+ } from "./chunk-IEA53J7P.mjs";
245
222
  import {
246
223
  chunk
247
224
  } from "./chunk-E6C5EZQA.mjs";
@@ -254,6 +231,32 @@ import {
254
231
  import {
255
232
  difference
256
233
  } from "./chunk-JPUKH67K.mjs";
234
+ import {
235
+ differenceBy
236
+ } from "./chunk-LVWX5B43.mjs";
237
+ import {
238
+ differenceWith
239
+ } from "./chunk-6NVQAHXP.mjs";
240
+ import "./chunk-FZEZH22H.mjs";
241
+ import {
242
+ AbortError
243
+ } from "./chunk-DXB7EZUQ.mjs";
244
+ import "./chunk-236H5JZJ.mjs";
245
+ import {
246
+ once
247
+ } from "./chunk-CMBC4MXG.mjs";
248
+ import {
249
+ throttle
250
+ } from "./chunk-QOJGMVVR.mjs";
251
+ import {
252
+ debounce
253
+ } from "./chunk-3IP4JVLL.mjs";
254
+ import {
255
+ negate
256
+ } from "./chunk-BOOQBJN2.mjs";
257
+ import {
258
+ noop
259
+ } from "./chunk-LKXCOLNY.mjs";
257
260
  import "./chunk-2HIMLZE3.mjs";
258
261
  export {
259
262
  AbortError,
@@ -316,6 +319,7 @@ export {
316
319
  shuffle,
317
320
  snakeCase,
318
321
  sum,
322
+ sumBy,
319
323
  tail,
320
324
  take,
321
325
  takeRight,
@@ -6,4 +6,5 @@ export { random } from './random.mjs';
6
6
  export { randomInt } from './randomInt.mjs';
7
7
  export { round } from './round.mjs';
8
8
  export { sum } from './sum.mjs';
9
+ export { sumBy } from './sumBy.mjs';
9
10
  export { range } from './range.mjs';
@@ -6,4 +6,5 @@ export { random } from './random.js';
6
6
  export { randomInt } from './randomInt.js';
7
7
  export { round } from './round.js';
8
8
  export { sum } from './sum.js';
9
+ export { sumBy } from './sumBy.js';
9
10
  export { range } from './range.js';
@@ -28,7 +28,8 @@ __export(math_exports, {
28
28
  randomInt: () => randomInt,
29
29
  range: () => range,
30
30
  round: () => round,
31
- sum: () => sum
31
+ sum: () => sum,
32
+ sumBy: () => sumBy
32
33
  });
33
34
  module.exports = __toCommonJS(math_exports);
34
35
 
@@ -98,6 +99,12 @@ function round(value, precision = 0) {
98
99
  return Math.round(value * multiplier) / multiplier;
99
100
  }
100
101
 
102
+ // src/math/sumBy.ts
103
+ function sumBy(items, getValue) {
104
+ const nums = items.map((x) => getValue(x));
105
+ return sum(nums);
106
+ }
107
+
101
108
  // src/math/range.ts
102
109
  function range(start, end, step) {
103
110
  if (end == null) {
@@ -127,6 +134,7 @@ function range(start, end, step) {
127
134
  randomInt,
128
135
  range,
129
136
  round,
130
- sum
137
+ sum,
138
+ sumBy
131
139
  });
132
140
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/math/index.ts","../../src/math/clamp.ts","../../src/math/inRange.ts","../../src/math/sum.ts","../../src/math/mean.ts","../../src/math/meanBy.ts","../../src/math/random.ts","../../src/math/randomInt.ts","../../src/math/round.ts","../../src/math/range.ts"],"sourcesContent":["export { clamp } from './clamp.ts';\nexport { inRange } from './inRange.ts';\nexport { mean } from './mean.ts';\nexport { meanBy } from './meanBy.ts';\nexport { random } from './random.ts';\nexport { randomInt } from './randomInt.ts';\nexport { round } from './round.ts';\nexport { sum } from './sum.ts';\nexport { range } from './range.ts';\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","export function inRange(value: number, maximum: number): boolean;\nexport function inRange(value: number, minimum: number, maximum: number): boolean;\n/**\n * Checks if the value is within a specified range.\n *\n * @param {number} value The value to check.\n * @param {number} minimum The lower bound of the range (inclusive).\n * @param {number} maximum The upper bound of the range (exclusive).\n * @returns {boolean} `true` if the value is within the specified range, otherwise `false`.\n * @throws {Error} Throws an error if the `minimum` is greater or equal than the `maximum`.\n *\n * @example\n * const result1 = inRange(3, 5); // result1 will be true.\n * const result2 = inRange(1, 2, 5); // result2 will be false.\n * const result3 = inRange(1, 5, 2); // If the minimum is greater or equal than the maximum, an error is thrown.\n */\nexport function inRange(value: number, minimum: number, maximum?: number): boolean {\n if (maximum == null) {\n maximum = minimum;\n minimum = 0;\n }\n\n if (minimum >= maximum) {\n throw new Error('The maximum value must be greater than the minimum value.');\n }\n\n return minimum <= value && value < maximum;\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: readonly number[]): number {\n let result = 0;\n\n for (let i = 0; i < nums.length; i++) {\n result += nums[i];\n }\n\n return result;\n}\n","import { sum } from './sum.ts';\n\n/**\n * Calculates the average of an array of numbers.\n *\n * If the array is empty, this function returns `NaN`.\n *\n * @param {number[]} nums - An array of numbers to calculate the average.\n * @returns {number} The average of all the numbers in the array.\n *\n * @example\n * const numbers = [1, 2, 3, 4, 5];\n * const result = average(numbers);\n * // result will be 3\n */\nexport function mean(nums: readonly number[]): number {\n return sum(nums) / nums.length;\n}\n","import { mean } from './mean.ts';\n\n/**\n * Calculates the average of an array of numbers when applying\n * the `getValue` function to each element.\n *\n * If the array is empty, this function returns `NaN`.\n *\n * @template T - The type of elements in the array.\n * @param {T[]} items An array to calculate the average.\n * @param {(element: T) => number} getValue A function that selects a numeric value from each element.\n * @returns {number} The average of all the numbers as determined by the `getValue` function.\n *\n * @example\n * meanBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: 2\n * meanBy([], x => x.a); // Returns: NaN\n */\nexport function meanBy<T>(items: readonly T[], getValue: (element: T) => number): number {\n const nums = items.map(x => getValue(x));\n\n return mean(nums);\n}\n","/**\n * Generate a random number within the given range.\n *\n * If only one argument is provided, a number between `0` and the given number is returned.\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(maximum: number): number;\nexport function random(minimum: number, maximum: number): number;\nexport function random(minimum: number, maximum?: number): number {\n if (maximum == null) {\n maximum = minimum;\n minimum = 0;\n }\n\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.ts';\n\n/**\n * Generates a random integer between minimum (inclusive) and maximum (exclusive).\n *\n * If only one argument is provided, a number between `0` and the given number is returned.\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(maximum: number): number;\nexport function randomInt(minimum: number, maximum: number): number;\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 * Returns an array of numbers from `start` to `end`, incrementing by `step`.\n *\n * If `step` is not provided, it defaults to `1`.\n *\n * @param {number} start - The starting number of the range (inclusive).\n * @param {number} [end] - The end number of the range (exclusive).\n * @param {number} [step] - The step value for the range. (default: 1)\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 * // 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;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;;;ACTO,SAAS,QAAQ,OAAe,SAAiB,SAA2B;AACjF,MAAI,WAAW,MAAM;AACnB,cAAU;AACV,cAAU;AAAA,EACZ;AAEA,MAAI,WAAW,SAAS;AACtB,UAAM,IAAI,MAAM,2DAA2D;AAAA,EAC7E;AAEA,SAAO,WAAW,SAAS,QAAQ;AACrC;;;ACdO,SAAS,IAAI,MAAiC;AACnD,MAAI,SAAS;AAEb,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,cAAU,KAAK,CAAC;AAAA,EAClB;AAEA,SAAO;AACT;;;ACNO,SAAS,KAAK,MAAiC;AACpD,SAAO,IAAI,IAAI,IAAI,KAAK;AAC1B;;;ACAO,SAAS,OAAU,OAAqB,UAA0C;AACvF,QAAM,OAAO,MAAM,IAAI,OAAK,SAAS,CAAC,CAAC;AAEvC,SAAO,KAAK,IAAI;AAClB;;;ACJO,SAAS,OAAO,SAAiB,SAA0B;AAChE,MAAI,WAAW,MAAM;AACnB,cAAU;AACV,cAAU;AAAA,EACZ;AAEA,MAAI,WAAW,SAAS;AACtB,UAAM,IAAI,MAAM,0EAA0E;AAAA,EAC5F;AAEA,SAAO,KAAK,OAAO,KAAK,UAAU,WAAW;AAC/C;;;ACVO,SAAS,UAAU,SAAiB,SAA0B;AACnE,SAAO,KAAK,MAAM,OAAO,SAAS,OAAQ,CAAC;AAC7C;;;ACHO,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;;;ACMO,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
+ {"version":3,"sources":["../../src/math/index.ts","../../src/math/clamp.ts","../../src/math/inRange.ts","../../src/math/sum.ts","../../src/math/mean.ts","../../src/math/meanBy.ts","../../src/math/random.ts","../../src/math/randomInt.ts","../../src/math/round.ts","../../src/math/sumBy.ts","../../src/math/range.ts"],"sourcesContent":["export { clamp } from './clamp.ts';\nexport { inRange } from './inRange.ts';\nexport { mean } from './mean.ts';\nexport { meanBy } from './meanBy.ts';\nexport { random } from './random.ts';\nexport { randomInt } from './randomInt.ts';\nexport { round } from './round.ts';\nexport { sum } from './sum.ts';\nexport { sumBy } from './sumBy.ts';\nexport { range } from './range.ts';\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","export function inRange(value: number, maximum: number): boolean;\nexport function inRange(value: number, minimum: number, maximum: number): boolean;\n/**\n * Checks if the value is within a specified range.\n *\n * @param {number} value The value to check.\n * @param {number} minimum The lower bound of the range (inclusive).\n * @param {number} maximum The upper bound of the range (exclusive).\n * @returns {boolean} `true` if the value is within the specified range, otherwise `false`.\n * @throws {Error} Throws an error if the `minimum` is greater or equal than the `maximum`.\n *\n * @example\n * const result1 = inRange(3, 5); // result1 will be true.\n * const result2 = inRange(1, 2, 5); // result2 will be false.\n * const result3 = inRange(1, 5, 2); // If the minimum is greater or equal than the maximum, an error is thrown.\n */\nexport function inRange(value: number, minimum: number, maximum?: number): boolean {\n if (maximum == null) {\n maximum = minimum;\n minimum = 0;\n }\n\n if (minimum >= maximum) {\n throw new Error('The maximum value must be greater than the minimum value.');\n }\n\n return minimum <= value && value < maximum;\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: readonly number[]): number {\n let result = 0;\n\n for (let i = 0; i < nums.length; i++) {\n result += nums[i];\n }\n\n return result;\n}\n","import { sum } from './sum.ts';\n\n/**\n * Calculates the average of an array of numbers.\n *\n * If the array is empty, this function returns `NaN`.\n *\n * @param {number[]} nums - An array of numbers to calculate the average.\n * @returns {number} The average of all the numbers in the array.\n *\n * @example\n * const numbers = [1, 2, 3, 4, 5];\n * const result = average(numbers);\n * // result will be 3\n */\nexport function mean(nums: readonly number[]): number {\n return sum(nums) / nums.length;\n}\n","import { mean } from './mean.ts';\n\n/**\n * Calculates the average of an array of numbers when applying\n * the `getValue` function to each element.\n *\n * If the array is empty, this function returns `NaN`.\n *\n * @template T - The type of elements in the array.\n * @param {T[]} items An array to calculate the average.\n * @param {(element: T) => number} getValue A function that selects a numeric value from each element.\n * @returns {number} The average of all the numbers as determined by the `getValue` function.\n *\n * @example\n * meanBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: 2\n * meanBy([], x => x.a); // Returns: NaN\n */\nexport function meanBy<T>(items: readonly T[], getValue: (element: T) => number): number {\n const nums = items.map(x => getValue(x));\n\n return mean(nums);\n}\n","/**\n * Generate a random number within the given range.\n *\n * If only one argument is provided, a number between `0` and the given number is returned.\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(maximum: number): number;\nexport function random(minimum: number, maximum: number): number;\nexport function random(minimum: number, maximum?: number): number {\n if (maximum == null) {\n maximum = minimum;\n minimum = 0;\n }\n\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.ts';\n\n/**\n * Generates a random integer between minimum (inclusive) and maximum (exclusive).\n *\n * If only one argument is provided, a number between `0` and the given number is returned.\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(maximum: number): number;\nexport function randomInt(minimum: number, maximum: number): number;\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","import { sum } from './sum.ts';\n\n/**\n * Calculates the sum of an array of numbers when applying\n * the `getValue` function to each element.\n *\n * If the array is empty, this function returns `0`.\n *\n * @template T - The type of elements in the array.\n * @param {T[]} items An array to calculate the average.\n * @param {(element: T) => number} getValue A function that selects a numeric value from each element.\n * @returns {number} The average of all the numbers as determined by the `getValue` function.\n *\n * @example\n * sumBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: 6\n * sumBy([], x => x.a); // Returns: 0\n */\nexport function sumBy<T>(items: readonly T[], getValue: (element: T) => number): number {\n const nums = items.map(x => getValue(x));\n\n return sum(nums);\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`.\n *\n * @param {number} start - The starting number of the range (inclusive).\n * @param {number} [end] - The end number of the range (exclusive).\n * @param {number} [step] - The step value for the range. (default: 1)\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 * // 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;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;;;ACTO,SAAS,QAAQ,OAAe,SAAiB,SAA2B;AACjF,MAAI,WAAW,MAAM;AACnB,cAAU;AACV,cAAU;AAAA,EACZ;AAEA,MAAI,WAAW,SAAS;AACtB,UAAM,IAAI,MAAM,2DAA2D;AAAA,EAC7E;AAEA,SAAO,WAAW,SAAS,QAAQ;AACrC;;;ACdO,SAAS,IAAI,MAAiC;AACnD,MAAI,SAAS;AAEb,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,cAAU,KAAK,CAAC;AAAA,EAClB;AAEA,SAAO;AACT;;;ACNO,SAAS,KAAK,MAAiC;AACpD,SAAO,IAAI,IAAI,IAAI,KAAK;AAC1B;;;ACAO,SAAS,OAAU,OAAqB,UAA0C;AACvF,QAAM,OAAO,MAAM,IAAI,OAAK,SAAS,CAAC,CAAC;AAEvC,SAAO,KAAK,IAAI;AAClB;;;ACJO,SAAS,OAAO,SAAiB,SAA0B;AAChE,MAAI,WAAW,MAAM;AACnB,cAAU;AACV,cAAU;AAAA,EACZ;AAEA,MAAI,WAAW,SAAS;AACtB,UAAM,IAAI,MAAM,0EAA0E;AAAA,EAC5F;AAEA,SAAO,KAAK,OAAO,KAAK,UAAU,WAAW;AAC/C;;;ACVO,SAAS,UAAU,SAAiB,SAA0B;AACnE,SAAO,KAAK,MAAM,OAAO,SAAS,OAAQ,CAAC;AAC7C;;;ACHO,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;;;ACNO,SAAS,MAAS,OAAqB,UAA0C;AACtF,QAAM,OAAO,MAAM,IAAI,OAAK,SAAS,CAAC,CAAC;AAEvC,SAAO,IAAI,IAAI;AACjB;;;ACQO,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,4 +1,4 @@
1
- import "../chunk-TLVQSNOE.mjs";
1
+ import "../chunk-FUS75PJ7.mjs";
2
2
  import {
3
3
  meanBy
4
4
  } from "../chunk-IZAD2S7R.mjs";
@@ -11,6 +11,9 @@ import {
11
11
  import {
12
12
  round
13
13
  } from "../chunk-5PMKOZ2F.mjs";
14
+ import {
15
+ sumBy
16
+ } from "../chunk-5L4VWRFX.mjs";
14
17
  import {
15
18
  sum
16
19
  } from "../chunk-2SLTL4KL.mjs";
@@ -36,6 +39,7 @@ export {
36
39
  randomInt,
37
40
  range,
38
41
  round,
39
- sum
42
+ sum,
43
+ sumBy
40
44
  };
41
45
  //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Calculates the sum of an array of numbers when applying
3
+ * the `getValue` function to each element.
4
+ *
5
+ * If the array is empty, this function returns `0`.
6
+ *
7
+ * @template T - The type of elements in the array.
8
+ * @param {T[]} items An array to calculate the average.
9
+ * @param {(element: T) => number} getValue A function that selects a numeric value from each element.
10
+ * @returns {number} The average of all the numbers as determined by the `getValue` function.
11
+ *
12
+ * @example
13
+ * sumBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: 6
14
+ * sumBy([], x => x.a); // Returns: 0
15
+ */
16
+ declare function sumBy<T>(items: readonly T[], getValue: (element: T) => number): number;
17
+
18
+ export { sumBy };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Calculates the sum of an array of numbers when applying
3
+ * the `getValue` function to each element.
4
+ *
5
+ * If the array is empty, this function returns `0`.
6
+ *
7
+ * @template T - The type of elements in the array.
8
+ * @param {T[]} items An array to calculate the average.
9
+ * @param {(element: T) => number} getValue A function that selects a numeric value from each element.
10
+ * @returns {number} The average of all the numbers as determined by the `getValue` function.
11
+ *
12
+ * @example
13
+ * sumBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: 6
14
+ * sumBy([], x => x.a); // Returns: 0
15
+ */
16
+ declare function sumBy<T>(items: readonly T[], getValue: (element: T) => number): number;
17
+
18
+ export { sumBy };
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/math/sumBy.ts
21
+ var sumBy_exports = {};
22
+ __export(sumBy_exports, {
23
+ sumBy: () => sumBy
24
+ });
25
+ module.exports = __toCommonJS(sumBy_exports);
26
+
27
+ // src/math/sum.ts
28
+ function sum(nums) {
29
+ let result = 0;
30
+ for (let i = 0; i < nums.length; i++) {
31
+ result += nums[i];
32
+ }
33
+ return result;
34
+ }
35
+
36
+ // src/math/sumBy.ts
37
+ function sumBy(items, getValue) {
38
+ const nums = items.map((x) => getValue(x));
39
+ return sum(nums);
40
+ }
41
+ // Annotate the CommonJS export names for ESM import in node:
42
+ 0 && (module.exports = {
43
+ sumBy
44
+ });
45
+ //# sourceMappingURL=sumBy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/math/sumBy.ts","../../src/math/sum.ts"],"sourcesContent":["import { sum } from './sum.ts';\n\n/**\n * Calculates the sum of an array of numbers when applying\n * the `getValue` function to each element.\n *\n * If the array is empty, this function returns `0`.\n *\n * @template T - The type of elements in the array.\n * @param {T[]} items An array to calculate the average.\n * @param {(element: T) => number} getValue A function that selects a numeric value from each element.\n * @returns {number} The average of all the numbers as determined by the `getValue` function.\n *\n * @example\n * sumBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: 6\n * sumBy([], x => x.a); // Returns: 0\n */\nexport function sumBy<T>(items: readonly T[], getValue: (element: T) => number): number {\n const nums = items.map(x => getValue(x));\n\n return sum(nums);\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: readonly number[]): number {\n let result = 0;\n\n for (let i = 0; i < nums.length; i++) {\n result += nums[i];\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACaO,SAAS,IAAI,MAAiC;AACnD,MAAI,SAAS;AAEb,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,cAAU,KAAK,CAAC;AAAA,EAClB;AAEA,SAAO;AACT;;;ADJO,SAAS,MAAS,OAAqB,UAA0C;AACtF,QAAM,OAAO,MAAM,IAAI,OAAK,SAAS,CAAC,CAAC;AAEvC,SAAO,IAAI,IAAI;AACjB;","names":[]}
@@ -0,0 +1,9 @@
1
+ import {
2
+ sumBy
3
+ } from "../chunk-5L4VWRFX.mjs";
4
+ import "../chunk-2SLTL4KL.mjs";
5
+ import "../chunk-2HIMLZE3.mjs";
6
+ export {
7
+ sumBy
8
+ };
9
+ //# sourceMappingURL=sumBy.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,4 +1,7 @@
1
1
  import "../chunk-OSOLEQLZ.mjs";
2
+ import {
3
+ clone
4
+ } from "../chunk-U4JGKZ56.mjs";
2
5
  import {
3
6
  invert
4
7
  } from "../chunk-74WWMNZS.mjs";
@@ -14,9 +17,6 @@ import {
14
17
  import {
15
18
  pickBy
16
19
  } from "../chunk-6CLH5YZ2.mjs";
17
- import {
18
- clone
19
- } from "../chunk-U4JGKZ56.mjs";
20
20
  import "../chunk-2HIMLZE3.mjs";
21
21
  export {
22
22
  clone,
@@ -1,4 +1,7 @@
1
1
  import "../chunk-ASJF5D6C.mjs";
2
+ import {
3
+ isEqual
4
+ } from "../chunk-JYSV56US.mjs";
2
5
  import {
3
6
  isNil
4
7
  } from "../chunk-WMM7TZLG.mjs";
@@ -11,9 +14,6 @@ import {
11
14
  import {
12
15
  isUndefined
13
16
  } from "../chunk-VH2PV426.mjs";
14
- import {
15
- isEqual
16
- } from "../chunk-JYSV56US.mjs";
17
17
  import "../chunk-R6QA7PLZ.mjs";
18
18
  import "../chunk-DVK2N4KC.mjs";
19
19
  import "../chunk-2HIMLZE3.mjs";
@@ -1,4 +1,7 @@
1
1
  import "../chunk-QKYYOLJ6.mjs";
2
+ import {
3
+ capitalize
4
+ } from "../chunk-U56ZOM7L.mjs";
2
5
  import {
3
6
  kebabCase
4
7
  } from "../chunk-EYYZQFOP.mjs";
@@ -12,9 +15,6 @@ import {
12
15
  camelCase
13
16
  } from "../chunk-XGCYC4YA.mjs";
14
17
  import "../chunk-WVQ7YVRK.mjs";
15
- import {
16
- capitalize
17
- } from "../chunk-U56ZOM7L.mjs";
18
18
  import "../chunk-2HIMLZE3.mjs";
19
19
  export {
20
20
  camelCase,
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.10.0-dev.204+acb74650",
4
+ "version": "1.10.0-dev.206+3441d7d8",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {
@@ -1 +0,0 @@
1
- //# sourceMappingURL=chunk-TLVQSNOE.mjs.map