-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
276 lines (240 loc) · 7.36 KB
/
index.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
/**
* @description Iterate over each key of an object
* @param obj
* @param callback
*/
const eachKey = (obj, callback) => {
Object.keys(obj).forEach((key, index) => callback(obj[key], key, index));
};
/**
* @description Augment object with properties from other objects
* @returns {object}
*/
const extend = function() {
let obj = arguments[0];
let enhancedObj = Object.assign(obj, {});
let extenders = [];
eachKey(arguments, (argument, key, index) => {
if ( index > 0 ) {
extenders.push(argument);
}
});
extenders.forEach((extender) => {
Object.assign(enhancedObj, extender);
});
return enhancedObj;
};
/**
* @description Is function
* @param val
* @returns {boolean}
*/
const isFunc = val => typeof val === 'function';
/**
* @description Check if value is of type 'object'
* @param val
* @returns {boolean}
*/
const isObj = val => typeof val === 'object' && !isArr(val) && !isNull(val);
/**
* @description Check if value is of type 'null'
* @param val
* @returns {boolean}
*/
const isNull = val => val === null;
/**
* @description Check if value is of type 'array'
* @param val
* @returns {boolean}
*/
const isArr = val => Array.isArray(val);
/**
* @description Check if value is of type 'string'
* @param val
* @returns {boolean}
*/
const isStr = val => typeof val === 'string';
/**
* @description Check if value is of type 'undefined'
* @param val
* @returns {boolean}
*/
const isUndef = val => typeof val === 'undefined';
/**
* @description Extract nexted prop
* @param obj
* @param key
* @returns {*}
*/
const extractNestedProp = (obj, key) => {
const keys = key.split('.');
const keysLength = keys.length - 1;
let keysIndex = 0;
let isValidKey = true;
let targetObj = Object.assign({}, obj);
let targetProp;
let nextTarget;
if (keys.length > 0) {
while (isValidKey) {
nextTarget = targetObj[keys[keysIndex]];
if (keysIndex === keysLength) {
targetProp = !isUndef(nextTarget) && !isNull(nextTarget) ? nextTarget : undefined;
break;
}
if (!isObj(nextTarget)) {
isValidKey = false;
break;
}
targetObj = nextTarget;
keysIndex++;
}
}
return targetProp;
};
/**
* @description Sort by
* @param items
* @param key
* @param propType
* @param direction
*/
const sortBy = (items, key, propType, direction) => {
return items.sort((a, b) => {
let aVal = isStr(key) ? extractNestedProp(a, key) : '';
let bVal = isStr(key) ? extractNestedProp(b, key) : '';
if (isUndef(aVal) || isNull(aVal)) {
return direction === 'asc' ? -1 : 1;
}
if (isUndef(bVal) || isNull(bVal)) {
return direction === 'asc' ? 1 : -1;
}
if (propType === 'string' || propType === 'email') {
if (aVal.toLowerCase() > bVal.toLowerCase()) {
return direction === 'asc' ? 1 : -1;
}
if (aVal.toLowerCase() < bVal.toLowerCase()) {
return direction === 'asc' ? -1 : 1;
}
return 0;
} else if (propType === 'number' || propType === 'integer' || propType === 'float') {
if (aVal > bVal) {
return direction === 'asc' ? 1 : -1;
}
if (aVal < bVal) {
return direction === 'asc' ? -1 : 1;
}
return 0;
} else if (propType === 'date') {
return direction === 'asc'
? new Date(aVal) - new Date(bVal)
: new Date(bVal) - new Date(aVal);
} else if (propType === 'combo') {
aVal = key.map(key => extractNestedProp(a, key)).join(' ');
bVal = key.map(key => extractNestedProp(b, key)).join(' ');
if (isUndef(aVal) || isNull(aVal)) {
return direction === 'asc' ? -1 : 1;
}
if (isUndef(bVal) || isNull(bVal)) {
return direction === 'asc' ? 1 : -1;
}
if (aVal.toLowerCase() > bVal.toLowerCase()) {
return direction === 'asc' ? 1 : -1;
}
if (aVal.toLowerCase() < bVal.toLowerCase()) {
return direction === 'asc' ? -1 : 1;
}
return 0;
}
});
};
/**
* @description Filter by unique
* @param items
* @param key
* @returns {*}
*/
const filterByUnique = (items, key) => items.reduce((accumulator, item) => {
const itemProp = extractNestedProp(item, key);
const isDuplicate = accumulator.filter(filteredItem => {
const prop = extractNestedProp(filteredItem, key);
return prop === itemProp;
}).length > 0;
if (isDuplicate) {
return accumulator;
}
const modifiedItem = extend({}, item);
accumulator.push(modifiedItem);
return accumulator;
}, []);
/**
* @description Filter by duplicate
* @param items
* @param key
* @param duplicateLength
* @returns {*}
*/
const filterByDuplicate = (items, key, duplicateLength = 2) => items.filter(item => {
const itemProp = extractNestedProp(item, key);
const duplicatesCount = duplicateLength - 1;
return items.filter(innerItem => {
const prop = extractNestedProp(innerItem, key);
return prop === itemProp;
}).length > duplicatesCount;
});
/**
* @description Shape
* @param {array} items
* @returns {*}
*/
const shape = items => {
let shapeItems = [...items];
return {
fetch: () => shapeItems,
fetchIndex(index) {
return shapeItems[index];
},
filterByUnique(key) {
shapeItems = filterByUnique(shapeItems, key);
return this;
},
filterByDuplicate(key) {
const length = 2;
shapeItems = filterByDuplicate(shapeItems, key, length);
return this;
},
filterByProp(key, value) {
shapeItems = shapeItems.filter(item => extractNestedProp(item, key) === value);
return this;
},
sortBy(key, type = 'string', direction = 'asc') {
shapeItems = sortBy(shapeItems, key, type, direction);
return this;
},
reduceTo(key, augmenter) {
const hasAugmenter = isFunc(augmenter);
shapeItems = shapeItems.reduce((accumulator, item) => {
const prop = extractNestedProp(item, key);
let augmentObj;
if (isFunc(augmenter)) {
augmentObj = augmenter(item, prop, key);
}
if (isArr(prop)) {
let nextProp = prop;
if (isObj(prop[0]) && hasAugmenter) {
nextProp = prop.map(propItem => extend({}, propItem, isObj(augmentObj) ? augmentObj : {}))
}
return [...accumulator, ...nextProp];
} else if (!isUndef(prop) && !isNull(prop)) {
let nextProp = prop;
if (isObj(prop) && hasAugmenter) {
nextProp = extend({}, prop, isObj(augmentObj) ? augmentObj : {});
}
return [...accumulator, nextProp];
}
return accumulator;
}, []);
return this;
}
};
};
module.exports = { shape };