1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const path_1 = require("path");
|
4 | let objUid = 0;
|
5 | let objUidMap = new WeakMap();
|
6 |
|
7 | function getType(key) {
|
8 | const t = Object.prototype.toString.call(key);
|
9 | return t.slice(8, -1).toLowerCase();
|
10 | }
|
11 | exports.getType = getType;
|
12 |
|
13 | function hashAny(key) {
|
14 | switch (getType(key)) {
|
15 | case 'undefined':
|
16 | case 'null':
|
17 | case 'boolean':
|
18 | case 'number':
|
19 | case 'regexp':
|
20 | return key + '';
|
21 | case 'date':
|
22 | return '📅' + key.getTime();
|
23 | case 'string':
|
24 | return '📝' + key;
|
25 | case 'array':
|
26 | return '🔗' + key.map(k => hashAny(k)).join('⁞');
|
27 | case 'object':
|
28 | return key.constructor.name + JSON.stringify(key, (k, v) => {
|
29 | if (!k)
|
30 | return v;
|
31 | return hashAny(v);
|
32 | });
|
33 | default:
|
34 | let uid = objUidMap.get(key);
|
35 | if (!uid) {
|
36 | uid = ++objUid;
|
37 | objUidMap.set(key, uid);
|
38 | }
|
39 | return '⭕️' + uid;
|
40 | }
|
41 | }
|
42 | exports.hashAny = hashAny;
|
43 | exports.sleep = (ms) => {
|
44 | return new Promise(res => setTimeout(res, ms));
|
45 | };
|
46 | function throttle(cb, ms) {
|
47 | let timer;
|
48 | let newCb = (...args) => {
|
49 | timer && clearTimeout(timer);
|
50 | timer = setTimeout(cb, ms, ...args);
|
51 | };
|
52 | return newCb;
|
53 | }
|
54 | exports.throttle = throttle;
|
55 | exports.Is = {
|
56 | defed(v) {
|
57 | return typeof v !== 'undefined' && v !== null;
|
58 | },
|
59 | str(v) {
|
60 | return typeof v === 'string';
|
61 | },
|
62 | bool(v) {
|
63 | return typeof v === 'boolean';
|
64 | },
|
65 | fn(v) {
|
66 | return typeof v === 'function';
|
67 | },
|
68 | obj(v) {
|
69 | return v && typeof v === 'object';
|
70 | },
|
71 | num(v) {
|
72 | return typeof v === 'number';
|
73 | }
|
74 | };
|
75 | function defaults(...args) {
|
76 | let [val, ...defaultVals] = args;
|
77 | if (exports.Is.defed(val))
|
78 | return val;
|
79 | if (defaultVals.length === 0) {
|
80 | return val;
|
81 | }
|
82 | return defaults(...defaultVals);
|
83 | }
|
84 | exports.defaults = defaults;
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function namespacify(obj, ns = '', sep = ':') {
|
106 | for (const key in obj) {
|
107 | const val = obj[key] || key;
|
108 | if (exports.Is.str(val)) {
|
109 | obj[key] = `${ns}${ns && sep}${val}`;
|
110 | }
|
111 | else if (exports.Is.obj(val)) {
|
112 | namespacify(val, `${ns}${ns && sep}${key}`, sep);
|
113 | }
|
114 | }
|
115 | return obj;
|
116 | }
|
117 | exports.namespacify = namespacify;
|
118 | exports.DefaultLogFile = path_1.join(process.cwd(), 'foy.log');
|
119 |
|
\ | No newline at end of file |