@web-ts-toolkit/utils 0.29.0 → 0.30.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.
package/README.md CHANGED
@@ -10,8 +10,8 @@ pnpm add @web-ts-toolkit/utils
10
10
 
11
11
  ## Highlights
12
12
 
13
- - object-path helpers such as `get(...)` and `set(...)`
14
- - collection helpers such as `map(...)`, `filter(...)`, `uniq(...)`, and `orderBy(...)`
13
+ - object-path helpers such as `get(...)`, `set(...)`, and `hasOwn(...)`
14
+ - collection helpers such as `map(...)`, `filter(...)`, `uniq(...)`, `uniqBy(...)`, and `orderBy(...)`
15
15
  - small type guards
16
16
  - URL helpers such as `normalizeUrlPath(...)`
17
17
  - async helpers such as `mapValuesAsync(...)`
@@ -19,7 +19,16 @@ pnpm add @web-ts-toolkit/utils
19
19
  ## Quick Start
20
20
 
21
21
  ```ts
22
- import { get, groupBy, normalizeUrlPath, parseBooleanString, startCase, sumBy } from '@web-ts-toolkit/utils';
22
+ import {
23
+ get,
24
+ groupBy,
25
+ hasOwn,
26
+ normalizeUrlPath,
27
+ parseBooleanString,
28
+ startCase,
29
+ sumBy,
30
+ uniqBy,
31
+ } from '@web-ts-toolkit/utils';
23
32
 
24
33
  const payload = {
25
34
  user: {
@@ -30,6 +39,7 @@ const payload = {
30
39
  };
31
40
 
32
41
  get(payload, 'user.profile.name');
42
+ hasOwn(payload.user.profile, 'name');
33
43
  groupBy(
34
44
  [
35
45
  { type: 'fruit', name: 'apple' },
@@ -38,6 +48,14 @@ groupBy(
38
48
  ],
39
49
  'type',
40
50
  );
51
+ uniqBy(
52
+ [
53
+ { id: 'a', name: 'Ada' },
54
+ { id: 'a', name: 'Ada Lovelace' },
55
+ { id: 'b', name: 'Grace' },
56
+ ],
57
+ 'id',
58
+ );
41
59
  startCase('api_response_time');
42
60
  sumBy([{ hours: 2 }, { hours: 3 }], 'hours');
43
61
  normalizeUrlPath('api//users');
@@ -46,8 +64,8 @@ parseBooleanString('true', false);
46
64
 
47
65
  ## Main Exports
48
66
 
49
- - object helpers: `get`, `set`, `pick`, `pickBy`, `omit`, `assign`, `cloneDeep`, `mapKeys`
50
- - collection helpers: `map`, `filter`, `reduce`, `find`, `flatten`, `uniq`, `orderBy`, `groupBy`, `sum`, `sumBy`
67
+ - object helpers: `get`, `set`, `hasOwn`, `pick`, `pickBy`, `omit`, `omitBy`, `assign`, `cloneDeep`, `mapKeys`
68
+ - collection helpers: `map`, `filter`, `reduce`, `find`, `flatten`, `uniq`, `uniqBy`, `orderBy`, `groupBy`, `sum`, `sumBy`
51
69
  - string helpers: `startCase`, `upperCase`
52
70
  - guards: `isArray`, `isPlainObject`, `isString`, `isPromise`
53
71
  - URL helpers: `addLeadingSlash`, `removeConsecutiveSlashesFromUrl`, `normalizeUrlPath`
package/index.d.mts CHANGED
@@ -29,6 +29,8 @@ declare function get<T = unknown>(object: unknown, path: PropertyPath, defaultVa
29
29
 
30
30
  declare function groupBy<T>(collection: T[] | Record<string, T> | null | undefined, iteratee: string | number | ((value: T, key: number | string, collection: T[] | Record<string, T>) => unknown)): Record<string, T[]>;
31
31
 
32
+ declare function hasOwn<TKey extends PropertyKey>(value: unknown, key: TKey): value is Record<TKey, unknown>;
33
+
32
34
  declare function intersection<T>(...arrays: Array<T[] | null | undefined>): T[];
33
35
 
34
36
  declare function intersectionBy<T>(...args: unknown[]): T[];
@@ -75,6 +77,8 @@ declare function noop(): void;
75
77
 
76
78
  declare function omit<T extends object>(object: T, paths: PropertyPath | PropertyPath[]): Partial<T>;
77
79
 
80
+ declare function omitBy<TValue>(object: Record<string, TValue> | null | undefined, predicate: (value: TValue, key: string, object: Record<string, TValue>) => boolean): Record<string, TValue>;
81
+
78
82
  declare function parseBooleanString(str: string | undefined, defaultValue?: boolean): boolean | undefined;
79
83
 
80
84
  declare function orderBy<T>(collection: T[] | null | undefined, iteratees?: Array<string | number | ((value: T) => unknown)>, orders?: string[]): T[];
@@ -106,6 +110,8 @@ declare function toStringRecord(value: unknown): Record<string, string> | undefi
106
110
 
107
111
  declare function uniq<T>(array: T[] | null | undefined): T[];
108
112
 
113
+ declare function uniqBy<T>(array: T[] | null | undefined, iteratee?: string | number | ((value: T, key: number, collection: T[]) => unknown)): T[];
114
+
109
115
  declare function upperCase(value: unknown): string;
110
116
 
111
- export { addLeadingSlash, arrayToRecord, assign, castArray, cloneDeep, compact, difference, filter, find, flatten, flattenDeep, forEach, get, groupBy, intersection, intersectionBy, isArray, isBoolean, isEmpty, isEqual, isFunction, isMatch, isNaNValue as isNaN, isNil, isNumber, isObject, isPlainObject, isPromise, isString, isUndefined, keys, map, mapKeys, mapValues, mapValuesAsync, noop, normalizeUrlPath, omit, orderBy, padEnd, parseBooleanString, pick, pickBy, reduce, removeConsecutiveSlashesFromUrl, set, startCase, sum, sumBy, toAsyncFn, toStringRecord, uniq, upperCase };
117
+ export { addLeadingSlash, arrayToRecord, assign, castArray, cloneDeep, compact, difference, filter, find, flatten, flattenDeep, forEach, get, groupBy, hasOwn, intersection, intersectionBy, isArray, isBoolean, isEmpty, isEqual, isFunction, isMatch, isNaNValue as isNaN, isNil, isNumber, isObject, isPlainObject, isPromise, isString, isUndefined, keys, map, mapKeys, mapValues, mapValuesAsync, noop, normalizeUrlPath, omit, omitBy, orderBy, padEnd, parseBooleanString, pick, pickBy, reduce, removeConsecutiveSlashesFromUrl, set, startCase, sum, sumBy, toAsyncFn, toStringRecord, uniq, uniqBy, upperCase };
package/index.d.ts CHANGED
@@ -29,6 +29,8 @@ declare function get<T = unknown>(object: unknown, path: PropertyPath, defaultVa
29
29
 
30
30
  declare function groupBy<T>(collection: T[] | Record<string, T> | null | undefined, iteratee: string | number | ((value: T, key: number | string, collection: T[] | Record<string, T>) => unknown)): Record<string, T[]>;
31
31
 
32
+ declare function hasOwn<TKey extends PropertyKey>(value: unknown, key: TKey): value is Record<TKey, unknown>;
33
+
32
34
  declare function intersection<T>(...arrays: Array<T[] | null | undefined>): T[];
33
35
 
34
36
  declare function intersectionBy<T>(...args: unknown[]): T[];
@@ -75,6 +77,8 @@ declare function noop(): void;
75
77
 
76
78
  declare function omit<T extends object>(object: T, paths: PropertyPath | PropertyPath[]): Partial<T>;
77
79
 
80
+ declare function omitBy<TValue>(object: Record<string, TValue> | null | undefined, predicate: (value: TValue, key: string, object: Record<string, TValue>) => boolean): Record<string, TValue>;
81
+
78
82
  declare function parseBooleanString(str: string | undefined, defaultValue?: boolean): boolean | undefined;
79
83
 
80
84
  declare function orderBy<T>(collection: T[] | null | undefined, iteratees?: Array<string | number | ((value: T) => unknown)>, orders?: string[]): T[];
@@ -106,6 +110,8 @@ declare function toStringRecord(value: unknown): Record<string, string> | undefi
106
110
 
107
111
  declare function uniq<T>(array: T[] | null | undefined): T[];
108
112
 
113
+ declare function uniqBy<T>(array: T[] | null | undefined, iteratee?: string | number | ((value: T, key: number, collection: T[]) => unknown)): T[];
114
+
109
115
  declare function upperCase(value: unknown): string;
110
116
 
111
- export { addLeadingSlash, arrayToRecord, assign, castArray, cloneDeep, compact, difference, filter, find, flatten, flattenDeep, forEach, get, groupBy, intersection, intersectionBy, isArray, isBoolean, isEmpty, isEqual, isFunction, isMatch, isNaNValue as isNaN, isNil, isNumber, isObject, isPlainObject, isPromise, isString, isUndefined, keys, map, mapKeys, mapValues, mapValuesAsync, noop, normalizeUrlPath, omit, orderBy, padEnd, parseBooleanString, pick, pickBy, reduce, removeConsecutiveSlashesFromUrl, set, startCase, sum, sumBy, toAsyncFn, toStringRecord, uniq, upperCase };
117
+ export { addLeadingSlash, arrayToRecord, assign, castArray, cloneDeep, compact, difference, filter, find, flatten, flattenDeep, forEach, get, groupBy, hasOwn, intersection, intersectionBy, isArray, isBoolean, isEmpty, isEqual, isFunction, isMatch, isNaNValue as isNaN, isNil, isNumber, isObject, isPlainObject, isPromise, isString, isUndefined, keys, map, mapKeys, mapValues, mapValuesAsync, noop, normalizeUrlPath, omit, omitBy, orderBy, padEnd, parseBooleanString, pick, pickBy, reduce, removeConsecutiveSlashesFromUrl, set, startCase, sum, sumBy, toAsyncFn, toStringRecord, uniq, uniqBy, upperCase };
package/index.js CHANGED
@@ -33,6 +33,7 @@ __export(index_exports, {
33
33
  forEach: () => forEach,
34
34
  get: () => get,
35
35
  groupBy: () => groupBy,
36
+ hasOwn: () => hasOwn,
36
37
  intersection: () => intersection,
37
38
  intersectionBy: () => intersectionBy,
38
39
  isArray: () => isArray,
@@ -57,6 +58,7 @@ __export(index_exports, {
57
58
  noop: () => noop,
58
59
  normalizeUrlPath: () => normalizeUrlPath,
59
60
  omit: () => omit,
61
+ omitBy: () => omitBy,
60
62
  orderBy: () => orderBy,
61
63
  padEnd: () => padEnd,
62
64
  parseBooleanString: () => parseBooleanString,
@@ -71,6 +73,7 @@ __export(index_exports, {
71
73
  toAsyncFn: () => toAsyncFn,
72
74
  toStringRecord: () => toStringRecord,
73
75
  uniq: () => uniq,
76
+ uniqBy: () => uniqBy,
74
77
  upperCase: () => upperCase
75
78
  });
76
79
  module.exports = __toCommonJS(index_exports);
@@ -537,6 +540,14 @@ function groupBy(collection, iteratee) {
537
540
  return result;
538
541
  }
539
542
 
543
+ // src/hasOwn.ts
544
+ function hasOwn(value, key) {
545
+ if (value === null || value === void 0) {
546
+ return false;
547
+ }
548
+ return Object.hasOwn(Object(value), key);
549
+ }
550
+
540
551
  // src/intersection.ts
541
552
  function intersection(...arrays) {
542
553
  const [first = [], ...rest] = arrays;
@@ -744,6 +755,23 @@ function omit(object, paths) {
744
755
  return result;
745
756
  }
746
757
 
758
+ // src/omitBy.ts
759
+ function omitBy(object, predicate) {
760
+ if (!object) {
761
+ return {};
762
+ }
763
+ const result = {};
764
+ const keys2 = Object.keys(object);
765
+ for (let index = 0; index < keys2.length; index++) {
766
+ const key = keys2[index];
767
+ const value = object[key];
768
+ if (!predicate(value, key, object)) {
769
+ result[key] = value;
770
+ }
771
+ }
772
+ return result;
773
+ }
774
+
747
775
  // src/parseBooleanString.ts
748
776
  function parseBooleanString(str, defaultValue) {
749
777
  return str ? str === "true" : defaultValue;
@@ -919,6 +947,26 @@ function uniq(array) {
919
947
  return baseUniq(array);
920
948
  }
921
949
 
950
+ // src/uniqBy.ts
951
+ function uniqBy(array, iteratee) {
952
+ if (!Array.isArray(array)) {
953
+ return [];
954
+ }
955
+ const callback = getIteratee(iteratee);
956
+ const result = [];
957
+ const seen = [];
958
+ for (let index = 0; index < array.length; index++) {
959
+ const value = array[index];
960
+ const computed = callback(value, index, array);
961
+ if (seen.some((entry) => sameValueZero(entry, computed))) {
962
+ continue;
963
+ }
964
+ seen.push(computed);
965
+ result.push(value);
966
+ }
967
+ return result;
968
+ }
969
+
922
970
  // src/upperCase.ts
923
971
  function upperCase(value) {
924
972
  return words(value).join(" ").toUpperCase();
@@ -939,6 +987,7 @@ function upperCase(value) {
939
987
  forEach,
940
988
  get,
941
989
  groupBy,
990
+ hasOwn,
942
991
  intersection,
943
992
  intersectionBy,
944
993
  isArray,
@@ -963,6 +1012,7 @@ function upperCase(value) {
963
1012
  noop,
964
1013
  normalizeUrlPath,
965
1014
  omit,
1015
+ omitBy,
966
1016
  orderBy,
967
1017
  padEnd,
968
1018
  parseBooleanString,
@@ -977,5 +1027,6 @@ function upperCase(value) {
977
1027
  toAsyncFn,
978
1028
  toStringRecord,
979
1029
  uniq,
1030
+ uniqBy,
980
1031
  upperCase
981
1032
  });
package/index.mjs CHANGED
@@ -460,6 +460,14 @@ function groupBy(collection, iteratee) {
460
460
  return result;
461
461
  }
462
462
 
463
+ // src/hasOwn.ts
464
+ function hasOwn(value, key) {
465
+ if (value === null || value === void 0) {
466
+ return false;
467
+ }
468
+ return Object.hasOwn(Object(value), key);
469
+ }
470
+
463
471
  // src/intersection.ts
464
472
  function intersection(...arrays) {
465
473
  const [first = [], ...rest] = arrays;
@@ -667,6 +675,23 @@ function omit(object, paths) {
667
675
  return result;
668
676
  }
669
677
 
678
+ // src/omitBy.ts
679
+ function omitBy(object, predicate) {
680
+ if (!object) {
681
+ return {};
682
+ }
683
+ const result = {};
684
+ const keys2 = Object.keys(object);
685
+ for (let index = 0; index < keys2.length; index++) {
686
+ const key = keys2[index];
687
+ const value = object[key];
688
+ if (!predicate(value, key, object)) {
689
+ result[key] = value;
690
+ }
691
+ }
692
+ return result;
693
+ }
694
+
670
695
  // src/parseBooleanString.ts
671
696
  function parseBooleanString(str, defaultValue) {
672
697
  return str ? str === "true" : defaultValue;
@@ -842,6 +867,26 @@ function uniq(array) {
842
867
  return baseUniq(array);
843
868
  }
844
869
 
870
+ // src/uniqBy.ts
871
+ function uniqBy(array, iteratee) {
872
+ if (!Array.isArray(array)) {
873
+ return [];
874
+ }
875
+ const callback = getIteratee(iteratee);
876
+ const result = [];
877
+ const seen = [];
878
+ for (let index = 0; index < array.length; index++) {
879
+ const value = array[index];
880
+ const computed = callback(value, index, array);
881
+ if (seen.some((entry) => sameValueZero(entry, computed))) {
882
+ continue;
883
+ }
884
+ seen.push(computed);
885
+ result.push(value);
886
+ }
887
+ return result;
888
+ }
889
+
845
890
  // src/upperCase.ts
846
891
  function upperCase(value) {
847
892
  return words(value).join(" ").toUpperCase();
@@ -861,6 +906,7 @@ export {
861
906
  forEach,
862
907
  get,
863
908
  groupBy,
909
+ hasOwn,
864
910
  intersection,
865
911
  intersectionBy,
866
912
  isArray,
@@ -885,6 +931,7 @@ export {
885
931
  noop,
886
932
  normalizeUrlPath,
887
933
  omit,
934
+ omitBy,
888
935
  orderBy,
889
936
  padEnd,
890
937
  parseBooleanString,
@@ -899,5 +946,6 @@ export {
899
946
  toAsyncFn,
900
947
  toStringRecord,
901
948
  uniq,
949
+ uniqBy,
902
950
  upperCase
903
951
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@web-ts-toolkit/utils",
3
3
  "description": "Shared collection, object, async, and URL helpers used across workspace packages",
4
4
  "homepage": "https://web-ts-toolkit.pages.dev/docs/packages/utils",
5
- "version": "0.29.0",
5
+ "version": "0.30.0",
6
6
  "sideEffects": false,
7
7
  "keywords": [
8
8
  "utilities",