pacc 4.11.1 → 4.12.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/README.md +1 -0
- package/package.json +1 -1
- package/src/attributes.mjs +8 -1
- package/src/properties.mjs +37 -4
- package/src/settergetter.mjs +7 -2
- package/types/attributes.d.mts +1 -0
- package/types/properties.d.mts +1 -1
- package/types/settergetter.d.mts +1 -1
package/README.md
CHANGED
|
@@ -413,6 +413,7 @@ The name may be a property path like 'a.b.c'.
|
|
|
413
413
|
* `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
|
|
414
414
|
* `expression` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
|
|
415
415
|
* `value` **any** 
|
|
416
|
+
* `definition` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** type def
|
|
416
417
|
|
|
417
418
|
## getAttribute
|
|
418
419
|
|
package/package.json
CHANGED
package/src/attributes.mjs
CHANGED
|
@@ -6,7 +6,7 @@ const types = {
|
|
|
6
6
|
"unsigned-integer": { name: "unsigned-integer" },
|
|
7
7
|
boolean: {
|
|
8
8
|
name: "boolean",
|
|
9
|
-
|
|
9
|
+
convertValue: value => (!value || value === "0" ? false : true)
|
|
10
10
|
},
|
|
11
11
|
url: { name: "url" },
|
|
12
12
|
object: { name: "object" }
|
|
@@ -73,3 +73,10 @@ export function* attributeIterator(definition, path = []) {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
|
+
|
|
77
|
+
export function convertValue(value, definition) {
|
|
78
|
+
if (definition?.type?.convertValue) {
|
|
79
|
+
return definition.type.convertValue(value);
|
|
80
|
+
}
|
|
81
|
+
return value;
|
|
82
|
+
}
|
package/src/properties.mjs
CHANGED
|
@@ -1,10 +1,43 @@
|
|
|
1
1
|
import { attributeIterator } from "./attributes.mjs";
|
|
2
|
-
import { getAttribute } from "./settergetter.mjs";
|
|
2
|
+
import { getAttribute, setAttribute } from "./settergetter.mjs";
|
|
3
|
+
import { convertValue } from "./attributes.mjs";
|
|
3
4
|
|
|
4
|
-
export function
|
|
5
|
-
|
|
5
|
+
export function definePropertiesFromAttributes(
|
|
6
|
+
object,
|
|
7
|
+
attributes,
|
|
8
|
+
values,
|
|
9
|
+
properties
|
|
10
|
+
) {
|
|
11
|
+
const applyLater = {};
|
|
12
|
+
|
|
13
|
+
for (const [path, attribute] of attributeIterator(attributes)) {
|
|
6
14
|
const name = path.join(".");
|
|
7
15
|
|
|
8
|
-
let value = getAttribute(values, name,
|
|
16
|
+
let value = getAttribute(values, name, attribute);
|
|
17
|
+
|
|
18
|
+
if (value !== undefined && path.length === 1) {
|
|
19
|
+
const op = Object.getOwnPropertyDescriptor(
|
|
20
|
+
object.constructor.prototype,
|
|
21
|
+
name
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
value = convertValue(value, attribute);
|
|
25
|
+
const property = properties[name];
|
|
26
|
+
|
|
27
|
+
console.log(name, value);
|
|
28
|
+
if (op?.set || property?.set) {
|
|
29
|
+
applyLater[name] = value;
|
|
30
|
+
} else {
|
|
31
|
+
properties[name] = Object.assign(
|
|
32
|
+
{ value, writable: attribute.writable },
|
|
33
|
+
property
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
setAttribute(object, name, value, attribute);
|
|
38
|
+
}
|
|
9
39
|
}
|
|
40
|
+
|
|
41
|
+
Object.defineProperties(object, properties);
|
|
42
|
+
Object.assign(object, applyLater);
|
|
10
43
|
}
|
package/src/settergetter.mjs
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
STAR
|
|
17
17
|
} from "./tokens.mjs";
|
|
18
18
|
import { parse } from "./expression.mjs";
|
|
19
|
+
import { convertValue } from "./attributes.mjs";
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
22
|
* Set object attribute.
|
|
@@ -47,7 +48,7 @@ export function setAttribute(object, expression, value, definition) {
|
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
if (anchor) {
|
|
50
|
-
anchor[anchorKey] = value;
|
|
51
|
+
anchor[anchorKey] = convertValue(value, definition);
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
54
|
|
|
@@ -58,7 +59,7 @@ export function setAttribute(object, expression, value, definition) {
|
|
|
58
59
|
* @param {string} expression
|
|
59
60
|
* @returns {any} value associated with the given property name
|
|
60
61
|
*/
|
|
61
|
-
export function getAttribute(object, expression) {
|
|
62
|
+
export function getAttribute(object, expression, definition) {
|
|
62
63
|
const { path } = parse({ tokens: tokens(expression) });
|
|
63
64
|
|
|
64
65
|
for (const key of path) {
|
|
@@ -79,6 +80,10 @@ export function getAttribute(object, expression) {
|
|
|
79
80
|
}
|
|
80
81
|
}
|
|
81
82
|
|
|
83
|
+
if(object === undefined && definition) {
|
|
84
|
+
object = definition.default;
|
|
85
|
+
}
|
|
86
|
+
|
|
82
87
|
return object;
|
|
83
88
|
}
|
|
84
89
|
|
package/types/attributes.d.mts
CHANGED
package/types/properties.d.mts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export function
|
|
1
|
+
export function definePropertiesFromAttributes(object: any, attributes: any, values: any, properties: any): void;
|
package/types/settergetter.d.mts
CHANGED
|
@@ -14,7 +14,7 @@ export function setAttribute(object: any, expression: string, value: any, defini
|
|
|
14
14
|
* @param {string} expression
|
|
15
15
|
* @returns {any} value associated with the given property name
|
|
16
16
|
*/
|
|
17
|
-
export function getAttribute(object: any, expression: string): any;
|
|
17
|
+
export function getAttribute(object: any, expression: string, definition: any): any;
|
|
18
18
|
/**
|
|
19
19
|
* Deliver attribute value and operator.
|
|
20
20
|
* The name may be a property path like 'a.b.c <='.
|