pacc 10.2.0 → 10.3.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
@@ -287,11 +287,11 @@ Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Globa
287
287
  * `writable` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** can this attribute be modified
288
288
  * `mandatory` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 
289
289
  * `collection` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** are we a collection (set, map, array, object)
290
- * `owner` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** are we the owner of the value
291
- * `constructor` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** (collection) constructor
292
290
  * `private` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** should the value be shown
293
291
  * `credential` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** any type of credential
294
292
  * `persistent` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** should we be stored (especially critical for credentials)
293
+ * `backpointer` **[AttributeDefinition](#attributedefinition)?** 
294
+ * `constructor` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** (collection) constructor
295
295
  * `depends` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** name of an attribute we depend on
296
296
  * `description` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** human readable
297
297
  * `default` **any?** the default value
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "10.2.0",
3
+ "version": "10.3.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -9,11 +9,11 @@ import { types } from "./types.mjs";
9
9
  * @property {boolean} writable can this attribute be modified
10
10
  * @property {boolean} mandatory
11
11
  * @property {boolean} collection are we a collection (set, map, array, object)
12
- * @property {AttributeDefinition} [backpointer]
13
- * @property {Function} [constructor] (collection) constructor
14
12
  * @property {boolean} [private] should the value be shown
15
13
  * @property {boolean} [credential] any type of credential
16
14
  * @property {boolean} [persistent] should we be stored (especially critical for credentials)
15
+ * @property {AttributeDefinition} [backpointer]
16
+ * @property {Function} [constructor] (collection) constructor
17
17
  * @property {string} [depends] name of an attribute we depend on
18
18
  * @property {string} [description] human readable
19
19
  * @property {any} [default] the default value
@@ -0,0 +1,42 @@
1
+ import { extendingAttributeIterator } from "pacc";
2
+
3
+ export function extract(object, type = object.constructor) {
4
+ const result = {};
5
+ for (const [path, attribute] of extendingAttributeIterator(
6
+ type,
7
+ attribute => !attribute.private
8
+ )) {
9
+ const name = path.join(".");
10
+ const value = object[name];
11
+
12
+ if (value !== undefined) {
13
+ if (attribute.type.primitive) {
14
+ if (attribute.collection) {
15
+ if ((value.size ?? value.length) > 0) {
16
+ result[name] = [...value.values()];
17
+ }
18
+ } else {
19
+ result[name] = value;
20
+ }
21
+ } else {
22
+ const key = value.constructor.key;
23
+
24
+ if (attribute.backpointer) {
25
+ if (attribute.collection) {
26
+ if ((value.size ?? value.length) > 0) {
27
+ result[name] = Object.fromEntries(
28
+ [...value.values()].map(v => [v[key], v])
29
+ );
30
+ }
31
+ } else {
32
+ result[name] = extract(value);
33
+ }
34
+ } else {
35
+ result[name] = { [key]: value[key], type: value.constructor.name };
36
+ }
37
+ }
38
+ }
39
+ }
40
+
41
+ return result;
42
+ }
package/src/module.mjs CHANGED
@@ -2,6 +2,7 @@ export * from "./types.mjs";
2
2
  export * from "./time.mjs";
3
3
  export * from "./bytes.mjs";
4
4
  export * from "./attributes.mjs";
5
+ export * from "./extract.mjs";
5
6
  export * from "./tokens.mjs";
6
7
  export * from "./filter.mjs";
7
8
  export * from "./multiple.mjs";
package/src/multiple.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { setAttribute, getAttribute } from "./settergetter.mjs";
2
2
  import { attributeIterator, toExternal } from "./attributes.mjs";
3
3
  import { default_attribute } from "./common-attributes.mjs";
4
+
4
5
  /**
5
6
  * Copies attribute values from a source object into a destination object.
6
7
  * @param {Object} object target object to be modified
@@ -9,25 +10,25 @@ import { default_attribute } from "./common-attributes.mjs";
9
10
  * @param {function?} cb callback to be executed for each copied value
10
11
  */
11
12
  export function setAttributes(object, source, definitions, cb) {
12
- for (const [path, def] of attributeIterator(definitions)) {
13
+ for (const [path, attribute] of attributeIterator(definitions)) {
13
14
  const name = path.join(".");
14
15
 
15
16
  let value = getAttribute(source, name);
16
17
 
17
18
  if (value === undefined) {
18
19
  if (
19
- def.default === undefined ||
20
+ attribute.default === undefined ||
20
21
  getAttribute(object, name) !== undefined
21
22
  ) {
22
23
  continue;
23
24
  }
24
- value = def.default;
25
+ value = attribute.default;
25
26
  }
26
27
 
27
- setAttribute(object, name, value, def);
28
+ setAttribute(object, name, value, attribute);
28
29
 
29
30
  if (cb) {
30
- cb(def, name, value);
31
+ cb(attribute, name, value);
31
32
  }
32
33
  }
33
34
  }
@@ -42,12 +43,11 @@ export function setAttributes(object, source, definitions, cb) {
42
43
  export function getAttributes(object, definitions, filter) {
43
44
  const result = {};
44
45
 
45
- for (const [path, def] of attributeIterator(definitions, filter)) {
46
+ for (const [path, attribute] of attributeIterator(definitions, filter)) {
46
47
  const name = path.join(".");
47
-
48
- const value = getAttribute(object, name, def);
48
+ const value = getAttribute(object, name, attribute);
49
49
  if (value !== undefined) {
50
- setAttribute(result, name, value, def);
50
+ setAttribute(result, name, value, attribute);
51
51
  }
52
52
  }
53
53
  return result;
@@ -63,16 +63,16 @@ export function getAttributes(object, definitions, filter) {
63
63
  export function getAttributesJSON(object, definitions, filter) {
64
64
  const result = {};
65
65
 
66
- for (const [path, def] of attributeIterator(definitions, filter)) {
66
+ for (const [path, attribute] of attributeIterator(definitions, filter)) {
67
67
  const name = path.join(".");
68
68
 
69
- let value = getAttribute(object, name, def);
69
+ let value = getAttribute(object, name, attribute);
70
70
  if (value !== undefined) {
71
71
  value = toExternal(value);
72
72
  if (value instanceof Set) {
73
73
  value = [...value];
74
74
  }
75
- setAttribute(result, def.externalName ?? name, value, default_attribute);
75
+ setAttribute(result, attribute.externalName ?? name, value, default_attribute);
76
76
  }
77
77
  }
78
78
  return result;
package/src/types.mjs CHANGED
@@ -98,8 +98,8 @@ export const types = {
98
98
  },
99
99
  url: {
100
100
  name: "url",
101
- toInternal: emptyStringIsUndefined,
102
- primitive: true
101
+ primitive: true,
102
+ toInternal: emptyStringIsUndefined
103
103
  },
104
104
  object: { name: "object", primitive: false }
105
105
  };
@@ -193,11 +193,17 @@ export function resolveTypeLinks() {
193
193
  type.owners = type.owners.map(owner => raiseOnUnknownType(owner, type));
194
194
  }
195
195
 
196
- for (const [path, attribute] of attributeIterator(type.attributes)) {
197
- if (typeof attribute.type === "string") {
198
- attribute.type = oneOfType(attribute.type);
199
- }
196
+ for (const [path, attribute] of attributeIterator(
197
+ type.attributes,
198
+ attribute => typeof attribute.type === "string"
199
+ )) {
200
+ attribute.type = oneOfType(attribute.type);
200
201
  }
201
202
  }
202
203
  }
203
204
 
205
+ export function create(type, owner, data) {
206
+ const factory = type.factoryFor?.(owner, data) || type;
207
+ return new factory(data);
208
+ }
209
+
@@ -7,11 +7,11 @@
7
7
  * @property {boolean} writable can this attribute be modified
8
8
  * @property {boolean} mandatory
9
9
  * @property {boolean} collection are we a collection (set, map, array, object)
10
- * @property {AttributeDefinition} [backpointer]
11
- * @property {Function} [constructor] (collection) constructor
12
10
  * @property {boolean} [private] should the value be shown
13
11
  * @property {boolean} [credential] any type of credential
14
12
  * @property {boolean} [persistent] should we be stored (especially critical for credentials)
13
+ * @property {AttributeDefinition} [backpointer]
14
+ * @property {Function} [constructor] (collection) constructor
15
15
  * @property {string} [depends] name of an attribute we depend on
16
16
  * @property {string} [description] human readable
17
17
  * @property {any} [default] the default value
@@ -130,12 +130,12 @@ export namespace yesno_attribute_writable {
130
130
  export let key: boolean;
131
131
  export let mandatory: boolean;
132
132
  export let collection: boolean;
133
- export let backpointer: AttributeDefinition | undefined;
134
- export let constructor: Function | undefined;
135
133
  let _private: boolean | undefined;
136
134
  export { _private as private };
137
135
  export let credential: boolean | undefined;
138
136
  export let persistent: boolean | undefined;
137
+ export let backpointer: AttributeDefinition | undefined;
138
+ export let constructor: Function | undefined;
139
139
  export let depends: string | undefined;
140
140
  export let description: string | undefined;
141
141
  let _default: any;
@@ -300,11 +300,6 @@ export type AttributeDefinition = {
300
300
  * are we a collection (set, map, array, object)
301
301
  */
302
302
  collection: boolean;
303
- backpointer?: AttributeDefinition | undefined;
304
- /**
305
- * (collection) constructor
306
- */
307
- constructor?: Function | undefined;
308
303
  /**
309
304
  * should the value be shown
310
305
  */
@@ -317,6 +312,11 @@ export type AttributeDefinition = {
317
312
  * should we be stored (especially critical for credentials)
318
313
  */
319
314
  persistent?: boolean | undefined;
315
+ backpointer?: AttributeDefinition | undefined;
316
+ /**
317
+ * (collection) constructor
318
+ */
319
+ constructor?: Function | undefined;
320
320
  /**
321
321
  * name of an attribute we depend on
322
322
  */
@@ -0,0 +1 @@
1
+ export function extract(object: any, type?: any): {};
@@ -2,6 +2,7 @@ export * from "./types.mjs";
2
2
  export * from "./time.mjs";
3
3
  export * from "./bytes.mjs";
4
4
  export * from "./attributes.mjs";
5
+ export * from "./extract.mjs";
5
6
  export * from "./tokens.mjs";
6
7
  export * from "./filter.mjs";
7
8
  export * from "./multiple.mjs";
package/types/types.d.mts CHANGED
@@ -1,6 +1,7 @@
1
1
  export function addType(type: any): any;
2
2
  export function oneOfType(definition: any): any;
3
3
  export function resolveTypeLinks(): void;
4
+ export function create(type: any, owner: any, data: any): any;
4
5
  export const types: {
5
6
  string: {
6
7
  name: string;
@@ -52,8 +53,8 @@ export const types: {
52
53
  };
53
54
  url: {
54
55
  name: string;
55
- toInternal: (value: any) => any;
56
56
  primitive: boolean;
57
+ toInternal: (value: any) => any;
57
58
  };
58
59
  object: {
59
60
  name: string;