@web-ts-toolkit/utils 0.2.0 → 0.4.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/index.d.mts CHANGED
@@ -1,5 +1,7 @@
1
1
  declare function addLeadingSlash(value: string): string;
2
2
 
3
+ declare function arrayToRecord(arr: string[]): Record<string, true>;
4
+
3
5
  declare function assign<T extends object>(target: T, ...sources: unknown[]): T;
4
6
 
5
7
  declare function castArray<T>(value: T | T[]): T[];
@@ -61,6 +63,10 @@ declare function keys(value: unknown): string[];
61
63
 
62
64
  declare function map<T, TResult>(collection: T[] | Record<string, T> | null | undefined, iteratee: string | number | ((value: T, key: number | string, collection: T[] | Record<string, T>) => TResult)): TResult[];
63
65
 
66
+ declare function mapValues<TValue, TResult>(object: Record<string, TValue> | null | undefined, iteratee: (value: TValue, key: string, object: Record<string, TValue>) => TResult): Record<string, TResult>;
67
+
68
+ declare function mapValuesAsync<TObject extends Record<string, unknown>, TResult>(object: TObject, asyncFn: (value: TObject[keyof TObject], key: string, object: TObject) => Promise<TResult> | TResult): Promise<Record<string, TResult>>;
69
+
64
70
  declare function noop(): void;
65
71
 
66
72
  declare function omit<T extends object>(object: T, paths: PropertyPath | PropertyPath[]): Partial<T>;
@@ -71,13 +77,19 @@ declare function padEnd(value: unknown, length?: number, chars?: string): string
71
77
 
72
78
  declare function pick<T extends object>(object: T, paths: PropertyPath | PropertyPath[]): Partial<T>;
73
79
 
80
+ declare function normalizeUrlPath(url: string): string;
81
+
74
82
  declare function reduce<T, TResult>(collection: T[] | null | undefined, iteratee: (accumulator: TResult, value: T, key: number, collection: T[]) => TResult, accumulator?: TResult): TResult;
75
83
  declare function reduce<T, TResult>(collection: Record<string, T> | null | undefined, iteratee: (accumulator: TResult, value: T, key: string, collection: Record<string, T>) => TResult, accumulator?: TResult): TResult;
76
84
 
85
+ declare function removeConsecutiveSlashesFromUrl(url: string): string;
86
+
77
87
  declare function set<T>(object: T, path: PropertyPath, value: unknown): T;
78
88
 
89
+ declare function toAsyncFn<TArgs extends unknown[], TResult>(fn?: ((this: unknown, ...args: TArgs) => TResult | PromiseLike<TResult>) | null, defaultValue?: TResult): (this: unknown, ...args: TArgs) => PromiseLike<unknown> | Promise<any>;
90
+
79
91
  declare function toStringRecord(value: unknown): Record<string, string> | undefined;
80
92
 
81
93
  declare function uniq<T>(array: T[] | null | undefined): T[];
82
94
 
83
- export { addLeadingSlash, assign, castArray, cloneDeep, compact, difference, filter, find, flatten, flattenDeep, forEach, get, intersection, intersectionBy, isArray, isBoolean, isEmpty, isEqual, isFunction, isMatch, isNaNValue as isNaN, isNil, isNumber, isObject, isPlainObject, isPromise, isString, isUndefined, keys, map, noop, omit, orderBy, padEnd, pick, reduce, set, toStringRecord, uniq };
95
+ export { addLeadingSlash, arrayToRecord, assign, castArray, cloneDeep, compact, difference, filter, find, flatten, flattenDeep, forEach, get, intersection, intersectionBy, isArray, isBoolean, isEmpty, isEqual, isFunction, isMatch, isNaNValue as isNaN, isNil, isNumber, isObject, isPlainObject, isPromise, isString, isUndefined, keys, map, mapValues, mapValuesAsync, noop, normalizeUrlPath, omit, orderBy, padEnd, pick, reduce, removeConsecutiveSlashesFromUrl, set, toAsyncFn, toStringRecord, uniq };
package/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  declare function addLeadingSlash(value: string): string;
2
2
 
3
+ declare function arrayToRecord(arr: string[]): Record<string, true>;
4
+
3
5
  declare function assign<T extends object>(target: T, ...sources: unknown[]): T;
4
6
 
