Skip to content

Commit a6b73e9

Browse files
author
Murat Mehmet
committed
fix(plist): add $delete support
1 parent beb75ab commit a6b73e9

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/__tests__/unit/tasks/pListTask.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,39 @@ describe('pListTask', () => {
212212
{ forth: 'test2' },
213213
]);
214214
});
215+
it('should delete value', () => {
216+
let content: Record<string, any> = {
217+
first: {
218+
second: {
219+
third: 'test',
220+
},
221+
},
222+
};
223+
const task: PlistTaskType = {
224+
task: 'plist',
225+
actions: [
226+
{
227+
set: {
228+
first: {
229+
second: {
230+
$delete: true,
231+
},
232+
},
233+
},
234+
strategy: 'merge',
235+
},
236+
],
237+
};
238+
239+
content = plistTask({
240+
configPath: 'path/to/config',
241+
task: task,
242+
content,
243+
packageName: 'test-package',
244+
});
245+
expect(content.first).toEqual({});
246+
});
247+
215248
it('should skip if condition not met', () => {
216249
let content: Record<string, any> = {
217250
first: {

src/utils/applyObjectModification.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ export function modifyObject(
3939
}
4040
} else {
4141
/* eslint-disable @typescript-eslint/no-unsafe-return */
42-
const customizer = function (objValue: any, srcValue: any) {
42+
const fieldsToDelete: [Record<string, any>, string][] = [];
43+
const customizer = function (
44+
objValue: any,
45+
srcValue: any,
46+
field: string,
47+
objParent: Record<string, any>
48+
) {
4349
if (strategy == 'merge_concat') {
4450
if (Array.isArray(objValue) && Array.isArray(srcValue)) {
4551
return objValue.concat(srcValue);
@@ -61,6 +67,10 @@ export function modifyObject(
6167
delete srcValue.$append;
6268
return objValue;
6369
}
70+
if (typeof srcValue === 'object' && srcValue.$delete) {
71+
fieldsToDelete.push([objParent, field]);
72+
return true;
73+
}
6474
if (
6575
Array.isArray(objValue) &&
6676
typeof srcValue === 'object' &&
@@ -75,6 +85,7 @@ export function modifyObject(
7585
};
7686

7787
content = mergeWith(content, modifier, customizer);
88+
fieldsToDelete.forEach(([obj, field]) => delete obj[field]);
7889
/* eslint-enable @typescript-eslint/no-unsafe-return */
7990
}
8091
return content;

0 commit comments

Comments
 (0)