@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 +14 -8
- package/esm/object/get.js +14 -8
- package/package.json +1 -1
package/cjs/object/get.js
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
13
|
-
if (
|
|
14
|
-
|
|
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
|
-
|
|
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
|
-
|
|
11
|
-
if (
|
|
12
|
-
|
|
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