Skip to content

Commit 219e16a

Browse files
committed
feat(common): deepKeys util
1 parent ee350c1 commit 219e16a

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/_common.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var isDefined = angular.isDefined,
1515

1616

1717
/**
18+
* @description
1819
* get an object and return array of values
1920
* @param object
2021
* @returns {Array}
@@ -27,7 +28,6 @@ function toArray(object) {
2728
}
2829

2930
/**
30-
*
3131
* @param value
3232
* @returns {boolean}
3333
*/
@@ -36,6 +36,7 @@ function isNull(value) {
3636
}
3737

3838
/**
39+
* @description
3940
* return if object contains partial object
4041
* @param partial{object}
4142
* @param object{object}
@@ -51,6 +52,7 @@ function objectContains(partial, object) {
5152
}
5253

5354
/**
55+
* @description
5456
* search for approximate pattern in string
5557
* @param word
5658
* @param pattern
@@ -69,6 +71,7 @@ function hasApproxPattern(word, pattern) {
6971
}
7072

7173
/**
74+
* @description
7275
* return the first n element of an array,
7376
* if expression provided, is returns as long the expression return truthy
7477
* @param array
@@ -96,12 +99,40 @@ if (!String.prototype.contains) {
9699
}
97100

98101
/**
99-
*
100102
* @param num {Number}
101103
* @param decimal {Number}
102104
* @param $math
103105
* @returns {Number}
104106
*/
105107
function convertToDecimal(num, decimal, $math){
106108
return $math.round(num * $math.pow(10,decimal)) / ($math.pow(10,decimal));
109+
}
110+
111+
/**
112+
* @description
113+
* Get an object, and return an array composed of it's properties names(nested too).
114+
* @param obj {Object}
115+
* @param stack {Array}
116+
* @param parent {String}
117+
* @returns {Array}
118+
* @example
119+
* parseKeys({ a:1, b: { c:2, d: { e: 3 } } }) ==> ["a", "b.c", "b.d.e"]
120+
*/
121+
function deepKeys(obj, stack, parent) {
122+
stack = stack || [];
123+
var keys = Object.keys(obj);
124+
125+
keys.forEach(function(el) {
126+
//if it's a nested object
127+
if(isObject(obj[el]) && !isArray(obj[el])) {
128+
//concatenate the new parent if exist
129+
var p = parent ? parent + '.' + el : parent;
130+
deepKeys(obj[el], stack, p || el);
131+
} else {
132+
//create and save the key
133+
var key = parent ? parent + '.' + el : el;
134+
stack.push(key)
135+
}
136+
});
137+
return stack
107138
}

0 commit comments

Comments
 (0)