/
util.ts
189 lines (141 loc) · 3.91 KB
/
util.ts
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
function extractPropKey(key: string): string[] {
return key.split('.');
}
function _parseArgs(args: string) {
if (typeof args !== 'string') return args;
const arr = args.split(' ');
const result = {};
for (let i = 0, len = arr.length; i < len; i++) {
const key = arr[i];
switch (key[0]) {
case '+':
result[key.slice(1)] = 1;
break;
case '-':
result[key.slice(1)] = -1;
break;
default:
result[key] = 1;
}
}
return result;
}
export function shuffle(array) {
if (!Array.isArray(array)) throw new TypeError('array must be an Array!');
const $array = array.slice();
const { length } = $array;
const { random, floor } = Math;
for (let i = 0; i < length; i++) {
// @see https://github.com/lodash/lodash/blob/4.17.10/lodash.js#L6718
// @see https://github.com/lodash/lodash/blob/4.17.10/lodash.js#L3884
const rand = i + floor(random() * (length - i));
const temp = $array[i];
$array[i] = $array[rand];
$array[rand] = temp;
}
return $array;
}
export function getProp(obj, key) {
if (typeof obj !== 'object') throw new TypeError('obj must be an object!');
if (!key) throw new TypeError('key is required!');
if (!key.includes('.')) {
return obj[key];
}
const token = extractPropKey(key);
let result = obj;
const len = token.length;
for (let i = 0; i < len; i++) {
result = result[token[i]];
}
return result;
}
export function setProp(obj, key, value) {
if (typeof obj !== 'object') throw new TypeError('obj must be an object!');
if (!key) throw new TypeError('key is required!');
if (!key.includes('.')) {
obj[key] = value;
return;
}
const token = extractPropKey(key);
const lastKey = token.pop();
const len = token.length;
let cursor = obj;
for (let i = 0; i < len; i++) {
const name = token[i];
cursor[name] = cursor[name] || {};
cursor = cursor[name];
}
cursor[lastKey] = value;
}
export function delProp(obj, key) {
if (typeof obj !== 'object') throw new TypeError('obj must be an object!');
if (!key) throw new TypeError('key is required!');
if (!key.includes('.')) {
delete obj[key];
return;
}
const token = extractPropKey(key);
const lastKey = token.pop();
const len = token.length;
let cursor = obj;
for (let i = 0; i < len; i++) {
const name = token[i];
if (cursor[name]) {
cursor = cursor[name];
} else {
return;
}
}
delete cursor[lastKey];
}
export function setGetter(obj, key, fn) {
if (typeof obj !== 'object') throw new TypeError('obj must be an object!');
if (!key) throw new TypeError('key is required!');
if (typeof fn !== 'function') throw new TypeError('fn must be a function!');
if (!key.includes('.')) {
obj.__defineGetter__(key, fn);
return;
}
const token = extractPropKey(key);
const lastKey = token.pop();
const len = token.length;
let cursor = obj;
for (let i = 0; i < len; i++) {
const name = token[i];
cursor[name] = cursor[name] || {};
cursor = cursor[name];
}
cursor.__defineGetter__(lastKey, fn);
}
export function arr2obj(arr, value) {
if (!Array.isArray(arr)) throw new TypeError('arr must be an array!');
const obj = {};
let i = arr.length;
while (i--) {
obj[arr[i]] = value;
}
return obj;
}
export function reverse(arr) {
if (!Array.isArray(arr)) throw new TypeError('arr must be an array!');
const len = arr.length;
if (!len) return arr;
for (let left = 0, right = len - 1; left < right; left++, right--) {
const tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
}
return arr;
}
export function parseArgs(orderby: string | object, order?: string | number | object) {
let result;
if (order) {
result = {};
result[orderby as string] = order;
} else if (typeof orderby === 'string') {
result = _parseArgs(orderby);
} else {
result = orderby;
}
return result;
}