@prismicio/types-internal 3.11.0 → 3.11.2-alpha.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.
@@ -165,16 +165,18 @@ function collectSharedSlices(customType) {
165
165
  exports.collectSharedSlices = collectSharedSlices;
166
166
  function traverseCustomType(args) {
167
167
  const { customType, onField } = args;
168
- return {
169
- ...customType,
170
- json: Object.entries(customType.json).reduce((acc, [key, section]) => {
171
- acc[key] = (0, Section_1.traverseSection)({
172
- path: [key],
173
- section,
174
- onField,
175
- });
176
- return acc;
177
- }, {}),
178
- };
168
+ const json = {};
169
+ let changed = false;
170
+ for (const [key, section] of Object.entries(customType.json)) {
171
+ const newSection = (0, Section_1.traverseSection)({
172
+ path: [key],
173
+ section,
174
+ onField,
175
+ });
176
+ if (!changed && newSection !== section)
177
+ changed = true;
178
+ json[key] = newSection;
179
+ }
180
+ return changed ? { ...customType, json } : customType;
179
181
  }
180
182
  exports.traverseCustomType = traverseCustomType;
@@ -21,9 +21,11 @@ exports.Sections = {
21
21
  },
22
22
  };
23
23
  function traverseSection(args) {
24
- const { path: prevPath, section, onField } = args;
25
- return Object.entries(section).reduce((acc, [key, prevModel]) => {
26
- const path = prevPath.concat(key);
24
+ const { path: prevPath, section: prevSection, onField } = args;
25
+ const section = {};
26
+ let changed = false;
27
+ for (const [key, prevModel] of Object.entries(prevSection)) {
28
+ const path = [...prevPath, key];
27
29
  let model;
28
30
  switch (prevModel.type) {
29
31
  case "Choice":
@@ -53,8 +55,10 @@ function traverseSection(args) {
53
55
  });
54
56
  break;
55
57
  }
56
- acc[key] = model;
57
- return acc;
58
- }, {});
58
+ if (!changed && model !== prevModel)
59
+ changed = true;
60
+ section[key] = model;
61
+ }
62
+ return changed ? section : prevSection;
59
63
  }
60
64
  exports.traverseSection = traverseSection;
@@ -34,61 +34,71 @@ 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
- const fields = ((_a = group.config) === null || _a === void 0 ? void 0 : _a.fields) &&
38
- Object.entries(group.config.fields).reduce((acc, [key, model]) => {
39
- const path = prevPath.concat(key);
40
- acc[key] = onField({
41
- path,
42
- key,
43
- field: model,
44
- });
45
- return acc;
46
- }, {});
47
- return {
48
- ...group,
49
- ...(group.config && {
37
+ if (!((_a = group.config) === null || _a === void 0 ? void 0 : _a.fields))
38
+ return group;
39
+ const fields = {};
40
+ let changed = false;
41
+ for (const [key, field] of Object.entries(group.config.fields)) {
42
+ const path = [...prevPath, key];
43
+ const newField = onField({
44
+ path,
45
+ key,
46
+ field,
47
+ });
48
+ if (!changed && field !== newField)
49
+ changed = true;
50
+ fields[key] = newField;
51
+ }
52
+ return changed
53
+ ? {
54
+ ...group,
50
55
  config: {
51
56
  ...group.config,
52
- ...(fields && { fields }),
57
+ fields,
53
58
  },
54
- }),
55
- };
59
+ }
60
+ : group;
56
61
  }
57
62
  exports.traverseNestedGroup = traverseNestedGroup;
