@kizmann/pico-js 0.3.21 → 0.3.22

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.21",
3
+ "version": "0.3.22",
4
4
  "license": "MIT",
5
5
  "private": false,
6
6
  "author": "Eduard Kizmann <kizmann@protonmail.ch>",
@@ -20,7 +20,7 @@ export class Obj
20
20
  }
21
21
 
22
22
  if ( keys === null || keys === undefined ) {
23
- return obj;
23
+ return fallback;
24
24
  }
25
25
 
26
26
  if ( Any.isArray(keys) ) {
@@ -35,6 +35,10 @@ export class Obj
35
35
 
36
36
  let index = 0, length = keys.length;
37
37
 
38
+ if ( length === 0 ) {
39
+ return fallback;
40
+ }
41
+
38
42
  while (obj !== undefined && index < length) {
39
43
  obj = obj[keys[index++]];
40
44
  }
@@ -48,26 +52,49 @@ export class Obj
48
52
 
49
53
  static set(obj, keys, val)
50
54
  {
51
- keys = (typeof keys === 'string' && keys.match(/^[^\.]+(\.[^\.]+)*$/)) ?
52
- keys.split('.') : keys;
55
+ if ( Any.isArray(keys) ) {
56
+ keys = keys.join('.');
57
+ }
53
58
 
54
- let key = keys.shift();
59
+ if ( ! Any.isString(keys) ) {
60
+ keys = keys.toString();
61
+ }
55
62
 
56
- if ( obj[key] === undefined || obj[key] === null ) {
57
- obj[key] = {};
63
+ keys = keys.split('.');
64
+
65
+ let index = 0, length = keys.length, nested = obj;
66
+
67
+ if ( length === 0 ) {
68
+ return obj;
58
69
  }
59
70
 
60
- if ( keys.length === 0 ) {
61
- 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++]];
62
82
  }
63
83
 
64
- return this.set(obj[key], keys, val);
84
+ return obj;
65
85
  }
66
86
 
67
87
  static unset(obj, keys)
68
88
  {
69
- keys = (typeof keys === 'string' && keys.match(/^[^\.]+(\.[^\.]+)*$/)) ?
70
- 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('.');
71
98
 
72
99
  let key = keys.shift();
73
100