@prismicio/types-internal 3.10.2-alpha.2 → 3.10.2-alpha.3

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.
@@ -107,43 +107,32 @@ function fillDocumentWithDefaultValues(customType, document) {
107
107
  const { fields } = customType && customtypes_1.StaticCustomType.is(customType)
108
108
  ? simplifyCustomType(customType)
109
109
  : customType;
110
- // Create a shallow copy of the original document
111
- const result = { ...document };
112
- // Process fields from the custom type
113
- const fieldKeys = Object.keys(fields);
114
- for (let i = 0; i < fieldKeys.length; i++) {
115
- const fieldKey = fieldKeys[i];
116
- if (!fieldKey)
117
- continue;
118
- const fieldDef = fields[fieldKey];
119
- if (!fieldDef)
120
- continue;
110
+ return Object.entries(fields).reduce((updatedDocument, [fieldKey, fieldDef]) => {
121
111
  const fieldContent = document[fieldKey];
122
- let updatedField;
123
- switch (fieldDef.type) {
124
- case "Group":
125
- updatedField = (0, fields_1.isGroupContent)(fieldContent)
126
- ? (0, fields_1.groupContentWithDefaultValues)(fieldDef, fieldContent)
127
- : fieldContent;
128
- break;
129
- case "Choice":
130
- case "Slices":
131
- updatedField = (0, fields_1.isSlicesContent)(fieldContent)
132
- ? (0, fields_1.slicesContentWithDefaultValues)(fieldDef, fieldContent)
133
- : fieldContent;
134
- break;
135
- default:
136
- updatedField =
137
- fieldContent === undefined && customtypes_1.NestableWidget.is(fieldDef)
112
+ const updatedField = (() => {
113
+ switch (fieldDef.type) {
114
+ case "Group":
115
+ return (0, fields_1.isGroupContent)(fieldContent)
116
+ ? (0, fields_1.groupContentWithDefaultValues)(fieldDef, fieldContent)
117
+ : fieldContent;
118
+ case "Choice":
119
+ case "Slices":
120
+ return (0, fields_1.isSlicesContent)(fieldContent)
121
+ ? (0, fields_1.slicesContentWithDefaultValues)(fieldDef, fieldContent)
122
+ : fieldContent;
123
+ default:
124
+ return fieldContent === undefined && customtypes_1.NestableWidget.is(fieldDef)
138
125
  ? (0, fields_1.NestableContentDefaultValue)(fieldDef)
139
126
  : fieldContent;
140
- break;
141
- }
142
- if (updatedField) {
143
- result[fieldKey] = updatedField;
144
- }
145
- }
146
- return result;
127
+ }
128
+ })();
129
+ return updatedField
130
+ ? {
131
+ ...updatedDocument,
132
+ [fieldKey]: updatedField,
133
+ }
134
+ : updatedDocument;
135
+ }, document);
147
136
  }
148
137
  exports.fillDocumentWithDefaultValues = fillDocumentWithDefaultValues;