58
63
  function traverseGroup(args) {
59
64
  var _a;
60
65
  const { path: prevPath, group, onField } = args;
61
- const fields = ((_a = group.config) === null || _a === void 0 ? void 0 : _a.fields) &&
62
- Object.entries(group.config.fields).reduce((acc, [key, prevField]) => {
63
- const path = prevPath.concat(key);
64
- let field;
65
- switch (prevField.type) {
66
- case "Group":
67
- field = traverseNestedGroup({
68
- path,
69
- group: prevField,
70
- onField: onField,
71
- });
72
- break;
73
- default:
74
- field = prevField;
75
- break;
76
- }
77
- acc[key] = onField({
78
- path,
79
- key,
80
- field,
81
- });
82
- return acc;
83
- }, {});
84
- return {
85
- ...group,
86
- ...(group.config && {
66
+ if (!((_a = group.config) === null || _a === void 0 ? void 0 : _a.fields))
67
+ return group;
68
+ const fields = {};
69
+ let changed = false;
70
+ for (const [key, prevField] of Object.entries(group.config.fields)) {
71
+ const path = [...prevPath, key];
72
+ let field;
73
+ switch (prevField.type) {
74
+ case "Group":
75
+ field = traverseNestedGroup({
76
+ path,
77
+ group: prevField,
78
+ onField: onField,
79
+ });
80
+ break;
81
+ default:
82
+ field = prevField;
83
+ break;
84
+ }
85
+ const newField = onField({
86
+ path,
87
+ key,
88
+ field,
89
+ });
90
+ if (!changed && field !== newField)
91
+ changed = true;
92
+ fields[key] = newField;
93
+ }
94
+ return changed
95
+ ? {
96
+ ...group,
87
97
  config: {
88
98
  ...group.config,
89
- ...(fields && { fields }),
99
+ fields,
90
100
  },
91
- }),
92
- };
101
+ }
102
+ : group;
93
103
  }
94
104
  exports.traverseGroup = traverseGroup;
@@ -29,27 +29,32 @@ function isCompositeSlice(slice) {
29
29
  }
30
30
  exports.isCompositeSlice = isCompositeSlice;
31
31
  function traverseCompositeSlice(args) {
32
+ var _a, _b;
32
33
  const { path: prevPath, slice, onField } = args;
33
- const nonRepeat = slice["non-repeat"] &&
34
- Object.entries(slice["non-repeat"]).reduce((acc, [key, field]) => {
35
- const path = prevPath.concat("non-repeat", key);
36
- acc[key] = onField({ path, key, field });
37
- return acc;
38
- }, {});
39
- const repeat = slice.repeat &&
40
- Object.entries(slice.repeat).reduce((acc, [key, field]) => {
41
- const path = prevPath.concat("repeat", key);
42
- acc[key] = onField({ path, key, field });
43
- return acc;
44
- }, {});
45
- return {
46
- ...slice,
47
- ...(nonRepeat && {
48
- "non-repeat": nonRepeat,
49
- }),
50
- ...(repeat && {
51
- repeat,
52
- }),
53
- };
34
+ const nonRepeat = {};
35
+ let nonRepeatChanged = false;
36
+ for (const [key, field] of Object.entries((_a = slice["non-repeat"]) !== null && _a !== void 0 ? _a : {})) {
37
+ const path = [...prevPath, "non-repeat", key];
38
+ const newField = onField({ path, key, field });
39
+ if (!nonRepeatChanged && field !== newField)
40
+ nonRepeatChanged = true;
41
+ nonRepeat[key] = newField;
42
+ }
43
+ const repeat = {};
44
+ let repeatChanged = false;
45
+ for (const [key, field] of Object.entries((_b = slice.repeat) !== null && _b !== void 0 ? _b : {})) {
46
+ const path = [...prevPath, "repeat", key];
47
+ const newField = onField({ path, key, field });
48
+ if (!repeatChanged && field !== newField)
49
+ repeatChanged = true;
50
+ repeat[key] = newField;
51
+ }
52
+ return nonRepeatChanged || repeatChanged
53
+ ? {
54
+ ...slice,
55
+ ...(nonRepeatChanged && { "non-repeat": nonRepeat }),
56
+ ...(repeatChanged && { repeat }),
57
+ }
58
+ : slice;
54
59
  }
55
60
  exports.traverseCompositeSlice = traverseCompositeSlice;
@@ -48,59 +48,72 @@ function isDynamicSharedSlice(slice) {
48
48
  }
49
49
  exports.isDynamicSharedSlice = isDynamicSharedSlice;
