1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const vm = require('vm');
|
5 |
|
6 | const query = require('./query');
|
7 | const style = require('./style');
|
8 | const emojify = require('./emojify');
|
9 | const colorize = require('./colorize');
|
10 |
|
11 | const {
|
12 | camelize, deepClone, distinct, expand, filter, flatten, formatBytes, formatNumber, merge,
|
13 | precisionRound, project, range, remove, resolve, resolves, set, setTypes, size, unique,
|
14 | } = require('./utils');
|
15 |
|
16 | Math.$round = precisionRound;
|
17 |
|
18 | Number.$asBytes = formatBytes;
|
19 | Number.$format = formatNumber;
|
20 | Number.$range = range;
|
21 |
|
22 | Object.$expand = expand;
|
23 | Object.$filter = filter;
|
24 | Object.$flatten = flatten;
|
25 | Object.$project = project;
|
26 | Object.$query = query;
|
27 | Object.$remove = remove;
|
28 | Object.$resolve = resolve;
|
29 | Object.$resolves = resolves;
|
30 | Object.$set = set;
|
31 | Object.$setTypes = setTypes;
|
32 | Object.$size = size;
|
33 |
|
34 | //////////
|
35 |
|
36 | Object.defineProperty(Array.prototype, '$all', {
|
37 | value (predicate) {
|
38 | return this.reduce((acc, value) => acc && predicate(value), true);
|
39 | },
|
40 | enumerable: false,
|
41 | configurable: true,
|
42 | });
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | Object.defineProperty(Array.prototype, '$byId', {
|
54 | value (needle, caseInsensitive) {
|
55 | return this.byKey('id', needle, caseInsensitive);
|
56 | },
|
57 | enumerable: false,
|
58 | configurable: true,
|
59 | });
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | Object.defineProperty(Array.prototype, '$byName', {
|
71 | value (needle, caseInsensitive) {
|
72 | return this.byKey('name', needle, caseInsensitive);
|
73 | },
|
74 | enumerable: false,
|
75 | configurable: true,
|
76 | });
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | Object.defineProperty(Array.prototype, '$byKey', {
|
89 | value (key, needle, caseInsensitive) {
|
90 | for (let i = 0; this.length; i++) {
|
91 | if (typeof this[i] !== 'object') {
|
92 | return undefined;
|
93 | }
|
94 | if (!this[i][key]) {
|
95 | return undefined;
|
96 | }
|
97 | if (this[i][key] === needle) {
|
98 | return this[i];
|
99 | }
|
100 | if (caseInsensitive && this[i][key].toLowerCase() === needle.toLowerCase()) {
|
101 | return this[i];
|
102 | }
|
103 | }
|
104 | return undefined;
|
105 | },
|
106 | enumerable: false,
|
107 | configurable: true,
|
108 | });
|
109 |
|
110 | Object.defineProperty(Array.prototype, '$distinct', {
|
111 | value (selector) {
|
112 | return distinct(this, selector);
|
113 | },
|
114 | enumerable: false,
|
115 | configurable: true,
|
116 | });
|
117 |
|
118 | Object.defineProperty(Array.prototype, '$first', {
|
119 | value () {
|
120 | if (this.length) {
|
121 | return this[0];
|
122 | }
|
123 | return undefined;
|
124 | },
|
125 | enumerable: false,
|
126 | configurable: true,
|
127 | });
|
128 |
|
129 | Object.defineProperty(Array.prototype, '$last', {
|
130 | value () {
|
131 | if (this.length) {
|
132 | return this[this.length - 1];
|
133 | }
|
134 | return undefined;
|
135 | },
|
136 | enumerable: false,
|
137 | configurable: true,
|
138 | });
|
139 |
|
140 | Object.defineProperty(Array.prototype, '$none', {
|
141 | value (predicate) {
|
142 | return this.reduce((acc, value) => !acc && !predicate(value), false);
|
143 | },
|
144 | enumerable: false,
|
145 | configurable: true,
|
146 | });
|
147 |
|
148 | Object.defineProperty(Array.prototype, '$partition', {
|
149 | value (predicate) {
|
150 | return this.reduce((result, item) => {
|
151 | const [ listA, listB ] = result;
|
152 |
|
153 | if (predicate(item) === true) {
|
154 | listA.push(item);
|
155 | } else {
|
156 | listB.push(item);
|
157 | }
|
158 |
|
159 | return result;
|
160 | }, [ [], [] ]);
|
161 | },
|
162 | enumerable: false,
|
163 | configurable: true,
|
164 | });
|
165 |
|
166 | Object.defineProperty(Array.prototype, '$pick', {
|
167 | value (count, asArray) {
|
168 | const arr = this.slice();
|
169 | const picks = [];
|
170 |
|
171 | if (count === 1 && arr.length === 1) {
|
172 | return arr[0];
|
173 | } else if (count >= arr.length) {
|
174 | return arr;
|
175 | }
|
176 |
|
177 | while (picks.length < count) {
|
178 | const i = arr.$random();
|
179 | picks.push(arr[i]);
|
180 | arr.splice(i, 1);
|
181 | }
|
182 |
|
183 | if (picks.length === 1 && !asArray) {
|
184 | return picks[0];
|
185 | }
|
186 | return picks;
|
187 | },
|
188 | enumerable: false,
|
189 | configurable: true,
|
190 | });
|
191 |
|
192 | Object.defineProperty(Array.prototype, '$random', {
|
193 | value () {
|
194 | return Math.floor(Math.random() * this.length);
|
195 | },
|
196 | enumerable: false,
|
197 | configurable: true,
|
198 | });
|
199 |
|
200 | Object.defineProperty(Array.prototype, '$shuffle', {
|
201 | value () {
|
202 | let j;
|
203 | let x;
|
204 | let i;
|
205 |
|
206 | for (i = this.length; i; i--) {
|
207 | j = Math.floor(Math.random() * i);
|
208 | x = this[i - 1];
|
209 | this[i - 1] = this[j];
|
210 | this[j] = x;
|
211 | }
|
212 | return this;
|
213 | },
|
214 | enumerable: false,
|
215 | configurable: true,
|
216 | });
|
217 |
|
218 | Object.defineProperty(Array.prototype, '$some', {
|
219 | value (predicate) {
|
220 | return this.reduce((acc, value) => acc || predicate(value), false);
|
221 | },
|
222 | enumerable: false,
|
223 | configurable: true,
|
224 | });
|
225 |
|
226 | Object.defineProperty(Array.prototype, '$unique', {
|
227 | value () {
|
228 | return unique(this);
|
229 | },
|
230 | enumerable: false,
|
231 | configurable: true,
|
232 | });
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | Object.defineProperty(String.prototype, '$colorize', {
|
241 | value (colorName) {
|
242 | return colorize(colorName, this);
|
243 | },
|
244 | enumerable: false,
|
245 | configurable: true,
|
246 | });
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | Object.defineProperty(String.prototype, '$rgb', {
|
253 | value (rgbArray) {
|
254 | return colorize.rgb(rgbArray, this);
|
255 | },
|
256 | enumerable: false,
|
257 | configurable: true,
|
258 | });
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | Object.defineProperty(String.prototype, '$style', {
|
266 | value (a, b) {
|
267 | return style(this, a, b);
|
268 | },
|
269 | enumerable: false,
|
270 | configurable: true,
|
271 | });
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | Object.defineProperty(String.prototype, '$stripWhitespace', {
|
277 | value () {
|
278 | return this.replace(/\s/g, '');
|
279 | },
|
280 | enumerable: false,
|
281 | configurable: true,
|
282 | });
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | Object.defineProperty(String.prototype, '$emojify', {
|
288 | value () {
|
289 | return emojify(this);
|
290 | },
|
291 | enumerable: false,
|
292 | configurable: true,
|
293 | });
|
294 |
|
295 |
|
296 |
|
297 |
|
298 | Object.defineProperty(String.prototype, '$capitalize', {
|
299 | value () {
|
300 | return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
|
301 | },
|
302 | enumerable: false,
|
303 | configurable: true,
|
304 | });
|
305 |
|
306 |
|
307 |
|
308 |
|
309 | Object.defineProperty(String.prototype, '$camelize', {
|
310 | value () {
|
311 | return camelize(this);
|
312 | },
|
313 | enumerable: false,
|
314 | configurable: true,
|
315 | });
|
316 |
|
317 |
|
318 |
|
319 |
|
320 | JSON.$read = function (path) {
|
321 | return vm.runInNewContext(`JSON.parse(fs.readFileSync('${ path }'));`, { fs });
|
322 | };
|
323 |
|
324 | JSON.$write = function (path, object) {
|
325 | return fs.writeFileSync(path, JSON.stringify(object, null, 2));
|
326 | };
|
327 |
|
328 | Object.$clone = function (object, deep = false) {
|
329 | if (deep) {
|
330 | return deepClone(object);
|
331 | }
|
332 | return JSON.parse(JSON.stringify(object));
|
333 | };
|
334 |
|
335 | Object.$deepClone = function (object) {
|
336 | return deepClone(object);
|
337 | };
|
338 |
|
339 | Object.$merge = function (objectA, objectB, createNew = false) {
|
340 | return merge(objectA, objectB, createNew);
|
341 | };
|
342 |
|
343 |
|
344 |
|
345 |
|
346 | Object.$private = function (body, key, value) {
|
347 | if (typeof body !== 'object') {
|
348 | return;
|
349 | }
|
350 | if (body.hasOwnProperty(key)) {
|
351 | body[key] = value;
|
352 | } else {
|
353 | Object.defineProperty(body, key, {
|
354 | value,
|
355 | enumerable: false,
|
356 | writable: true,
|
357 | configurable: true,
|
358 | });
|
359 | }
|
360 | };
|