pacc 4.10.0 → 4.11.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
@@ -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
 
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "4.10.0",
3
+ "version": "4.11.1",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
7
7
  },
8
+ "packageManager": "npm@11.6.0+sha512.77f3fb0dbbd881835d7bd1217deabdf7ed77fc4ec4f363df64fc3cb986404abf6c437661041080f5c5d225103508e7c9ea30cb2742b7e942675d05a10af2d7b9",
8
9
  "types": "./types/module.d.mts",
9
10
  "exports": {
10
11
  ".": {
@@ -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
+ from: value => (!value || value === "0" ? false : true)
10
+ },
8
11
  url: { name: "url" },
9
12
  object: { name: "object" }
10
13
  };
@@ -15,14 +18,12 @@ const types = {
15
18
  * @param {Object|undefined} presentDefinitions optional merg in attributes
16
19
  * @return {Object} attributes
17
20
  */
18
- export function prepareAttributesDefinitions(newDefinitions, presentDefinitions) {
19
- for (const [name, d] of Object.entries(newDefinitions)) {
20
- if (d.attributes === undefined) {
21
- d.type = types[d.type] || types.base;
22
- }
23
- else {
24
- prepareAttributesDefinitions(d.attributes);
25
- }
21
+ export function prepareAttributesDefinitions(
22
+ newDefinitions,
23
+ presentDefinitions
24
+ ) {
25
+ for (const [path, def] of attributeIterator(newDefinitions)) {
26
+ def.type = types[def.type] || types.base;
26
27
  }
27
28
 
28
29
  return mergeAttributeDefinitions(newDefinitions, presentDefinitions);
@@ -50,4 +51,25 @@ function mergeAttributeDefinitions(dest, atts) {
50
51
  }
51
52
 
52
53
  return dest;
53
- }
54
+ }
55
+
56
+ /**
57
+ * iterate over all attributes.
58
+ * @param {Object} definition
59
+ * @param {string[]} path
60
+ */
61
+ export function* attributeIterator(definition, path = []) {
62
+ if (definition) {
63
+ for (const [name, def] of Object.entries(definition)) {
64
+ path.push(name);
65
+
66
+ if (def.attributes) {
67
+ yield* attributeIterator(def.attributes, path);
68
+ }
69
+
70
+ yield [path, def];
71
+
72
+ path.pop();
73
+ }
74
+ }
75
+ }
package/src/multiple.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import { setAttribute, getAttribute } from "./settergetter.mjs";
2
+ import { attributeIterator } from "./attributes.mjs";
2
3
 
3
4
  /**
4
5
  * Copies attribute values from a source object into a destination object.
@@ -6,18 +7,10 @@ import { setAttribute, getAttribute } from "./settergetter.mjs";
6
7
  * @param {Object} source origin of the data to be copied
7
8
  * @param {Object} definitions attribute definitions to be used
8
9
  * @param {function?} cb callback to be executed for each copied value
9
- * @param {string?} prefix name parefix
10
10
  */
11
- export function setAttributes(object, source, definitions, cb, prefix) {
12
- for (let [name, def] of Object.entries(definitions)) {
13
- if (prefix !== undefined) {
14
- name = prefix + name;
15
- }
16
-
17
- if (def.attributes) {
18
- setAttributes(object, source, def.attributes, cb, name + ".");
19
- continue;
20
- }
11
+ export function setAttributes(object, source, definitions, cb) {
12
+ for (const [path, def] of attributeIterator(definitions)) {
13
+ const name = path.join(".");
21
14
 
22
15
  let value = getAttribute(source, name);
23
16
 
@@ -52,12 +45,13 @@ export function setAttributes(object, source, definitions, cb, prefix) {
52
45
  export function getAttributes(object, definitions) {
53
46
  const result = {};
54
47
 
55
- Object.keys(definitions).forEach(name => {
48
+ for (const [path, def] of attributeIterator(definitions)) {
49
+ const name = path.join(".");
50
+
56
51
  const value = getAttribute(object, name);
57
52
  if (value !== undefined) {
58
53
  result[name] = value;
59
54
  }
60
- });
61
-
55
+ }
62
56
  return result;
63
57
  }
@@ -0,0 +1,10 @@
1
+ import { attributeIterator } from "./attributes.mjs";
2
+ import { getAttribute } from "./settergetter.mjs";
3
+
4
+ export function createPropertiesFromAttributes(object, attributes, values) {
5
+ for (const [path, def] of attributeIterator(attributes)) {
6
+ const name = path.join(".");
7
+
8
+ let value = getAttribute(values, name, def);
9
+ }
10
+ }
@@ -23,8 +23,9 @@ import { parse } from "./expression.mjs";
23
23
  * @param {Object} object
24
24
  * @param {string} expression
25
25
  * @param {any} value
26
+ * @param {Object} definition type def
26
27
  */
27
- export function setAttribute(object, expression, value) {
28
+ export function setAttribute(object, expression, value, definition) {
28
29
  const { path } = parse({ tokens: tokens(expression) });
29
30
 
30
31
  let anchor, anchorKey;
@@ -5,3 +5,9 @@
5
5
  * @return {Object} attributes
6
6
  */
7
7
  export function prepareAttributesDefinitions(newDefinitions: any, presentDefinitions: any | undefined): any;
8
+ /**
9
+ * iterate over all attributes.
10
+ * @param {Object} definition
11
+ * @param {string[]} path
12
+ */
13
+ export function attributeIterator(definition: any, path?: string[]): any;
@@ -4,9 +4,8 @@
4
4
  * @param {Object} source origin of the data to be copied
5
5
  * @param {Object} definitions attribute definitions to be used
6
6
  * @param {function?} cb callback to be executed for each copied value
7
- * @param {string?} prefix name parefix
8
7
  */
9
- export function setAttributes(object: any, source: any, definitions: any, cb: Function | null, prefix: string | null): void;
8
+ export function setAttributes(object: any, source: any, definitions: any, cb: Function | null): void;
10
9
  /**
11
10
  * Retrive attribute values from an object.
12
11
  * @param {Object} object attribute value source
@@ -0,0 +1 @@
1
+ export function createPropertiesFromAttributes(object: any, attributes: any, values: 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]