pacc 4.11.0 → 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 CHANGED
@@ -31,6 +31,8 @@ const result = getAttribute({ a: [0,{ b: 4 }]}, "a[1].b");
31
31
  * [Parameters](#parameters)
32
32
  * [mergeAttributeDefinitions](#mergeattributedefinitions)
33
33
  * [Parameters](#parameters-1)
34
+ * [attributeIterator](#attributeiterator)
35
+ * [Parameters](#parameters-2)
34
36
  * [AttributeDefinition](#attributedefinition)
35
37
  * [Properties](#properties)
36
38
  * [default\_attribute](#default_attribute)
@@ -77,25 +79,25 @@ const result = getAttribute({ a: [0,{ b: 4 }]}, "a[1].b");
77
79
  * [timeout\_attribute](#timeout_attribute)
78
80
  * [language\_attribute](#language_attribute)
79
81
  * [filter](#filter)
80
- * [Parameters](#parameters-2)
81
- * [setAttributes](#setattributes)
82
82
  * [Parameters](#parameters-3)
83
- * [getAttributes](#getattributes)
83
+ * [setAttributes](#setattributes)
84
84
  * [Parameters](#parameters-4)
85
+ * [getAttributes](#getattributes)
86
+ * [Parameters](#parameters-5)
85
87
  * [tokens](#tokens)
86
88
  * [tokens](#tokens-1)
87
- * [Parameters](#parameters-5)
88
- * [setAttribute](#setattribute)
89
89
  * [Parameters](#parameters-6)
90
- * [getAttribute](#getattribute)
90
+ * [setAttribute](#setattribute)
91
91
  * [Parameters](#parameters-7)
92
- * [getAttributeAndOperator](#getattributeandoperator)
92
+ * [getAttribute](#getattribute)
93
93
  * [Parameters](#parameters-8)
94
+ * [getAttributeAndOperator](#getattributeandoperator)
95
+ * [Parameters](#parameters-9)
94
96
  * [lookup](#lookup)
95
97
  * [Token](#token)
96
98
  * [Properties](#properties-1)
97
99
  * [createToken](#createtoken)
98
- * [Parameters](#parameters-9)
100
+ * [Parameters](#parameters-10)
99
101
  * [PLUS](#plus)
100
102
  * [MINUS](#minus)
101
103
  * [STAR](#star)
@@ -146,6 +148,15 @@ Merge attribute definitions.
146
148
 
147
149
  Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** merged definitions (dest)
148
150
 
151
+ ## attributeIterator
152
+
153
+ iterate over all attributes.
154
+
155
+ ### Parameters
156
+
157
+ * `definition` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
158
+ * `path` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** (optional, default `[]`)
159
+
149
160
  ## AttributeDefinition
150
161
 
151
162
  Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)
@@ -370,7 +381,6 @@ Copies attribute values from a source object into a destination object.
370
381
  * `source` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** origin of the data to be copied
371
382
  * `definitions` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** attribute definitions to be used
372
383
  * `cb` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** callback to be executed for each copied value
373
- * `prefix` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** name parefix
374
384
 
375
385
  ## getAttributes
376
386
 
@@ -403,6 +413,7 @@ The name may be a property path like 'a.b.c'.
403
413
  * `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**&#x20;
404
414
  * `expression` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**&#x20;
405
415
  * `value` **any**&#x20;
416
+ * `definition` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** type def
406
417
 
407
418
  ## getAttribute
408
419
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "4.11.0",
3
+ "version": "4.12.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -4,7 +4,10 @@ const types = {
4
4
  number: { name: "number" },
5
5
  integer: { name: "integer" },
6
6
  "unsigned-integer": { name: "unsigned-integer" },
7
- boolean: { name: "boolean" },
7
+ boolean: {
8
+ name: "boolean",
9
+ convertValue: value => (!value || value === "0" ? false : true)
10
+ },
8
11
  url: { name: "url" },
9
12
  object: { name: "object" }
10
13
  };
@@ -19,12 +22,8 @@ export function prepareAttributesDefinitions(
19
22
  newDefinitions,
20
23
  presentDefinitions
21
24
  ) {
22
- for (const [name, d] of Object.entries(newDefinitions)) {
23
- if (d.attributes === undefined) {
24
- d.type = types[d.type] || types.base;
25
- } else {
26
- prepareAttributesDefinitions(d.attributes);
27
- }
25
+ for (const [path, def] of attributeIterator(newDefinitions)) {
26
+ def.type = types[def.type] || types.base;
28
27
  }
29
28
 
30
29
  return mergeAttributeDefinitions(newDefinitions, presentDefinitions);
@@ -60,15 +59,24 @@ function mergeAttributeDefinitions(dest, atts) {
60
59
  * @param {string[]} path
61
60
  */
62
61
  export function* attributeIterator(definition, path = []) {
63
- for (const [name, def] of Object.entries(definition)) {
64
- path.push(name);
62
+ if (definition) {
63
+ for (const [name, def] of Object.entries(definition)) {
64
+ path.push(name);
65
65
 
66
- if (def.attributes) {
67
- yield* attributeIterator(def.attributes, path);
68
- }
66
+ if (def.attributes) {
67
+ yield* attributeIterator(def.attributes, path);
68
+ }
69
+
70
+ yield [path, def];
69
71
 
70
- yield [path, def];
72
+ path.pop();
73
+ }
74
+ }
75
+ }
71
76
 
72
- path.pop();
77
+ export function convertValue(value, definition) {
78
+ if (definition?.type?.convertValue) {
79
+ return definition.type.convertValue(value);
73
80
  }
81
+ return value;
74
82
  }
@@ -0,0 +1,43 @@
1
+ import { attributeIterator } from "./attributes.mjs";
2
+ import { getAttribute, setAttribute } from "./settergetter.mjs";
3
+ import { convertValue } from "./attributes.mjs";
4
+
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)) {
14
+ const name = path.join(".");
15
+
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
+ }
39
+ }
40
+
41
+ Object.defineProperties(object, properties);
42
+ Object.assign(object, applyLater);
43
+ }
@@ -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.
@@ -23,8 +24,9 @@ import { parse } from "./expression.mjs";
23
24
  * @param {Object} object
24
25
  * @param {string} expression
25
26
  * @param {any} value
27
+ * @param {Object} definition type def
26
28
  */
27
- export function setAttribute(object, expression, value) {
29
+ export function setAttribute(object, expression, value, definition) {
28
30
  const { path } = parse({ tokens: tokens(expression) });
29
31
 
30
32
  let anchor, anchorKey;
@@ -46,7 +48,7 @@ export function setAttribute(object, expression, value) {
46
48
  }
47
49
 
48
50
  if (anchor) {
49
- anchor[anchorKey] = value;
51
+ anchor[anchorKey] = convertValue(value, definition);
50
52
  }
51
53
  }
52
54
 
@@ -57,7 +59,7 @@ export function setAttribute(object, expression, value) {
57
59
  * @param {string} expression
58
60
  * @returns {any} value associated with the given property name
59
61
  */
60
- export function getAttribute(object, expression) {
62
+ export function getAttribute(object, expression, definition) {
61
63
  const { path } = parse({ tokens: tokens(expression) });
62
64
 
63
65
  for (const key of path) {
@@ -78,6 +80,10 @@ export function getAttribute(object, expression) {
78
80
  }
79
81
  }
80
82
 
83
+ if(object === undefined && definition) {
84
+ object = definition.default;
85
+ }
86
+
81
87
  return object;
82
88
  }
83
89
 
@@ -11,3 +11,4 @@ export function prepareAttributesDefinitions(newDefinitions: any, presentDefinit
11
11
  * @param {string[]} path
12
12
  */
13
13
  export function attributeIterator(definition: any, path?: string[]): any;
14
+ export function convertValue(value: any, definition: any): any;
@@ -0,0 +1 @@
1
+ export function definePropertiesFromAttributes(object: any, attributes: any, values: any, properties: any): void;
@@ -4,8 +4,9 @@
4
4
  * @param {Object} object
5
5
  * @param {string} expression
6
6
  * @param {any} value
7
+ * @param {Object} definition type def
7
8
  */
8
- export function setAttribute(object: any, expression: string, value: any): void;
9
+ export function setAttribute(object: any, expression: string, value: any, definition: any): void;
9
10
  /**
10
11
  * Deliver attribute value.
11
12
  * The name may be a property path like 'a.b.c' or a[2]
@@ -13,7 +14,7 @@ export function setAttribute(object: any, expression: string, value: any): void;
13
14
  * @param {string} expression
14
15
  * @returns {any} value associated with the given property name
15
16
  */
16
- export function getAttribute(object: any, expression: string): any;
17
+ export function getAttribute(object: any, expression: string, definition: any): any;
17
18
  /**
18
19
  * Deliver attribute value and operator.
19
20
  * The name may be a property path like 'a.b.c <='.