@payloadcms/plugin-import-export 4.0.0-internal.5f0cd13 → 4.0.0-internal.688c4d0
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/dist/@types/assets.d.js +2 -0
- package/dist/@types/assets.d.js.map +1 -0
- package/dist/components/ImportPreview/index.d.ts.map +1 -1
- package/dist/components/ImportPreview/index.js +16 -2
- package/dist/components/ImportPreview/index.js.map +1 -1
- package/dist/utilities/applyFieldHooks.d.ts.map +1 -1
- package/dist/utilities/applyFieldHooks.js +1 -1
- package/dist/utilities/applyFieldHooks.js.map +1 -1
- package/dist/utilities/flattenedFields.d.ts.map +1 -1
- package/dist/utilities/flattenedFields.js +3 -0
- package/dist/utilities/flattenedFields.js.map +1 -1
- package/dist/utilities/processRichTextField.d.ts.map +1 -1
- package/dist/utilities/processRichTextField.js +7 -0
- package/dist/utilities/processRichTextField.js.map +1 -1
- package/dist/utilities/processRichTextField.spec.js +308 -0
- package/dist/utilities/processRichTextField.spec.js.map +1 -0
- package/dist/utilities/unflattenObject.d.ts.map +1 -1
- package/dist/utilities/unflattenObject.js +21 -2
- package/dist/utilities/unflattenObject.js.map +1 -1
- package/dist/utilities/unflattenObject.spec.js +106 -0
- package/dist/utilities/unflattenObject.spec.js.map +1 -1
- package/dist/utilities/unflattenPostProcess.d.ts.map +1 -1
- package/dist/utilities/unflattenPostProcess.js +39 -13
- package/dist/utilities/unflattenPostProcess.js.map +1 -1
- package/package.json +7 -7
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { processRichTextField } from './processRichTextField.js';
|
|
3
|
+
// Minimal helpers to keep assertions readable
|
|
4
|
+
const asRecord = (v)=>v;
|
|
5
|
+
const asArray = (v)=>v;
|
|
6
|
+
// ─── Fixtures ────────────────────────────────────────────────────────────────
|
|
7
|
+
const textNode = (overrides = {})=>({
|
|
8
|
+
detail: 0,
|
|
9
|
+
format: 0,
|
|
10
|
+
mode: 'normal',
|
|
11
|
+
text: 'Hello',
|
|
12
|
+
type: 'text',
|
|
13
|
+
version: 1,
|
|
14
|
+
...overrides
|
|
15
|
+
});
|
|
16
|
+
const paragraphNode = (children = [
|
|
17
|
+
textNode()
|
|
18
|
+
])=>({
|
|
19
|
+
children,
|
|
20
|
+
direction: 'ltr',
|
|
21
|
+
format: '',
|
|
22
|
+
indent: 0,
|
|
23
|
+
type: 'paragraph',
|
|
24
|
+
version: 1
|
|
25
|
+
});
|
|
26
|
+
const lexicalDoc = (children = [
|
|
27
|
+
paragraphNode()
|
|
28
|
+
])=>({
|
|
29
|
+
children,
|
|
30
|
+
direction: 'ltr',
|
|
31
|
+
format: '',
|
|
32
|
+
indent: 0,
|
|
33
|
+
type: 'root',
|
|
34
|
+
version: 1
|
|
35
|
+
});
|
|
36
|
+
// ─── Primitive pass-through ──────────────────────────────────────────────────
|
|
37
|
+
describe('processRichTextField — primitive pass-through', ()=>{
|
|
38
|
+
it('should return null unchanged', ()=>{
|
|
39
|
+
expect(processRichTextField(null)).toBeNull();
|
|
40
|
+
});
|
|
41
|
+
it('should return undefined unchanged', ()=>{
|
|
42
|
+
expect(processRichTextField(undefined)).toBeUndefined();
|
|
43
|
+
});
|
|
44
|
+
it('should return a number unchanged', ()=>{
|
|
45
|
+
expect(processRichTextField(42)).toBe(42);
|
|
46
|
+
});
|
|
47
|
+
it('should return false unchanged', ()=>{
|
|
48
|
+
expect(processRichTextField(false)).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
// ─── String input (the recovered-from-missed-hook path) ─────────────────────
|
|
52
|
+
describe('processRichTextField — JSON string input', ()=>{
|
|
53
|
+
it('should parse a valid JSON string into an object', ()=>{
|
|
54
|
+
const doc = lexicalDoc();
|
|
55
|
+
expect(processRichTextField(JSON.stringify(doc))).toEqual(doc);
|
|
56
|
+
});
|
|
57
|
+
it('should return an unparseable string unchanged', ()=>{
|
|
58
|
+
expect(processRichTextField('not json')).toBe('not json');
|
|
59
|
+
});
|
|
60
|
+
it('should return an empty string unchanged', ()=>{
|
|
61
|
+
expect(processRichTextField('')).toBe('');
|
|
62
|
+
});
|
|
63
|
+
it('should parse a string and still convert numeric string properties', ()=>{
|
|
64
|
+
const node = {
|
|
65
|
+
type: 'text',
|
|
66
|
+
detail: '0',
|
|
67
|
+
format: '3',
|
|
68
|
+
indent: '0',
|
|
69
|
+
version: '1',
|
|
70
|
+
text: 'hi'
|
|
71
|
+
};
|
|
72
|
+
const result = asRecord(processRichTextField(JSON.stringify(node)));
|
|
73
|
+
expect(result.detail).toBe(0);
|
|
74
|
+
expect(result.format).toBe(3);
|
|
75
|
+
expect(result.indent).toBe(0);
|
|
76
|
+
expect(result.version).toBe(1);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
// ─── Numeric property coercion ───────────────────────────────────────────────
|
|
80
|
+
describe('processRichTextField — numeric property coercion', ()=>{
|
|
81
|
+
it('should convert detail, format, indent, version from strings to numbers', ()=>{
|
|
82
|
+
const input = textNode({
|
|
83
|
+
detail: '0',
|
|
84
|
+
format: '0',
|
|
85
|
+
indent: '0',
|
|
86
|
+
version: '1'
|
|
87
|
+
});
|
|
88
|
+
const result = asRecord(processRichTextField(input));
|
|
89
|
+
expect(result.detail).toBe(0);
|
|
90
|
+
expect(result.format).toBe(0);
|
|
91
|
+
expect(result.indent).toBe(0);
|
|
92
|
+
expect(result.version).toBe(1);
|
|
93
|
+
});
|
|
94
|
+
it('should convert start and value from strings to numbers (list/listitem nodes)', ()=>{
|
|
95
|
+
const listItemNode = {
|
|
96
|
+
type: 'listitem',
|
|
97
|
+
start: '1',
|
|
98
|
+
value: '2',
|
|
99
|
+
version: '1'
|
|
100
|
+
};
|
|
101
|
+
const result = asRecord(processRichTextField(listItemNode));
|
|
102
|
+
expect(result.start).toBe(1);
|
|
103
|
+
expect(result.value).toBe(2);
|
|
104
|
+
});
|
|
105
|
+
it('should convert textFormat and textStyle from strings to numbers', ()=>{
|
|
106
|
+
const input = {
|
|
107
|
+
type: 'text',
|
|
108
|
+
textFormat: '4',
|
|
109
|
+
textStyle: '0',
|
|
110
|
+
version: '1',
|
|
111
|
+
text: 'x'
|
|
112
|
+
};
|
|
113
|
+
const result = asRecord(processRichTextField(input));
|
|
114
|
+
expect(result.textFormat).toBe(4);
|
|
115
|
+
expect(result.textStyle).toBe(0);
|
|
116
|
+
});
|
|
117
|
+
it('should leave a non-numeric string in a numeric-property slot unchanged', ()=>{
|
|
118
|
+
// Lexical paragraph format can be "" (empty) or alignment strings
|
|
119
|
+
const input = paragraphNode();
|
|
120
|
+
const result = asRecord(processRichTextField(input));
|
|
121
|
+
expect(result.format).toBe('');
|
|
122
|
+
});
|
|
123
|
+
it('should leave already-numeric values unchanged', ()=>{
|
|
124
|
+
const input = textNode({
|
|
125
|
+
detail: 0,
|
|
126
|
+
format: 3,
|
|
127
|
+
version: 1
|
|
128
|
+
});
|
|
129
|
+
const result = asRecord(processRichTextField(input));
|
|
130
|
+
expect(result.detail).toBe(0);
|
|
131
|
+
expect(result.format).toBe(3);
|
|
132
|
+
expect(result.version).toBe(1);
|
|
133
|
+
});
|
|
134
|
+
it('should not convert boolean values on numeric-property keys', ()=>{
|
|
135
|
+
const input = {
|
|
136
|
+
type: 'custom',
|
|
137
|
+
format: true,
|
|
138
|
+
version: '1'
|
|
139
|
+
};
|
|
140
|
+
const result = asRecord(processRichTextField(input));
|
|
141
|
+
expect(result.format).toBe(true);
|
|
142
|
+
expect(result.version).toBe(1);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
// ─── Recursive children processing ──────────────────────────────────────────
|
|
146
|
+
describe('processRichTextField — recursive children', ()=>{
|
|
147
|
+
it('should recursively convert numeric strings inside children arrays', ()=>{
|
|
148
|
+
const input = paragraphNode([
|
|
149
|
+
textNode({
|
|
150
|
+
detail: '0',
|
|
151
|
+
format: '0',
|
|
152
|
+
version: '1'
|
|
153
|
+
})
|
|
154
|
+
]);
|
|
155
|
+
const result = asRecord(processRichTextField(input));
|
|
156
|
+
const child = asRecord(asArray(result.children)[0]);
|
|
157
|
+
expect(child.detail).toBe(0);
|
|
158
|
+
expect(child.format).toBe(0);
|
|
159
|
+
expect(child.version).toBe(1);
|
|
160
|
+
});
|
|
161
|
+
it('should handle three levels of nesting (root → paragraph → text)', ()=>{
|
|
162
|
+
const input = lexicalDoc([
|
|
163
|
+
paragraphNode([
|
|
164
|
+
textNode({
|
|
165
|
+
version: '1',
|
|
166
|
+
format: '2'
|
|
167
|
+
})
|
|
168
|
+
])
|
|
169
|
+
]);
|
|
170
|
+
const result = asRecord(processRichTextField(input));
|
|
171
|
+
const paragraph = asRecord(asArray(result.children)[0]);
|
|
172
|
+
const text = asRecord(asArray(paragraph.children)[0]);
|
|
173
|
+
expect(text.version).toBe(1);
|
|
174
|
+
expect(text.format).toBe(2);
|
|
175
|
+
});
|
|
176
|
+
it('should handle multiple children at the same level', ()=>{
|
|
177
|
+
const input = lexicalDoc([
|
|
178
|
+
paragraphNode([
|
|
179
|
+
textNode({
|
|
180
|
+
text: 'Bold',
|
|
181
|
+
format: '1'
|
|
182
|
+
}),
|
|
183
|
+
textNode({
|
|
184
|
+
text: ' normal',
|
|
185
|
+
format: '0'
|
|
186
|
+
})
|
|
187
|
+
]),
|
|
188
|
+
paragraphNode([
|
|
189
|
+
textNode({
|
|
190
|
+
text: 'Second para',
|
|
191
|
+
version: '1'
|
|
192
|
+
})
|
|
193
|
+
])
|
|
194
|
+
]);
|
|
195
|
+
const result = asRecord(processRichTextField(input));
|
|
196
|
+
const firstPara = asRecord(asArray(result.children)[0]);
|
|
197
|
+
const secondPara = asRecord(asArray(result.children)[1]);
|
|
198
|
+
const bold = asRecord(asArray(firstPara.children)[0]);
|
|
199
|
+
const normal = asRecord(asArray(firstPara.children)[1]);
|
|
200
|
+
const secondText = asRecord(asArray(secondPara.children)[0]);
|
|
201
|
+
expect(bold.format).toBe(1);
|
|
202
|
+
expect(normal.format).toBe(0);
|
|
203
|
+
expect(secondText.version).toBe(1);
|
|
204
|
+
});
|
|
205
|
+
it('should skip null and non-object entries in children without throwing', ()=>{
|
|
206
|
+
const input = {
|
|
207
|
+
type: 'root',
|
|
208
|
+
version: 1,
|
|
209
|
+
children: [
|
|
210
|
+
null,
|
|
211
|
+
undefined,
|
|
212
|
+
'stray',
|
|
213
|
+
textNode()
|
|
214
|
+
]
|
|
215
|
+
};
|
|
216
|
+
const result = asRecord(processRichTextField(input));
|
|
217
|
+
const children = asArray(result.children);
|
|
218
|
+
expect(children[0]).toBeNull();
|
|
219
|
+
expect(children[1]).toBeUndefined();
|
|
220
|
+
expect(children[2]).toBe('stray');
|
|
221
|
+
expect(asRecord(children[3]).type).toBe('text');
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
// ─── Nested non-children objects ─────────────────────────────────────────────
|
|
225
|
+
describe('processRichTextField — nested non-children objects', ()=>{
|
|
226
|
+
it('should recurse into plain nested objects (e.g. decorator node data)', ()=>{
|
|
227
|
+
const input = {
|
|
228
|
+
type: 'decorator',
|
|
229
|
+
version: '1',
|
|
230
|
+
data: {
|
|
231
|
+
detail: '2',
|
|
232
|
+
label: 'keep me'
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
const result = asRecord(processRichTextField(input));
|
|
236
|
+
const data = asRecord(result.data);
|
|
237
|
+
expect(result.version).toBe(1);
|
|
238
|
+
expect(data.detail).toBe(2);
|
|
239
|
+
expect(data.label).toBe('keep me');
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
// ─── Full Lexical document round-trips ───────────────────────────────────────
|
|
243
|
+
describe('processRichTextField — full document', ()=>{
|
|
244
|
+
it('should produce a stable result when called on an already-processed object', ()=>{
|
|
245
|
+
const doc = lexicalDoc();
|
|
246
|
+
const firstPass = processRichTextField(doc);
|
|
247
|
+
const secondPass = processRichTextField(firstPass);
|
|
248
|
+
expect(secondPass).toEqual(firstPass);
|
|
249
|
+
});
|
|
250
|
+
it('should fully round-trip a complex Lexical document serialised to JSON', ()=>{
|
|
251
|
+
const doc = lexicalDoc([
|
|
252
|
+
paragraphNode([
|
|
253
|
+
textNode({
|
|
254
|
+
text: 'Bold',
|
|
255
|
+
format: '1',
|
|
256
|
+
detail: '0',
|
|
257
|
+
indent: '0',
|
|
258
|
+
version: '1'
|
|
259
|
+
}),
|
|
260
|
+
textNode({
|
|
261
|
+
text: ' normal',
|
|
262
|
+
format: '0',
|
|
263
|
+
version: '1'
|
|
264
|
+
})
|
|
265
|
+
]),
|
|
266
|
+
{
|
|
267
|
+
type: 'list',
|
|
268
|
+
listType: 'number',
|
|
269
|
+
start: '1',
|
|
270
|
+
version: '1',
|
|
271
|
+
indent: '0',
|
|
272
|
+
direction: 'ltr',
|
|
273
|
+
children: [
|
|
274
|
+
{
|
|
275
|
+
type: 'listitem',
|
|
276
|
+
value: '1',
|
|
277
|
+
detail: '0',
|
|
278
|
+
format: '0',
|
|
279
|
+
version: '1',
|
|
280
|
+
text: 'Item A'
|
|
281
|
+
},
|
|
282
|
+
{
|
|
283
|
+
type: 'listitem',
|
|
284
|
+
value: '2',
|
|
285
|
+
detail: '0',
|
|
286
|
+
format: '0',
|
|
287
|
+
version: '1',
|
|
288
|
+
text: 'Item B'
|
|
289
|
+
}
|
|
290
|
+
]
|
|
291
|
+
}
|
|
292
|
+
]);
|
|
293
|
+
const result = asRecord(processRichTextField(JSON.stringify(doc)));
|
|
294
|
+
const para = asRecord(asArray(result.children)[0]);
|
|
295
|
+
const boldText = asRecord(asArray(para.children)[0]);
|
|
296
|
+
expect(boldText.format).toBe(1);
|
|
297
|
+
expect(boldText.detail).toBe(0);
|
|
298
|
+
const list = asRecord(asArray(result.children)[1]);
|
|
299
|
+
expect(list.start).toBe(1);
|
|
300
|
+
expect(list.indent).toBe(0);
|
|
301
|
+
const itemA = asRecord(asArray(list.children)[0]);
|
|
302
|
+
const itemB = asRecord(asArray(list.children)[1]);
|
|
303
|
+
expect(itemA.value).toBe(1);
|
|
304
|
+
expect(itemB.value).toBe(2);
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
//# sourceMappingURL=processRichTextField.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/processRichTextField.spec.ts"],"sourcesContent":["import { describe, expect, it } from 'vitest'\n\nimport { processRichTextField } from './processRichTextField.js'\n\n// Minimal helpers to keep assertions readable\nconst asRecord = (v: unknown) => v as Record<string, unknown>\nconst asArray = (v: unknown) => v as unknown[]\n\n// ─── Fixtures ────────────────────────────────────────────────────────────────\n\nconst textNode = (overrides: Record<string, unknown> = {}) => ({\n detail: 0,\n format: 0,\n mode: 'normal',\n text: 'Hello',\n type: 'text',\n version: 1,\n ...overrides,\n})\n\nconst paragraphNode = (children: unknown[] = [textNode()]) => ({\n children,\n direction: 'ltr',\n format: '',\n indent: 0,\n type: 'paragraph',\n version: 1,\n})\n\nconst lexicalDoc = (children: unknown[] = [paragraphNode()]) => ({\n children,\n direction: 'ltr',\n format: '',\n indent: 0,\n type: 'root',\n version: 1,\n})\n\n// ─── Primitive pass-through ──────────────────────────────────────────────────\n\ndescribe('processRichTextField — primitive pass-through', () => {\n it('should return null unchanged', () => {\n expect(processRichTextField(null)).toBeNull()\n })\n\n it('should return undefined unchanged', () => {\n expect(processRichTextField(undefined)).toBeUndefined()\n })\n\n it('should return a number unchanged', () => {\n expect(processRichTextField(42)).toBe(42)\n })\n\n it('should return false unchanged', () => {\n expect(processRichTextField(false)).toBe(false)\n })\n})\n\n// ─── String input (the recovered-from-missed-hook path) ─────────────────────\n\ndescribe('processRichTextField — JSON string input', () => {\n it('should parse a valid JSON string into an object', () => {\n const doc = lexicalDoc()\n expect(processRichTextField(JSON.stringify(doc))).toEqual(doc)\n })\n\n it('should return an unparseable string unchanged', () => {\n expect(processRichTextField('not json')).toBe('not json')\n })\n\n it('should return an empty string unchanged', () => {\n expect(processRichTextField('')).toBe('')\n })\n\n it('should parse a string and still convert numeric string properties', () => {\n const node = { type: 'text', detail: '0', format: '3', indent: '0', version: '1', text: 'hi' }\n const result = asRecord(processRichTextField(JSON.stringify(node)))\n expect(result.detail).toBe(0)\n expect(result.format).toBe(3)\n expect(result.indent).toBe(0)\n expect(result.version).toBe(1)\n })\n})\n\n// ─── Numeric property coercion ───────────────────────────────────────────────\n\ndescribe('processRichTextField — numeric property coercion', () => {\n it('should convert detail, format, indent, version from strings to numbers', () => {\n const input = textNode({ detail: '0', format: '0', indent: '0', version: '1' })\n const result = asRecord(processRichTextField(input))\n expect(result.detail).toBe(0)\n expect(result.format).toBe(0)\n expect(result.indent).toBe(0)\n expect(result.version).toBe(1)\n })\n\n it('should convert start and value from strings to numbers (list/listitem nodes)', () => {\n const listItemNode = { type: 'listitem', start: '1', value: '2', version: '1' }\n const result = asRecord(processRichTextField(listItemNode))\n expect(result.start).toBe(1)\n expect(result.value).toBe(2)\n })\n\n it('should convert textFormat and textStyle from strings to numbers', () => {\n const input = { type: 'text', textFormat: '4', textStyle: '0', version: '1', text: 'x' }\n const result = asRecord(processRichTextField(input))\n expect(result.textFormat).toBe(4)\n expect(result.textStyle).toBe(0)\n })\n\n it('should leave a non-numeric string in a numeric-property slot unchanged', () => {\n // Lexical paragraph format can be \"\" (empty) or alignment strings\n const input = paragraphNode()\n const result = asRecord(processRichTextField(input))\n expect(result.format).toBe('')\n })\n\n it('should leave already-numeric values unchanged', () => {\n const input = textNode({ detail: 0, format: 3, version: 1 })\n const result = asRecord(processRichTextField(input))\n expect(result.detail).toBe(0)\n expect(result.format).toBe(3)\n expect(result.version).toBe(1)\n })\n\n it('should not convert boolean values on numeric-property keys', () => {\n const input = { type: 'custom', format: true, version: '1' }\n const result = asRecord(processRichTextField(input))\n expect(result.format).toBe(true)\n expect(result.version).toBe(1)\n })\n})\n\n// ─── Recursive children processing ──────────────────────────────────────────\n\ndescribe('processRichTextField — recursive children', () => {\n it('should recursively convert numeric strings inside children arrays', () => {\n const input = paragraphNode([textNode({ detail: '0', format: '0', version: '1' })])\n const result = asRecord(processRichTextField(input))\n const child = asRecord(asArray(result.children)[0])\n expect(child.detail).toBe(0)\n expect(child.format).toBe(0)\n expect(child.version).toBe(1)\n })\n\n it('should handle three levels of nesting (root → paragraph → text)', () => {\n const input = lexicalDoc([paragraphNode([textNode({ version: '1', format: '2' })])])\n const result = asRecord(processRichTextField(input))\n const paragraph = asRecord(asArray(result.children)[0])\n const text = asRecord(asArray(paragraph.children)[0])\n expect(text.version).toBe(1)\n expect(text.format).toBe(2)\n })\n\n it('should handle multiple children at the same level', () => {\n const input = lexicalDoc([\n paragraphNode([\n textNode({ text: 'Bold', format: '1' }),\n textNode({ text: ' normal', format: '0' }),\n ]),\n paragraphNode([textNode({ text: 'Second para', version: '1' })]),\n ])\n const result = asRecord(processRichTextField(input))\n const firstPara = asRecord(asArray(result.children)[0])\n const secondPara = asRecord(asArray(result.children)[1])\n const bold = asRecord(asArray(firstPara.children)[0])\n const normal = asRecord(asArray(firstPara.children)[1])\n const secondText = asRecord(asArray(secondPara.children)[0])\n expect(bold.format).toBe(1)\n expect(normal.format).toBe(0)\n expect(secondText.version).toBe(1)\n })\n\n it('should skip null and non-object entries in children without throwing', () => {\n const input = { type: 'root', version: 1, children: [null, undefined, 'stray', textNode()] }\n const result = asRecord(processRichTextField(input))\n const children = asArray(result.children)\n expect(children[0]).toBeNull()\n expect(children[1]).toBeUndefined()\n expect(children[2]).toBe('stray')\n expect(asRecord(children[3]).type).toBe('text')\n })\n})\n\n// ─── Nested non-children objects ─────────────────────────────────────────────\n\ndescribe('processRichTextField — nested non-children objects', () => {\n it('should recurse into plain nested objects (e.g. decorator node data)', () => {\n const input = {\n type: 'decorator',\n version: '1',\n data: { detail: '2', label: 'keep me' },\n }\n const result = asRecord(processRichTextField(input))\n const data = asRecord(result.data)\n expect(result.version).toBe(1)\n expect(data.detail).toBe(2)\n expect(data.label).toBe('keep me')\n })\n})\n\n// ─── Full Lexical document round-trips ───────────────────────────────────────\n\ndescribe('processRichTextField — full document', () => {\n it('should produce a stable result when called on an already-processed object', () => {\n const doc = lexicalDoc()\n const firstPass = processRichTextField(doc)\n const secondPass = processRichTextField(firstPass)\n expect(secondPass).toEqual(firstPass)\n })\n\n it('should fully round-trip a complex Lexical document serialised to JSON', () => {\n const doc = lexicalDoc([\n paragraphNode([\n textNode({ text: 'Bold', format: '1', detail: '0', indent: '0', version: '1' }),\n textNode({ text: ' normal', format: '0', version: '1' }),\n ]),\n {\n type: 'list',\n listType: 'number',\n start: '1',\n version: '1',\n indent: '0',\n direction: 'ltr',\n children: [\n { type: 'listitem', value: '1', detail: '0', format: '0', version: '1', text: 'Item A' },\n { type: 'listitem', value: '2', detail: '0', format: '0', version: '1', text: 'Item B' },\n ],\n },\n ])\n\n const result = asRecord(processRichTextField(JSON.stringify(doc)))\n\n const para = asRecord(asArray(result.children)[0])\n const boldText = asRecord(asArray(para.children)[0])\n expect(boldText.format).toBe(1)\n expect(boldText.detail).toBe(0)\n\n const list = asRecord(asArray(result.children)[1])\n expect(list.start).toBe(1)\n expect(list.indent).toBe(0)\n\n const itemA = asRecord(asArray(list.children)[0])\n const itemB = asRecord(asArray(list.children)[1])\n expect(itemA.value).toBe(1)\n expect(itemB.value).toBe(2)\n })\n})\n"],"names":["describe","expect","it","processRichTextField","asRecord","v","asArray","textNode","overrides","detail","format","mode","text","type","version","paragraphNode","children","direction","indent","lexicalDoc","toBeNull","undefined","toBeUndefined","toBe","doc","JSON","stringify","toEqual","node","result","input","listItemNode","start","value","textFormat","textStyle","child","paragraph","firstPara","secondPara","bold","normal","secondText","data","label","firstPass","secondPass","listType","para","boldText","list","itemA","itemB"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,MAAM,EAAEC,EAAE,QAAQ,SAAQ;AAE7C,SAASC,oBAAoB,QAAQ,4BAA2B;AAEhE,8CAA8C;AAC9C,MAAMC,WAAW,CAACC,IAAeA;AACjC,MAAMC,UAAU,CAACD,IAAeA;AAEhC,gFAAgF;AAEhF,MAAME,WAAW,CAACC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M,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC,CAAA;IACvC,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAA;IACvD,GAAG,EAAE,cAAc,CAAA;CACpB,CAAA;
|
|
1
|
+
{"version":3,"file":"unflattenObject.d.ts","sourceRoot":"","sources":["../../src/utilities/unflattenObject.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAE7D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAKvD,KAAK,aAAa,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC,CAAA;IACvC,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAA;IACvD,GAAG,EAAE,cAAc,CAAA;CACpB,CAAA;AAuCD;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,eAAe,GAAI,kDAM7B,aAAa,KAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAyUxC,CAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getNestedFlattenedFields } from './flattenedFields.js';
|
|
1
|
+
import { getBlockFlattenedFields, getNestedFlattenedFields } from './flattenedFields.js';
|
|
2
2
|
import { postProcessDocument } from './unflattenPostProcess.js';
|
|
3
3
|
const indexSegment = /^\d+$/;
|
|
4
4
|
const collectArrayLikeNames = (fields, into)=>{
|
|
@@ -9,6 +9,11 @@ const collectArrayLikeNames = (fields, into)=>{
|
|
|
9
9
|
if (field.type === 'array' || field.type === 'blocks') {
|
|
10
10
|
into.add(field.name);
|
|
11
11
|
}
|
|
12
|
+
if (field.type === 'blocks') {
|
|
13
|
+
for (const block of field.blocks ?? []){
|
|
14
|
+
collectArrayLikeNames(getBlockFlattenedFields(block), into);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
12
17
|
const nested = getNestedFlattenedFields(field);
|
|
13
18
|
if (nested) {
|
|
14
19
|
collectArrayLikeNames(nested, into);
|
|
@@ -230,7 +235,9 @@ const collectArrayLikeNames = (fields, into)=>{
|
|
|
230
235
|
blockObject[blockFieldName] = value;
|
|
231
236
|
} else {
|
|
232
237
|
if (!blockObject[blockFieldName] || typeof blockObject[blockFieldName] !== 'object') {
|
|
233
|
-
|
|
238
|
+
const segmentAfterBlockField = pathSegments[i + 4];
|
|
239
|
+
const blockFieldIsArray = segmentAfterBlockField !== undefined && indexSegment.test(segmentAfterBlockField);
|
|
240
|
+
blockObject[blockFieldName] = blockFieldIsArray ? [] : {};
|
|
234
241
|
}
|
|
235
242
|
currentObject = blockObject[blockFieldName];
|
|
236
243
|
i = i + 3;
|
|
@@ -254,6 +261,18 @@ const collectArrayLikeNames = (fields, into)=>{
|
|
|
254
261
|
currentObject = arr[arrayIndex];
|
|
255
262
|
i++;
|
|
256
263
|
}
|
|
264
|
+
} else if (Array.isArray(currentObject) && indexSegment.test(segment)) {
|
|
265
|
+
// currentObject is an array we arrived at via block field traversal — treat the
|
|
266
|
+
// segment as a numeric index rather than a plain property name.
|
|
267
|
+
const idx = parseInt(segment, 10);
|
|
268
|
+
const arr = currentObject;
|
|
269
|
+
while(arr.length <= idx){
|
|
270
|
+
arr.push(null);
|
|
271
|
+
}
|
|
272
|
+
if (arr[idx] === null || arr[idx] === undefined) {
|
|
273
|
+
arr[idx] = {};
|
|
274
|
+
}
|
|
275
|
+
currentObject = arr[idx];
|
|
257
276
|
} else {
|
|
258
277
|
// Skip if already set to null (polymorphic relationship already processed)
|
|
259
278
|
if (currentObject[segment] === null && isLast && segment === 'relationTo') {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utilities/unflattenObject.ts"],"sourcesContent":["import type { FlattenedField, PayloadRequest } from 'payload'\n\nimport type { ImportFieldHookEntry } from '../types.js'\n\nimport { getNestedFlattenedFields } from './flattenedFields.js'\nimport { postProcessDocument } from './unflattenPostProcess.js'\n\ntype UnflattenArgs = {\n data: Record<string, unknown>\n fields: FlattenedField[]\n format?: 'csv' | 'json' | ({} & string)\n importFieldHooks?: Record<string, ImportFieldHookEntry>\n req: PayloadRequest\n}\n\nconst indexSegment = /^\\d+$/\n\nconst collectArrayLikeNames = (fields: FlattenedField[], into: Set<string>): void => {\n for (const field of fields) {\n if (!('name' in field) || !field.name) {\n continue\n }\n if (field.type === 'array' || field.type === 'blocks') {\n into.add(field.name)\n }\n const nested = getNestedFlattenedFields(field)\n if (nested) {\n collectArrayLikeNames(nested, into)\n }\n }\n}\n\n/**\n * Drops numeric array-index segments from a flat key so a runtime key like\n * `items_0_note` matches the static, index-free key a user hook is registered\n * under. A digit-only segment is only stripped when the segment immediately\n * before it names an array or blocks field, so a literal field name like\n * `2024` is preserved.\n */\nconst toLogicalKey = (flatKey: string, arrayLikeNames: Set<string>): string => {\n const segments = flatKey.split('_')\n return segments\n .filter((seg, i) => !indexSegment.test(seg) || !arrayLikeNames.has(segments[i - 1] ?? ''))\n .join('_')\n}\n\n/**\n * Converts flattened CSV data back into a nested document structure.\n *\n * The algorithm:\n * 1. Sorts keys to ensure array indices are processed in order\n * 2. For each flattened key (e.g., \"blocks_0_hero_title\"), splits by underscore into path segments\n * 3. Traverses/builds the nested structure, handling:\n * - Arrays (numeric segments like \"0\", \"1\")\n * - Blocks (blockType detection from slug patterns)\n * - Polymorphic relationships (_relationTo and _id suffix pairs)\n * - Regular nested objects\n * 4. Post-processes to handle localized fields, hasMany conversions, and relationship transforms\n */\nexport const unflattenObject = ({\n data,\n fields,\n format = 'csv',\n importFieldHooks = {},\n req,\n}: UnflattenArgs): Record<string, unknown> => {\n if (!data || typeof data !== 'object') {\n return {}\n }\n\n const result: Record<string, unknown> = {}\n\n const arrayLikeNames = new Set<string>()\n collectArrayLikeNames(fields, arrayLikeNames)\n\n // Sort keys to ensure array indices are processed in order\n const sortedKeys = Object.keys(data).sort((a, b) => {\n // Extract array indices from flattened keys (e.g., \"field_0_subfield\" -> \"0\")\n const aMatch = a.match(/_(\\d+)(?:_|$)/)\n const bMatch = b.match(/_(\\d+)(?:_|$)/)\n\n if (aMatch && bMatch && aMatch.index !== undefined && bMatch.index !== undefined) {\n const aBase = a.substring(0, aMatch.index)\n const bBase = b.substring(0, bMatch.index)\n\n if (aBase === bBase) {\n return (parseInt(aMatch?.[1] ?? '0', 10) || 0) - (parseInt(bMatch?.[1] ?? '0', 10) || 0)\n }\n }\n\n return a.localeCompare(b)\n })\n\n for (const flatKey of sortedKeys) {\n let value = data[flatKey]\n\n // Skip undefined values but keep null for required field validation\n if (value === undefined) {\n continue\n }\n\n // Preserve system fields with underscore prefix (like _status) without splitting\n if (flatKey === '_status') {\n result[flatKey] = value\n continue\n }\n\n // Check if this is a _relationTo key for a polymorphic relationship\n if (flatKey.endsWith('_relationTo')) {\n const baseKey = flatKey.replace(/_relationTo$/, '')\n const idKey = `${baseKey}_id`\n\n // Check if this is a polymorphic relationship field\n const isPolymorphic = fields.some(\n (field) =>\n field.name === baseKey &&\n field.type === 'relationship' &&\n 'relationTo' in field &&\n Array.isArray(field.relationTo),\n )\n\n if (isPolymorphic) {\n if (baseKey in result) {\n continue\n }\n\n // If the corresponding _id key is undefined, skip processing entirely\n // This prevents creating empty objects when we should preserve existing data\n if (!(idKey in data) || data[idKey] === undefined) {\n continue\n }\n }\n }\n\n // Check if this is a _id key for a polymorphic relationship where _relationTo is undefined\n if (flatKey.endsWith('_id')) {\n const baseKey = flatKey.replace(/_id$/, '')\n const relationToKey = `${baseKey}_relationTo`\n\n // Check if this is a polymorphic relationship field\n const isPolymorphic = fields.some(\n (field) =>\n field.name === baseKey &&\n field.type === 'relationship' &&\n 'relationTo' in field &&\n Array.isArray(field.relationTo),\n )\n\n if (isPolymorphic) {\n // If the corresponding _relationTo key is undefined, skip processing entirely\n // This prevents creating empty objects when we should preserve existing data\n if (!(relationToKey in data) || data[relationToKey] === undefined) {\n continue\n }\n }\n }\n\n const importHookEntry =\n importFieldHooks[flatKey] ?? importFieldHooks[toLogicalKey(flatKey, arrayLikeNames)]\n if (importHookEntry) {\n try {\n value = importHookEntry.fn({\n columnName: flatKey,\n data,\n format,\n siblingData: data,\n siblingDoc: data,\n value,\n })\n } catch (error) {\n req.payload.logger.error({\n err: error,\n msg: `[plugin-import-export] Field-level beforeImport hook for \"${flatKey}\" threw — falling back to original value`,\n })\n // Keep the original value so the row is not dropped — downstream\n // validation will surface any deeper issue per-row.\n }\n }\n\n // Example: \"blocks_0_content_text\" -> [\"blocks\", \"0\", \"content\", \"text\"]\n const pathSegments = flatKey.split('_')\n let currentObject: Record<string, unknown> = result\n\n for (let i = 0; i < pathSegments.length; i++) {\n const segment = pathSegments[i]\n if (!segment) {\n continue\n } // Skip empty segments\n\n const nextSegment = pathSegments[i + 1]\n const isLast = i === pathSegments.length - 1\n\n // Check if next segment is a numeric array index (e.g., \"0\", \"1\", \"2\")\n const isArrayIndex = nextSegment !== undefined && /^\\d+$/.test(nextSegment)\n\n if (isLast) {\n // Special handling for blockType suffix in blocks\n if (segment === 'blockType' && i >= 3) {\n // Pattern: blocks_0_hero_blockType -> set blockType on the block\n const blockFieldName = pathSegments[0] // 'blocks'\n const isBlockField = fields.some(\n (field) => field.name === blockFieldName && field.type === 'blocks',\n )\n\n if (isBlockField && pathSegments[1]?.match(/^\\d+$/)) {\n const parent = getParentObject(result, pathSegments.slice(0, 2))\n if (parent && typeof parent === 'object') {\n parent.blockType = value\n }\n continue\n }\n }\n\n // Special handling for relationship fields with _id suffix\n if (segment === 'id' && i > 0) {\n const parentKey = pathSegments[i - 1]\n const isPreviousSegmentArrayIndex = parentKey ? /^\\d+$/.test(parentKey) : false\n\n if (!isPreviousSegmentArrayIndex) {\n // Check if this is a relationship field\n const isRelationship = fields.some(\n (field) => field.name === parentKey && field.type === 'relationship',\n )\n\n if (isRelationship) {\n // Check if this is a polymorphic relationship field\n const field = fields.find((f) => f.name === parentKey && f.type === 'relationship')\n const isPolymorphic =\n field && 'relationTo' in field && Array.isArray(field.relationTo)\n\n if (isPolymorphic) {\n const relationToKey = pathSegments.slice(0, i).concat('relationTo').join('_')\n const relationToValue = data[relationToKey]\n\n const parent = getParentObject(result, pathSegments.slice(0, i - 1))\n if (parent && parentKey && typeof parent === 'object') {\n // Both fields must be defined to create/update the relationship\n // If either is undefined, skip the field entirely (preserve existing data)\n if (value !== undefined && relationToValue !== undefined) {\n // Check if both are explicitly null\n if (relationToValue === null && value === null) {\n // Only set to null if explicitly null (user typed \"null\" in CSV)\n parent[parentKey] = null\n } else if (relationToValue || value) {\n // At least one has a value, create the relationship\n parent[parentKey] = {\n relationTo: relationToValue,\n value, // This will be transformed to proper format in postProcess\n }\n }\n // If both are empty strings, don't set the field (handled by not meeting the above conditions)\n }\n // If either is undefined, don't set the field at all (preserve existing data)\n }\n continue\n } else if (!isPolymorphic) {\n const parent = getParentObject(result, pathSegments.slice(0, i - 1))\n if (parent && parentKey && typeof parent === 'object') {\n parent[parentKey] = value\n }\n continue\n }\n }\n }\n }\n\n // _relationTo suffix is handled when processing the _id field above\n if (segment === 'relationTo' && i > 0) {\n const parentKey = pathSegments[i - 1]\n if (parentKey && !parentKey.match(/^\\d+$/)) {\n const field = fields.find((f) => f.name === parentKey && f.type === 'relationship')\n const isPolymorphic = field && 'relationTo' in field && Array.isArray(field.relationTo)\n\n if (isPolymorphic) {\n // For polymorphic relationships, this is handled when processing the _id field\n // Skip it entirely\n continue\n }\n }\n }\n\n currentObject[segment] = value\n } else if (isArrayIndex && nextSegment !== undefined) {\n if (!currentObject[segment] || !Array.isArray(currentObject[segment])) {\n currentObject[segment] = []\n }\n\n const arrayIndex = parseInt(nextSegment)\n const arr = currentObject[segment] as unknown[]\n\n // Ensure array has sufficient length\n while (arr.length <= arrayIndex) {\n arr.push(null)\n }\n\n // Handle array of objects\n if (arr[arrayIndex] === null || arr[arrayIndex] === undefined) {\n arr[arrayIndex] = {}\n }\n\n // Handle blocks field with block slug pattern (e.g., blocks_0_hero_title)\n const isBlocksField = fields.some((f) => f.name === segment && f.type === 'blocks')\n if (isBlocksField && i + 3 < pathSegments.length) {\n const blockSlug = pathSegments[i + 2]\n const blockFieldName = pathSegments[i + 3]\n\n if (blockSlug && blockFieldName) {\n const blockObject = arr[arrayIndex] as Record<string, unknown>\n blockObject.blockType = blockSlug\n\n if (i + 3 === pathSegments.length - 1) {\n blockObject[blockFieldName] = value\n } else {\n if (!blockObject[blockFieldName] || typeof blockObject[blockFieldName] !== 'object') {\n blockObject[blockFieldName] = {}\n }\n currentObject = blockObject[blockFieldName] as Record<string, unknown>\n i = i + 3\n continue\n }\n break\n }\n }\n\n if (i + 2 === pathSegments.length - 1) {\n const lastSegment = pathSegments[pathSegments.length - 1]\n if (lastSegment && arr[arrayIndex] && typeof arr[arrayIndex] === 'object') {\n ;(arr[arrayIndex] as Record<string, unknown>)[lastSegment] = value\n }\n break\n } else if (i + 1 === pathSegments.length - 1) {\n // Direct array value (e.g., tags_0 = \"value\")\n arr[arrayIndex] = value\n break\n } else {\n currentObject = arr[arrayIndex] as Record<string, unknown>\n i++\n }\n } else {\n // Skip if already set to null (polymorphic relationship already processed)\n if (currentObject[segment] === null && isLast && segment === 'relationTo') {\n continue\n }\n\n if (\n !currentObject[segment] ||\n typeof currentObject[segment] !== 'object' ||\n Array.isArray(currentObject[segment])\n ) {\n currentObject[segment] = {}\n }\n\n // Handle polymorphic relationship arrays\n if (segment === 'relationTo' && i > 0 && pathSegments[i - 1]?.match(/^\\d+$/)) {\n currentObject[segment] = value\n } else if (\n typeof currentObject[segment] === 'object' &&\n !Array.isArray(currentObject[segment]) &&\n currentObject[segment] !== null\n ) {\n currentObject = currentObject[segment] as Record<string, unknown>\n }\n }\n }\n }\n\n try {\n // Post-process to handle special structures\n postProcessDocument(result, fields)\n } catch (err) {\n // Log but don't throw - return partially processed result\n\n req.payload.logger.error({\n err,\n msg: '[plugin-import-export] Error in postProcessDocument',\n })\n }\n\n return result\n}\n\nconst getParentObject = (\n obj: Record<string, unknown>,\n segments: string[],\n): Record<string, unknown> | undefined => {\n let current: Record<string, unknown> = obj\n\n for (let i = 0; i < segments.length; i++) {\n const segment = segments[i]\n const nextSegment = segments[i + 1]\n\n if (!segment) {\n continue\n }\n\n if (nextSegment && /^\\d+$/.test(nextSegment)) {\n const arrayIndex = parseInt(nextSegment)\n const arr = current[segment] as unknown[]\n\n if (Array.isArray(arr) && arr[arrayIndex]) {\n current = arr[arrayIndex] as Record<string, unknown>\n i++ // Skip the index\n } else {\n return undefined\n }\n } else {\n const next = current[segment]\n if (typeof next === 'object' && next !== null && !Array.isArray(next)) {\n current = next as Record<string, unknown>\n } else {\n return undefined\n }\n }\n }\n\n return current\n}\n"],"names":["getNestedFlattenedFields","postProcessDocument","indexSegment","collectArrayLikeNames","fields","into","field","name","type","add","nested","toLogicalKey","flatKey","arrayLikeNames","segments","split","filter","seg","i","test","has","join","unflattenObject","data","format","importFieldHooks","req","result","Set","sortedKeys","Object","keys","sort","a","b","aMatch","match","bMatch","index","undefined","aBase","substring","bBase","parseInt","localeCompare","value","endsWith","baseKey","replace","idKey","isPolymorphic","some","Array","isArray","relationTo","relationToKey","importHookEntry","fn","columnName","siblingData","siblingDoc","error","payload","logger","err","msg","pathSegments","currentObject","length","segment","nextSegment","isLast","isArrayIndex","blockFieldName","isBlockField","parent","getParentObject","slice","blockType","parentKey","isPreviousSegmentArrayIndex","isRelationship","find","f","concat","relationToValue","arrayIndex","arr","push","isBlocksField","blockSlug","blockObject","lastSegment","obj","current","next"],"mappings":"AAIA,SAASA,wBAAwB,QAAQ,uBAAsB;AAC/D,SAASC,mBAAmB,QAAQ,4BAA2B;AAU/D,MAAMC,eAAe;AAErB,MAAMC,wBAAwB,CAACC,QAA0BC;IACvD,KAAK,MAAMC,SAASF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B,MAAMoD,cAAcxD,QAAQ,CAACI,IAAI,EAAE;QAEnC,IAAI,CAACmD,SAAS;YACZ;QACF;QAEA,IAAIC,eAAe,QAAQnD,IAAI,CAACmD,cAAc;YAC5C,MAAMgB,aAAa3C,SAAS2B;YAC5B,MAAMiB,MAAMO,OAAO,CAACzB,QAAQ;YAE5B,IAAIjB,MAAMC,OAAO,CAACkC,QAAQA,GAAG,CAACD,WAAW,EAAE;gBACzCQ,UAAUP,GAAG,CAACD,WAAW;gBACzBpE,KAAI,iBAAiB;YACvB,OAAO;gBACL,OAAOqB;YACT;QACF,OAAO;YACL,MAAMwD,OAAOD,OAAO,CAACzB,QAAQ;YAC7B,IAAI,OAAO0B,SAAS,YAAYA,SAAS,QAAQ,CAAC3C,MAAMC,OAAO,CAAC0C,OAAO;gBACrED,UAAUC;YACZ,OAAO;gBACL,OAAOxD;YACT;QACF;IACF;IAEA,OAAOuD;AACT"}
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/unflattenObject.ts"],"sourcesContent":["import type { FlattenedField, PayloadRequest } from 'payload'\n\nimport type { ImportFieldHookEntry } from '../types.js'\n\nimport { getBlockFlattenedFields, getNestedFlattenedFields } from './flattenedFields.js'\nimport { postProcessDocument } from './unflattenPostProcess.js'\n\ntype UnflattenArgs = {\n data: Record<string, unknown>\n fields: FlattenedField[]\n format?: 'csv' | 'json' | ({} & string)\n importFieldHooks?: Record<string, ImportFieldHookEntry>\n req: PayloadRequest\n}\n\nconst indexSegment = /^\\d+$/\n\nconst collectArrayLikeNames = (fields: FlattenedField[], into: Set<string>): void => {\n for (const field of fields) {\n if (!('name' in field) || !field.name) {\n continue\n }\n if (field.type === 'array' || field.type === 'blocks') {\n into.add(field.name)\n }\n if (field.type === 'blocks') {\n for (const block of (field as { blocks?: Array<{ flattenedFields?: FlattenedField[] }> })\n .blocks ?? []) {\n collectArrayLikeNames(getBlockFlattenedFields(block), into)\n }\n }\n const nested = getNestedFlattenedFields(field)\n if (nested) {\n collectArrayLikeNames(nested, into)\n }\n }\n}\n\n/**\n * Drops numeric array-index segments from a flat key so a runtime key like\n * `items_0_note` matches the static, index-free key a user hook is registered\n * under. A digit-only segment is only stripped when the segment immediately\n * before it names an array or blocks field, so a literal field name like\n * `2024` is preserved.\n */\nconst toLogicalKey = (flatKey: string, arrayLikeNames: Set<string>): string => {\n const segments = flatKey.split('_')\n return segments\n .filter((seg, i) => !indexSegment.test(seg) || !arrayLikeNames.has(segments[i - 1] ?? ''))\n .join('_')\n}\n\n/**\n * Converts flattened CSV data back into a nested document structure.\n *\n * The algorithm:\n * 1. Sorts keys to ensure array indices are processed in order\n * 2. For each flattened key (e.g., \"blocks_0_hero_title\"), splits by underscore into path segments\n * 3. Traverses/builds the nested structure, handling:\n * - Arrays (numeric segments like \"0\", \"1\")\n * - Blocks (blockType detection from slug patterns)\n * - Polymorphic relationships (_relationTo and _id suffix pairs)\n * - Regular nested objects\n * 4. Post-processes to handle localized fields, hasMany conversions, and relationship transforms\n */\nexport const unflattenObject = ({\n data,\n fields,\n format = 'csv',\n importFieldHooks = {},\n req,\n}: UnflattenArgs): Record<string, unknown> => {\n if (!data || typeof data !== 'object') {\n return {}\n }\n\n const result: Record<string, unknown> = {}\n\n const arrayLikeNames = new Set<string>()\n collectArrayLikeNames(fields, arrayLikeNames)\n\n // Sort keys to ensure array indices are processed in order\n const sortedKeys = Object.keys(data).sort((a, b) => {\n // Extract array indices from flattened keys (e.g., \"field_0_subfield\" -> \"0\")\n const aMatch = a.match(/_(\\d+)(?:_|$)/)\n const bMatch = b.match(/_(\\d+)(?:_|$)/)\n\n if (aMatch && bMatch && aMatch.index !== undefined && bMatch.index !== undefined) {\n const aBase = a.substring(0, aMatch.index)\n const bBase = b.substring(0, bMatch.index)\n\n if (aBase === bBase) {\n return (parseInt(aMatch?.[1] ?? '0', 10) || 0) - (parseInt(bMatch?.[1] ?? '0', 10) || 0)\n }\n }\n\n return a.localeCompare(b)\n })\n\n for (const flatKey of sortedKeys) {\n let value = data[flatKey]\n\n // Skip undefined values but keep null for required field validation\n if (value === undefined) {\n continue\n }\n\n // Preserve system fields with underscore prefix (like _status) without splitting\n if (flatKey === '_status') {\n result[flatKey] = value\n continue\n }\n\n // Check if this is a _relationTo key for a polymorphic relationship\n if (flatKey.endsWith('_relationTo')) {\n const baseKey = flatKey.replace(/_relationTo$/, '')\n const idKey = `${baseKey}_id`\n\n // Check if this is a polymorphic relationship field\n const isPolymorphic = fields.some(\n (field) =>\n field.name === baseKey &&\n field.type === 'relationship' &&\n 'relationTo' in field &&\n Array.isArray(field.relationTo),\n )\n\n if (isPolymorphic) {\n if (baseKey in result) {\n continue\n }\n\n // If the corresponding _id key is undefined, skip processing entirely\n // This prevents creating empty objects when we should preserve existing data\n if (!(idKey in data) || data[idKey] === undefined) {\n continue\n }\n }\n }\n\n // Check if this is a _id key for a polymorphic relationship where _relationTo is undefined\n if (flatKey.endsWith('_id')) {\n const baseKey = flatKey.replace(/_id$/, '')\n const relationToKey = `${baseKey}_relationTo`\n\n // Check if this is a polymorphic relationship field\n const isPolymorphic = fields.some(\n (field) =>\n field.name === baseKey &&\n field.type === 'relationship' &&\n 'relationTo' in field &&\n Array.isArray(field.relationTo),\n )\n\n if (isPolymorphic) {\n // If the corresponding _relationTo key is undefined, skip processing entirely\n // This prevents creating empty objects when we should preserve existing data\n if (!(relationToKey in data) || data[relationToKey] === undefined) {\n continue\n }\n }\n }\n\n const importHookEntry =\n importFieldHooks[flatKey] ?? importFieldHooks[toLogicalKey(flatKey, arrayLikeNames)]\n if (importHookEntry) {\n try {\n value = importHookEntry.fn({\n columnName: flatKey,\n data,\n format,\n siblingData: data,\n siblingDoc: data,\n value,\n })\n } catch (error) {\n req.payload.logger.error({\n err: error,\n msg: `[plugin-import-export] Field-level beforeImport hook for \"${flatKey}\" threw — falling back to original value`,\n })\n // Keep the original value so the row is not dropped — downstream\n // validation will surface any deeper issue per-row.\n }\n }\n\n // Example: \"blocks_0_content_text\" -> [\"blocks\", \"0\", \"content\", \"text\"]\n const pathSegments = flatKey.split('_')\n let currentObject: Record<string, unknown> = result\n\n for (let i = 0; i < pathSegments.length; i++) {\n const segment = pathSegments[i]\n if (!segment) {\n continue\n } // Skip empty segments\n\n const nextSegment = pathSegments[i + 1]\n const isLast = i === pathSegments.length - 1\n\n // Check if next segment is a numeric array index (e.g., \"0\", \"1\", \"2\")\n const isArrayIndex = nextSegment !== undefined && /^\\d+$/.test(nextSegment)\n\n if (isLast) {\n // Special handling for blockType suffix in blocks\n if (segment === 'blockType' && i >= 3) {\n // Pattern: blocks_0_hero_blockType -> set blockType on the block\n const blockFieldName = pathSegments[0] // 'blocks'\n const isBlockField = fields.some(\n (field) => field.name === blockFieldName && field.type === 'blocks',\n )\n\n if (isBlockField && pathSegments[1]?.match(/^\\d+$/)) {\n const parent = getParentObject(result, pathSegments.slice(0, 2))\n if (parent && typeof parent === 'object') {\n parent.blockType = value\n }\n continue\n }\n }\n\n // Special handling for relationship fields with _id suffix\n if (segment === 'id' && i > 0) {\n const parentKey = pathSegments[i - 1]\n const isPreviousSegmentArrayIndex = parentKey ? /^\\d+$/.test(parentKey) : false\n\n if (!isPreviousSegmentArrayIndex) {\n // Check if this is a relationship field\n const isRelationship = fields.some(\n (field) => field.name === parentKey && field.type === 'relationship',\n )\n\n if (isRelationship) {\n // Check if this is a polymorphic relationship field\n const field = fields.find((f) => f.name === parentKey && f.type === 'relationship')\n const isPolymorphic =\n field && 'relationTo' in field && Array.isArray(field.relationTo)\n\n if (isPolymorphic) {\n const relationToKey = pathSegments.slice(0, i).concat('relationTo').join('_')\n const relationToValue = data[relationToKey]\n\n const parent = getParentObject(result, pathSegments.slice(0, i - 1))\n if (parent && parentKey && typeof parent === 'object') {\n // Both fields must be defined to create/update the relationship\n // If either is undefined, skip the field entirely (preserve existing data)\n if (value !== undefined && relationToValue !== undefined) {\n // Check if both are explicitly null\n if (relationToValue === null && value === null) {\n // Only set to null if explicitly null (user typed \"null\" in CSV)\n parent[parentKey] = null\n } else if (relationToValue || value) {\n // At least one has a value, create the relationship\n parent[parentKey] = {\n relationTo: relationToValue,\n value, // This will be transformed to proper format in postProcess\n }\n }\n // If both are empty strings, don't set the field (handled by not meeting the above conditions)\n }\n // If either is undefined, don't set the field at all (preserve existing data)\n }\n continue\n } else if (!isPolymorphic) {\n const parent = getParentObject(result, pathSegments.slice(0, i - 1))\n if (parent && parentKey && typeof parent === 'object') {\n parent[parentKey] = value\n }\n continue\n }\n }\n }\n }\n\n // _relationTo suffix is handled when processing the _id field above\n if (segment === 'relationTo' && i > 0) {\n const parentKey = pathSegments[i - 1]\n if (parentKey && !parentKey.match(/^\\d+$/)) {\n const field = fields.find((f) => f.name === parentKey && f.type === 'relationship')\n const isPolymorphic = field && 'relationTo' in field && Array.isArray(field.relationTo)\n\n if (isPolymorphic) {\n // For polymorphic relationships, this is handled when processing the _id field\n // Skip it entirely\n continue\n }\n }\n }\n\n currentObject[segment] = value\n } else if (isArrayIndex && nextSegment !== undefined) {\n if (!currentObject[segment] || !Array.isArray(currentObject[segment])) {\n currentObject[segment] = []\n }\n\n const arrayIndex = parseInt(nextSegment)\n const arr = currentObject[segment] as unknown[]\n\n // Ensure array has sufficient length\n while (arr.length <= arrayIndex) {\n arr.push(null)\n }\n\n // Handle array of objects\n if (arr[arrayIndex] === null || arr[arrayIndex] === undefined) {\n arr[arrayIndex] = {}\n }\n\n // Handle blocks field with block slug pattern (e.g., blocks_0_hero_title)\n const isBlocksField = fields.some((f) => f.name === segment && f.type === 'blocks')\n if (isBlocksField && i + 3 < pathSegments.length) {\n const blockSlug = pathSegments[i + 2]\n const blockFieldName = pathSegments[i + 3]\n\n if (blockSlug && blockFieldName) {\n const blockObject = arr[arrayIndex] as Record<string, unknown>\n blockObject.blockType = blockSlug\n\n if (i + 3 === pathSegments.length - 1) {\n blockObject[blockFieldName] = value\n } else {\n if (!blockObject[blockFieldName] || typeof blockObject[blockFieldName] !== 'object') {\n const segmentAfterBlockField = pathSegments[i + 4]\n const blockFieldIsArray =\n segmentAfterBlockField !== undefined && indexSegment.test(segmentAfterBlockField)\n blockObject[blockFieldName] = blockFieldIsArray ? [] : {}\n }\n currentObject = blockObject[blockFieldName] as Record<string, unknown>\n i = i + 3\n continue\n }\n break\n }\n }\n\n if (i + 2 === pathSegments.length - 1) {\n const lastSegment = pathSegments[pathSegments.length - 1]\n if (lastSegment && arr[arrayIndex] && typeof arr[arrayIndex] === 'object') {\n ;(arr[arrayIndex] as Record<string, unknown>)[lastSegment] = value\n }\n break\n } else if (i + 1 === pathSegments.length - 1) {\n // Direct array value (e.g., tags_0 = \"value\")\n arr[arrayIndex] = value\n break\n } else {\n currentObject = arr[arrayIndex] as Record<string, unknown>\n i++\n }\n } else if (Array.isArray(currentObject) && indexSegment.test(segment)) {\n // currentObject is an array we arrived at via block field traversal — treat the\n // segment as a numeric index rather than a plain property name.\n const idx = parseInt(segment, 10)\n const arr = currentObject as unknown[]\n while (arr.length <= idx) {\n arr.push(null)\n }\n if (arr[idx] === null || arr[idx] === undefined) {\n arr[idx] = {}\n }\n currentObject = arr[idx] as Record<string, unknown>\n } else {\n // Skip if already set to null (polymorphic relationship already processed)\n if (currentObject[segment] === null && isLast && segment === 'relationTo') {\n continue\n }\n\n if (\n !currentObject[segment] ||\n typeof currentObject[segment] !== 'object' ||\n Array.isArray(currentObject[segment])\n ) {\n currentObject[segment] = {}\n }\n\n // Handle polymorphic relationship arrays\n if (segment === 'relationTo' && i > 0 && pathSegments[i - 1]?.match(/^\\d+$/)) {\n currentObject[segment] = value\n } else if (\n typeof currentObject[segment] === 'object' &&\n !Array.isArray(currentObject[segment]) &&\n currentObject[segment] !== null\n ) {\n currentObject = currentObject[segment] as Record<string, unknown>\n }\n }\n }\n }\n\n try {\n // Post-process to handle special structures\n postProcessDocument(result, fields)\n } catch (err) {\n // Log but don't throw - return partially processed result\n\n req.payload.logger.error({\n err,\n msg: '[plugin-import-export] Error in postProcessDocument',\n })\n }\n\n return result\n}\n\nconst getParentObject = (\n obj: Record<string, unknown>,\n segments: string[],\n): Record<string, unknown> | undefined => {\n let current: Record<string, unknown> = obj\n\n for (let i = 0; i < segments.length; i++) {\n const segment = segments[i]\n const nextSegment = segments[i + 1]\n\n if (!segment) {\n continue\n }\n\n if (nextSegment && /^\\d+$/.test(nextSegment)) {\n const arrayIndex = parseInt(nextSegment)\n const arr = current[segment] as unknown[]\n\n if (Array.isArray(arr) && arr[arrayIndex]) {\n current = arr[arrayIndex] as Record<string, unknown>\n i++ // Skip the index\n } else {\n return undefined\n }\n } else {\n const next = current[segment]\n if (typeof next === 'object' && next !== null && !Array.isArray(next)) {\n current = next as Record<string, unknown>\n } else {\n return undefined\n }\n }\n }\n\n return current\n}\n"],"names":["getBlockFlattenedFields","getNestedFlattenedFields","postProcessDocument","indexSegment","collectArrayLikeNames","fields","into","field","name","type","add","block","blocks","nested","toLogicalKey","flatKey","arrayLikeNames","segments","split","filter","seg","i","test","has","join","unflattenObject","data","format","importFieldHooks","req","result","Set","sortedKeys","Object","keys","sort","a","b","aMatch","match","bMatch","index","undefined","aBase","substring","bBase","parseInt","localeCompare","value","endsWith","baseKey","replace","idKey","isPolymorphic","some","Array","isArray","relationTo","relationToKey","importHookEntry","fn","columnName","siblingData","siblingDoc","error","payload","logger","err","msg","pathSegments","currentObject","length","segment","nextSegment","isLast","isArrayIndex","blockFieldName","isBlockField","parent","getParentObject","slice","blockType","parentKey","isPreviousSegmentArrayIndex","isRelationship","find","f","concat","relationToValue","arrayIndex","arr","push","isBlocksField","blockSlug","blockObject","segmentAfterBlockField","blockFieldIsArray","lastSegment","idx","obj","current","next"],"mappings":"AAIA,SAASA,uBAAuB,EAAEC,wBAAwB,QAAQ,uBAAsB;AACxF,SAASC,mBAAmB,QAAQ,4BAA2B;AAU/D,MAAMC,eAAe;AAErB,MAAMC,wBAAwB,CAACC,QAA0BC;IACvD,KAAK,MAAMC,SAASF,OAAQ;QAC1B,IAAI,CAAE,CAAA,UAAUE,KAAI,KAAM,CAACA,MAAMC,IAAI,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
+
import { getImportFieldFunctions } from './getImportFieldFunctions.js';
|
|
1
2
|
import { unflattenObject } from './unflattenObject.js';
|
|
2
3
|
import { describe, it, expect, vi } from 'vitest';
|
|
3
4
|
describe('unflattenObject', ()=>{
|
|
@@ -660,6 +661,111 @@ describe('unflattenObject', ()=>{
|
|
|
660
661
|
expect(archives[0]['2024']).toBe('transformed-raw');
|
|
661
662
|
});
|
|
662
663
|
});
|
|
664
|
+
describe('blocks with nested arrays', ()=>{
|
|
665
|
+
const faqFields = [
|
|
666
|
+
{
|
|
667
|
+
name: 'faqContent',
|
|
668
|
+
type: 'blocks',
|
|
669
|
+
blocks: [
|
|
670
|
+
{
|
|
671
|
+
slug: 'faqSection',
|
|
672
|
+
flattenedFields: [
|
|
673
|
+
{
|
|
674
|
+
name: 'faqs',
|
|
675
|
+
type: 'array',
|
|
676
|
+
flattenedFields: [
|
|
677
|
+
{
|
|
678
|
+
name: 'id',
|
|
679
|
+
type: 'text'
|
|
680
|
+
},
|
|
681
|
+
{
|
|
682
|
+
name: 'question',
|
|
683
|
+
type: 'text'
|
|
684
|
+
},
|
|
685
|
+
{
|
|
686
|
+
name: 'answer',
|
|
687
|
+
type: 'richText'
|
|
688
|
+
}
|
|
689
|
+
]
|
|
690
|
+
}
|
|
691
|
+
]
|
|
692
|
+
}
|
|
693
|
+
]
|
|
694
|
+
}
|
|
695
|
+
];
|
|
696
|
+
it('should unflatten a nested array inside a block as an array, not an object with numeric keys', ()=>{
|
|
697
|
+
const data = {
|
|
698
|
+
faqContent_0_faqSection_id: '6a1714b81e5f4cdbb51f18b1',
|
|
699
|
+
faqContent_0_faqSection_faqs_0_id: '6a1714c01e5f4cdbb51f18b2',
|
|
700
|
+
faqContent_0_faqSection_faqs_0_question: 'ipsum',
|
|
701
|
+
faqContent_0_faqSection_blockType: 'faqSection'
|
|
702
|
+
};
|
|
703
|
+
const result = unflattenObject({
|
|
704
|
+
data,
|
|
705
|
+
fields: faqFields,
|
|
706
|
+
req: mockReq
|
|
707
|
+
});
|
|
708
|
+
const faqContent = result.faqContent;
|
|
709
|
+
expect(Array.isArray(faqContent)).toBe(true);
|
|
710
|
+
expect(faqContent).toHaveLength(1);
|
|
711
|
+
expect(faqContent[0].blockType).toBe('faqSection');
|
|
712
|
+
const faqs = faqContent[0].faqs;
|
|
713
|
+
expect(Array.isArray(faqs)).toBe(true);
|
|
714
|
+
const faqItems = faqs;
|
|
715
|
+
expect(faqItems[0].question).toBe('ipsum');
|
|
716
|
+
expect(faqItems[0].id).toBe('6a1714c01e5f4cdbb51f18b2');
|
|
717
|
+
});
|
|
718
|
+
it('should parse a richText JSON string inside a nested array within a block', ()=>{
|
|
719
|
+
const richTextValue = {
|
|
720
|
+
root: {
|
|
721
|
+
children: [
|
|
722
|
+
{
|
|
723
|
+
children: [
|
|
724
|
+
{
|
|
725
|
+
detail: 0,
|
|
726
|
+
format: 0,
|
|
727
|
+
mode: 'normal',
|
|
728
|
+
text: 'porksum',
|
|
729
|
+
type: 'text',
|
|
730
|
+
version: 1
|
|
731
|
+
}
|
|
732
|
+
],
|
|
733
|
+
direction: null,
|
|
734
|
+
format: '',
|
|
735
|
+
indent: 0,
|
|
736
|
+
type: 'paragraph',
|
|
737
|
+
version: 1
|
|
738
|
+
}
|
|
739
|
+
],
|
|
740
|
+
direction: null,
|
|
741
|
+
format: '',
|
|
742
|
+
indent: 0,
|
|
743
|
+
type: 'root',
|
|
744
|
+
version: 1
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
const data = {
|
|
748
|
+
faqContent_0_faqSection_id: '6a1714b81e5f4cdbb51f18b1',
|
|
749
|
+
faqContent_0_faqSection_faqs_0_id: '6a1714c01e5f4cdbb51f18b2',
|
|
750
|
+
faqContent_0_faqSection_faqs_0_question: 'ipsum',
|
|
751
|
+
faqContent_0_faqSection_faqs_0_answer: JSON.stringify(richTextValue),
|
|
752
|
+
faqContent_0_faqSection_blockType: 'faqSection'
|
|
753
|
+
};
|
|
754
|
+
const importFieldHooks = getImportFieldFunctions({
|
|
755
|
+
fields: faqFields
|
|
756
|
+
});
|
|
757
|
+
const result = unflattenObject({
|
|
758
|
+
data,
|
|
759
|
+
fields: faqFields,
|
|
760
|
+
importFieldHooks,
|
|
761
|
+
req: mockReq
|
|
762
|
+
});
|
|
763
|
+
const faqContent = result.faqContent;
|
|
764
|
+
const faqs = faqContent[0].faqs;
|
|
765
|
+
expect(typeof faqs[0].answer).not.toBe('string');
|
|
766
|
+
expect(faqs[0].answer).toEqual(richTextValue);
|
|
767
|
+
});
|
|
768
|
+
});
|
|
663
769
|
describe('edge cases', ()=>{
|
|
664
770
|
it('should handle empty data', ()=>{
|
|
665
771
|
const result = unflattenObject({
|