pacc 7.2.1 → 8.0.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
@@ -543,6 +543,7 @@ Split property path into tokens
543
543
  ### Parameters
544
544
 
545
545
  * `string` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
546
+ * `options` (optional, default `{}`)
546
547
 
547
548
  ## setAttribute
548
549
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "7.2.1",
3
+ "version": "8.0.1",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -59,6 +59,7 @@ export { default_attribute_writable as string_attribute_writable };
59
59
 
60
60
  export const string_collection_attribute = {
61
61
  ...default_attribute,
62
+ separator: " ",
62
63
  collection: true
63
64
  };
64
65
 
@@ -71,8 +71,9 @@ function binopError(op, left, right) {
71
71
  error(`Unexpected '${op.str || op}'`);
72
72
  }
73
73
 
74
- export function parse(input, context = { globals }) {
75
- input = tokens(input);
74
+ export function parse(input, context = {}) {
75
+ const getGlobal = context?.getGlobal ?? (a => globals[a]);
76
+ input = tokens(input, context);
76
77
 
77
78
  let node, token, value;
78
79
 
@@ -101,12 +102,12 @@ export function parse(input, context = { globals }) {
101
102
  result = r;
102
103
  } else {
103
104
  if (result === undefined) {
104
- result = context.globals?.[p];
105
+ result = getGlobal(p);
105
106
  } else {
106
107
  if (result instanceof Map) {
107
108
  result = result.get(p);
108
109
  } else {
109
- result = result[p] ?? context.globals?.[p];
110
+ result = result[p] ?? getGlobal(p);
110
111
  }
111
112
  }
112
113
  }
@@ -283,7 +284,7 @@ export function parse(input, context = { globals }) {
283
284
  const args = node.path[1].map(a =>
284
285
  typeof a === "object" ? a.eval(a, current) : a
285
286
  );
286
- return context.globals[node.path[0]](...args);
287
+ return getGlobal(node.path[0])(...args);
287
288
  };
288
289
 
289
290
  advance();
package/src/tokens.mjs CHANGED
@@ -56,7 +56,7 @@ export /** @type {Token} */ const DOUBLE_BAR = createToken("||", 30, "infixr");
56
56
  export /** @type {Token} */ const IDENTIFIER = createToken("IDENTIFIER", 0);
57
57
  export /** @type {Token} */ const EOF = createToken("EOF", -1, "eof");
58
58
 
59
- const keywords = {
59
+ export const keywords = {
60
60
  true: [true],
61
61
  false: [false]
62
62
  };
@@ -76,10 +76,14 @@ const esc = {
76
76
  * @param {string} string
77
77
  * @yields {Token}
78
78
  */
79
- export function* tokens(string) {
79
+ export function* tokens(string, options = {}) {
80
+ options.keywords ||= keywords;
81
+ options.parseFloat ||= parseFloat;
82
+
80
83
  let state, value, hex, quote;
81
84
 
82
- const keywordOrIdentifier = () => keywords[value] || [IDENTIFIER, value];
85
+ const keywordOrIdentifier = () =>
86
+ options.keywords[value] || [IDENTIFIER, value];
83
87
  const startString = c => {
84
88
  value = "";
85
89
  state = "string";
@@ -116,7 +120,7 @@ export function* tokens(string) {
116
120
  case " ":
117
121
  switch (state) {
118
122
  case "number":
119
- yield [parseFloat(value)];
123
+ yield [options.parseFloat(value)];
120
124
  state = undefined;
121
125
  case undefined:
122
126
  break;
@@ -145,7 +149,7 @@ export function* tokens(string) {
145
149
  case "'":
146
150
  switch (state) {
147
151
  case "number":
148
- yield [parseFloat(value)];
152
+ yield [options.parseFloat(value)];
149
153
  case undefined:
150
154
  startString(c);
151
155
  break;
@@ -173,7 +177,7 @@ export function* tokens(string) {
173
177
  case "|":
174
178
  switch (state) {
175
179
  case "number":
176
- yield [parseFloat(value)];
180
+ yield [options.parseFloat(value)];
177
181
  case undefined:
178
182
  state = c;
179
183
  break;
@@ -204,7 +208,7 @@ export function* tokens(string) {
204
208
  case "=":
205
209
  switch (state) {
206
210
  case "number":
207
- yield [parseFloat(value)];
211
+ yield [options.parseFloat(value)];
208
212
  case undefined:
209
213
  state = c;
210
214
  break;
@@ -222,12 +226,11 @@ export function* tokens(string) {
222
226
 
223
227
  case ".":
224
228
  if (state === "number") {
225
- value += '.';
229
+ value += ".";
226
230
  break;
227
- }
228
- else if (state === "-") {
229
- value = '-.';
230
- state="number";
231
+ } else if (state === "-") {
232
+ value = "-.";
233
+ state = "number";
231
234
  break;
232
235
  }
233
236
 
@@ -246,7 +249,7 @@ export function* tokens(string) {
246
249
  case "}":
247
250
  switch (state) {
248
251
  case "number":
249
- yield [parseFloat(value)];
252
+ yield [options.parseFloat(value)];
250
253
  case undefined:
251
254
  state = c;
252
255
  break;
@@ -282,7 +285,7 @@ export function* tokens(string) {
282
285
  case "-":
283
286
  state = "number";
284
287
  value = "-" + c;
285
- break;
288
+ break;
286
289
  case ".":
287
290
  state = "number";
288
291
  value = ".";
@@ -297,7 +300,7 @@ export function* tokens(string) {
297
300
  default:
298
301
  switch (state) {
299
302
  case "number":
300
- yield [parseFloat(value)];
303
+ yield [options.parseFloat(value)];
301
304
  case undefined:
302
305
  state = "identifier";
303
306
  value = c;
@@ -320,7 +323,7 @@ export function* tokens(string) {
320
323
  case "string":
321
324
  throw new Error("unterminated string", { cause: string });
322
325
  case "number":
323
- yield [parseFloat(value)];
326
+ yield [options.parseFloat(value)];
324
327
  break;
325
328
  case "identifier":
326
329
  yield keywordOrIdentifier();
package/src/types.mjs CHANGED
@@ -13,7 +13,26 @@ const emptyStringIsUndefined = value =>
13
13
  typeof value === "string" && value.length === 0 ? undefined : value;
14
14
 
15
15
  export const types = {
16
- string: { name: "string", primitive: true },
16
+ string: {
17
+ name: "string",
18
+ primitive: true,
19
+ toInternal: (value, attribute) => {
20
+ if (value !== undefined) {
21
+ if (attribute.collection) {
22
+ return value.split(attribute.separator);
23
+ }
24
+ }
25
+ return value;
26
+ },
27
+ toExternal: (value, attribute) => {
28
+ if (value !== undefined) {
29
+ if (attribute.collection) {
30
+ return (Array.isArray(value) ? value : [...value]).join(attribute.separator);
31
+ }
32
+ }
33
+ return value;
34
+ }
35
+ },
17
36
  number: {
18
37
  name: "number",
19
38
  primitive: true,
@@ -55,7 +74,8 @@ export const types = {
55
74
  name: "duration",
56
75
  primitive: true,
57
76
  toInternal: value => parseDuration(value),
58
- toExternal: value => value === undefined ? undefined : formatDuration(value)
77
+ toExternal: value =>
78
+ value === undefined ? undefined : formatDuration(value)
59
79
  },
60
80
  duration_ms: {
61
81
  name: "duration_ms",
@@ -31,6 +31,7 @@ export const default_attribute: AttributeDefinition;
31
31
  */
32
32
  export const default_attribute_writable: AttributeDefinition;
33
33
  export namespace string_collection_attribute {
34
+ export let separator: string;
34
35
  export let collection: boolean;
35
36
  export let type: object;
36
37
  export let isKey: boolean;
@@ -1,18 +1,5 @@
1
1
  export function binop(op: any, left: any, right: any, fallback: any): any;
2
- export function parse(input: any, context?: {
3
- globals: {
4
- in: (a: any, b: any) => boolean;
5
- ceil: (x: number) => number;
6
- floor: (x: number) => number;
7
- abs: (x: number) => number;
8
- min: (...values: number[]) => number;
9
- max: (...values: number[]) => number;
10
- uppercase: (a: any) => any;
11
- lowercase: (a: any) => any;
12
- substring: (s: any, a: any, b: any) => any;
13
- length: (s: any) => any;
14
- };
15
- }): any;
2
+ export function parse(input: any, context?: {}): any;
16
3
  export namespace globals {
17
4
  export function _in(a: any, b: any): boolean;
18
5
  export { _in as in };
@@ -4,7 +4,7 @@
4
4
  * @param {string} string
5
5
  * @yields {Token}
6
6
  */
7
- export function tokens(string: string): Generator<any, void, unknown>;
7
+ export function tokens(string: string, options?: {}): Generator<any, void, unknown>;
8
8
  export namespace PLUS {
9
9
  let str: string;
10
10
  }
@@ -35,6 +35,12 @@ export namespace BAR { }
35
35
  export namespace DOUBLE_BAR { }
36
36
  export namespace IDENTIFIER { }
37
37
  export namespace EOF { }
38
+ export namespace keywords {
39
+ let _true: boolean[];
40
+ export { _true as true };
41
+ let _false: boolean[];
42
+ export { _false as false };
43
+ }
38
44
  export type Token = {
39
45
  str: string;
40
46
  };
package/types/types.d.mts CHANGED
@@ -6,6 +6,8 @@ export const types: {
6
6
  string: {
7
7
  name: string;
8
8
  primitive: boolean;
9
+ toInternal: (value: any, attribute: any) => any;
10
+ toExternal: (value: any, attribute: any) => any;
9
11
  };
10
12
  number: {
11
13
  name: string;