5
7
  declare function castArray<T>(value: T | T[]): T[];
@@ -61,6 +63,10 @@ declare function keys(value: unknown): string[];
61
63
 
62
64
  declare function map<T, TResult>(collection: T[] | Record<string, T> | null | undefined, iteratee: string | number | ((value: T, key: number | string, collection: T[] | Record<string, T>) => TResult)): TResult[];
63
65
 
66
+ declare function mapValues<TValue, TResult>(object: Record<string, TValue> | null | undefined, iteratee: (value: TValue, key: string, object: Record<string, TValue>) => TResult): Record<string, TResult>;
67
+
68
+ declare function mapValuesAsync<TObject extends Record<string, unknown>, TResult>(object: TObject, asyncFn: (value: TObject[keyof TObject], key: string, object: TObject) => Promise<TResult> | TResult): Promise<Record<string, TResult>>;
69
+
64
70
  declare function noop(): void;
65
71
 
66
72
  declare function omit<T extends object>(object: T, paths: PropertyPath | PropertyPath[]): Partial<T>;
@@ -71,13 +77,19 @@ declare function padEnd(value: unknown, length?: number, chars?: string): string
71
77
 
72
78
  declare function pick<T extends object>(object: T, paths: PropertyPath | PropertyPath[]): Partial<T>;
73
79
 
80
+ declare function normalizeUrlPath(url: string): string;
81
+
74
82
  declare function reduce<T, TResult>(collection: T[] | null | undefined, iteratee: (accumulator: TResult, value: T, key: number, collection: T[]) => TResult, accumulator?: TResult): TResult;
75
83
  declare function reduce<T, TResult>(collection: Record<string, T> | null | undefined, iteratee: (accumulator: TResult, value: T, key: string, collection: Record<string, T>) => TResult, accumulator?: TResult): TResult;
76
84
 
85
+ declare function removeConsecutiveSlashesFromUrl(url: string): string;
86
+
77
87
  declare function set<T>(object: T, path: PropertyPath, value: unknown): T;
78
88
 
89
+ declare function toAsyncFn<TArgs extends unknown[], TResult>(fn?: ((this: unknown, ...args: TArgs) => TResult | PromiseLike<TResult>) | null, defaultValue?: TResult): (this: unknown, ...args: TArgs) => PromiseLike<unknown> | Promise<any>;
90
+
79
91
  declare function toStringRecord(value: unknown): Record<string, string> | undefined;
80
92
 
81
93
  declare function uniq<T>(array: T[] | null | undefined): T[];
82
94
 
