/
Obj.js
394 lines (309 loc) · 6.41 KB
/
Obj.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/**
* Created by igor on 30.05.16.
* @module obj
* @description Function for object
*/
/**
* Return object keys
* @param {Object} obj
*/
const keys = obj => Object.keys(obj);
/**
* Check v be no undefined or null
* @param {*}v
*/
let isSet = (v) => typeof v !== 'undefined' && v !== null;
/**
* Check is object
* @param {Object}obj
* @returns {boolean}
*/
const isObj = (obj) => Object.prototype.toString.call(obj) === '[object Object]';
/**
* ForEach for Object fn(key, val) and sort keys by fnSort
* @param {Object}obj
* @param {Function}fn
* @param {Function}fnSort
*/
const each = (obj, fn, fnSort) => {
if (isObj(obj)) {
fn = typeof fn !== 'function' ?
function () {
} : fn;
let k = keys(obj);
if (fnSort) {
k = k.sort(fnSort);
}
for (let i = 0, len = k.length; i < len; ++i)
fn(k[i], obj[k[i]]);
}
};
/**
* Return object. When keys is specified property, value is object from array objects
* @param {Array}arr
* @param {String}prop
* @param {Function}fnIterProp
* @returns {{}}
*/
const arrToObjByKey = (arr, prop, fnIterProp) => {
var obj = {};
if (!Array.isArray(arr) || !prop)
return obj;
for (let i = 0; i < arr.length; ++i) {
let el = arr[i];
if (el[prop]) {
let key = el[prop];
if (typeof fnIterProp === 'function')
key = fnIterProp(key, el);
obj[key] = el;
}
}
return obj;
};
/**
* Get properties from obj by keys
* @param {object} obj
* @param {*} ks [{keyOld : keyNew,...}]
* @returns {{}}
*/
const keysChange = (obj, ks) => {
let r = {},
arKey,
isNewkey = false;
if (isObj(ks)) {
arKey = keys(ks);
isNewkey = true;
} else
arKey = ks;
if (obj && Array.isArray(arKey)) {
for (let i = 0; i < arKey.length; ++i) {
let k = arKey[i];
if (obj[k] || obj[k] === false)
r[isNewkey ? ks[k] : k] = obj[k];
}
}
return r;
};
/**
* Return new object when keys be sort
* @param {Object}obj
* @param {Boolean}down
* @returns {{}}
*/
const sort = (obj, down) => {
down = down || false;
let r = {},
k = keys(obj).sort();
if (down)
k.reverse();
for (let i = 0; i < keys.length; i++)
r[k[i]] = obj[k[i]];
return r;
};
/**
* Increment object property to the specified value
* @param {Object} obj
* @param {String} prop
* @param {Number} byVal
* */
const propInc = (obj, prop, byVal) => {
let val = byVal || 1;
val = isNaN(val) ? 1 : Number(val);
if (!isObj(obj))
return obj;
if (obj[prop] === undefined)
obj[prop] = val;
else {
if (isNaN(obj[prop]))
return obj;
else
obj[prop] = Number(obj[prop]);
obj[prop] += val;
}
return obj;
};
/**
* Check is set object. If not, it creates it with the specified value
* @param {Object} ob
* @param {String} prop
* @param {Object} def
*/
const beInObj = (ob, prop, def) => {
if (def === undefined) def = {};
if (ob && !ob[prop]) ob[prop] = def;
return ob;
};
/**
* Create path in object
* @param {Object|Array|String}obj
* @param {Array|String|null|undefined}path
* @param {*}def
*/
const pathCreate = function (obj, path, def) {
if (!isSet(path)) {
//noinspection JSValidateTypes
path = obj;
obj = {};
}
if (typeof path === 'string')
path = path.split('.');
let b = obj;
let len = path.length;
if (len === 1) { //noinspection JSCheckFunctionSignatures
beInObj(b, path[0]);
} else if (len > 1) {
len--;
for (let i = 0; i < len; i++) {
//noinspection JSCheckFunctionSignatures
beInObj(b, path[i]);
b = b[path[i]];
}
beInObj(b, path[len], def);
}
return obj;
};
/**
* Check exist path in object
* @param {Object}obj
* @param {*} path
* @returns {Boolean}
*/
const isPathExist = (obj, path) => {
let arPath = [];
if (typeof path === 'string')
arPath = path.split('.');
else if (Array.isArray(path))
arPath = path;
if (!arPath.length) return true;
let p = arPath.shift();
return obj.hasOwnProperty(p) || obj[p] !== undefined ? isPathExist(obj[p], arPath) : false;
};
/**
* Get element by path in object
* @param {Object}obj
* @param {String}path
* @returns {*}
*/
const pathVal = (obj, path) => {
if (!isSet(obj) || !isObj(obj))
return undefined;
let r = obj;
if (typeof path === 'string') { //noinspection JSValidateTypes
path = path.split('.');
}
for (let i = 0; i < path.length; ++i) {
let next = path[i];
if (r[next])
r = r[next];
else
return undefined;
}
return r;
};
/**
* Serialize object to url params
* @param {Object} obj
* @returns {string}
*/
const urlParams = (obj) => {
let arr = [];
//noinspection JSCheckFunctionSignatures
each(obj, (key, val) => arr.push(encodeURIComponent(key) + '=' + encodeURIComponent(val)));
return arr.join('&');
};
/**
* Properties in add object adding to obj or if properties not exits create him
* @param {Object}obj
* @param {Object}add
* @returns {*}
*/
const ext = (obj, add) => {
let j = (obj, op) => {
//noinspection JSCheckFunctionSignatures
each(op, (k, val) => {
if (isObj(val))
if (!obj[k])
obj[k] = val;
else
j(obj[k], val);
else
obj[k] = val;
});
};
obj = obj || {};
if (add)
j(obj, add);
return obj;
};
/**
* Get property from array objects
* @param {Array}arObj
* @param {String}prop
* @returns {Array}
*/
const getPropToArr = (arObj, prop) => {
let r = [];
if (!Array.isArray(arObj)) arObj = [arObj];
for (let i = 0; i < arObj.length; ++i)
if (isSet(arObj[i][prop]))
r.push(arObj[i][prop]);
return r;
};
/**
* Collect object property to array
* @param {Object}obj
* @returns {Array}
*/
const propToArr = (obj) => {
let arr = [];
if (!isObj(obj))
return arr;
//noinspection JSCheckFunctionSignatures
each(obj, (key) => arr.push(obj[key]));
return arr;
};
/**
* if o is object return true else false
* @param {Object}obj
* @returns {boolean}
*/
const isEmpty = (obj) => !isObj(obj) || !keys(obj).length;
/**
* If the object property exists, its rounded
* @param {Object}ob
* @param {String}prop
* @param {Number}round
*/
const beRound = (ob, prop, round) => {
if (ob[prop]) ob[prop] = isNaN(ob[prop]) ? 0 : Number(parseFloat(Number(ob[prop])).toFixed(round));
return ob;
};
/**
* Apply function iter for each properties.
* Use map function.
*
* @param obj
* @param iter
*/
const map = (obj, iter) => keys(obj).map(prop => iter(prop, obj[prop]));
export default {
keys,
beRound,
isEmpty,
each,
propToArr,
getPropToArr,
isSet,
isObj,
ext,
urlParams,
pathVal,
isPathExist,
pathCreate,
beInObj,
propInc,
sort,
keysChange,
arrToObjByKey,
map
};