es-toolkit 1.10.1-dev.247 → 1.10.1-dev.248

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.
@@ -0,0 +1,34 @@
1
+ // src/object/get.ts
2
+ function get(obj, path, defaultValue) {
3
+ if (!Array.isArray(path)) {
4
+ const pathArr = path.split(".");
5
+ path = [];
6
+ for (let i = 0; i < pathArr.length; i++) {
7
+ if (/^.+\[[0-9]+\]$/.test(pathArr[i])) {
8
+ path.push(/^.+(?=\[[0-9]+\]$)/.exec(pathArr[i])[0]);
9
+ path.push(`${new RegExp("(?<=^.+\\[)[0-9]+(?=\\]$)").exec(pathArr[i])[0]}`);
10
+ } else {
11
+ path.push(pathArr[i]);
12
+ }
13
+ }
14
+ }
15
+ const callback = (innerObj, innerPath) => {
16
+ if (typeof innerObj[innerPath] === "undefined") {
17
+ return void 0;
18
+ }
19
+ return innerObj[innerPath];
20
+ };
21
+ let currentObj = obj;
22
+ for (let i = 0; i < path.length; i++) {
23
+ currentObj = callback(currentObj, path[i]);
24
+ if (typeof currentObj === "undefined") {
25
+ return typeof defaultValue === "undefined" ? void 0 : defaultValue;
26
+ }
27
+ }
28
+ return currentObj;
29
+ }
30
+
31
+ export {
32
+ get
33
+ };
34
+ //# sourceMappingURL=chunk-4RQPJUPQ.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/object/get.ts"],"sourcesContent":["/**\n * Get a value from an object by path.\n * \n * This function takes an object and a path, and default value that will be returned if\n * a returnable value about the given path does not exist.\n * \n * @template O - Type of the object to get value from. \n * @template T - Type of the default value to return if the value does not exist.\n * @param {O} obj - The object to get value from. \n * @param {string | string[]} path - The path to get value from. \n * @param {T} defaultValue - The default value to return if the value does not exist.\n * @returns {O[keyof O] | undefined} - The value from the object by the given path.\n * \n * @example\n * const obj = { a: { b: 3 } };\n * get(obj, 'a.b', null); // 3\n */\n\nexport function get<O, T>(obj: O, path: string | string[], defaultValue?: T): O[keyof O] | T | undefined {\n if (!Array.isArray(path)) {\n const pathArr = path.split('.');\n path = []\n for (let i = 0; i < pathArr.length; i++) {\n if (/^.+\\[[0-9]+\\]$/.test(pathArr[i])) {\n path.push(/^.+(?=\\[[0-9]+\\]$)/.exec(pathArr[i])![0])\n path.push(`${/(?<=^.+\\[)[0-9]+(?=\\]$)/.exec(pathArr[i])![0]}`)\n } else {\n path.push(pathArr[i])\n }\n }\n }\n\n const callback = (innerObj: any, innerPath: string) => {\n if (typeof innerObj[innerPath] === 'undefined') {\n return undefined\n }\n return innerObj[innerPath]\n }\n\n let currentObj = obj\n for (let i = 0; i < path.length; i++) {\n currentObj = callback(currentObj, path[i])\n if (typeof currentObj === 'undefined') {\n return typeof defaultValue === 'undefined' ? undefined : defaultValue\n }\n }\n return currentObj as O[keyof O]\n}"],"mappings":";AAkBO,SAAS,IAAU,KAAQ,MAAyB,cAA8C;AACvG,MAAI,CAAC,MAAM,QAAQ,IAAI,GAAG;AACxB,UAAM,UAAU,KAAK,MAAM,GAAG;AAC9B,WAAO,CAAC;AACR,aAAS,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;AACvC,UAAI,iBAAiB,KAAK,QAAQ,CAAC,CAAC,GAAG;AACrC,aAAK,KAAK,qBAAqB,KAAK,QAAQ,CAAC,CAAC,EAAG,CAAC,CAAC;AACnD,aAAK,KAAK,GAAG,WAAC,2BAAwB,EAAC,KAAK,QAAQ,CAAC,CAAC,EAAG,CAAC,CAAC,EAAE;AAAA,MAC/D,OAAO;AACL,aAAK,KAAK,QAAQ,CAAC,CAAC;AAAA,MACtB;AAAA,IACF;AAAA,EACF;AAEA,QAAM,WAAW,CAAC,UAAe,cAAsB;AACrD,QAAI,OAAO,SAAS,SAAS,MAAM,aAAa;AAC9C,aAAO;AAAA,IACT;AACA,WAAO,SAAS,SAAS;AAAA,EAC3B;AAEA,MAAI,aAAa;AACjB,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,iBAAa,SAAS,YAAY,KAAK,CAAC,CAAC;AACzC,QAAI,OAAO,eAAe,aAAa;AACrC,aAAO,OAAO,iBAAiB,cAAc,SAAY;AAAA,IAC3D;AAAA,EACF;AACA,SAAO;AACT;","names":[]}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-54TK5BIS.mjs.map
@@ -70,6 +70,7 @@ export { pick } from '../object/pick.mjs';
70
70
  export { pickBy } from '../object/pickBy.mjs';
