es-toolkit 1.7.1 → 1.8.0

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 (55) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/array/index.d.mts +1 -0
  3. package/dist/array/index.d.ts +1 -0
  4. package/dist/array/index.js +27 -0
  5. package/dist/array/index.js.map +1 -1
  6. package/dist/array/index.mjs +13 -9
  7. package/dist/array/orderBy.d.mts +35 -0
  8. package/dist/array/orderBy.d.ts +35 -0
  9. package/dist/array/orderBy.js +53 -0
  10. package/dist/array/orderBy.js.map +1 -0
  11. package/dist/array/orderBy.mjs +8 -0
  12. package/dist/chunk-7B7W36UP.mjs +29 -0
  13. package/dist/chunk-7B7W36UP.mjs.map +1 -0
  14. package/dist/chunk-BH6HLGBA.mjs +16 -0
  15. package/dist/chunk-BH6HLGBA.mjs.map +1 -0
  16. package/dist/chunk-FLTT2MGF.mjs +14 -0
  17. package/dist/chunk-FLTT2MGF.mjs.map +1 -0
  18. package/dist/chunk-OC7ZNKWX.mjs +1 -0
  19. package/dist/chunk-PMHULJNL.mjs +1 -0
  20. package/dist/chunk-TLVQSNOE.mjs +1 -0
  21. package/dist/chunk-TLVQSNOE.mjs.map +1 -0
  22. package/dist/index.d.mts +3 -0
  23. package/dist/index.d.ts +3 -0
  24. package/dist/index.js +53 -0
  25. package/dist/index.js.map +1 -1
  26. package/dist/index.mjs +30 -18
  27. package/dist/math/inRange.d.mts +4 -0
  28. package/dist/math/inRange.d.ts +4 -0
  29. package/dist/math/inRange.js +40 -0
  30. package/dist/math/inRange.js.map +1 -0
  31. package/dist/math/inRange.mjs +8 -0
  32. package/dist/math/inRange.mjs.map +1 -0
  33. package/dist/math/index.d.mts +1 -0
  34. package/dist/math/index.d.ts +1 -0
  35. package/dist/math/index.js +14 -0
  36. package/dist/math/index.js.map +1 -1
  37. package/dist/math/index.mjs +5 -1
  38. package/dist/object/index.d.mts +1 -0
  39. package/dist/object/index.d.ts +1 -0
  40. package/dist/object/index.js +12 -0
  41. package/dist/object/index.js.map +1 -1
  42. package/dist/object/index.mjs +8 -4
  43. package/dist/object/invert.d.mts +24 -0
  44. package/dist/object/invert.d.ts +24 -0
  45. package/dist/object/invert.js +38 -0
  46. package/dist/object/invert.js.map +1 -0
  47. package/dist/object/invert.mjs +8 -0
  48. package/dist/object/invert.mjs.map +1 -0
  49. package/package.json +1 -1
  50. package/dist/chunk-T5JCRGXA.mjs +0 -1
  51. package/dist/chunk-TZE6EDCX.mjs +0 -1
  52. package/dist/chunk-XJR5WV2Z.mjs +0 -1
  53. /package/dist/{chunk-T5JCRGXA.mjs.map → array/orderBy.mjs.map} +0 -0
  54. /package/dist/{chunk-TZE6EDCX.mjs.map → chunk-OC7ZNKWX.mjs.map} +0 -0
  55. /package/dist/{chunk-XJR5WV2Z.mjs.map → chunk-PMHULJNL.mjs.map} +0 -0
