@mongez/reinforcements 2.0.8 → 2.0.9

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/cjs/object/get.js CHANGED
@@ -1,7 +1,16 @@
1
1
  "use strict";
2
2
 
3
- var flatten = require("./flatten.js");
4
-
3
+ function getValue(object, keyChain, defaultValue) {
4
+ // split the key chain into array by dot
5
+ // then loop through the array and get the value of each key until the last key
6
+ // then return the value of the last key
7
+ return keyChain.split(".").reduce(function (acc, key) {
8
+ if (acc === undefined) {
9
+ return defaultValue;
10
+ }
11
+ return acc.hasOwnProperty(key) ? acc[key] : defaultValue;
12
+ }, object);
13
+ }
5
14
  /**
6
15
  * Get the value of the given key
7
16
  */
@@ -9,12 +18,9 @@ function get(object, key, defaultValue) {
9
18
  if (defaultValue === void 0) {
10
19
  defaultValue = null;
11
20
  }
12
- var flattenObject = flatten(object, ".", true);
13
- if (!flattenObject) return defaultValue;
14
- if (flattenObject.hasOwnProperty(key)) {
15
- return flattenObject[key];
16
- }
17
- return defaultValue;
21
+ if (!object) return defaultValue;
22
+ if (object[key]) return object[key];
23
+ return getValue(object, key, defaultValue);
18
24
  }
19
25
 
20
26
  module.exports = get;
package/esm/object/get.js CHANGED
@@ -1,5 +1,14 @@
1
- import flatten from "./flatten.js";
2
-
1
+ function getValue(object, keyChain, defaultValue) {
2
+ // split the key chain into array by dot
3
+ // then loop through the array and get the value of each key until the last key
4
+ // then return the value of the last key
5
+ return keyChain.split(".").reduce(function (acc, key) {
6
+ if (acc === undefined) {
7
+ return defaultValue;
8
+ }
9
+ return acc.hasOwnProperty(key) ? acc[key] : defaultValue;
10
+ }, object);
11
+ }
3
12
  /**
4
13
  * Get the value of the given key
5
14
  */
@@ -7,12 +16,9 @@ function get(object, key, defaultValue) {
7
16
  if (defaultValue === void 0) {
8
17
  defaultValue = null;
9
18
  }
10
- var flattenObject = flatten(object, ".", true);
11
- if (!flattenObject) return defaultValue;
12
- if (flattenObject.hasOwnProperty(key)) {
13
- return flattenObject[key];
14
- }
15
- return defaultValue;
19
+ if (!object) return defaultValue;
20
+ if (object[key]) return object[key];
21
+ return getValue(object, key, defaultValue);
16
22
  }
17
23
 
18
24
  export { get as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mongez/reinforcements",
3
- "version": "2.0.8",
3
+ "version": "2.0.9",
4
4
  "description": "A lightweight package to give a massive reinforcements to variant types of data in Nodejs/Javascript",
5
5
  "main": "./cjs/index.js",
6
6
  "dependencies": {