149
138
  /**
@@ -165,34 +165,21 @@ function collectSharedSlices(customType) {
165
165
  exports.collectSharedSlices = collectSharedSlices;
166
166
  function traverseCustomType(args) {
167
167
  const { customType, onField } = args;
168
- // Create a new result object directly
169
- const result = Object.create(null);
170
- // Copy all properties directly
171
- result.id = customType.id;
172
- result.label = customType.label;
173
- result.repeatable = customType.repeatable;
174
- result.status = customType.status;
175
- // Copy optional properties
176
- if (customType.format !== undefined)
177
- result.format = customType.format;
178
- // Process JSON sections
179
- const newJson = Object.create(null);
168
+ const result = { ...customType };
169
+ const newJson = {};
180
170
  const keys = Object.keys(customType.json);
181
- const keyCount = keys.length;
182
- for (let i = 0; i < keyCount; i++) {
171
+ for (let i = 0; i < keys.length; i++) {
183
172
  const key = keys[i];
184
- if (!key)
185
- continue;
186
- const section = customType.json[key];
187
- if (!section)
188
- continue;
189
- // Create path array directly
190
- const newPath = [key];
191
- newJson[key] = (0, Section_1.traverseSection)({
192
- path: newPath,
193
- section,
194
- onField,
195
- });
173
+ if (key) {
174
+ const section = customType.json[key];
175
+ if (section) {
176
+ newJson[key] = (0, Section_1.traverseSection)({
177
+ path: [key],
178
+ section,
179
+ onField,
180
+ });
181
+ }
182
+ }
196
183
  }
197
184
  result.json = newJson;
198
185
  return result;
@@ -22,53 +22,46 @@ exports.Sections = {
22
22
  };
23
23
  function traverseSection(args) {
24
24
  const { path: prevPath, section, onField } = args;
25
- const result = Object.create(null);
25
+ const result = {};
26
26
  const keys = Object.keys(section);
27
- const keyCount = keys.length;
28
- for (let i = 0; i < keyCount; i++) {
27
+ for (let i = 0; i < keys.length; i++) {
29
28
  const key = keys[i];
30
- if (!key)
31
- continue;
32
- const prevModel = section[key];
33
- if (!prevModel)
34
- continue;
35
- // Create path array directly
36
- const pathLength = prevPath.length;
37
- const newPath = new Array(pathLength + 1);
38
- for (let j = 0; j < pathLength; j++) {
39
- newPath[j] = prevPath[j];
29
+ if (key) {
30
+ const prevModel = section[key];
31
+ if (prevModel) {
32
+ const path = [...prevPath, key];
33
+ let model;
34
+ switch (prevModel.type) {
35
+ case "Choice":
36
+ case "Slices":
37
+ model = (0, widgets_1.traverseSlices)({
38
+ path,
39
+ slices: prevModel,
40
+ onField: onField,
41
+ });
42
+ break;
43
+ case "Group":
44
+ model = onField({
45
+ path,
46
+ key,
47
+ field: (0, widgets_1.traverseGroup)({
48
+ path,
49
+ group: prevModel,
50
+ onField: onField,
51
+ }),
52
+ });
53
+ break;
54
+ default:
55
+ model = onField({
56
+ path,
57
+ key,
58
+ field: prevModel,
59
+ });
60
+ break;
61
+ }
62
+ result[key] = model;
63
+ }
40
64
  }
41
- newPath[pathLength] = key;
42
- let model;
43
- switch (prevModel.type) {
44
- case "Choice":
45
- case "Slices":
46
- model = (0, widgets_1.traverseSlices)({
47
- path: newPath,
48
- slices: prevModel,
49
- onField: onField,
50
- });
51
- break;
52
- case "Group":
53
- model = onField({
54
- path: newPath,
55
- key,
56
- field: (0, widgets_1.traverseGroup)({
57
- path: newPath,
58
- group: prevModel,
59
- onField: onField,
60
- }),
61
- });
62
- break;
63
- default:
64
- model = onField({
65
- path: newPath,
66
- key,
67
- field: prevModel,
68
- });
69
- break;
70
- }
71
- result[key] = model;
72
65
  }
73
66
  return result;
74
67
  }
@@ -34,119 +34,81 @@ exports.Group = createGroup(t.union([NestableWidget_1.NestableWidget, exports.Ne
34
34
  function traverseNestedGroup(args) {
35
35
  var _a;
36
36
  const { path: prevPath, group, onField } = args;
37
- // If no fields, return the group as is to avoid unnecessary object creation
38
- if (!((_a = group.config) === null || _a === void 0 ? void 0 : _a.fields)) {
39
- return group;
40
- }
41
- // Create result object only once with direct property assignment
42
- const result = Object.create(null);
43
- result.type = group.type;
44
- // Copy non-config properties directly
45
- if (group.fieldset !== undefined)
46
- result.fieldset = group.fieldset;
47
- if (group.icon !== undefined)
48
- result.icon = group.icon;
49
- if (group.description !== undefined)
50
- result.description = group.description;
51
- // Create config object
52
- const config = Object.create(null);
53
- if (group.config.label !== undefined)
54
- config.label = group.config.label;
55
- if (group.config.repeat !== undefined)
56
- config.repeat = group.config.repeat;
57
- // Process fields
58
- const fields = Object.create(null);
59
- const fieldEntries = group.config.fields;
60
- const keys = Object.keys(fieldEntries);
61
- const keyCount = keys.length;
62
- for (let i = 0; i < keyCount; i++) {
63
- const key = keys[i];
64
- if (!key)
65
- continue;
66
- const model = fieldEntries[key];
67
- if (!model)
68
- continue;
69
- // Create new path array directly
70
- const pathLength = prevPath.length;
71
- const newPath = new Array(pathLength + 1);
72
- for (let j = 0; j < pathLength; j++) {
73
- newPath[j] = prevPath[j];
37
+ let fields = undefined;
38
+ if ((_a = group.config) === null || _a === void 0 ? void 0 : _a.fields) {
39
+ fields = {};
40
+ const fieldEntries = group.config.fields;
41
+ const keys = Object.keys(fieldEntries);
42
+ for (let i = 0; i < keys.length; i++) {
43
+ const key = keys[i];
44
+ if (key) {
45
+ const model = fieldEntries[key];
46
+ if (model) {
47
+ // Create new path array instead of using concat
48
+ const newPath = [...prevPath, key];
49
+ fields[key] = onField({
50
+ path: newPath,
51
+ key,
52
+ field: model,
53
+ });
54
+ }
55
+ }
74
56
  }
75
- newPath[pathLength] = key;
76
- fields[key] = onField({
77
- path: newPath,
78
- key,
79
- field: model,
80
- });
81
57
  }
82
- config.fields = fields;
83
- result.config = config;
84
- return result;
58
+ return {
59
+ ...group,
60
+ ...(group.config && {
61
+ config: {
62
+ ...group.config,
63
+ ...(fields && { fields }),
64
+ },
65
+ }),
66
+ };
85
67
  }
86
68
  exports.traverseNestedGroup = traverseNestedGroup;
87
69
  function traverseGroup(args) {
88
70
  var _a;
89
71
  const { path: prevPath, group, onField } = args;
90
- // If no fields, return the group as is to avoid unnecessary object creation
91
- if (!((_a = group.config) === null || _a === void 0 ? void 0 : _a.fields)) {
92
- return group;
93
- }
94
- // Create result object only once with direct property assignment
95
- const result = Object.create(null);
96
- result.type = group.type;
97
- // Copy non-config properties directly
98
- if (group.fieldset !== undefined)
99
- result.fieldset = group.fieldset;
100
- if (group.icon !== undefined)
101
- result.icon = group.icon;
102
- if (group.description !== undefined)
103
- result.description = group.description;
104
- // Create config object
105
- const config = Object.create(null);
106
- if (group.config.label !== undefined)
107
- config.label = group.config.label;
108
- if (group.config.repeat !== undefined)
109
- config.repeat = group.config.repeat;
110
- // Process fields
111
- const fields = Object.create(null);
112
- const fieldEntries = group.config.fields;
113
- const keys = Object.keys(fieldEntries);
114
- const keyCount = keys.length;
115
- for (let i = 0; i < keyCount; i++) {
116
- const key = keys[i];
117
- if (!key)
118
- continue;
119
- const prevField = fieldEntries[key];
120
- if (!prevField)
121
- continue;
122
- // Create new path array directly
123
- const pathLength = prevPath.length;
124
- const newPath = new Array(pathLength + 1);
125
- for (let j = 0; j < pathLength; j++) {
126
- newPath[j] = prevPath[j];
127
- }
128
- newPath[pathLength] = key;
129
- let field;
130
- switch (prevField.type) {
131
- case "Group":
132
- field = traverseNestedGroup({
72
+ let fields = undefined;
73
+ if ((_a = group.config) === null || _a === void 0 ? void 0 : _a.fields) {
74
+ fields = {};
75
+ const fieldEntries = group.config.fields;
76
+ const keys = Object.keys(fieldEntries);
77
+ for (let i = 0; i < keys.length; i++) {
78
+ const key = keys[i];
79
+ if (key) {
80
+ const prevField = fieldEntries[key];
81
+ // Create new path array instead of using concat
82
+ const newPath = [...prevPath, key];
83
+ let field;
84
+ switch (prevField.type) {
85
+ case "Group":
86
+ field = traverseNestedGroup({
87
+ path: newPath,
88
+ group: prevField,
89
+ onField: onField,
90
+ });
91
+ break;
92
+ default:
93
+ field = prevField;
94
+ break;
95
+ }
96
+ fields[key] = onField({
133
97
  path: newPath,
134
- group: prevField,
135
- onField: onField,
98
+ key,
99
+ field,
136
100
  });
137
- break;
138
- default:
139
- field = prevField;
140
- break;
101
+ }
141
102
  }
142
- fields[key] = onField({
143
- path: newPath,
144
- key,
145
- field,
146
- });
147
103
  }
148
- config.fields = fields;
149
- result.config = config;
150
- return result;
104
+ return {
105
+ ...group,
106
+ ...(group.config && {
107
+ config: {
108
+ ...group.config,
109
+ ...(fields && { fields }),
110
+ },
111
+ }),
112
+ };
151
113
  }
152
114
  exports.traverseGroup = traverseGroup;
@@ -49,88 +49,55 @@ function isDynamicSharedSlice(slice) {
49
49
  exports.isDynamicSharedSlice = isDynamicSharedSlice;
50
50
  function traverseVariation(args) {
51
51
  const { path: prevPath, variation, onField } = args;
52
- // If no primary or items, return the variation as is
53
- if (!variation.primary && !variation.items) {
54
- return variation;
55
- }
56
- // Create result object directly
57
- const result = Object.create(null);
58
- // Copy all basic properties
59
- result.id = variation.id;
60
- result.name = variation.name;
61
- result.description = variation.description;
62
- result.imageUrl = variation.imageUrl;
63
- result.docURL = variation.docURL;
64
- result.version = variation.version;
65
- // Copy optional properties if they exist
66
- if (variation.display !== undefined)
67
- result.display = variation.display;
68
- // Process primary fields if they exist
52
+ const result = { ...variation };
69
53
  if (variation.primary) {
70
- const primary = Object.create(null);
54
+ const primary = {};
71
55
  const keys = Object.keys(variation.primary);
72
- const keyCount = keys.length;
73
- for (let i = 0; i < keyCount; i++) {
56
+ for (let i = 0; i < keys.length; i++) {
74
57
  const key = keys[i];
75
- if (!key)
76
- continue;
77
- const prevField = variation.primary[key];
78
- if (!prevField)
79
- continue;
80
- // Create path array directly
81
- const pathLength = prevPath.length;
82
- const newPath = new Array(pathLength + 2);
83
- for (let j = 0; j < pathLength; j++) {
84
- newPath[j] = prevPath[j];
85
- }
86
- newPath[pathLength] = "primary";
87
- newPath[pathLength + 1] = key;
88
- let field;
89
- switch (prevField.type) {
90
- case "Group":
91
- field = (0, Group_1.traverseGroup)({
92
- path: newPath,
93
- group: prevField,
94
- onField: onField,
58
+ if (key) {
59
+ const prevField = variation.primary[key];
60
+ if (prevField) {
61
+ const path = [...prevPath, "primary", key];
62
+ let field;
63
+ switch (prevField.type) {
64
+ case "Group":
65
+ field = (0, Group_1.traverseGroup)({
66
+ path,
67
+ group: prevField,
68
+ onField: onField,
69
+ });
70
+ break;
71
+ default:
72
+ field = prevField;
73
+ break;
74
+ }
75
+ primary[key] = onField({
76
+ path,
77
+ key,
78
+ field,
95
79
  });
96
- break;
97
- default:
98
- field = prevField;
99
- break;
80
+ }
100
81
  }
101
- primary[key] = onField({
102
- path: newPath,
103
- key,
104
- field,
105
- });
106
82
  }
107
83
  result.primary = primary;
108
84
  }
109
- // Process items fields if they exist
110
85
  if (variation.items) {
111
- const items = Object.create(null);
86
+ const items = {};
112
87
  const keys = Object.keys(variation.items);
113
- const keyCount = keys.length;
114
- for (let i = 0; i < keyCount; i++) {
88
+ for (let i = 0; i < keys.length; i++) {
115
89
  const key = keys[i];
116
- if (!key)
117
- continue;
118
- const field = variation.items[key];
119
- if (!field)
120
- continue;
121
- // Create path array directly
122
- const pathLength = prevPath.length;
123
- const newPath = new Array(pathLength + 2);
124
- for (let j = 0; j < pathLength; j++) {
125
- newPath[j] = prevPath[j];
90
+ if (key) {
91
+ const field = variation.items[key];
92
+ if (field) {
93
+ const path = [...prevPath, "items", key];
94
+ items[key] = onField({
95
+ path,
96
+ key,
97
+ field,
98
+ });
99
+ }
126
100
  }
127
- newPath[pathLength] = "items";
128
- newPath[pathLength + 1] = key;
129
- items[key] = onField({
130
- path: newPath,
131
- key,
132
- field,
133
- });
134
101
  }
135
102
  result.items = items;
136
103
  }
@@ -139,36 +106,18 @@ function traverseVariation(args) {
139
106
  exports.traverseVariation = traverseVariation;
140
107
  function traverseSharedSlice(args) {
141
108
  const { path: prevPath, slice, onField } = args;
142
- // Create the result object directly
143
- const result = Object.create(null);
144
- // Copy all base properties
145
- result.id = slice.id;
146
- result.type = slice.type;
147
- result.name = slice.name;
148
- // Copy optional properties
149
- if (slice.description !== undefined)
150
- result.description = slice.description;
151
- if (slice.legacyPaths !== undefined)
152
- result.legacyPaths = slice.legacyPaths;
153
- // Process variations
154
- const variationCount = slice.variations.length;
155
- const variations = new Array(variationCount);
156
- for (let i = 0; i < variationCount; i++) {
109
+ const result = { ...slice };
110
+ const variations = [];
111
+ for (let i = 0; i < slice.variations.length; i++) {
157
112
  const variation = slice.variations[i];
158
- if (!variation)
159
- continue;
160
- // Create path array directly
161
- const pathLength = prevPath.length;
162
- const newPath = new Array(pathLength + 1);
163
- for (let j = 0; j < pathLength; j++) {
164
- newPath[j] = prevPath[j];
113
+ if (variation) {
114
+ const path = [...prevPath, variation.id];
115
+ variations.push(traverseVariation({
116
+ path,
117
+ variation,
118
+ onField,
119
+ }));
165
120
  }
166
- newPath[pathLength] = variation.id;
167
- variations[i] = traverseVariation({
168
- path: newPath,
169
- variation,
170
- onField,
171
- });
172
121
  }
173
122
  result.variations = variations;
174
123
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "3.10.2-alpha.2",
3
+ "version": "3.10.2-alpha.3",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -195,47 +195,37 @@ export function fillDocumentWithDefaultValues(
195
195
  ? simplifyCustomType(customType)
196
196
  : customType
197
197
 
198
- // Create a shallow copy of the original document
199
- const result = { ...document }
200
-
201
- // Process fields from the custom type
202
- const fieldKeys = Object.keys(fields)
203
- for (let i = 0; i < fieldKeys.length; i++) {
204
- const fieldKey = fieldKeys[i]
205
- if (!fieldKey) continue
206
-
207
- const fieldDef = fields[fieldKey]
208
- if (!fieldDef) continue
209
-
210
- const fieldContent = document[fieldKey]
211
- let updatedField
212
-
213
- switch (fieldDef.type) {
214
- case "Group":
215
- updatedField = isGroupContent(fieldContent)
216
- ? groupContentWithDefaultValues(fieldDef, fieldContent)
217
- : fieldContent
218
- break
219
- case "Choice":
220
- case "Slices":
221
- updatedField = isSlicesContent(fieldContent)
222
- ? slicesContentWithDefaultValues(fieldDef, fieldContent)
223
- : fieldContent
224
- break
225
- default:
226
- updatedField =
227
- fieldContent === undefined && NestableWidget.is(fieldDef)
228
- ? NestableContentDefaultValue(fieldDef)
229
- : fieldContent
230
- break
231
- }
232
-
233
- if (updatedField) {
234
- result[fieldKey] = updatedField
235
- }
236
- }
237
-
238
- return result
198
+ return Object.entries(fields).reduce<Document>(
199
+ (updatedDocument, [fieldKey, fieldDef]) => {
200
+ const fieldContent = document[fieldKey]
201
+
202
+ const updatedField = (() => {
203
+ switch (fieldDef.type) {
204
+ case "Group":
205
+ return isGroupContent(fieldContent)
206
+ ? groupContentWithDefaultValues(fieldDef, fieldContent)
207
+ : fieldContent
208
+ case "Choice":
209
+ case "Slices":
210
+ return isSlicesContent(fieldContent)
211
+ ? slicesContentWithDefaultValues(fieldDef, fieldContent)
212
+ : fieldContent
213
+ default:
214
+ return fieldContent === undefined && NestableWidget.is(fieldDef)
215
+ ? NestableContentDefaultValue(fieldDef)
216
+ : fieldContent
217
+ }
218
+ })()
219
+
220
+ return updatedField
221
+ ? {
222
+ ...updatedDocument,
223
+ [fieldKey]: updatedField,
224
+ }
225
+ : updatedDocument
226
+ },
227
+ document,
228
+ )
239
229
  }
240
230
  /**
241
231
  * @param model: Can be optional if we simply want to loop through the content
@@ -260,39 +260,22 @@ export function traverseCustomType<
260
260
  onField: OnFieldFn<UID | NestableWidget | Group | NestedGroup>
261
261
  }): T {
262
262
  const { customType, onField } = args
263
+ const result = { ...customType } as T
264
+ const newJson: Record<string, typeof customType.json[string]> = {}
263
265
 
264
- // Create a new result object directly
265
- const result = Object.create(null) as T
266
-
267
- // Copy all properties directly
268
- result.id = customType.id
269
- result.label = customType.label
270
- result.repeatable = customType.repeatable
271
- result.status = customType.status
272
-
273
- // Copy optional properties
274
- if (customType.format !== undefined) result.format = customType.format
275
-
276
- // Process JSON sections
277
- const newJson = Object.create(null)
278
266
  const keys = Object.keys(customType.json)
279
- const keyCount = keys.length
280
-
281
- for (let i = 0; i < keyCount; i++) {
267
+ for (let i = 0; i < keys.length; i++) {
282
268
  const key = keys[i]
283
- if (!key) continue
284
-
285
- const section = customType.json[key]
286
- if (!section) continue
287
-
288
- // Create path array directly
289
- const newPath = [key]
290
-
291
- newJson[key] = traverseSection({
292
- path: newPath,
293
- section,
294
- onField,
295
- })
269
+ if (key) {
270
+ const section = customType.json[key]
271
+ if (section) {
272
+ newJson[key] = traverseSection({
273
+ path: [key],
274
+ section,
275
+ onField,
276
+ })
277
+ }
278
+ }
296
279
  }
297
280
 
298
281
  result.json = newJson
@@ -51,58 +51,51 @@ export function traverseSection<
51
51
  onField: OnFieldFn<UID | NestableWidget | Group | NestedGroup>
52
52
  }): T {
53
53
  const { path: prevPath, section, onField } = args
54
- const result = Object.create(null) as T
54
+ const result = {} as T
55
55
 
56
56
  const keys = Object.keys(section)
57
- const keyCount = keys.length
58
-
59
- for (let i = 0; i < keyCount; i++) {
57
+ for (let i = 0; i < keys.length; i++) {
60
58
  const key = keys[i]
61
- if (!key) continue
59
+ if (key) {
60
+ const prevModel = section[key]
61
+ if (prevModel) {
62
+ const path = [...prevPath, key]
63
+ let model
62
64
 
63
- const prevModel = section[key]
64
- if (!prevModel) continue
65
+ switch (prevModel.type) {
66
+ case "Choice":
67
+ case "Slices":
68
+ model = traverseSlices({
69
+ path,
70
+ slices: prevModel,
71
+ onField: onField as OnFieldFn<
72
+ NestableWidget | Group | NestedGroup
73
+ >,
74
+ })
75
+ break
76
+ case "Group":
77
+ model = onField({
78
+ path,
79
+ key,
80
+ field: traverseGroup({
81
+ path,
82
+ group: prevModel,
83
+ onField: onField as OnFieldFn<NestableWidget | NestedGroup>,
84
+ }),
85
+ })
86
+ break
87
+ default:
88
+ model = onField({
89
+ path,
90
+ key,
91
+ field: prevModel,
92
+ })
93
+ break
94
+ }
65
95
 
66
- // Create path array directly
67
- const pathLength = prevPath.length
68
- const newPath = new Array(pathLength + 1)
69
- for (let j = 0; j < pathLength; j++) {
70
- newPath[j] = prevPath[j]
96
+ result[key] = model as any
97
+ }
71
98
  }
72
- newPath[pathLength] = key
73
-
74
- let model
75
-
76
- switch (prevModel.type) {
77
- case "Choice":
78
- case "Slices":
79
- model = traverseSlices({
80
- path: newPath,
81
- slices: prevModel,
82
- onField: onField as OnFieldFn<NestableWidget | Group | NestedGroup>,
83
- })
84
- break
85
- case "Group":
86
- model = onField({
87
- path: newPath,
88
- key,
89
- field: traverseGroup({
90
- path: newPath,
91
- group: prevModel,
92
- onField: onField as OnFieldFn<NestableWidget | NestedGroup>,
93
- }),
94
- })
95
- break
96
- default:
97
- model = onField({
98
- path: newPath,
99
- key,
100
- field: prevModel,
101
- })
102
- break
103
- }
104
-
105
- result[key] = model
106
99
  }
107
100
 
108
101
  return result
@@ -55,58 +55,39 @@ export function traverseNestedGroup(args: {
55
55
  onField: OnFieldFn<NestableWidget>
56
56
  }): NestedGroup {
57
57
  const { path: prevPath, group, onField } = args
58
-
59
- // If no fields, return the group as is to avoid unnecessary object creation
60
- if (!group.config?.fields) {
61
- return group
62
- }
63
-
64
- // Create result object only once with direct property assignment
65
- const result = Object.create(null)
66
- result.type = group.type
67
-
68
- // Copy non-config properties directly
69
- if (group.fieldset !== undefined) result.fieldset = group.fieldset
70
- if (group.icon !== undefined) result.icon = group.icon
71
- if (group.description !== undefined) result.description = group.description
72
-
73
- // Create config object
74
- const config = Object.create(null)
75
- if (group.config.label !== undefined) config.label = group.config.label
76
- if (group.config.repeat !== undefined) config.repeat = group.config.repeat
77
-
78
- // Process fields
79
- const fields = Object.create(null)
80
- const fieldEntries = group.config.fields
81
- const keys = Object.keys(fieldEntries)
82
- const keyCount = keys.length
83
-
84
- for (let i = 0; i < keyCount; i++) {
85
- const key = keys[i]
86
- if (!key) continue
87
-
88
- const model = fieldEntries[key]
89
- if (!model) continue
90
-
91
- // Create new path array directly
92
- const pathLength = prevPath.length
93
- const newPath = new Array(pathLength + 1)
94
- for (let j = 0; j < pathLength; j++) {
95
- newPath[j] = prevPath[j]
58
+ let fields: Record<string, NestableWidget> | undefined = undefined
59
+
60
+ if (group.config?.fields) {
61
+ fields = {}
62
+ const fieldEntries = group.config.fields
63
+ const keys = Object.keys(fieldEntries)
64
+
65
+ for (let i = 0; i < keys.length; i++) {
66
+ const key = keys[i]
67
+ if (key) {
68
+ const model = fieldEntries[key]
69
+ if (model) {
70
+ // Create new path array instead of using concat
71
+ const newPath = [...prevPath, key]
72
+ fields[key] = onField({
73
+ path: newPath,
74
+ key,
75
+ field: model,
76
+ })
77
+ }
78
+ }
96
79
  }
97
- newPath[pathLength] = key
98
-
99
- fields[key] = onField({
100
- path: newPath,
101
- key,
102
- field: model,
103
- })
104
80
  }
105
81
 
106
- config.fields = fields
107
- result.config = config
108
-
109
- return result
82
+ return {
83
+ ...group,
84
+ ...(group.config && {
85
+ config: {
86
+ ...group.config,
87
+ ...(fields && { fields }),
88
+ },
89
+ }),
90
+ }
110
91
  }
111
92
 
112
93
  export function traverseGroup(args: {
@@ -115,71 +96,51 @@ export function traverseGroup(args: {
115
96
  onField: OnFieldFn<NestableWidget | NestedGroup>
116
97
  }): Group {
117
98
  const { path: prevPath, group, onField } = args
118
-
119
- // If no fields, return the group as is to avoid unnecessary object creation
120
- if (!group.config?.fields) {
121
- return group
122
- }
123
-
124
- // Create result object only once with direct property assignment
125
- const result = Object.create(null)
126
- result.type = group.type
127
-
128
- // Copy non-config properties directly
129
- if (group.fieldset !== undefined) result.fieldset = group.fieldset
130
- if (group.icon !== undefined) result.icon = group.icon
131
- if (group.description !== undefined) result.description = group.description
132
-
133
- // Create config object
134
- const config = Object.create(null)
135
- if (group.config.label !== undefined) config.label = group.config.label
136
- if (group.config.repeat !== undefined) config.repeat = group.config.repeat
137
-
138
- // Process fields
139
- const fields = Object.create(null)
140
- const fieldEntries = group.config.fields
141
- const keys = Object.keys(fieldEntries)
142
- const keyCount = keys.length
143
-
144
- for (let i = 0; i < keyCount; i++) {
145
- const key = keys[i]
146
- if (!key) continue
147
-
148
- const prevField = fieldEntries[key]
149
- if (!prevField) continue
150
-
151
- // Create new path array directly
152
- const pathLength = prevPath.length
153
- const newPath = new Array(pathLength + 1)
154
- for (let j = 0; j < pathLength; j++) {
155
- newPath[j] = prevPath[j]
156
- }
157
- newPath[pathLength] = key
158
-
159
- let field
160
-
161
- switch (prevField.type) {
162
- case "Group":
163
- field = traverseNestedGroup({
99
+ let fields: Record<string, NestableWidget | NestedGroup> | undefined =
100
+ undefined
101
+
102
+ if (group.config?.fields) {
103
+ fields = {}
104
+ const fieldEntries = group.config.fields
105
+ const keys = Object.keys(fieldEntries)
106
+
107
+ for (let i = 0; i < keys.length; i++) {
108
+ const key = keys[i]
109
+ if (key) {
110
+ const prevField = fieldEntries[key] as NestableWidget | NestedGroup
111
+ // Create new path array instead of using concat
112
+ const newPath = [...prevPath, key] as ReadonlyArray<string>
113
+ let field
114
+
115
+ switch (prevField.type) {
116
+ case "Group":
117
+ field = traverseNestedGroup({
118
+ path: newPath,
119
+ group: prevField,
120
+ onField: onField as OnFieldFn<NestableWidget>,
121
+ })
122
+ break
123
+ default:
124
+ field = prevField
125
+ break
126
+ }
127
+
128
+ fields[key] = onField({
164
129
  path: newPath,
165
- group: prevField,
166
- onField: onField as OnFieldFn<NestableWidget>,
130
+ key,
131
+ field,
167
132
  })
168
- break
169
- default:
170
- field = prevField
171
- break
133
+ }
172
134
  }
173
-
174
- fields[key] = onField({
175
- path: newPath,
176
- key,
177
- field,
178
- })
179
135
  }
180
136
 
181
- config.fields = fields
182
- result.config = config
183
-
184
- return result
137
+ return {
138
+ ...group,
139
+ ...(group.config && {
140
+ config: {
141
+ ...group.config,
142
+ ...(fields && { fields }),
143
+ },
144
+ }),
145
+ }
185
146
  }
@@ -78,100 +78,62 @@ export function traverseVariation(args: {
78
78
  onField: OnFieldFn<NestableWidget | Group | NestedGroup>
79
79
  }): Variation {
80
80
  const { path: prevPath, variation, onField } = args
81
+ const result = { ...variation }
81
82
 
82
- // If no primary or items, return the variation as is
83
- if (!variation.primary && !variation.items) {
84
- return variation
85
- }
86
-
87
- // Create result object directly
88
- const result = Object.create(null)
89
-
90
- // Copy all basic properties
91
- result.id = variation.id
92
- result.name = variation.name
93
- result.description = variation.description
94
- result.imageUrl = variation.imageUrl
95
- result.docURL = variation.docURL
96
- result.version = variation.version
97
-
98
- // Copy optional properties if they exist
99
- if (variation.display !== undefined) result.display = variation.display
100
-
101
- // Process primary fields if they exist
102
83
  if (variation.primary) {
103
- const primary = Object.create(null)
84
+ const primary: Record<string, NestableWidget | Group> = {}
104
85
  const keys = Object.keys(variation.primary)
105
- const keyCount = keys.length
106
86
 
107
- for (let i = 0; i < keyCount; i++) {
87
+ for (let i = 0; i < keys.length; i++) {
108
88
  const key = keys[i]
109
- if (!key) continue
110
-
111
- const prevField = variation.primary[key]
112
- if (!prevField) continue
113
-
114
- // Create path array directly
115
- const pathLength = prevPath.length
116
- const newPath = new Array(pathLength + 2)
117
- for (let j = 0; j < pathLength; j++) {
118
- newPath[j] = prevPath[j]
119
- }
120
- newPath[pathLength] = "primary"
121
- newPath[pathLength + 1] = key
122
-
123
- let field
124
-
125
- switch (prevField.type) {
126
- case "Group":
127
- field = traverseGroup({
128
- path: newPath,
129
- group: prevField,
130
- onField: onField as OnFieldFn<NestableWidget | NestedGroup>,
89
+ if (key) {
90
+ const prevField = variation.primary[key]
91
+ if (prevField) {
92
+ const path = [...prevPath, "primary", key]
93
+ let field
94
+
95
+ switch (prevField.type) {
96
+ case "Group":
97
+ field = traverseGroup({
98
+ path,
99
+ group: prevField,
100
+ onField: onField as OnFieldFn<NestableWidget | NestedGroup>,
101
+ })
102
+ break
103
+ default:
104
+ field = prevField
105
+ break
106
+ }
107
+
108
+ primary[key] = onField({
109
+ path,
110
+ key,
111
+ field,
131
112
  })
132
- break
133
- default:
134
- field = prevField
135
- break
113
+ }
136
114
  }
137
-
138
- primary[key] = onField({
139
- path: newPath,
140
- key,
141
- field,
142
- })
143
115
  }
144
116
 
145
117
  result.primary = primary
146
118
  }
147
119
 
148
- // Process items fields if they exist
149
120
  if (variation.items) {
150
- const items = Object.create(null)
121
+ const items: Record<string, NestableWidget> = {}
151
122
  const keys = Object.keys(variation.items)
152
- const keyCount = keys.length
153
123
 
154
- for (let i = 0; i < keyCount; i++) {
124
+ for (let i = 0; i < keys.length; i++) {
155
125
  const key = keys[i]
156
- if (!key) continue
157
-
158
- const field = variation.items[key]
159
- if (!field) continue
160
-
161
- // Create path array directly
162
- const pathLength = prevPath.length
163
- const newPath = new Array(pathLength + 2)
164
- for (let j = 0; j < pathLength; j++) {
165
- newPath[j] = prevPath[j]
126
+ if (key) {
127
+ const field = variation.items[key]
128
+ if (field) {
129
+ const path = [...prevPath, "items", key]
130
+ items[key] = (onField as OnFieldFn<NestableWidget>)({
131
+ path,
132
+ key,
133
+ field,
134
+ })
135
+ }
166
136
  }
167
- newPath[pathLength] = "items"
168
- newPath[pathLength + 1] = key
169
-
170
- items[key] = (onField as OnFieldFn<NestableWidget>)({
171
- path: newPath,
172
- key,
173
- field,
174
- })
175
137
  }
176
138
 
177
139
  result.items = items
@@ -186,40 +148,21 @@ export function traverseSharedSlice(args: {
186
148
  onField: OnFieldFn<NestableWidget | Group | NestedGroup>
187
149
  }): SharedSlice {
188
150
  const { path: prevPath, slice, onField } = args
151
+ const result = { ...slice }
152
+ const variations: Variation[] = []
189
153
 
190
- // Create the result object directly
191
- const result = Object.create(null)
192
-
193
- // Copy all base properties
194
- result.id = slice.id
195
- result.type = slice.type
196
- result.name = slice.name
197
-
198
- // Copy optional properties
199
- if (slice.description !== undefined) result.description = slice.description
200
- if (slice.legacyPaths !== undefined) result.legacyPaths = slice.legacyPaths
201
-
202
- // Process variations
203
- const variationCount = slice.variations.length
204
- const variations = new Array(variationCount)
205
-
206
- for (let i = 0; i < variationCount; i++) {
154
+ for (let i = 0; i < slice.variations.length; i++) {
207
155
  const variation = slice.variations[i]
208
- if (!variation) continue
209
-
210
- // Create path array directly
211
- const pathLength = prevPath.length
212
- const newPath = new Array(pathLength + 1)
213
- for (let j = 0; j < pathLength; j++) {
214
- newPath[j] = prevPath[j]
156
+ if (variation) {
157
+ const path = [...prevPath, variation.id]
158
+ variations.push(
159
+ traverseVariation({
160
+ path,
161
+ variation,
162
+ onField,
163
+ }),
164
+ )
215
165
  }
216
- newPath[pathLength] = variation.id
217
-
218
- variations[i] = traverseVariation({
219
- path: newPath,
220
- variation,
221
- onField,
222
- })
223
166
  }
224
167
 
225
168
  result.variations = variations as ReadonlyArray<Variation>