pacc 4.11.1 → 4.12.1

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
@@ -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
 
@@ -423,6 +424,7 @@ The name may be a property path like 'a.b.c' or a\[2]
423
424
 
424
425
  * `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
425
426
  * `expression` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
427
+ * `definition`  
426
428
 
427
429
  Returns **any** value associated with the given property name
428
430
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "4.11.1",
3
+ "version": "4.12.1",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -2,11 +2,14 @@ const types = {
2
2
  base: { name: "base" },
3
3
  string: { name: "string" },
4
4
  number: { name: "number" },
5
- integer: { name: "integer" },
6
- "unsigned-integer": { name: "unsigned-integer" },
5
+ integer: {
6
+ name: "integer",
7
+ convertValue: value => (typeof value === "string" ? parseInt(value) : value)
8
+ },
9
+ "unsigned-integer": { name: "unsigned-integer", extends: "integer" },
7
10
  boolean: {
8
11
  name: "boolean",
9
- from: value => (!value || value === "0" ? false : true)
12
+ convertValue: value => (!value || value === "0" ? false : true)
10
13
  },
11
14
  url: { name: "url" },
12
15
  object: { name: "object" }
@@ -15,7 +18,7 @@ const types = {
15
18
  /**
16
19
  * Create attributes from its definition.
17
20
  * @param {Object} newDefinitions
18
- * @param {Object|undefined} presentDefinitions optional merg in attributes
21
+ * @param {Object} [presentDefinitions] optional merg in attributes
19
22
  * @return {Object} attributes
20
23
  */
21
24
  export function prepareAttributesDefinitions(
@@ -73,3 +76,10 @@ export function* attributeIterator(definition, path = []) {
73
76
  }
74
77
  }
75
78
  }
79
+
80
+ export function convertValue(value, definition) {
81
+ if (definition?.type?.convertValue) {
82
+ return definition.type.convertValue(value);
83
+ }
84
+ return value;
85
+ }
package/src/module.mjs CHANGED
@@ -3,6 +3,7 @@ export * from "./filter.mjs";
3
3
  export * from "./multiple.mjs";
4
4
  export * from "./attributes.mjs";
5
5
  export * from "./common-attributes.mjs";
6
+ export * from "./properties.mjs";
6
7
  export {
7
8
  setAttribute,
8
9
  getAttribute,
@@ -1,10 +1,42 @@
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 createPropertiesFromAttributes(object, attributes, values) {
5
- for (const [path, def] of attributeIterator(attributes)) {
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, def);
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
+ if (op?.set || property?.set) {
28
+ applyLater[name] = value;
29
+ } else {
30
+ properties[name] = Object.assign(
31
+ { value, writable: attribute.writable },
32
+ property
33
+ );
34
+ }
35
+ } else {
36
+ setAttribute(object, name, value, attribute);
37
+ }
9
38
  }
39
+
40
+ Object.defineProperties(object, properties);
41
+ Object.assign(object, applyLater);
10
42
  }
@@ -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
 
@@ -1,13 +1,14 @@
1
1
  /**
2
2
  * Create attributes from its definition.
3
3
  * @param {Object} newDefinitions
4
- * @param {Object|undefined} presentDefinitions optional merg in attributes
4
+ * @param {Object} [presentDefinitions] optional merg in attributes
5
5
  * @return {Object} attributes
6
6
  */
7
- export function prepareAttributesDefinitions(newDefinitions: any, presentDefinitions: any | undefined): any;
7
+ export function prepareAttributesDefinitions(newDefinitions: any, presentDefinitions?: any): any;
8
8
  /**
9
9
  * iterate over all attributes.
10
10
  * @param {Object} definition
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;
@@ -3,4 +3,5 @@ export * from "./filter.mjs";
3
3
  export * from "./multiple.mjs";
4
4
  export * from "./attributes.mjs";
5
5
  export * from "./common-attributes.mjs";
6
+ export * from "./properties.mjs";
6
7
  export { setAttribute, getAttribute, getAttributeAndOperator } from "./settergetter.mjs";
@@ -1 +1 @@
1
- export function createPropertiesFromAttributes(object: any, attributes: any, values: any): void;
1
+ export function definePropertiesFromAttributes(object: any, attributes: any, values: any, properties?: {}): void;
@@ -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 <='.