1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 |
|
7 | const _null = Symbol(null),
|
8 | __GLOBAL = new Function("try { return this === global; } catch (e) { return false; }")() ? global : window,
|
9 | { typeToInt, intToType } = require('./DataTypes');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function dot (keys = [], obj = {}, options = {}) {
|
20 | let opts = Object.assign({
|
21 | autoCreate: true,
|
22 | value: false,
|
23 | unset: false,
|
24 | defined: false
|
25 | }, options),
|
26 | _apply = function (keys, obj, value) {
|
27 | if (keys.length == 1 && (value || opts.unset)) {
|
28 | return opts.unset ? delete obj[keys[0]] : obj[keys[0]] = value.apply(this, [obj[keys[0]] || _null]);
|
29 | } else if (!keys[0]) {
|
30 | return obj;
|
31 | } else if (!opts.autoCreate || opts.defined || opts.unset) {
|
32 | return (obj.hasOwnProperty(keys[0]) ? _apply(keys.slice(1), obj[keys[0]], value) : undefined);
|
33 | }
|
34 | return _apply(keys.slice(1), (obj.hasOwnProperty(keys[0]) ? obj[keys[0]] : (obj[keys[0]] = (!isNaN(parseInt(keys[1], 10)) ? [] : {}))), value);
|
35 | };
|
36 |
|
37 | try {
|
38 | return _apply(keys, obj, (opts.value ? (is(opts.value, '!Function') ? (current, next) => { return next } : opts.value) : opts.value));
|
39 | } catch (e) {
|
40 | return undefined;
|
41 | }
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function getType (val) {
|
48 | try {
|
49 | if (__GLOBAL['Buffer'] && Buffer.isBuffer && Buffer.isBuffer(val)) {
|
50 | return 'Buffer';
|
51 | }
|
52 | } catch (error) { }
|
53 | return Object.prototype.toString.call(val).match(/(?:\[object (.+)\])/i)[1]
|
54 | };
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function is (val, type, typeOf = false) {
|
61 | try {
|
62 | return (/^!/.test(type) !== ((typeOf ? typeof val : getType(val).toLowerCase()) === type.toLowerCase().replace(/^!/,'')));
|
63 | } catch (error) {
|
64 | return false;
|
65 | }
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | function deepClone (obj, _Map = Map) {
|
72 | if (is(obj, 'Null') || is(obj, '!Object', true)) {
|
73 | return obj;
|
74 | }
|
75 | switch (getType(obj)) {
|
76 | case 'Date':
|
77 | return new Date(obj.getTime());
|
78 | case 'MapQL': case 'Map':
|
79 | return new _Map(deepClone(Array.from(obj), _Map));
|
80 | case 'Set':
|
81 | return new Set(deepClone(Array.from(obj), _Map));
|
82 | case 'RegExp':
|
83 | return new RegExp(obj);
|
84 | case 'Array':
|
85 | return new Array(obj.length).fill(0).map((val, idx) => {
|
86 | return deepClone(obj[idx], _Map)
|
87 | });
|
88 | case 'Object':
|
89 | return ((cloned) => {
|
90 | for (let prop in obj) {
|
91 | if (obj.hasOwnProperty(prop)) {
|
92 | cloned[prop] = deepClone(obj[prop], _Map);
|
93 | }
|
94 | }
|
95 | return cloned;
|
96 | })({});
|
97 | default: return obj;
|
98 | }
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | module.exports = {
|
105 | __GLOBAL: __GLOBAL,
|
106 | _null: _null,
|
107 | dotNotation: (keys = _null, obj = {}, options = {}) => {
|
108 |
|
109 | return Array.isArray(obj) ? obj.map((_obj, idx) => {
|
110 |
|
111 | return dot(String(Array.isArray(keys) ? keys[idx] : keys).trim().split('.'), _obj, options);
|
112 | }) : dot((keys !== _null ? String(keys).trim().split('.') : []), obj, options);
|
113 | },
|
114 | is: (val, type, typeOf = false, some = false) => {
|
115 | return Array.isArray(type) ? type[some ? 'some' : 'every']((t) => is(val, t, typeOf)) : is(val, type, typeOf);
|
116 | },
|
117 | getType: getType,
|
118 | deepClone: deepClone,
|
119 | typeToInt: typeToInt,
|
120 | intToType: intToType
|
121 | };
|