-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
202 lines (173 loc) · 5.18 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* Utils
* Util functions for Mono modules.
*
* */
var Utils = module.exports = {};
/**
* findValue
* Finds a value in parent (object) using the dot notation passed in dotNot.
*
* @name findValue
* @function
* @param {Object} parent The object containing the searched value
* @param {String} dotNot Path to the value
* @return {Anything} Found value or undefined
*/
Utils.findValue = function(parent, dotNot) {
if (!dotNot || !dotNot) return undefined;
var splits = dotNot.split(".");
var value;
for (var i = 0; i < splits.length; ++i) {
value = parent[splits[i]];
if (value === undefined) return undefined;
if (typeof value === "object") parent = value;
}
return value;
};
/**
* findFunction
* Finds a function in parent (object) using the dot notation passed in dotNot.
*
* @name findFunction
* @function
* @param {Object} parent The object containing the searched function
* @param {String} dotNot Path to the function value
* @return {Function} Function that was found in the parent object
*/
Utils.findFunction = function(parent, dotNot) {
var func = Utils.findValue(parent, dotNot);
if (typeof func !== "function") {
return undefined;
}
return func;
};
/**
* flattenObject
* Converts an object to a flat one
*
* @name flattenObject
* @function
* @param {Object} obj The object that should be converted
* @return {Object} Flatten object
*/
Utils.flattenObject = function(obj) {
var result = {};
for (var key in obj) {
if (!obj.hasOwnProperty(key)) continue;
if (typeof obj[key] === 'object' && obj[key].constructor === Object) {
var flat = Utils.flattenObject(obj[key]);
for (var x in flat) {
if (!flat.hasOwnProperty(x)) {
continue;
}
result[key + '.' + x] = flat[x];
}
} else {
result[key] = obj[key];
}
}
return result;
};
/**
* unflattenObject
* Converts a flat object to an unflatten one
*
* @name unflattenObject
* @function
* @param {Object} flat The flat object that should be converted
* @return {Object} Unflatten object
*/
Utils.unflattenObject = function(flat) {
var result = {};
var parentObj = result;
var keys = Object.keys(flat);
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
var subkeys = key.split('.');
var last = subkeys.pop();
for (var ii = 0; ii < subkeys.length; ++ii) {
var subkey = subkeys[ii];
parentObj[subkey] = typeof parentObj[subkey] === 'undefined' ? {} : parentObj[subkey];
parentObj = parentObj[subkey];
}
parentObj[last] = flat[key];
parentObj = result;
}
return result;
};
/**
* cloneObject
* Clones an object
*
* @name cloneObject
* @function
* @param {Object} item Object that should be cloned
* @param {Boolean} deepClone If true, the subfields of the @item function will be cloned
* @return {Object} The cloned object
*/
Utils.cloneObject = function(item, deepClone) {
if (!deepClone) {
var c = function() {};
c.prototype = Object(item);
return new c();
}
if (!item) {
return item;
} // null, undefined values check
var types = [Number, String, Boolean];
var result;
// normalizing primitives if someone did new String('aaa'), or new Number('444');
types.forEach(function(type) {
if (item instanceof type) {
result = type(item);
}
});
if (typeof result == "undefined") {
if (Object.prototype.toString.call(item) === "[object Array]") {
result = [];
item.forEach(function(child, index, array) {
result[index] = Utils.cloneObject(child, true);
});
} else if (typeof item == "object") {
// testing that this is DOM
if (item.nodeType && typeof item.cloneNode == "function") {
result = item.cloneNode(true);
} else if (!item.prototype) { // check that this is a literal
if (item instanceof Date) {
result = new Date(item);
} else {
// it is an object literal
result = {};
for (var i in item) {
result[i] = Utils.cloneObject(item[i], true);
}
}
} else {
// depending what you would like here,
// just keep the reference, or create new object
if (false && item.constructor) {
// would not advice to do that, reason? Read below
result = new item.constructor();
} else {
result = item;
}
}
} else {
result = item;
}
}
return result;
};
/**
* slug
* Converts a string to slug
*
* @name slug
* @function
* @param {String} input The input string that should be converted to slug
* @return {String} The slug that was generated
*/
Utils.slug = function(input) {
return input.replace(/[^A-Za-z0-9-]+/g, '-').toLowerCase();
};