pacc 6.9.0 → 7.0.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
@@ -90,34 +90,38 @@ const result = getAttribute({ a: [0,{ b: 4 }]}, "a[1].b");
90
90
  * [expand](#expand)
91
91
  * [Parameters](#parameters-5)
92
92
  * [promises](#promises)
93
- * [filter](#filter)
93
+ * [error](#error)
94
94
  * [Parameters](#parameters-6)
95
- * [setAttributes](#setattributes)
95
+ * [binopError](#binoperror)
96
96
  * [Parameters](#parameters-7)
97
- * [getAttributes](#getattributes)
97
+ * [filter](#filter)
98
98
  * [Parameters](#parameters-8)
99
- * [getAttributesJSON](#getattributesjson)
99
+ * [setAttributes](#setattributes)
100
100
  * [Parameters](#parameters-9)
101
+ * [getAttributes](#getattributes)
102
+ * [Parameters](#parameters-10)
103
+ * [getAttributesJSON](#getattributesjson)
104
+ * [Parameters](#parameters-11)
101
105
  * [tokens](#tokens)
102
106
  * [tokens](#tokens-1)
103
- * [Parameters](#parameters-10)
107
+ * [Parameters](#parameters-12)
104
108
  * [setAttribute](#setattribute)
105
- * [Parameters](#parameters-11)
109
+ * [Parameters](#parameters-13)
106
110
  * [getAttribute](#getattribute)
107
- * [Parameters](#parameters-12)
111
+ * [Parameters](#parameters-14)
108
112
  * [getAttributeAndOperator](#getattributeandoperator)
109
- * [Parameters](#parameters-13)
113
+ * [Parameters](#parameters-15)
110
114
  * [parseDuration](#parseduration)
111
- * [Parameters](#parameters-14)
115
+ * [Parameters](#parameters-16)
112
116
  * [formatDuration](#formatduration)
113
- * [Parameters](#parameters-15)
117
+ * [Parameters](#parameters-17)
114
118
  * [formatDurationISO](#formatdurationiso)
115
- * [Parameters](#parameters-16)
119
+ * [Parameters](#parameters-18)
116
120
  * [lookup](#lookup)
117
121
  * [Token](#token)
118
122
  * [Properties](#properties-1)
119
123
  * [createToken](#createtoken)
120
- * [Parameters](#parameters-17)
124
+ * [Parameters](#parameters-19)
121
125
  * [PLUS](#plus)
122
126
  * [MINUS](#minus)
123
127
  * [STAR](#star)
@@ -149,7 +153,7 @@ const result = getAttribute({ a: [0,{ b: 4 }]}, "a[1].b");
149
153
  * [Type](#type)
150
154
  * [Properties](#properties-2)
151
155
  * [raiseOnUnknownType](#raiseonunknowntype)
152
- * [Parameters](#parameters-18)
156
+ * [Parameters](#parameters-20)
153
157
 
154
158
  ## prepareAttributesDefinitions
155
159
 
@@ -461,6 +465,20 @@ Returns **any** 
461
465
 
462
466
  Type: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<any>>
463
467
 
468
+ ## error
469
+
470
+ ### Parameters
471
+
472
+ * `message` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**&#x20;
473
+
474
+ ## binopError
475
+
476
+ ### Parameters
477
+
478
+ * `op` **[Token](#token)**&#x20;
479
+ * `left` **AST**&#x20;
480
+ * `right` **AST**&#x20;
481
+
464
482
  ## filter
465
483
 
466
484
  Generate filter function.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "6.9.0",
3
+ "version": "7.0.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -70,11 +70,23 @@ export function* writableAttributeIterator(definition) {
70
70
  yield* attributeIterator(definition, filterWritable);
71
71
  }
72
72
 
73
- export function prepareValue(value, attribute) {
73
+ export function toInternal(value, attribute) {
74
74
  if (attribute) {
75
- const prepareValue = attribute.prepareValue ?? attribute.type?.prepareValue;
76
- if (prepareValue) {
77
- return prepareValue(value, attribute);
75
+ const toInternal = attribute.toInternal ?? attribute.type?.toInternal;
76
+ if (toInternal) {
77
+ return toInternal(value, attribute);
78
+ }
79
+ }
80
+ return value;
81
+ }
82
+
83
+ export { toInternal as prepareValue }; // DEPRECATED
84
+
85
+ export function toExternal(value, attribute) {
86
+ if (attribute) {
87
+ const toExternal = attribute.toExternal ?? attribute.type?.toExternal;
88
+ if (toExternal) {
89
+ return toExternal(value, attribute);
78
90
  }
79
91
  }
80
92
  return value;
@@ -16,7 +16,8 @@ import { types } from "./types.mjs";
16
16
  * @property {any} [default] the default value
17
17
  * @property {Function} [set] set the value
18
18
  * @property {Function} [get] get the value can be used to calculate default values
19
- * @property {Function} [prepareValue]
19
+ * @property {Function} [toInternal]
20
+ * @property {Function} [toExternal]
20
21
  * @property {Set<any>} [values] allowed values
21
22
  * @property {string} [externalName] attribute name used by external system
22
23
  * @property {string[]|string} [env] environment variable(s) used to provide the value
@@ -178,11 +179,21 @@ export { boolean_attribute_writable_true as active_attribute };
178
179
  /**
179
180
  * @type {AttributeDefinition}
180
181
  */
181
- export const empty_attribute = {
182
- ...default_attribute,
183
- type: types.boolean
182
+ export const yesno_attribute = {
183
+ ...boolean_attribute,
184
+ type: types.yesno
185
+ };
186
+
187
+ export const yesno_attribute_writable = {
188
+ ...yesno_attribute,
189
+ writable: true
184
190
  };
185
191
 
192
+ /**
193
+ * @type {AttributeDefinition}
194
+ */
195
+ export { boolean_attribute as empty_attribute };
196
+
186
197
  /**
187
198
  * @type {AttributeDefinition}
188
199
  */
@@ -53,19 +53,39 @@ export function binop(op, left, right, fallback) {
53
53
  return fallback(op, left, right);
54
54
  }
55
55
 
56
+ /**
57
+ *
58
+ * @param {string} message
59
+ */
60
+ function error(message) {
61
+ const error = new Error(message);
62
+ throw error;
63
+ }
64
+ /**
65
+ *
66
+ * @param {Token} op
67
+ * @param {AST} left
68
+ * @param {AST} right
69
+ */
70
+ function binopError(op, left, right) {
71
+ error(`Unexpected '${op.str || op}'`);
72
+ }
73
+
56
74
  export function parse(input, context = { globals }) {
57
75
  input = tokens(input);
58
76
 
59
77
  let node, token, value;
60
78
 
61
- function error(message) {
62
- const error = new Error(message);
63
- throw error;
64
- }
65
- function binopError(op, left, right) {
66
- error(`Unexpected '${op.str || op}'`);
67
- }
68
-
79
+ const predicateIteratorEval = (node, current) => {
80
+ if (current instanceof Set) {
81
+ current = [...current];
82
+ } else if (current instanceof Map) {
83
+ current = [...current.values()];
84
+ }
85
+ return current
86
+ .filter(item => node.predicate(node, item))
87
+ .map(item => node.right.eval(node.right, item));
88
+ };
69
89
  const pathEval = (node, current) => {
70
90
  let result = current;
71
91
 
@@ -207,15 +227,26 @@ export function parse(input, context = { globals }) {
207
227
  }
208
228
  }
209
229
  if (last === DOT) {
210
- if (left.path) {
211
- left.path.push(...right.path);
212
- return left;
213
- }
214
230
  switch (typeof left) {
215
231
  case "number":
216
232
  right.path.unshift(left);
217
233
  return right;
218
234
  }
235
+
236
+ if (left.path) {
237
+ left.path.push(...right.path);
238
+ return left;
239
+ }
240
+
241
+ if (left.eval) {
242
+ return {
243
+ eval: predicateIteratorEval,
244
+ predicate: left.eval,
245
+ right
246
+ // path: right.path
247
+ };
248
+ }
249
+
219
250
  return { eval: pathEval, path: [left.token, right.token] };
220
251
  }
221
252
 
@@ -1,6 +1,6 @@
1
1
  import { attributeIterator } from "./attributes.mjs";
2
2
  import { getAttribute, setAttribute } from "./settergetter.mjs";
3
- import { prepareValue } from "./attributes.mjs";
3
+ import { toInternal } from "./attributes.mjs";
4
4
 
5
5
  function findPropertyDescriptor(obj, name) {
6
6
  let descriptor;
@@ -29,7 +29,7 @@ export function definePropertiesFromAttributes(
29
29
  attribute.default;
30
30
 
31
31
  if (value !== undefined) {
32
- value = prepareValue(value, attribute);
32
+ value = toInternal(value, attribute);
33
33
 
34
34
  if (path.length === 1) {
35
35
  const property = properties[name];
@@ -17,7 +17,7 @@ import {
17
17
  IDENTIFIER
18
18
  } from "./tokens.mjs";
19
19
  import { parse } from "./expression.mjs";
20
- import { prepareValue } from "./attributes.mjs";
20
+ import { toInternal } from "./attributes.mjs";
21
21
 
22
22
  /**
23
23
  * Set object attribute.
@@ -49,7 +49,7 @@ export function setAttribute(object, expression, value, definition) {
49
49
  }
50
50
 
51
51
  if (anchor) {
52
- anchor[anchorKey] = prepareValue(value, definition);
52
+ anchor[anchorKey] = toInternal(value, definition);
53
53
  }
54
54
  }
55
55
 
package/src/types.mjs CHANGED
@@ -6,7 +6,7 @@ import { parseBytes } from "./bytes.mjs";
6
6
  * @typedef {Object} Type
7
7
  * @property {string} name
8
8
  * @property {boolean} [primitive]
9
- * @property {Function} [prepareValue]
9
+ * @property {Function} [toInternal]
10
10
  */
11
11
 
12
12
  const emptyStringIsUndefined = value =>
@@ -17,45 +17,54 @@ export const types = {
17
17
  number: {
18
18
  name: "number",
19
19
  primitive: true,
20
- prepareValue: value =>
20
+ toInternal: value =>
21
21
  typeof value === "string" ? parseFloat(value) : value
22
22
  },
23
23
  boolean: {
24
24
  name: "boolean",
25
25
  primitive: true,
26
- prepareValue: value =>
26
+ toInternal: value =>
27
27
  !value || value === "0" || value === "false" || value === "no"
28
28
  ? false
29
29
  : true
30
30
  },
31
+ yesno: {
32
+ name: "yesno",
33
+ primitive: true,
34
+ toInternal: value =>
35
+ !value || value === "0" || value === "false" || value === "no"
36
+ ? false
37
+ : true,
38
+ toExternal: value => value ? "yes" : "no"
39
+ },
31
40
  integer: {
32
41
  name: "integer",
33
42
  primitive: true,
34
- prepareValue: value => (typeof value === "string" ? parseInt(value) : value)
43
+ toInternal: value => (typeof value === "string" ? parseInt(value) : value)
35
44
  },
36
45
  "unsigned-integer": {
37
46
  name: "unsigned-integer",
38
47
  primitive: true,
39
- prepareValue: value => (typeof value === "string" ? parseInt(value) : value)
48
+ toInternal: value => (typeof value === "string" ? parseInt(value) : value)
40
49
  },
41
50
  duration: {
42
51
  name: "duration",
43
52
  primitive: true,
44
- prepareValue: value => parseDuration(value)
53
+ toInternal: value => parseDuration(value)
45
54
  },
46
55
  duration_ms: {
47
56
  name: "duration_ms",
48
57
  primitive: true,
49
- prepareValue: value => parseDuration(value) * 1000
58
+ toInternal: value => parseDuration(value) * 1000
50
59
  },
51
60
  byte_size: {
52
61
  name: "byte_size",
53
62
  primitive: true,
54
- prepareValue: parseBytes
63
+ toInternal: parseBytes
55
64
  },
56
65
  url: {
57
66
  name: "url",
58
- prepareValue: emptyStringIsUndefined,
67
+ toInternal: emptyStringIsUndefined,
59
68
  primitive: true
60
69
  },
61
70
  object: { name: "object", primitive: false }
@@ -14,6 +14,8 @@ export function prepareAttributesDefinitions(newDefinitions: any, presentDefinit
14
14
  */
15
15
  export function attributeIterator(definition: any, filter?: Function, path?: string[]): Iterable<[string[], object]>;
16
16
  export function writableAttributeIterator(definition: any): Generator<[string[], any], void, any>;
17
- export function prepareValue(value: any, attribute: any): any;
17
+ export function toInternal(value: any, attribute: any): any;
18
+ export function toExternal(value: any, attribute: any): any;
18
19
  export function mandatoryAttributesPresent(object: any, attributes: any): boolean;
19
20
  export function filterWritable(name: any, attribute: any): any;
21
+ export { toInternal as prepareValue };
@@ -14,7 +14,8 @@
14
14
  * @property {any} [default] the default value
15
15
  * @property {Function} [set] set the value
16
16
  * @property {Function} [get] get the value can be used to calculate default values
17
- * @property {Function} [prepareValue]
17
+ * @property {Function} [toInternal]
18
+ * @property {Function} [toExternal]
18
19
  * @property {Set<any>} [values] allowed values
19
20
  * @property {string} [externalName] attribute name used by external system
20
21
  * @property {string[]|string} [env] environment variable(s) used to provide the value
@@ -45,7 +46,8 @@ export namespace string_collection_attribute {
45
46
  export { _default as default };
46
47
  export let set: Function;
47
48
  export let get: Function;
48
- export let prepareValue: Function;
49
+ export let toInternal: Function;
50
+ export let toExternal: Function;
49
51
  export let values: Set<any>;
50
52
  export let externalName: string;
51
53
  export let env: string[] | string;
@@ -103,7 +105,13 @@ export const boolean_attribute_false: AttributeDefinition;
103
105
  /**
104
106
  * @type {AttributeDefinition}
105
107
  */
106
- export const empty_attribute: AttributeDefinition;
108
+ export const yesno_attribute: AttributeDefinition;
109
+ export namespace yesno_attribute_writable {
110
+ let writable_1: boolean;
111
+ export { writable_1 as writable };
112
+ let collection_1: boolean;
113
+ export { collection_1 as collection };
114
+ }
107
115
  /**
108
116
  * @type {AttributeDefinition}
109
117
  */
@@ -140,11 +148,9 @@ export namespace bytes_size_attribute {
140
148
  let type_1: {
141
149
  name: string;
142
150
  primitive: boolean;
143
- prepareValue: typeof import("./bytes.mjs").parseBytes;
151
+ toInternal: typeof import("./bytes.mjs").parseBytes;
144
152
  };
145
153
  export { type_1 as type };
146
- let collection_1: boolean;
147
- export { collection_1 as collection };
148
154
  }
149
155
  /**
150
156
  * @type {AttributeDefinition}
@@ -240,7 +246,8 @@ export type AttributeDefinition = {
240
246
  * get the value can be used to calculate default values
241
247
  */
242
248
  get?: Function;
243
- prepareValue?: Function;
249
+ toInternal?: Function;
250
+ toExternal?: Function;
244
251
  /**
245
252
  * allowed values
246
253
  */
@@ -258,4 +265,4 @@ export type AttributeDefinition = {
258
265
  */
259
266
  additionalValues?: object;
260
267
  };
261
- export { default_attribute as string_attribute, default_attribute_writable as string_attribute_writable, description_attribute as description_attribute_writable, default_attribute as type_attribute, default_attribute_writable as state_attribute_writable, boolean_attribute_writable_true as active_attribute, secret_attribute as username_attribute, secret_attribute as password_attribute, secret_attribute as token_attribute, secret_attribute as certificate_attribute, integer_attribute as count_attribute, integer_attribute_writable as count_attribute_writable, integer_attribute as size_attribute, default_attribute_writable as body_attribute_writable };
268
+ export { default_attribute as string_attribute, default_attribute_writable as string_attribute_writable, description_attribute as description_attribute_writable, default_attribute as type_attribute, default_attribute_writable as state_attribute_writable, boolean_attribute_writable_true as active_attribute, boolean_attribute as empty_attribute, secret_attribute as username_attribute, secret_attribute as password_attribute, secret_attribute as token_attribute, secret_attribute as certificate_attribute, integer_attribute as count_attribute, integer_attribute_writable as count_attribute_writable, integer_attribute as size_attribute, default_attribute_writable as body_attribute_writable };
package/types/types.d.mts CHANGED
@@ -10,41 +10,47 @@ export const types: {
10
10
  number: {
11
11
  name: string;
12
12
  primitive: boolean;
13
- prepareValue: (value: any) => any;
13
+ toInternal: (value: any) => any;
14
14
  };
15
15
  boolean: {
16
16
  name: string;
17
17
  primitive: boolean;
18
- prepareValue: (value: any) => boolean;
18
+ toInternal: (value: any) => boolean;
19
+ };
20
+ yesno: {
21
+ name: string;
22
+ primitive: boolean;
23
+ toInternal: (value: any) => boolean;
24
+ toExternal: (value: any) => "no" | "yes";
19
25
  };
20
26
  integer: {
21
27
  name: string;
22
28
  primitive: boolean;
23
- prepareValue: (value: any) => any;
29
+ toInternal: (value: any) => any;
24
30
  };
25
31
  "unsigned-integer": {
26
32
  name: string;
27
33
  primitive: boolean;
28
- prepareValue: (value: any) => any;
34
+ toInternal: (value: any) => any;
29
35
  };
30
36
  duration: {
31
37
  name: string;
32
38
  primitive: boolean;
33
- prepareValue: (value: any) => number;
39
+ toInternal: (value: any) => number;
34
40
  };
35
41
  duration_ms: {
36
42
  name: string;
37
43
  primitive: boolean;
38
- prepareValue: (value: any) => number;
44
+ toInternal: (value: any) => number;
39
45
  };
40
46
  byte_size: {
41
47
  name: string;
42
48
  primitive: boolean;
43
- prepareValue: typeof parseBytes;
49
+ toInternal: typeof parseBytes;
44
50
  };
45
51
  url: {
46
52
  name: string;
47
- prepareValue: (value: any) => any;
53
+ toInternal: (value: any) => any;
48
54
  primitive: boolean;
49
55
  };
50
56
  object: {
@@ -55,6 +61,6 @@ export const types: {
55
61
  export type Type = {
56
62
  name: string;
57
63
  primitive?: boolean;
58
- prepareValue?: Function;
64
+ toInternal?: Function;
59
65
  };
60
66
  import { parseBytes } from "./bytes.mjs";