83
- export { addLeadingSlash, assign, castArray, cloneDeep, compact, difference, filter, find, flatten, flattenDeep, forEach, get, intersection, intersectionBy, isArray, isBoolean, isEmpty, isEqual, isFunction, isMatch, isNaNValue as isNaN, isNil, isNumber, isObject, isPlainObject, isPromise, isString, isUndefined, keys, map, noop, omit, orderBy, padEnd, pick, reduce, set, toStringRecord, uniq };
95
+ export { addLeadingSlash, arrayToRecord, assign, castArray, cloneDeep, compact, difference, filter, find, flatten, flattenDeep, forEach, get, intersection, intersectionBy, isArray, isBoolean, isEmpty, isEqual, isFunction, isMatch, isNaNValue as isNaN, isNil, isNumber, isObject, isPlainObject, isPromise, isString, isUndefined, keys, map, mapValues, mapValuesAsync, noop, normalizeUrlPath, omit, orderBy, padEnd, pick, reduce, removeConsecutiveSlashesFromUrl, set, toAsyncFn, toStringRecord, uniq };
package/index.js CHANGED
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  var index_exports = {};
21
21
  __export(index_exports, {
22
22
  addLeadingSlash: () => addLeadingSlash,
23
+ arrayToRecord: () => arrayToRecord,
23
24
  assign: () => assign,
24
25
  castArray: () => castArray,
25
26
  cloneDeep: () => cloneDeep,
@@ -49,13 +50,18 @@ __export(index_exports, {
49
50
  isUndefined: () => isUndefined,
50
51
  keys: () => keys,
51
52
  map: () => map,
53
+ mapValues: () => mapValues,
54
+ mapValuesAsync: () => mapValuesAsync,
52
55
  noop: () => noop,
56
+ normalizeUrlPath: () => normalizeUrlPath,
53
57
  omit: () => omit,
54
58
  orderBy: () => orderBy,
55
59
  padEnd: () => padEnd,
56
60
  pick: () => pick,
57
61
  reduce: () => reduce,
62
+ removeConsecutiveSlashesFromUrl: () => removeConsecutiveSlashesFromUrl,
58
63
  set: () => set,
64
+ toAsyncFn: () => toAsyncFn,
59
65
  toStringRecord: () => toStringRecord,
60
66
  uniq: () => uniq
61
67
  });
@@ -66,6 +72,15 @@ function addLeadingSlash(value) {
66
72
  return value.startsWith("/") ? value : `/${value}`;
67
73
  }
68
74
 
75
+ // src/arrayToRecord.ts
76
+ function arrayToRecord(arr) {
77
+ const obj = {};
78
+ for (let x = 0; x < arr.length; x++) {
79
+ obj[arr[x]] = true;
80
+ }
81
+ return obj;
82
+ }
83
+
69
84
  // src/assign.ts
70
85
  function assign(target, ...sources) {
71
86
  return Object.assign(target, ...sources);
@@ -624,6 +639,29 @@ function map(collection, iteratee) {
624
639
  return result;
625
640
  }
626
641
 
642
+ // src/mapValues.ts
643
+ function mapValues(object, iteratee) {
644
+ if (!object) {
645
+ return {};
646
+ }
647
+ const result = {};
648
+ const keys2 = Object.keys(object);
649
+ for (let index = 0; index < keys2.length; index++) {
650
+ const key = keys2[index];
651
+ result[key] = iteratee(object[key], key, object);
652
+ }
653
+ return result;
654
+ }
655
+
656
+ // src/mapValuesAsync.ts
657
+ async function mapValuesAsync(object, asyncFn) {
658
+ const entries = Object.entries(object);
659
+ const mapped = await Promise.all(
660
+ entries.map(async ([key, value]) => [key, await asyncFn(value, key, object)])
661
+ );
662
+ return Object.fromEntries(mapped);
663
+ }
664
+
627
665
  // src/noop.ts
628
666
  function noop() {
629
667
  }
@@ -681,6 +719,16 @@ function pick(object, paths) {
681
719
  return result;
682
720
  }
683
721
 
722
+ // src/removeConsecutiveSlashesFromUrl.ts
723
+ function removeConsecutiveSlashesFromUrl(url) {
724
+ return url.replace(/\/{2,}/g, "/");
725
+ }
726
+
727
+ // src/normalizeUrlPath.ts
728
+ function normalizeUrlPath(url) {
729
+ return addLeadingSlash(removeConsecutiveSlashesFromUrl(url));
730
+ }
731
+
684
732
  // src/reduce.ts
685
733
  function reduce(collection, iteratee, accumulator) {
686
734
  const hasAccumulator = arguments.length >= 3;
@@ -717,6 +765,15 @@ function set(object, path, value) {
717
765
  return setPath(object, path, value);
718
766
  }
719
767
 
768
+ // src/toAsyncFn.ts
769
+ function toAsyncFn(fn, defaultValue) {
770
+ if (!fn) return () => Promise.resolve(defaultValue);
771
+ return function asyncFn(...args) {
772
+ const ret = fn.apply(this, args);
773
+ return isPromise(ret) ? ret : Promise.resolve(ret);
774
+ };
775
+ }
776
+
720
777
  // src/toStringRecord.ts
721
778
  function toStringRecord(value) {
722
779
  if (!isPlainObject2(value)) {
@@ -741,6 +798,7 @@ function uniq(array) {
741
798
  // Annotate the CommonJS export names for ESM import in node:
742
799
  0 && (module.exports = {
743
800
  addLeadingSlash,
801
+ arrayToRecord,
744
802
  assign,
745
803
  castArray,
746
804
  cloneDeep,
@@ -770,13 +828,18 @@ function uniq(array) {
770
828
  isUndefined,
771
829
  keys,
772
830
  map,
831
+ mapValues,
832
+ mapValuesAsync,
773
833
  noop,
834
+ normalizeUrlPath,
774
835
  omit,
775
836
  orderBy,
776
837
  padEnd,
777
838
  pick,
778
839
  reduce,
840
+ removeConsecutiveSlashesFromUrl,
779
841
  set,
842
+ toAsyncFn,
780
843
  toStringRecord,
781
844
  uniq
782
845
  });
package/index.mjs CHANGED
@@ -3,6 +3,15 @@ function addLeadingSlash(value) {
3
3
  return value.startsWith("/") ? value : `/${value}`;
4
4
  }
5
5
 
6
+ // src/arrayToRecord.ts
7
+ function arrayToRecord(arr) {
8
+ const obj = {};
9
+ for (let x = 0; x < arr.length; x++) {
10
+ obj[arr[x]] = true;
11
+ }
12
+ return obj;
13
+ }
14
+
6
15
  // src/assign.ts
7
16
  function assign(target, ...sources) {
8
17
  return Object.assign(target, ...sources);
@@ -561,6 +570,29 @@ function map(collection, iteratee) {
561
570
  return result;
562
571
  }
563
572
 
573
+ // src/mapValues.ts
574
+ function mapValues(object, iteratee) {
575
+ if (!object) {
576
+ return {};
577
+ }
578
+ const result = {};
579
+ const keys2 = Object.keys(object);
580
+ for (let index = 0; index < keys2.length; index++) {
581
+ const key = keys2[index];
582
+ result[key] = iteratee(object[key], key, object);
583
+ }
584
+ return result;
585
+ }
586
+
587
+ // src/mapValuesAsync.ts
588
+ async function mapValuesAsync(object, asyncFn) {
589
+ const entries = Object.entries(object);
590
+ const mapped = await Promise.all(
591
+ entries.map(async ([key, value]) => [key, await asyncFn(value, key, object)])
592
+ );
593
+ return Object.fromEntries(mapped);
594
+ }
595
+
564
596
  // src/noop.ts
565
597
  function noop() {
566
598
  }
@@ -618,6 +650,16 @@ function pick(object, paths) {
618
650
  return result;
619
651
  }
620
652
 
653
+ // src/removeConsecutiveSlashesFromUrl.ts
654
+ function removeConsecutiveSlashesFromUrl(url) {
655
+ return url.replace(/\/{2,}/g, "/");
656
+ }
657
+
658
+ // src/normalizeUrlPath.ts
659
+ function normalizeUrlPath(url) {
660
+ return addLeadingSlash(removeConsecutiveSlashesFromUrl(url));
661
+ }
662
+
621
663
  // src/reduce.ts
622
664
  function reduce(collection, iteratee, accumulator) {
623
665
  const hasAccumulator = arguments.length >= 3;
@@ -654,6 +696,15 @@ function set(object, path, value) {
654
696
  return setPath(object, path, value);
655
697
  }
656
698
 
699
+ // src/toAsyncFn.ts
700
+ function toAsyncFn(fn, defaultValue) {
701
+ if (!fn) return () => Promise.resolve(defaultValue);
702
+ return function asyncFn(...args) {
703
+ const ret = fn.apply(this, args);
704
+ return isPromise(ret) ? ret : Promise.resolve(ret);
705
+ };
706
+ }
707
+
657
708
  // src/toStringRecord.ts
658
709
  function toStringRecord(value) {
659
710
  if (!isPlainObject2(value)) {
@@ -677,6 +728,7 @@ function uniq(array) {
677
728
  }
678
729
  export {
679
730
  addLeadingSlash,
731
+ arrayToRecord,
680
732
  assign,
681
733
  castArray,
682
734
  cloneDeep,
@@ -706,13 +758,18 @@ export {
706
758
  isUndefined,
707
759
  keys,
708
760
  map,
761
+ mapValues,
762
+ mapValuesAsync,
709
763
  noop,
764
+ normalizeUrlPath,
710
765
  omit,
711
766
  orderBy,
712
767
  padEnd,
713
768
  pick,
714
769
  reduce,
770
+ removeConsecutiveSlashesFromUrl,
715
771
  set,
772
+ toAsyncFn,
716
773
  toStringRecord,
717
774
  uniq
718
775
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@web-ts-toolkit/utils",
3
3
  "description": "Shared utility helpers used across workspace packages",
4
- "version": "0.2.0",
4
+ "version": "0.4.0",
5
5
  "main": "./index.js",
6
6
  "module": "./index.mjs",
7
7
  "types": "./index.d.ts",