pacc 4.37.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/types.mjs +47 -24
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacc",
3
- "version": "4.37.0",
3
+ "version": "4.37.1",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/types.mjs CHANGED
@@ -1,3 +1,5 @@
1
+ import { attributeIterator } from "./attributes.mjs";
2
+
1
3
  export const types = {
2
4
  string: { name: "string", primitive: true },
3
5
  number: {
@@ -28,40 +30,61 @@ export const types = {
28
30
  object: { name: "object", extends: "base" }
29
31
  };
30
32
 
33
+ function error(message) {
34
+ throw new Error(message);
35
+ }
36
+
31
37
  export function addType(type) {
38
+ if (types[type.name]) {
39
+ return Object.assign(types[type.name], type);
40
+ }
41
+
32
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
+
33
53
  return type;
34
54
  }
35
55
 
36
56
  export function oneOfType(definition) {
37
- const aggregate = list =>
38
- list.reduce(
39
- (a, c) => a.union(c?.members ?? new Set([types[c] ?? c])),
40
- new Set()
41
- );
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
+ };
42
71
 
43
- if (Array.isArray(definition)) {
44
- const name = definition
45
- .map(t => t.name ?? t)
46
- .sort()
47
- .join("|");
72
+ if (def.members.size < 2) {
73
+ delete def.members;
74
+ }
75
+ return types[name] || addType(def);
76
+ };
48
77
 
49
- return (
50
- types[name] ||
51
- addType({
52
- name,
53
- members: aggregate(definition)
54
- })
78
+ if (Array.isArray(definition)) {
79
+ return aggregate(
80
+ definition
81
+ .map(t => t.name ?? t)
82
+ .sort()
83
+ .join("|"),
84
+ definition
55
85
  );
56
86
  } else {
57
87
  const parts = definition.split("|").sort();
58
- const name = parts.join("|");
59
- return (
60
- types[name] ||
61
- addType({
62
- name,
63
- members: aggregate(parts.map(t => types[t]))
64
- })
65
- );
88
+ return aggregate(parts.join("|"), parts);
66
89
  }
67
90
  }