pacc 4.36.0 → 4.37.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "4.36.0",
3
+ "version": "4.37.1",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/types.mjs CHANGED
@@ -1,18 +1,16 @@
1
- export const baseTypes = new Set(["string", "number", "bigint", "boolean"]);
1
+ import { attributeIterator } from "./attributes.mjs";
2
2
 
3
3
  export const types = {
4
- base: { name: "base" },
5
-
6
- string: { name: "string", extends: "base" },
4
+ string: { name: "string", primitive: true },
7
5
  number: {
8
6
  name: "number",
9
- extends: "base",
7
+ primitive: true,
10
8
  prepareValue: value =>
11
9
  typeof value === "string" ? parseFloat(value) : value
12
10
  },
13
11
  boolean: {
14
12
  name: "boolean",
15
- extends: "base",
13
+ primitive: true,
16
14
  prepareValue: value =>
17
15
  !value || value === "0" || value === "false" || value === "no"
18
16
  ? false
@@ -20,53 +18,73 @@ export const types = {
20
18
  },
21
19
  integer: {
22
20
  name: "integer",
23
- extends: "base",
21
+ primitive: true,
24
22
  prepareValue: value => (typeof value === "string" ? parseInt(value) : value)
25
23
  },
26
24
  "unsigned-integer": {
27
25
  name: "unsigned-integer",
28
- prepareValue: value =>
29
- typeof value === "string" ? parseInt(value) : value,
30
- extends: "integer"
26
+ primitive: true,
27
+ prepareValue: value => (typeof value === "string" ? parseInt(value) : value)
31
28
  },
32
- url: { name: "url", extends: "string" },
29
+ url: { name: "url", primitive: true },
33
30
  object: { name: "object", extends: "base" }
34
31
  };
35
32
 
33
+ function error(message) {
34
+ throw new Error(message);
35
+ }
36
+
36
37
  export function addType(type) {
38
+ if (types[type.name]) {
39
+ return Object.assign(types[type.name], type);
40
+ }
41
+
37
42
  types[type.name] = type;
43
+
44
+ for (const [path, attribute] of attributeIterator(type.attributes)) {
45
+ if (typeof attribute.type === "string") {
46
+ attribute.type = oneOfType(attribute.type);
47
+ }
48
+ if(attribute.isKey && !type.key) {
49
+ type.key = path.join('.');
50
+ }
51
+ }
52
+
38
53
  return type;
39
54
  }
40
55
 
41
56
  export function oneOfType(definition) {
42
- const aggregate = list =>
43
- list.reduce(
44
- (a, c) => a.union(c?.members ?? new Set([types[c] ?? c])),
45
- new Set()
46
- );
57
+ const aggregate = (name, list) => {
58
+ const def = {
59
+ name,
60
+ members: list.reduce((all, type) => {
61
+ if (typeof type === "string") {
62
+ const t = types[type] || addType({ name: type });
63
+ if (!t) {
64
+ error(`Unknown type ${type} in '${definition}'`);
65
+ }
66
+ type = t;
67
+ }
68
+ return all.union(type.members ?? new Set([type]));
69
+ }, new Set())
70
+ };
47
71
 
48
- if (Array.isArray(definition)) {
49
- const name = definition
50
- .map(t => t.name ?? t)
51
- .sort()
52
- .join("|");
72
+ if (def.members.size < 2) {
73
+ delete def.members;
74
+ }
75
+ return types[name] || addType(def);
76
+ };
53
77
 
54
- return (
55
- types[name] ||
56
- addType({
57
- name,
58
- members: aggregate(definition)
59
- })
78
+ if (Array.isArray(definition)) {
79
+ return aggregate(
80
+ definition
81
+ .map(t => t.name ?? t)
82
+ .sort()
83
+ .join("|"),
84
+ definition
60
85
  );
61
86
  } else {
62
87
  const parts = definition.split("|").sort();
63
- const name = parts.join("|");
64
- return (
65
- types[name] ||
66
- addType({
67
- name,
68
- members: aggregate(parts.map(t => types[t]))
69
- })
70
- );
88
+ return aggregate(parts.join("|"), parts);
71
89
  }
72
90
  }
package/types/types.d.mts CHANGED
@@ -1,37 +1,33 @@
1
1
  export function addType(type: any): any;
2
2
  export function oneOfType(definition: any): any;
3
- export const baseTypes: Set<string>;
4
3
  export const types: {
5
- base: {
6
- name: string;
7
- };
8
4
  string: {
9
5
  name: string;
10
- extends: string;
6
+ primitive: boolean;
11
7
  };
12
8
  number: {
13
9
  name: string;
14
- extends: string;
10
+ primitive: boolean;
15
11
  prepareValue: (value: any) => any;
16
12
  };
17
13
  boolean: {
18
14
  name: string;
19
- extends: string;
15
+ primitive: boolean;
20
16
  prepareValue: (value: any) => boolean;
21
17
  };
22
18
  integer: {
23
19
  name: string;
24
- extends: string;
20
+ primitive: boolean;
25
21
  prepareValue: (value: any) => any;
26
22
  };
27
23
  "unsigned-integer": {
28
24
  name: string;
25
+ primitive: boolean;
29
26
  prepareValue: (value: any) => any;
30
- extends: string;
31
27
  };
32
28
  url: {
33
29
  name: string;
34
- extends: string;
30
+ primitive: boolean;
35
31
  };
36
32
  object: {
37
33
  name: string;