@@ -0,0 +1,29 @@
1
+ // src/array/orderBy.ts
2
+ function orderBy(collection, keys, orders) {
3
+ const compareValues = (a, b, order) => {
4
+ if (a < b) {
5
+ return order === "asc" ? -1 : 1;
6
+ }
7
+ if (a > b) {
8
+ return order === "asc" ? 1 : -1;
9
+ }
10
+ return 0;
11
+ };
12
+ const effectiveOrders = keys.map((_, index) => orders[index] || orders[orders.length - 1]);
13
+ return collection.slice().sort((a, b) => {
14
+ for (let i = 0; i < keys.length; i++) {
15
+ const key = keys[i];
16
+ const order = effectiveOrders[i];
17
+ const result = compareValues(a[key], b[key], order);
18
+ if (result !== 0) {
19
+ return result;
20
+ }
21
+ }
22
+ return 0;
23
+ });
24
+ }
25
+
26
+ export {
27
+ orderBy
28
+ };
29
+ //# sourceMappingURL=chunk-7B7W36UP.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/array/orderBy.ts"],"sourcesContent":["type Order = 'asc' | 'desc';\n\n/**\n * Sorts an array of objects based on multiple properties and their corresponding order directions.\n *\n * This function takes an array of objects, an array of keys to sort by, and an array of order directions.\n * It returns the sorted array, ordering by each key according to its corresponding direction\n * ('asc' for ascending or 'desc' for descending). If values for a key are equal,\n * it moves to the next key to determine the order.\n *\n * @template T - The type of elements in the array.\n * @param {T[]} collection - The array of objects to be sorted.\n * @param {Array<keyof T>} keys - An array of keys (properties) by which to sort.\n * @param {Order[]} orders - An array of order directions ('asc' for ascending or 'desc' for descending).\n * @returns {T[]} - The sorted array.\n *\n * @example\n * // Sort an array of objects by 'user' in ascending order and 'age' in descending order.\n * const users = [\n * { user: 'fred', age: 48 },\n * { user: 'barney', age: 34 },\n * { user: 'fred', age: 40 },\n * { user: 'barney', age: 36 },\n * ];\n * const result = orderBy(users, ['user', 'age'], ['asc', 'desc']);\n * // result will be:\n * // [\n * // { user: 'barney', age: 36 },\n * // { user: 'barney', age: 34 },\n * // { user: 'fred', age: 48 },\n * // { user: 'fred', age: 40 },\n * // ]\n */\nexport function orderBy<T>(collection: T[], keys: Array<keyof T>, orders: Order[]): T[] {\n const compareValues = (a: T[keyof T], b: T[keyof T], order: Order) => {\n if (a < b) {\n return order === 'asc' ? -1 : 1;\n }\n if (a > b) {\n return order === 'asc' ? 1 : -1;\n }\n return 0;\n };\n\n const effectiveOrders = keys.map((_, index) => orders[index] || orders[orders.length - 1]);\n\n return collection.slice().sort((a, b) => {\n for (let i = 0; i < keys.length; i++) {\n const key = keys[i];\n const order = effectiveOrders[i];\n const result = compareValues(a[key], b[key], order);\n if (result !== 0) {\n return result;\n }\n }\n return 0;\n });\n}\n"],"mappings":";AAiCO,SAAS,QAAW,YAAiB,MAAsB,QAAsB;AACtF,QAAM,gBAAgB,CAAC,GAAe,GAAe,UAAiB;AACpE,QAAI,IAAI,GAAG;AACT,aAAO,UAAU,QAAQ,KAAK;AAAA,IAChC;AACA,QAAI,IAAI,GAAG;AACT,aAAO,UAAU,QAAQ,IAAI;AAAA,IAC/B;AACA,WAAO;AAAA,EACT;AAEA,QAAM,kBAAkB,KAAK,IAAI,CAAC,GAAG,UAAU,OAAO,KAAK,KAAK,OAAO,OAAO,SAAS,CAAC,CAAC;AAEzF,SAAO,WAAW,MAAM,EAAE,KAAK,CAAC,GAAG,MAAM;AACvC,aAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,YAAM,MAAM,KAAK,CAAC;AAClB,YAAM,QAAQ,gBAAgB,CAAC;AAC/B,YAAM,SAAS,cAAc,EAAE,GAAG,GAAG,EAAE,GAAG,GAAG,KAAK;AAClD,UAAI,WAAW,GAAG;AAChB,eAAO;AAAA,MACT;AAAA,IACF;AACA,WAAO;AAAA,EACT,CAAC;AACH;","names":[]}
@@ -0,0 +1,16 @@
1
+ // src/math/inRange.ts
2
+ function inRange(value, minimum, maximum) {
3
+ if (maximum == null) {
4
+ maximum = minimum;
5
+ minimum = 0;
6
+ }
7
+ if (minimum >= maximum) {
8
+ throw new Error("The maximum value must be greater than the minimum value.");
9
+ }
10
+ return minimum <= value && value < maximum;
11
+ }
12
+
13
+ export {
14
+ inRange
15
+ };
16
+ //# sourceMappingURL=chunk-BH6HLGBA.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/math/inRange.ts"],"sourcesContent":["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"],"mappings":";AAgBO,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;","names":[]}
@@ -0,0 +1,14 @@
1
+ // src/object/invert.ts
2
+ function invert(obj) {
3
+ const result = {};
4
+ for (const key in obj) {
5
+ const value = obj[key];
6
+ result[value] = key;
7
+ }
8
+ return result;
9
+ }
10
+
11
+ export {
12
+ invert
13
+ };
14
+ //# sourceMappingURL=chunk-FLTT2MGF.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/object/invert.ts"],"sourcesContent":["/**\n * Inverts the keys and values of an object. The keys of the input object become the values of the output object and vice versa.\n *\n * This function takes an object and creates a new object by inverting its keys and values. If the input object has duplicate values,\n * the key of the last occurrence will be used as the value for the new key in the output object. It effectively creates a reverse mapping\n * of the input object's key-value pairs.\n *\n * @template K - Type of the keys in the input object (string, number, symbol)\n * @template V - Type of the values in the input object (string, number, symbol)\n * @param {Record<K, V>} obj - The input object whose keys and values are to be inverted\n * @returns {{ [key in V]: K }} - A new object with keys and values inverted\n *\n * @example\n * invert({ a: 1, b: 2, c: 3 }); // { 1: 'a', 2: 'b', 3: 'c' }\n * invert({ 1: 'a', 2: 'b', 3: 'c' }); // { a: '1', b: '2', c: '3' }\n * invert({ a: 1, 2: 'b', c: 3, 4: 'd' }); // { 1: 'a', b: '2', 3: 'c', d: '4' }\n * invert({ a: Symbol('sym1'), b: Symbol('sym2') }); // { [Symbol('sym1')]: 'a', [Symbol('sym2')]: 'b' }\n */\n\ntype PropertyKey = string | number | symbol;\n\nexport function invert<K extends PropertyKey, V extends PropertyKey>(obj: Record<K, V>): { [key in V]: K } {\n const result = {} as { [key in V]: K };\n\n for (const key in obj) {\n const value = obj[key as K] as V;\n result[value] = key;\n }\n\n return result;\n}\n"],"mappings":";AAqBO,SAAS,OAAqD,KAAsC;AACzG,QAAM,SAAS,CAAC;AAEhB,aAAW,OAAO,KAAK;AACrB,UAAM,QAAQ,IAAI,GAAQ;AAC1B,WAAO,KAAK,IAAI;AAAA,EAClB;AAEA,SAAO;AACT;","names":[]}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-OC7ZNKWX.mjs.map
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-PMHULJNL.mjs.map
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-TLVQSNOE.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
package/dist/index.d.mts CHANGED
@@ -17,6 +17,7 @@ export { intersectionWith } from './array/intersectionWith.mjs';
17
17
  export { keyBy } from './array/keyBy.mjs';
