@kizmann/pico-js 0.3.19 → 0.3.22

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kizmann/pico-js",
3
- "version": "0.3.19",
3
+ "version": "0.3.22",
4
4
  "license": "MIT",
5
5
  "private": false,
6
6
  "author": "Eduard Kizmann <kizmann@protonmail.ch>",
@@ -19,54 +19,82 @@ export class Obj
19
19
  return fallback;
20
20
  }
21
21
 
22
- keys = (typeof keys === 'string' && keys.match(/^[^\.]+(\.[^\.]+)*$/)) ?
23
- keys.split('.') : keys;
22
+ if ( keys === null || keys === undefined ) {
23
+ return fallback;
24
+ }
24
25
 
25
- if ( Any.isArray(keys) === false ) {
26
- keys = [keys];
26
+ if ( Any.isArray(keys) ) {
27
+ keys = keys.join('.');
27
28
  }
28
29
 
29
- let result = [];
30
+ if ( ! Any.isString(keys) ) {
31
+ keys = keys.toString();
32
+ }
30
33
 
31
- Arr.each(keys, (key) => {
32
- result = result.concat(key.split('.'))
33
- });
34
+ keys = keys.split('.');
34
35
 
35
- let key = result.shift();
36
+ let index = 0, length = keys.length;
36
37
 
37
- if ( typeof obj[key] === 'undefined' ) {
38
+ if ( length === 0 ) {
38
39
  return fallback;
39
40
  }
40
41
 
41
- if ( result.length === 0 ) {
42
- return obj[key];
42
+ while (obj !== undefined && index < length) {
43
+ obj = obj[keys[index++]];
43
44
  }
44
45
 
45
- return this.get(obj[key], result, fallback);
46
+ if ( typeof obj === 'undefined' ) {
47
+ return fallback;
48
+ }
49
+
50
+ return obj;
46
51
  }
47
52
 
48
53
  static set(obj, keys, val)
49
54
  {
50
- keys = (typeof keys === 'string' && keys.match(/^[^\.]+(\.[^\.]+)*$/)) ?
51
- keys.split('.') : keys;
55
+ if ( Any.isArray(keys) ) {
56
+ keys = keys.join('.');
57
+ }
52
58
 
53
- let key = keys.shift();
59
+ if ( ! Any.isString(keys) ) {
60
+ keys = keys.toString();
61
+ }
54
62
 
55
- if ( obj[key] === undefined || obj[key] === null ) {
56
- obj[key] = {};
63
+ keys = keys.split('.');
64
+
65
+ let index = 0, length = keys.length, nested = obj;
66
+
67
+ if ( length === 0 ) {
68
+ return obj;
57
69
  }
58
70
 
59
- if ( keys.length === 0 ) {
60
- return obj[key] = val;
71
+ while (nested !== null && index < length) {
72
+
73
+ if ( nested[keys[index]] === undefined || nested[keys[index]] === null ) {
74
+ nested[keys[index]] = {};
75
+ }
76
+
77
+ if ( index == length - 1 ) {
78
+ nested[keys[index]] = val;
79
+ }
80
+
81
+ nested = nested[keys[index++]];
61
82
  }
62
83
 
63
- return this.set(obj[key], keys, val);
84
+ return obj;
64
85
  }
65
86
 
66
87
  static unset(obj, keys)
67
88
  {
68
- keys = (typeof keys === 'string' && keys.match(/^[^\.]+(\.[^\.]+)*$/)) ?
69
- keys.split('.') : keys;
89
+ if ( Any.isArray(keys) ) {
90
+ keys = keys.join('.');
91
+ }
92
+
93
+ if ( ! Any.isString(keys) ) {
94
+ keys = keys.toString();
95
+ }
96
+
97
+ keys = keys.split('.');
70
98
 
71
99
  let key = keys.shift();
72
100