@kizmann/pico-js 0.3.20 → 0.3.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kizmann/pico-js",
3
- "version": "0.3.20",
3
+ "version": "0.3.23",
4
4
  "license": "MIT",
5
5
  "private": false,
6
6
  "author": "Eduard Kizmann <kizmann@protonmail.ch>",
@@ -19,62 +19,82 @@ export class Obj
19
19
  return fallback;
20
20
  }
21
21
 
22
- keys = (typeof keys === 'string' && keys.match(/^[^\.]+(\.[^\.]+)*$/)) ?
23
- keys.split('.') : keys;
24
-
25
- if ( Any.isArray(keys) === false ) {
26
- keys = [keys];
22
+ if ( keys === null || keys === undefined ) {
23
+ return fallback;
27
24
  }
28
25
 
29
- let result = [];
30
-
31
- Arr.each(keys, (key) => {
32
-
33
- key = (typeof key === 'string' && key.match(/^[^\.]+(\.[^\.]+)*$/)) ?
34
- key.split('.') : key;
26
+ if ( Any.isArray(keys) ) {
27
+ keys = keys.join('.');
28
+ }
35
29
 
36
- if ( Any.isArray(keys) === false ) {
37
- key = [key];
38
- }
30
+ if ( ! Any.isString(keys) ) {
31
+ keys = keys.toString();
32
+ }
39
33
 
40
- result = result.concat(key)
41
- });
34
+ keys = keys.split('.');
42
35
 
43
- let key = result.shift();
36
+ let index = 0, length = keys.length;
44
37
 
45
- if ( typeof obj[key] === 'undefined' ) {
38
+ if ( length === 0 ) {
46
39
  return fallback;
47
40
  }
48
41
 
49
- if ( result.length === 0 ) {
50
- return obj[key];
42
+ while (obj !== undefined && obj !== null && index < length) {
43
+ obj = obj[keys[index++]];
44
+ }
45
+
46
+ if ( obj === undefined || obj === null) {
47
+ return fallback;
51
48
  }
52
49
 
53
- return this.get(obj[key], result, fallback);
50
+ return obj;
54
51
  }
55
52
 
56
53
  static set(obj, keys, val)
57
54
  {
58
- keys = (typeof keys === 'string' && keys.match(/^[^\.]+(\.[^\.]+)*$/)) ?
59
- keys.split('.') : keys;
55
+ if ( Any.isArray(keys) ) {
56
+ keys = keys.join('.');
57
+ }
60
58
 
61
- let key = keys.shift();
59
+ if ( ! Any.isString(keys) ) {
60
+ keys = keys.toString();
61
+ }
62
+
63
+ keys = keys.split('.');
64
+
65
+ let index = 0, length = keys.length, nested = obj;
62
66
 
63
- if ( obj[key] === undefined || obj[key] === null ) {
64
- obj[key] = {};
67
+ if ( length === 0 ) {
68
+ return obj;
65
69
  }
66
70
 
67
- if ( keys.length === 0 ) {
68
- 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++]];
69
82
  }
70
83
 
71
- return this.set(obj[key], keys, val);
84
+ return obj;
72
85
  }
73
86
 
74
87
  static unset(obj, keys)
75
88
  {
76
- keys = (typeof keys === 'string' && keys.match(/^[^\.]+(\.[^\.]+)*$/)) ?
77
- 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('.');
78
98
 
79
99
  let key = keys.shift();
80
100