50
50
  function traverseVariation(args) {
51
+ var _a, _b;
51
52
  const { path: prevPath, variation, onField } = args;
52
- return {
53
- ...variation,
54
- ...(variation.primary && {
55
- primary: Object.entries(variation.primary).reduce((acc, [key, prevField]) => {
56
- const path = prevPath.concat("primary", key);
57
- let field;
58
- switch (prevField.type) {
59
- case "Group":
60
- field = (0, Group_1.traverseGroup)({
61
- path,
62
- group: prevField,
63
- onField: onField,
64
- });
65
- break;
66
- default:
67
- field = prevField;
68
- break;
69
- }
70
- acc[key] = onField({
53
+ const primary = {};
54
+ let primaryChanged = false;
55
+ for (const [key, prevField] of Object.entries((_a = variation.primary) !== null && _a !== void 0 ? _a : {})) {
56
+ const path = [...prevPath, "primary", key];
57
+ let field;
58
+ switch (prevField.type) {
59
+ case "Group":
60
+ field = (0, Group_1.traverseGroup)({
71
61
  path,
72
- key,
73
- field,
62
+ group: prevField,
63
+ onField: onField,
74
64
  });
75
- return acc;
76
- }, {}),
77
- }),
78
- ...(variation.items && {
79
- items: Object.entries(variation.items).reduce((acc, [key, field]) => {
80
- const path = prevPath.concat("items", key);
81
- acc[key] = onField({
82
- path,
83
- key,
84
- field,
85
- });
86
- return acc;
87
- }, {}),
88
- }),
89
- };
65
+ break;
66
+ default:
67
+ field = prevField;
68
+ break;
69
+ }
70
+ const newField = onField({ path, key, field });
71
+ if (!primaryChanged && field !== newField)
72
+ primaryChanged = true;
73
+ primary[key] = newField;
74
+ }
75
+ const items = {};
76
+ let itemsChanged = false;
77
+ for (const [key, prevField] of Object.entries((_b = variation.items) !== null && _b !== void 0 ? _b : {})) {
78
+ const path = [...prevPath, "items", key];
79
+ const newField = onField({
80
+ path,
81
+ key,
82
+ field: prevField,
83
+ });
84
+ if (!itemsChanged && prevField !== newField)
85
+ itemsChanged = true;
86
+ items[key] = newField;
87
+ }
88
+ return primaryChanged || itemsChanged
89
+ ? {
90
+ ...variation,
91
+ ...(primaryChanged && { primary }),
92
+ ...(itemsChanged && { items }),
93
+ }
94
+ : variation;
90
95
  }
91
96
  exports.traverseVariation = traverseVariation;
92
97
  function traverseSharedSlice(args) {
93
98
  const { path: prevPath, slice, onField } = args;
94
- return {
95
- ...slice,
96
- variations: slice.variations.map((variation) => {
97
- const path = prevPath.concat(variation.id);
98
- return traverseVariation({
99
- path,
100
- variation,
101
- onField,
102
- });
103
- }),
104
- };
99
+ const variations = [];
100
+ let changed = false;
101
+ for (const variation of slice.variations) {
102
+ const path = [...prevPath, variation.id];
103
+ const newVariation = traverseVariation({
104
+ path,
105
+ variation,
106
+ onField,
107
+ });
108
+ if (!changed && newVariation !== variation)
109
+ changed = true;
110
+ variations.push(newVariation);
111
+ }
112
+ return changed
113
+ ? {
114
+ ...slice,
115
+ variations,
116
+ }
117
+ : slice;
105
118
  }
106
119
  exports.traverseSharedSlice = traverseSharedSlice;
@@ -82,59 +82,63 @@ exports.Slices = {
82
82
  function traverseSlices(args) {
83
83
  var _a;
84
84
  const { path: prevPath, slices, onField } = args;
85
- const choices = ((_a = slices.config) === null || _a === void 0 ? void 0 : _a.choices) &&
86
- Object.entries(slices.config.choices).reduce((acc, [key, prevModel]) => {
87
- const path = prevPath.concat(key);
88
- let model;
89
- switch (prevModel.type) {
90
- case "Slice":
91
- model = (0, CompositeSlice_1.traverseCompositeSlice)({
85
+ if (!((_a = slices.config) === null || _a === void 0 ? void 0 : _a.choices))
86
+ return slices;
87
+ const choices = {};
88
+ let changed = false;
89
+ for (const [key, prevModel] of Object.entries(slices.config.choices)) {
90
+ const path = [...prevPath, key];
91
+ let model;
92
+ switch (prevModel.type) {
93
+ case "Slice":
94
+ model = (0, CompositeSlice_1.traverseCompositeSlice)({
95
+ path,
96
+ slice: prevModel,
97
+ onField: onField,
98
+ });
99
+ break;
100
+ case "SharedSlice":
101
+ if ("variations" in prevModel)
102
+ model = (0, SharedSlice_1.traverseSharedSlice)({
92
103
  path,
93
104
  slice: prevModel,
94
- onField: onField,
95
- });
96
- break;
97
- case "SharedSlice":
98
- if ("variations" in prevModel)
99
- model = (0, SharedSlice_1.traverseSharedSlice)({
100
- path,
101
- slice: prevModel,
102
- onField,
103
- });
104
- else
105
- model = prevModel;
106
- break;
107
- // Group and other fields are technically possible because of legacy slices.
108
- case "Group":
109
- model = onField({
110
- path,
111
- key,
112
- field: (0, Group_1.traverseNestedGroup)({
113
- path,
114
- group: prevModel,
115
- onField: onField,
116
- }),
105
+ onField,
117
106
  });
118
- break;
119
- default:
120
- model = onField({
107
+ else
108
+ model = prevModel;
109
+ break;
110
+ // Group and other fields are technically possible because of legacy slices.
111
+ case "Group":
112
+ model = onField({
113
+ path,
114
+ key,
115
+ field: (0, Group_1.traverseNestedGroup)({
121
116
  path,
122
- key,
123
- field: prevModel,
124
- });
125
- break;
126
- }
127
- acc[key] = model;
128
- return acc;
129
- }, {});
130
- return {
131
- ...slices,
132
- ...(slices.config && {
117
+ group: prevModel,
118
+ onField: onField,
119
+ }),
120
+ });
121
+ break;
122
+ default:
123
+ model = onField({
124
+ path,
125
+ key,
126
+ field: prevModel,
127
+ });
128
+ break;
129
+ }
130
+ if (!changed && model !== prevModel)
131
+ changed = true;
132
+ choices[key] = model;
133
+ }
134
+ return changed
135
+ ? {
136
+ ...slices,
133
137
  config: {
134
138
  ...slices.config,
135
- ...(choices && { choices }),
139
+ choices,
136
140
  },
137
- }),
138
- };
141
+ }
142
+ : slices;
139
143
  }
140
144
  exports.traverseSlices = traverseSlices;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "3.11.0",
3
+ "version": "3.11.2-alpha.0",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -260,17 +260,18 @@ export function traverseCustomType<
260
260
  onField: OnFieldFn<UID | NestableWidget | Group | NestedGroup>
261
261
  }): T {
262
262
  const { customType, onField } = args
263
- return {
264
- ...customType,
265
- json: Object.entries(customType.json).reduce<
266
- Record<string, typeof customType.json[string]>
267
- >((acc, [key, section]) => {
268
- acc[key] = traverseSection({
269
- path: [key],
270
- section,
271
- onField,
272
- })
273
- return acc
274
- }, {}),
263
+
264
+ const json: Record<string, typeof customType.json[string]> = {}
265
+ let changed = false
266
+ for (const [key, section] of Object.entries(customType.json)) {
267
+ const newSection = traverseSection({
268
+ path: [key],
269
+ section,
270
+ onField,
271
+ })
272
+ if (!changed && newSection !== section) changed = true
273
+ json[key] = newSection
275
274
  }
275
+
276
+ return changed ? { ...customType, json } : customType
276
277
  }
@@ -50,9 +50,12 @@ export function traverseSection<
50
50
  section: T
51
51
  onField: OnFieldFn<UID | NestableWidget | Group | NestedGroup>
52
52
  }): T {
53
- const { path: prevPath, section, onField } = args
54
- return Object.entries(section).reduce((acc, [key, prevModel]) => {
55
- const path = prevPath.concat(key)
53
+ const { path: prevPath, section: prevSection, onField } = args
54
+
55
+ const section: T = {} as T
56
+ let changed = false
57
+ for (const [key, prevModel] of Object.entries(prevSection)) {
58
+ const path = [...prevPath, key]
56
59
  let model
57
60
  switch (prevModel.type) {
58
61
  case "Choice":
@@ -82,7 +85,9 @@ export function traverseSection<
82
85
  })
83
86
  break
84
87
  }
85
- acc[key] = model
86
- return acc
87
- }, {} as T)
88
+ if (!changed && model !== prevModel) changed = true
89
+ section[key] = model
90
+ }
91
+
92
+ return changed ? section : prevSection
88
93
  }
