-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcommon_1_0.js
228 lines (213 loc) · 7.56 KB
/
common_1_0.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
function common_1_0_module() {
var unit = {};
function addRange(src, dst) {
var _5_col, _5_it, _5_length, item;
_5_it = 0;
_5_col = src;
_5_length = _5_col.length;
while (true) {
if (_5_it < _5_length) {
item = _5_col[_5_it];
dst.push(item)
_5_it++;
} else {
break;
}
}
}
function comparerAsc(property, left, right) {
var leftValue, rightValue;
leftValue = left[property]
rightValue = right[property]
if (( typeof leftValue === "string") && ( typeof rightValue === "string")) {
leftValue = leftValue.toLowerCase()
rightValue = rightValue.toLowerCase()
}
if (leftValue < rightValue) {
return -1
} else {
if (leftValue > rightValue) {
return 1
} else {
return 0
}
}
}
function comparerDesc(property, left, right) {
var comp;
comp = comparerAsc(property, left, right)
return -1 * comp
}
function deepClone(obj) {
var visited;
visited = new Set()
return deepCloneCore(visited, obj)
}
function deepCloneCore(visited, obj) {
var _32_col, _32_it, _32_length, _43_col, _43_it, _43_keys, _43_length, _sw_23, array, copy, item, key, value;
if ((obj === undefined) || (obj === null)) {
return undefined
} else {
_sw_23 = typeof obj;
if (_sw_23 === "number") {
return obj
} else {
if (_sw_23 === "boolean") {
return obj
} else {
if (_sw_23 === "string") {
return obj
} else {
if (_sw_23 === "bigint") {
return obj
} else {
if (_sw_23 === "function") {
return obj
} else {
if (_sw_23 === "symbol") {
return obj
} else {
if ((obj instanceof RegExp) || (obj instanceof Date)) {
return obj
} else {
if (visited.has(obj)) {
throw new Error(
"deepClone: cycle detected"
)
} else {
visited.add(obj)
if (Array.isArray(obj)) {
array = []
_32_it = 0;
_32_col = obj;
_32_length = _32_col.length;
while (true) {
if (_32_it < _32_length) {
item = _32_col[_32_it];
array.push(deepCloneCore(visited, item))
_32_it++;
} else {
break;
}
}
return array
} else {
copy = {}
_43_it = 0;
_43_col = obj;
_43_keys = Object.keys(_43_col);
_43_length = _43_keys.length;
while (true) {
if (_43_it < _43_length) {
key = _43_keys[_43_it];
value = _43_col[key];
copy[key] = deepCloneCore(visited, value)
_43_it++;
} else {
break;
}
}
return copy
}
}
}
}
}
}
}
}
}
}
}
function filterBy(array, property, value) {
array = array || []
return array.filter(
function (item) {
return item[property] === value
}
)
}
function findBy(array, property, value) {
var _6_col, _6_it, _6_length, item;
array = array || []
_6_it = 0;
_6_col = array;
_6_length = _6_col.length;
while (true) {
if (_6_it < _6_length) {
item = _6_col[_6_it];
if (item[property] === value) {
return item
} else {
_6_it++;
}
} else {
return undefined
}
}
}
function last(array) {
var length;
if (array) {
length = array.length
if (length === 0) {
return undefined
} else {
return array[length - 1]
}
} else {
return undefined
}
}
function remove(array, item) {
var index;
index = array.indexOf(item)
if (index === -1) {
} else {
array.splice(index, 1)
}
}
function sortBy(array, property, direction) {
var sorter;
if (array) {
direction = direction || "asc"
direction = direction.toLowerCase()
if (direction === "asc") {
sorter = comparerAsc
} else {
sorter = comparerDesc
}
array.sort(
function (left, right) {
return sorter(
property,
left,
right
)
}
)
}
}
function split(text, separator) {
var notEmpty, parts;
if (text) {
parts = text.split(separator)
notEmpty = function (part) {
return part.length > 0
}
return parts.filter(notEmpty)
} else {
return []
}
}
unit.addRange = addRange;
unit.deepClone = deepClone;
unit.filterBy = filterBy;
unit.findBy = findBy;
unit.last = last;
unit.remove = remove;
unit.sortBy = sortBy;
unit.split = split;
return unit;
}
module.exports = common_1_0_module