18
18
  export { maxBy } from './array/maxBy.mjs';
19
19
  export { minBy } from './array/minBy.mjs';
20
+ export { orderBy } from './array/orderBy.mjs';
20
21
  export { partition } from './array/partition.mjs';
21
22
  export { sample } from './array/sample.mjs';
22
23
  export { sampleSize } from './array/sampleSize.mjs';
@@ -45,6 +46,7 @@ export { noop } from './function/noop.mjs';
45
46
  export { once } from './function/once.mjs';
46
47
  export { throttle } from './function/throttle.mjs';
47
48
  export { clamp } from './math/clamp.mjs';
49
+ export { inRange } from './math/inRange.mjs';
48
50
  export { mean } from './math/mean.mjs';
49
51
  export { meanBy } from './math/meanBy.mjs';
50
52
  export { random } from './math/random.mjs';
@@ -56,6 +58,7 @@ export { omit } from './object/omit.mjs';
56
58
  export { omitBy } from './object/omitBy.mjs';
57
59
  export { pick } from './object/pick.mjs';
58
60
  export { pickBy } from './object/pickBy.mjs';
61
+ export { invert } from './object/invert.mjs';
59
62
  export { isNil } from './predicate/isNil.mjs';
60
63
  export { isNotNil } from './predicate/isNotNil.mjs';
61
64
  export { isNull } from './predicate/isNull.mjs';
package/dist/index.d.ts CHANGED
@@ -17,6 +17,7 @@ export { intersectionWith } from './array/intersectionWith.js';
17
17
  export { keyBy } from './array/keyBy.js';
18
18
  export { maxBy } from './array/maxBy.js';
19
19
  export { minBy } from './array/minBy.js';
20
+ export { orderBy } from './array/orderBy.js';
20
21
  export { partition } from './array/partition.js';
21
22
  export { sample } from './array/sample.js';
22
23
  export { sampleSize } from './array/sampleSize.js';
@@ -45,6 +46,7 @@ export { noop } from './function/noop.js';
45
46
  export { once } from './function/once.js';
46
47
  export { throttle } from './function/throttle.js';
47
48
  export { clamp } from './math/clamp.js';