@@ -55,29 +55,32 @@ export function traverseNestedGroup(args: {
55
55
  onField: OnFieldFn<NestableWidget>
56
56
  }): NestedGroup {
57
57
  const { path: prevPath, group, onField } = args
58
- const fields =
59
- group.config?.fields &&
60
- Object.entries(group.config.fields).reduce<Record<string, NestableWidget>>(
61
- (acc, [key, model]) => {
62
- const path = prevPath.concat(key)
63
- acc[key] = onField({
64
- path,
65
- key,
66
- field: model,
67
- })
68
- return acc
69
- },
70
- {},
71
- )
72
- return {
73
- ...group,
74
- ...(group.config && {
75
- config: {
76
- ...group.config,
77
- ...(fields && { fields }),
78
- },
79
- }),
58
+
59
+ if (!group.config?.fields) return group
60
+
61
+ const fields: Record<string, NestableWidget> = {}
62
+ let changed = false
63
+ for (const [key, field] of Object.entries(group.config.fields)) {
64
+ const path = [...prevPath, key]
65
+ const newField = onField({
66
+ path,
67
+ key,
68
+ field,
69
+ })
70
+
71
+ if (!changed && field !== newField) changed = true
72
+ fields[key] = newField
80
73
  }
74
+
75
+ return changed
76
+ ? {
77
+ ...group,
78
+ config: {
79
+ ...group.config,
80
+ fields,
81
+ },
82
+ }
83
+ : group
81
84
  }
82
85
 
83
86
  export function traverseGroup(args: {
@@ -86,39 +89,43 @@ export function traverseGroup(args: {
86
89
  onField: OnFieldFn<NestableWidget | NestedGroup>
87
90
  }): Group {
88
91
  const { path: prevPath, group, onField } = args
89
- const fields =
90
- group.config?.fields &&
91
- Object.entries(group.config.fields).reduce<
92
- Record<string, NestableWidget | NestedGroup>
93
- >((acc, [key, prevField]) => {
94
- const path = prevPath.concat(key)
95
- let field
96
- switch (prevField.type) {
97
- case "Group":
98
- field = traverseNestedGroup({
99
- path,
100
- group: prevField,
101
- onField: onField as OnFieldFn<NestableWidget>,
102
- })
103
- break
104
- default:
105
- field = prevField
106
- break
107
- }
108
- acc[key] = onField({
109
- path,
110
- key,
111
- field,
112
- })
113
- return acc
114
- }, {})
115
- return {
116
- ...group,
117
- ...(group.config && {
118
- config: {
119
- ...group.config,
120
- ...(fields && { fields }),
121
- },
122
- }),
92
+
93
+ if (!group.config?.fields) return group
94
+
95
+ const fields: Record<string, NestableWidget | NestedGroup> = {}
96
+ let changed = false
97
+ for (const [key, prevField] of Object.entries(group.config.fields)) {
98
+ const path = [...prevPath, key]
99
+ let field
100
+ switch (prevField.type) {
101
+ case "Group":
102
+ field = traverseNestedGroup({
103
+ path,
104
+ group: prevField,
105
+ onField: onField as OnFieldFn<NestableWidget>,
106
+ })
107
+ break
108
+ default:
109
+ field = prevField
110
+ break
111
+ }
112
+
113
+ const newField = onField({
114
+ path,
115
+ key,
116
+ field,
117
+ })
118
+ if (!changed && field !== newField) changed = true
119
+ fields[key] = newField
123
120
  }
121
+
122
+ return changed
123
+ ? {
124
+ ...group,
125
+ config: {
126
+ ...group.config,
127
+ fields,
128
+ },
129
+ }
130
+ : group
124
131
  }