-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
161 lines (141 loc) · 3.91 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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.merge = factory();
}
}(this, function() {
function isObject(a) {
return (typeof a === 'object') && !Array.isArray(a);
}
function isArray(a) {
return Array.isArray(a);
}
function clone(o) {
var result;
try {
result = JSON.parse(JSON.stringify(o));
} catch (e) {}
return result || {};
}
function merge(_target, _src) {
var target = clone(_target);
var src = clone(_src);
var array = Array.isArray(src);
var dst = array && [] || {};
if (array) {
target = target || [];
dst = dst.concat(target);
src.forEach(function(e, i) {
if (typeof dst[i] === 'undefined') {
dst[i] = e;
} else if (typeof e === 'object' && e) {
dst[i] = merge(target[i], e);
} else {
if (target.indexOf(e) === -1) {
dst.push(e);
}
}
});
dst = dst.filter(function(elem) { return !!elem; });
} else {
if (target && typeof target === 'object') {
Object.keys(target).forEach(function (key) {
dst[key] = target[key];
})
}
Object.keys(src).forEach(function (key) {
if (typeof src[key] !== 'object' || !src[key]) {
dst[key] = src[key];
}
else {
if (!target) target = {};
if (!target[key]) {
dst[key] = src[key];
} else {
dst[key] = merge(target[key], src[key]);
}
}
});
}
return dst;
}
function slice(_obj, startIndex, amount) {
var obj = clone(_obj);
var totalIndex = 0;
var sliced = 0;
var result = {};
/**
* Handle the .modules arrays specifically
*/
function deepSliceRecursivelyArray(subObjectArray, subResultArray) {
subObjectArray.forEach(function(arrayItem, i) {
/**
* Only do counting on
* non-higher-level modules
*/
if (!isArray((subObjectArray[i].options || {}).modules)) {
/**
* Only start adding if
* we've reached the start index
*/
if (totalIndex++ >= startIndex) {
if (sliced++ < amount) {
subResultArray[i] = {};
deepSliceRecursivelyObject(arrayItem, subResultArray[i]);
}
}
} else if (sliced < amount) {
subResultArray[i] = {};
deepSliceRecursivelyObject(arrayItem, subResultArray[i]);
}
});
}
/**
* Recursively slice an object
*/
function deepSliceRecursivelyObject(subObject, subResult) {
for (var property in subObject) {
if (!subObject[property]) {
continue;
}
if (isObject(subObject[property]) && !isArray(subObject[property])) {
subResult[property] = {};
deepSliceRecursivelyObject(subObject[property], subResult[property])
} else if (isArray(subObject[property]) && property === 'modules') {
subResult[property] = [];
deepSliceRecursivelyArray(subObject[property], subResult[property])
} else if (isArray(subObject[property])) {
subResult[property] = subObject[property];
} else {
subResult[property] = subObject[property];
}
}
}
deepSliceRecursivelyObject(obj, result);
return result;
}
function append(obj, list) {
var result = clone(obj);
if (
!result ||
!result.app ||
!result.app.options ||
!result.app.options.modules
) {
return result;
}
if (!list || !list.length) {
return result;
}
result.app.options.modules = result.app.options.modules.concat(clone(list));
return result;
}
return {
slice: slice,
merge: merge,
append: append
};
}));