pacc 10.1.1 → 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
@@ -83,6 +83,7 @@ tokens "abc" "
83
83
  * [Properties](#properties-1)
84
84
  * [default\_attribute](#default_attribute)
85
85
  * [default\_attribute\_writable](#default_attribute_writable)
86
+ * [default\_collection\_attribute\_writable](#default_collection_attribute_writable)
86
87
  * [string\_attribute](#string_attribute)
87
88
  * [string\_attribute\_writable](#string_attribute_writable)
88
89
  * [string\_collection\_attribute](#string_collection_attribute)
@@ -281,16 +282,16 @@ Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Globa
281
282
  ### Properties
282
283
 
283
284
  * `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
284
- * `type` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
285
- * `key` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 
286
- * `writable` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 
285
+ * `type` **[Type](#type)** 
286
+ * `key` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** are we an identifying attribute
287
+ * `writable` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** can this attribute be modified
287
288
  * `mandatory` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 
288
289
  * `collection` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** are we a collection (set, map, array, object)
289
- * `owner` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** are we the owner of the value
290
- * `constructor` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** (collection) constructor
291
290
  * `private` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** should the value be shown
292
291
  * `credential` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** any type of credential
293
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
294
295
  * `depends` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** name of an attribute we depend on
295
296
  * `description` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** human readable
296
297
  * `default` **any?** the default value
@@ -314,6 +315,10 @@ Type: [AttributeDefinition](#attributedefinition)
314
315
 
315
316
  Type: [AttributeDefinition](#attributedefinition)
316
317
 
318
+ ## default\_collection\_attribute\_writable
319
+
320
+ Type: [AttributeDefinition](#attributedefinition)
321
+
317
322
  ## string\_attribute
318
323
 
319
324
  Type: [AttributeDefinition](#attributedefinition)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "10.1.1",
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 {boolean} owner are we the owner of the value
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
@@ -37,7 +37,6 @@ export const default_attribute = {
37
37
  writable: false,
38
38
  mandatory: false,
39
39
  collection: false,
40
- owner: true,
41
40
  persistent: false,
42
41
  private: false,
43
42
  credential: false,
@@ -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 {boolean} owner are we the owner of the value
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 owner: boolean;
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,14 +300,6 @@ export type AttributeDefinition = {
300
300
  * are we a collection (set, map, array, object)
301
301
  */
302
302
  collection: boolean;
303
- /**
304
- * are we the owner of the value
305
- */
306
- owner: boolean;
307
- /**
308
- * (collection) constructor
309
- */
310
- constructor?: Function | undefined;
311
303
  /**
312
304
  * should the value be shown
313
305
  */
@@ -320,6 +312,11 @@ export type AttributeDefinition = {
320
312
  * should we be stored (especially critical for credentials)
321
313
  */
322
314
  persistent?: boolean | undefined;
315
+ backpointer?: AttributeDefinition | undefined;
316
+ /**
317
+ * (collection) constructor
318
+ */
319
+ constructor?: Function | undefined;
323
320
  /**
324
321
  * name of an attribute we depend on
325
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;