1 |
|
2 | 'use strict';
|
3 | const tinydate = require('tinydate');
|
4 |
|
5 | const $ = exports
|
6 |
|
7 |
|
8 | $.isObject = val => Boolean(val) && (val.constructor === Object);
|
9 |
|
10 | $.isEmptyObj = val => $.isObject(val) && !Object.keys(val).length;
|
11 |
|
12 | $.toArray = val => (val === null || val === undefined) ? [] : Array.isArray(val) ? val : [val];
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | $.formatTime = arr => {
|
20 | let unit = 'ms';
|
21 | let num = Math.round(arr[1] / 1000000);
|
22 | if (arr[0] > 0) {
|
23 | unit = 's';
|
24 | num = (arr[0] + num / 1000).toFixed(2);
|
25 | }
|
26 | return `${num}${unit}`;
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | $.getTime = tinydate('[{HH}:{mm}:{ss}]');
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | $.valUniq = (val, arr) => {
|
42 | let n = 0;
|
43 | let v = val;
|
44 | while (arr.indexOf(v) !== -1) {
|
45 | n++;
|
46 | v = val.concat(n);
|
47 | }
|
48 | return v;
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | $.getUniques = arr => {
|
57 | const len = arr.length;
|
58 | const res = [];
|
59 | let i = 0;
|
60 |
|
61 | for (; i < len; i++) {
|
62 | const curr = arr[i];
|
63 | if (res.indexOf(curr) === -1) {
|
64 | res.push(curr);
|
65 | }
|
66 | }
|
67 |
|
68 | return res;
|
69 | }
|
70 |
|
71 | function flat(arr, res) {
|
72 | let i = 0;
|
73 | const len = arr.length;
|
74 | for (; i < len; i++) {
|
75 | const cur = arr[i];
|
76 | Array.isArray(cur) ? flat(cur, res) : res.push(cur);
|
77 | }
|
78 | return res;
|
79 | }
|
80 |
|
81 | $.flatten = arr => flat(arr, []);
|