@oscarpalmer/atoms 0.19.0 → 0.20.0
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/dist/js/index.js +30 -15
- package/dist/js/proxy.js +55 -12
- package/dist/js/proxy.mjs +31 -13
- package/dist/js/value.js +4 -3
- package/dist/js/value.mjs +4 -3
- package/package.json +1 -1
- package/src/js/proxy.ts +45 -21
- package/src/js/value.ts +4 -3
- package/types/proxy.d.ts +11 -2
package/dist/js/index.js
CHANGED
|
@@ -290,9 +290,9 @@ var _getDiffs = function(first, second, prefix) {
|
|
|
290
290
|
continue;
|
|
291
291
|
}
|
|
292
292
|
const keys = Object.keys(value);
|
|
293
|
-
const
|
|
293
|
+
const { length } = keys;
|
|
294
294
|
let inner = 0;
|
|
295
|
-
for (;inner <
|
|
295
|
+
for (;inner < length; inner += 1) {
|
|
296
296
|
const key = keys[inner];
|
|
297
297
|
if (checked.has(key)) {
|
|
298
298
|
continue;
|
|
@@ -417,11 +417,12 @@ function merge(...values) {
|
|
|
417
417
|
function set(data, key, value) {
|
|
418
418
|
const parts = getString(key).split(".");
|
|
419
419
|
const { length } = parts;
|
|
420
|
+
const lastIndex = length - 1;
|
|
420
421
|
let index = 0;
|
|
421
422
|
let target = typeof data === "object" ? data ?? {} : {};
|
|
422
423
|
for (;index < length; index += 1) {
|
|
423
424
|
const part = parts[index];
|
|
424
|
-
if (parts.indexOf(part) ===
|
|
425
|
+
if (parts.indexOf(part) === lastIndex) {
|
|
425
426
|
_setValue(target, part, value);
|
|
426
427
|
break;
|
|
427
428
|
}
|
|
@@ -436,27 +437,34 @@ function set(data, key, value) {
|
|
|
436
437
|
}
|
|
437
438
|
|
|
438
439
|
// src/js/proxy.ts
|
|
439
|
-
var _createProxy = function(
|
|
440
|
-
if (!isArrayOrObject(value2) || _isProxy(value2)) {
|
|
440
|
+
var _createProxy = function(existing, value2) {
|
|
441
|
+
if (!isArrayOrObject(value2) || _isProxy(value2) && value2.$ === existing) {
|
|
441
442
|
return value2;
|
|
442
443
|
}
|
|
443
444
|
const isArray = Array.isArray(value2);
|
|
444
|
-
const
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
445
|
+
const proxy = new Proxy(isArray ? [] : {}, {
|
|
446
|
+
get(target, property) {
|
|
447
|
+
return property === "$" ? manager : Reflect.get(target, property);
|
|
448
|
+
},
|
|
449
|
+
set(target, property, value3) {
|
|
450
|
+
return property === "$" || Reflect.set(target, property, _createProxy(manager, value3));
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
const manager = existing ?? new Manager(proxy);
|
|
454
|
+
Object.defineProperty(proxy, "$", {
|
|
455
|
+
value: manager
|
|
448
456
|
});
|
|
449
457
|
const keys = Object.keys(value2);
|
|
450
|
-
const
|
|
458
|
+
const { length } = keys;
|
|
451
459
|
let index = 0;
|
|
452
|
-
for (;index <
|
|
460
|
+
for (;index < length; index += 1) {
|
|
453
461
|
const key = keys[index];
|
|
454
|
-
|
|
462
|
+
proxy[key] = value2[key];
|
|
455
463
|
}
|
|
456
|
-
return
|
|
464
|
+
return proxy;
|
|
457
465
|
};
|
|
458
466
|
var _isProxy = function(value2) {
|
|
459
|
-
return value2?.$ instanceof
|
|
467
|
+
return value2?.$ instanceof Manager;
|
|
460
468
|
};
|
|
461
469
|
function proxy(value2) {
|
|
462
470
|
if (!isArrayOrObject(value2)) {
|
|
@@ -465,7 +473,14 @@ function proxy(value2) {
|
|
|
465
473
|
return _createProxy(undefined, value2);
|
|
466
474
|
}
|
|
467
475
|
|
|
468
|
-
class
|
|
476
|
+
class Manager {
|
|
477
|
+
owner;
|
|
478
|
+
constructor(owner) {
|
|
479
|
+
this.owner = owner;
|
|
480
|
+
}
|
|
481
|
+
clone() {
|
|
482
|
+
return clone(merge(this.owner));
|
|
483
|
+
}
|
|
469
484
|
}
|
|
470
485
|
// src/js/timer.ts
|
|
471
486
|
function repeat(callback, options) {
|
package/dist/js/proxy.js
CHANGED
|
@@ -1,30 +1,66 @@
|
|
|
1
1
|
// src/js/value.ts
|
|
2
|
+
function clone(value) {
|
|
3
|
+
return structuredClone(value);
|
|
4
|
+
}
|
|
2
5
|
function isArrayOrObject(value) {
|
|
3
6
|
return /^(array|object)$/i.test(value?.constructor?.name);
|
|
4
7
|
}
|
|
8
|
+
function merge(...values) {
|
|
9
|
+
if (values.length === 0) {
|
|
10
|
+
return {};
|
|
11
|
+
}
|
|
12
|
+
const actual = values.filter(isArrayOrObject);
|
|
13
|
+
const result = actual.every(Array.isArray) ? [] : {};
|
|
14
|
+
const { length } = actual;
|
|
15
|
+
let itemIndex = 0;
|
|
16
|
+
for (;itemIndex < length; itemIndex += 1) {
|
|
17
|
+
const item = actual[itemIndex];
|
|
18
|
+
const keys = Object.keys(item);
|
|
19
|
+
const size = keys.length;
|
|
20
|
+
let keyIndex = 0;
|
|
21
|
+
for (;keyIndex < size; keyIndex += 1) {
|
|
22
|
+
const key = keys[keyIndex];
|
|
23
|
+
const next = item[key];
|
|
24
|
+
const previous = result[key];
|
|
25
|
+
if (isArrayOrObject(next)) {
|
|
26
|
+
result[key] = isArrayOrObject(previous) ? merge(previous, next) : merge(next);
|
|
27
|
+
} else {
|
|
28
|
+
result[key] = next;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
5
34
|
|
|
6
35
|
// src/js/proxy.ts
|
|
7
|
-
var _createProxy = function(
|
|
8
|
-
if (!isArrayOrObject(value2) || _isProxy(value2)) {
|
|
36
|
+
var _createProxy = function(existing, value2) {
|
|
37
|
+
if (!isArrayOrObject(value2) || _isProxy(value2) && value2.$ === existing) {
|
|
9
38
|
return value2;
|
|
10
39
|
}
|
|
11
40
|
const isArray = Array.isArray(value2);
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
41
|
+
const proxy = new Proxy(isArray ? [] : {}, {
|
|
42
|
+
get(target, property) {
|
|
43
|
+
return property === "$" ? manager : Reflect.get(target, property);
|
|
44
|
+
},
|
|
45
|
+
set(target, property, value3) {
|
|
46
|
+
return property === "$" || Reflect.set(target, property, _createProxy(manager, value3));
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
const manager = existing ?? new Manager(proxy);
|
|
50
|
+
Object.defineProperty(proxy, "$", {
|
|
51
|
+
value: manager
|
|
16
52
|
});
|
|
17
53
|
const keys = Object.keys(value2);
|
|
18
|
-
const
|
|
54
|
+
const { length } = keys;
|
|
19
55
|
let index = 0;
|
|
20
|
-
for (;index <
|
|
56
|
+
for (;index < length; index += 1) {
|
|
21
57
|
const key = keys[index];
|
|
22
|
-
|
|
58
|
+
proxy[key] = value2[key];
|
|
23
59
|
}
|
|
24
|
-
return
|
|
60
|
+
return proxy;
|
|
25
61
|
};
|
|
26
62
|
var _isProxy = function(value2) {
|
|
27
|
-
return value2?.$ instanceof
|
|
63
|
+
return value2?.$ instanceof Manager;
|
|
28
64
|
};
|
|
29
65
|
function proxy(value2) {
|
|
30
66
|
if (!isArrayOrObject(value2)) {
|
|
@@ -33,7 +69,14 @@ function proxy(value2) {
|
|
|
33
69
|
return _createProxy(undefined, value2);
|
|
34
70
|
}
|
|
35
71
|
|
|
36
|
-
class
|
|
72
|
+
class Manager {
|
|
73
|
+
owner;
|
|
74
|
+
constructor(owner) {
|
|
75
|
+
this.owner = owner;
|
|
76
|
+
}
|
|
77
|
+
clone() {
|
|
78
|
+
return clone(merge(this.owner));
|
|
79
|
+
}
|
|
37
80
|
}
|
|
38
81
|
export {
|
|
39
82
|
proxy
|
package/dist/js/proxy.mjs
CHANGED
|
@@ -1,26 +1,37 @@
|
|
|
1
1
|
// src/js/proxy.ts
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import {
|
|
3
|
+
clone,
|
|
4
|
+
isArrayOrObject,
|
|
5
|
+
merge
|
|
6
|
+
} from "./value";
|
|
7
|
+
var _createProxy = function(existing, value2) {
|
|
8
|
+
if (!isArrayOrObject(value2) || _isProxy(value2) && value2.$ === existing) {
|
|
5
9
|
return value2;
|
|
6
10
|
}
|
|
7
11
|
const isArray = Array.isArray(value2);
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
const proxy = new Proxy(isArray ? [] : {}, {
|
|
13
|
+
get(target, property) {
|
|
14
|
+
return property === "$" ? manager : Reflect.get(target, property);
|
|
15
|
+
},
|
|
16
|
+
set(target, property, value3) {
|
|
17
|
+
return property === "$" || Reflect.set(target, property, _createProxy(manager, value3));
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
const manager = existing ?? new Manager(proxy);
|
|
21
|
+
Object.defineProperty(proxy, "$", {
|
|
22
|
+
value: manager
|
|
12
23
|
});
|
|
13
24
|
const keys = Object.keys(value2);
|
|
14
|
-
const
|
|
25
|
+
const { length } = keys;
|
|
15
26
|
let index = 0;
|
|
16
|
-
for (;index <
|
|
27
|
+
for (;index < length; index += 1) {
|
|
17
28
|
const key = keys[index];
|
|
18
|
-
|
|
29
|
+
proxy[key] = value2[key];
|
|
19
30
|
}
|
|
20
|
-
return
|
|
31
|
+
return proxy;
|
|
21
32
|
};
|
|
22
33
|
var _isProxy = function(value2) {
|
|
23
|
-
return value2?.$ instanceof
|
|
34
|
+
return value2?.$ instanceof Manager;
|
|
24
35
|
};
|
|
25
36
|
function proxy(value2) {
|
|
26
37
|
if (!isArrayOrObject(value2)) {
|
|
@@ -29,7 +40,14 @@ function proxy(value2) {
|
|
|
29
40
|
return _createProxy(undefined, value2);
|
|
30
41
|
}
|
|
31
42
|
|
|
32
|
-
class
|
|
43
|
+
class Manager {
|
|
44
|
+
owner;
|
|
45
|
+
constructor(owner) {
|
|
46
|
+
this.owner = owner;
|
|
47
|
+
}
|
|
48
|
+
clone() {
|
|
49
|
+
return clone(merge(this.owner));
|
|
50
|
+
}
|
|
33
51
|
}
|
|
34
52
|
export {
|
|
35
53
|
proxy
|
package/dist/js/value.js
CHANGED
|
@@ -18,9 +18,9 @@ var _getDiffs = function(first, second, prefix) {
|
|
|
18
18
|
continue;
|
|
19
19
|
}
|
|
20
20
|
const keys = Object.keys(value);
|
|
21
|
-
const
|
|
21
|
+
const { length } = keys;
|
|
22
22
|
let inner = 0;
|
|
23
|
-
for (;inner <
|
|
23
|
+
for (;inner < length; inner += 1) {
|
|
24
24
|
const key = keys[inner];
|
|
25
25
|
if (checked.has(key)) {
|
|
26
26
|
continue;
|
|
@@ -145,11 +145,12 @@ function merge(...values) {
|
|
|
145
145
|
function set(data, key, value) {
|
|
146
146
|
const parts = getString(key).split(".");
|
|
147
147
|
const { length } = parts;
|
|
148
|
+
const lastIndex = length - 1;
|
|
148
149
|
let index = 0;
|
|
149
150
|
let target = typeof data === "object" ? data ?? {} : {};
|
|
150
151
|
for (;index < length; index += 1) {
|
|
151
152
|
const part = parts[index];
|
|
152
|
-
if (parts.indexOf(part) ===
|
|
153
|
+
if (parts.indexOf(part) === lastIndex) {
|
|
153
154
|
_setValue(target, part, value);
|
|
154
155
|
break;
|
|
155
156
|
}
|
package/dist/js/value.mjs
CHANGED
|
@@ -10,9 +10,9 @@ var _getDiffs = function(first, second, prefix) {
|
|
|
10
10
|
continue;
|
|
11
11
|
}
|
|
12
12
|
const keys = Object.keys(value);
|
|
13
|
-
const
|
|
13
|
+
const { length } = keys;
|
|
14
14
|
let inner = 0;
|
|
15
|
-
for (;inner <
|
|
15
|
+
for (;inner < length; inner += 1) {
|
|
16
16
|
const key = keys[inner];
|
|
17
17
|
if (checked.has(key)) {
|
|
18
18
|
continue;
|
|
@@ -137,11 +137,12 @@ function merge(...values) {
|
|
|
137
137
|
function set(data, key, value) {
|
|
138
138
|
const parts = getString(key).split(".");
|
|
139
139
|
const { length } = parts;
|
|
140
|
+
const lastIndex = length - 1;
|
|
140
141
|
let index = 0;
|
|
141
142
|
let target = typeof data === "object" ? data ?? {} : {};
|
|
142
143
|
for (;index < length; index += 1) {
|
|
143
144
|
const part = parts[index];
|
|
144
|
-
if (parts.indexOf(part) ===
|
|
145
|
+
if (parts.indexOf(part) === lastIndex) {
|
|
145
146
|
_setValue(target, part, value);
|
|
146
147
|
break;
|
|
147
148
|
}
|
package/package.json
CHANGED
package/src/js/proxy.ts
CHANGED
|
@@ -1,49 +1,73 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ArrayOrObject,
|
|
3
|
+
GenericObject,
|
|
4
|
+
clone,
|
|
5
|
+
isArrayOrObject,
|
|
6
|
+
merge,
|
|
7
|
+
} from './value';
|
|
2
8
|
|
|
3
|
-
class
|
|
9
|
+
class Manager<T extends ArrayOrObject = GenericObject> {
|
|
10
|
+
constructor(readonly owner: Proxied<T>) {}
|
|
4
11
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
12
|
+
clone(): Proxied<T> {
|
|
13
|
+
return clone(merge(this.owner)) as Proxied<T>;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type Proxied<T extends ArrayOrObject = GenericObject> = {
|
|
18
|
+
$: Manager<T>;
|
|
19
|
+
} & T;
|
|
8
20
|
|
|
9
21
|
function _createProxy<T extends ArrayOrObject>(
|
|
10
|
-
|
|
22
|
+
existing: Manager | undefined,
|
|
11
23
|
value: T,
|
|
12
|
-
):
|
|
13
|
-
if (!isArrayOrObject(value) || _isProxy(value)) {
|
|
24
|
+
): unknown {
|
|
25
|
+
if (!isArrayOrObject(value) || (_isProxy(value) && value.$ === existing)) {
|
|
14
26
|
return value;
|
|
15
27
|
}
|
|
16
28
|
|
|
17
29
|
const isArray = Array.isArray(value);
|
|
18
|
-
const proxyBlob = blob ?? new ProxyBlob();
|
|
19
|
-
const proxyValue = new Proxy(isArray ? [] : {}, {});
|
|
20
30
|
|
|
21
|
-
|
|
22
|
-
|
|
31
|
+
const proxy = new Proxy(isArray ? [] : {}, {
|
|
32
|
+
get(target, property) {
|
|
33
|
+
return property === '$' ? manager : Reflect.get(target, property);
|
|
34
|
+
},
|
|
35
|
+
set(target, property, value) {
|
|
36
|
+
return (
|
|
37
|
+
property === '$' ||
|
|
38
|
+
Reflect.set(target, property, _createProxy(manager, value))
|
|
39
|
+
);
|
|
40
|
+
},
|
|
41
|
+
}) as Proxied;
|
|
42
|
+
|
|
43
|
+
const manager = existing ?? new Manager(proxy);
|
|
44
|
+
|
|
45
|
+
Object.defineProperty(proxy, '$', {
|
|
46
|
+
value: manager,
|
|
23
47
|
});
|
|
24
48
|
|
|
25
|
-
const keys =
|
|
26
|
-
const
|
|
49
|
+
const keys = Object.keys(value);
|
|
50
|
+
const {length} = keys;
|
|
27
51
|
|
|
28
52
|
let index = 0;
|
|
29
53
|
|
|
30
|
-
for (; index <
|
|
54
|
+
for (; index < length; index += 1) {
|
|
31
55
|
const key = keys[index];
|
|
32
56
|
|
|
33
|
-
|
|
57
|
+
proxy[key as never] = value[key as never];
|
|
34
58
|
}
|
|
35
59
|
|
|
36
|
-
return
|
|
60
|
+
return proxy;
|
|
37
61
|
}
|
|
38
62
|
|
|
39
|
-
function _isProxy(value: unknown): value is
|
|
40
|
-
return (value as
|
|
63
|
+
function _isProxy(value: unknown): value is Proxied {
|
|
64
|
+
return (value as Proxied)?.$ instanceof Manager;
|
|
41
65
|
}
|
|
42
66
|
|
|
43
|
-
export function proxy<T extends ArrayOrObject>(value: T): T {
|
|
67
|
+
export function proxy<T extends ArrayOrObject>(value: T): Proxied<T> {
|
|
44
68
|
if (!isArrayOrObject(value)) {
|
|
45
69
|
throw new Error('Proxy value must be an array or object');
|
|
46
70
|
}
|
|
47
71
|
|
|
48
|
-
return _createProxy(undefined, value)
|
|
72
|
+
return _createProxy(undefined, value) as Proxied<T>;
|
|
49
73
|
}
|
package/src/js/value.ts
CHANGED
|
@@ -43,11 +43,11 @@ function _getDiffs(
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
const keys = Object.keys(value);
|
|
46
|
-
const
|
|
46
|
+
const {length} = keys;
|
|
47
47
|
|
|
48
48
|
let inner = 0;
|
|
49
49
|
|
|
50
|
-
for (; inner <
|
|
50
|
+
for (; inner < length; inner += 1) {
|
|
51
51
|
const key = keys[inner];
|
|
52
52
|
|
|
53
53
|
if (checked.has(key)) {
|
|
@@ -264,6 +264,7 @@ export function set<T extends ValueObject>(
|
|
|
264
264
|
): T {
|
|
265
265
|
const parts = getString(key).split('.');
|
|
266
266
|
const {length} = parts;
|
|
267
|
+
const lastIndex = length - 1;
|
|
267
268
|
|
|
268
269
|
let index = 0;
|
|
269
270
|
let target: ValueObject = typeof data === 'object' ? data ?? {} : {};
|
|
@@ -271,7 +272,7 @@ export function set<T extends ValueObject>(
|
|
|
271
272
|
for (; index < length; index += 1) {
|
|
272
273
|
const part = parts[index];
|
|
273
274
|
|
|
274
|
-
if (parts.indexOf(part) ===
|
|
275
|
+
if (parts.indexOf(part) === lastIndex) {
|
|
275
276
|
_setValue(target, part, value);
|
|
276
277
|
|
|
277
278
|
break;
|
package/types/proxy.d.ts
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
-
import { ArrayOrObject } from './value';
|
|
2
|
-
|
|
1
|
+
import { ArrayOrObject, GenericObject } from './value';
|
|
2
|
+
declare class Manager<T extends ArrayOrObject = GenericObject> {
|
|
3
|
+
readonly owner: Proxied<T>;
|
|
4
|
+
constructor(owner: Proxied<T>);
|
|
5
|
+
clone(): Proxied<T>;
|
|
6
|
+
}
|
|
7
|
+
export type Proxied<T extends ArrayOrObject = GenericObject> = {
|
|
8
|
+
$: Manager<T>;
|
|
9
|
+
} & T;
|
|
10
|
+
export declare function proxy<T extends ArrayOrObject>(value: T): Proxied<T>;
|
|
11
|
+
export {};
|