49
+ export { inRange } from './math/inRange.js';
48
50
  export { mean } from './math/mean.js';
49
51
  export { meanBy } from './math/meanBy.js';
50
52
  export { random } from './math/random.js';
@@ -56,6 +58,7 @@ export { omit } from './object/omit.js';
56
58
  export { omitBy } from './object/omitBy.js';
57
59
  export { pick } from './object/pick.js';
58
60
  export { pickBy } from './object/pickBy.js';
61
+ export { invert } from './object/invert.js';
59
62
  export { isNil } from './predicate/isNil.js';
60
63
  export { isNotNil } from './predicate/isNotNil.js';
61
64
  export { isNull } from './predicate/isNull.js';
package/dist/index.js CHANGED
@@ -51,9 +51,11 @@ __export(src_exports, {
51
51
  fill: () => fill,
52
52
  forEachRight: () => forEachRight,
53
53
  groupBy: () => groupBy,
54
+ inRange: () => inRange,
54
55
  intersection: () => intersection,
55
56
  intersectionBy: () => intersectionBy,
56
57
  intersectionWith: () => intersectionWith,
58
+ invert: () => invert,
57
59
  isNil: () => isNil,
58
60
  isNotNil: () => isNotNil,
59
61
  isNull: () => isNull,
@@ -67,6 +69,7 @@ __export(src_exports, {
67
69
  omit: () => omit,
68
70
  omitBy: () => omitBy,
69
71
  once: () => once,
72
+ orderBy: () => orderBy,
70
73
  partition: () => partition,
71
74
  pick: () => pick,
72
75
  pickBy: () => pickBy,
@@ -278,6 +281,31 @@ function minBy(items, getValue) {
278
281
  return minElement;
279
282
  }
280
283
 
284
+ // src/array/orderBy.ts
285
+ function orderBy(collection, keys, orders) {
286
+ const compareValues = (a, b, order) => {
287
+ if (a < b) {
288
+ return order === "asc" ? -1 : 1;
289
+ }
290
+ if (a > b) {
291
+ return order === "asc" ? 1 : -1;
292
+ }
293
+ return 0;
294
+ };
295
+ const effectiveOrders = keys.map((_, index) => orders[index] || orders[orders.length - 1]);
296
+ return collection.slice().sort((a, b) => {
297
+ for (let i = 0; i < keys.length; i++) {
298
+ const key = keys[i];
299
+ const order = effectiveOrders[i];
300
+ const result = compareValues(a[key], b[key], order);
301
+ if (result !== 0) {
302
+ return result;
303
+ }
304
+ }
305
+ return 0;
306
+ });
307
+ }
308
+
281
309
  // src/array/partition.ts
282
310
  function partition(arr, isInTruthy) {
283
311
  const truthy = [];
@@ -580,6 +608,18 @@ function clamp(value, bound1, bound2) {
580
608
  return Math.min(Math.max(value, bound1), bound2);
581
609
  }
582
610
 
611
+ // src/math/inRange.ts
612
+ function inRange(value, minimum, maximum) {
613
+ if (maximum == null) {
614
+ maximum = minimum;
615
+ minimum = 0;
616
+ }
617
+ if (minimum >= maximum) {
618
+ throw new Error("The maximum value must be greater than the minimum value.");
619
+ }
620
+ return minimum <= value && value < maximum;
621
+ }
622
+
583
623
  // src/math/sum.ts
584
624
  function sum(nums) {
585
625
  let result = 0;
@@ -671,6 +711,16 @@ function pickBy(obj, shouldPick) {
671
711
  return result;
672
712
  }
673
713
 
714
+ // src/object/invert.ts
715
+ function invert(obj) {
716
+ const result = {};
717
+ for (const key in obj) {
718
+ const value = obj[key];
719
+ result[value] = key;
720
+ }
721
+ return result;
722
+ }
723
+
674
724
  // src/predicate/isNil.ts
675
725
  function isNil(x) {
676
726
  return x === null || x === void 0;
@@ -727,9 +777,11 @@ function delay(ms, { signal } = {}) {
727
777
  fill,
728
778
  forEachRight,
729
779
  groupBy,
780
+ inRange,
730
781
  intersection,
731
782
  intersectionBy,
732
783
  intersectionWith,
784
+ invert,
733
785
  isNil,
734
786
  isNotNil,
735
787
  isNull,
@@ -743,6 +795,7 @@ function delay(ms, { signal } = {}) {
743
795
  omit,
744
796
  omitBy,
745
797
  once,
798
+ orderBy,
746
799
  partition,
747
800
  pick,
748
801
  pickBy,