71
71
  export { invert } from '../object/invert.mjs';
72
72
  export { clone } from '../object/clone.mjs';
73
+ export { get } from '../object/get.mjs';
73
74
  export { isEqual } from '../predicate/isEqual.mjs';
74
75
  export { isNil } from '../predicate/isNil.mjs';
75
76
  export { isNotNil } from '../predicate/isNotNil.mjs';
@@ -70,6 +70,7 @@ export { pick } from '../object/pick.js';
70
70
  export { pickBy } from '../object/pickBy.js';
71
71
  export { invert } from '../object/invert.js';
72
72
  export { clone } from '../object/clone.js';
73
+ export { get } from '../object/get.js';
73
74
  export { isEqual } from '../predicate/isEqual.js';
74
75
  export { isNil } from '../predicate/isNil.js';
75
76
  export { isNotNil } from '../predicate/isNotNil.js';
@@ -56,6 +56,7 @@ __export(compat_exports, {
56
56
  flatten: () => flatten,
57
57
  flattenDeep: () => flattenDeep,
58
58
  forEachRight: () => forEachRight,
59
+ get: () => get,
59
60
  groupBy: () => groupBy,
60
61
  head: () => head,
61
62
  inRange: () => inRange,
@@ -884,6 +885,36 @@ function isPrimitive(value) {
884
885
  return value == null || typeof value !== "object" && typeof value !== "function";
885
886
  }
886
887
 
888
+ // src/object/get.ts
889
+ function get(obj, path, defaultValue) {
890
+ if (!Array.isArray(path)) {
891
+ const pathArr = path.split(".");
892
+ path = [];
893
+ for (let i = 0; i < pathArr.length; i++) {
894
+ if (/^.+\[[0-9]+\]$/.test(pathArr[i])) {
895
+ path.push(/^.+(?=\[[0-9]+\]$)/.exec(pathArr[i])[0]);
896
+ path.push(`${new RegExp("(?<=^.+\\[)[0-9]+(?=\\]$)").exec(pathArr[i])[0]}`);
897
+ } else {
898
+ path.push(pathArr[i]);
899
+ }
900
+ }
901
+ }
902
+ const callback = (innerObj, innerPath) => {
903
+ if (typeof innerObj[innerPath] === "undefined") {
904
+ return void 0;
905
+ }
906
+ return innerObj[innerPath];
907
+ };
908
+ let currentObj = obj;
909
+ for (let i = 0; i < path.length; i++) {
910
+ currentObj = callback(currentObj, path[i]);
911
+ if (typeof currentObj === "undefined") {
912
+ return typeof defaultValue === "undefined" ? void 0 : defaultValue;
913
+ }
914
+ }
915
+ return currentObj;
916
+ }
917
+
887
918
  // src/predicate/isEqual.ts
888
919
  function isEqual(a, b) {
889
920
  if (Object.is(a, b)) {
@@ -1115,6 +1146,7 @@ function zipObjectDeep(keys, values) {
1115
1146
  flatten,
1116
1147
  flattenDeep,
1117
1148
  forEachRight,
1149
+ get,
1118
1150
  groupBy,
1119
1151
  head,
1120
1152
  inRange,