pacc 4.10.0 → 4.11.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/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "4.10.0",
3
+ "version": "4.11.0",
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
  ".": {
@@ -15,12 +15,14 @@ const types = {
15
15
  * @param {Object|undefined} presentDefinitions optional merg in attributes
16
16
  * @return {Object} attributes
17
17
  */
18
- export function prepareAttributesDefinitions(newDefinitions, presentDefinitions) {
18
+ export function prepareAttributesDefinitions(
19
+ newDefinitions,
20
+ presentDefinitions
21
+ ) {
19
22
  for (const [name, d] of Object.entries(newDefinitions)) {
20
23
  if (d.attributes === undefined) {
21
24
  d.type = types[d.type] || types.base;
22
- }
23
- else {
25
+ } else {
24
26
  prepareAttributesDefinitions(d.attributes);
25
27
  }
26
28
  }
@@ -50,4 +52,23 @@ function mergeAttributeDefinitions(dest, atts) {
50
52
  }
51
53
 
52
54
  return dest;
53
- }
55
+ }
56
+
57
+ /**
58
+ * iterate over all attributes.
59
+ * @param {Object} definition
60
+ * @param {string[]} path
61
+ */
62
+ export function* attributeIterator(definition, path = []) {
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
+ }
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
  }
@@ -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