Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DXCDT-372: Traverse assets tree for preservable fields #740

Merged
merged 5 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/keywordPreservation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,33 @@ export const shouldFieldBePreserved = (
return !hasArrayMarker && !hasStringMarker;
});
};

export const getPreservableFieldsFromAssets = (
asset: any,
keywordMappings: KeywordMappings
): string[] => {
if (typeof asset === 'string') {
if (shouldFieldBePreserved(asset, keywordMappings)) {
return [asset];
}
return [];
}
if (Array.isArray(asset)) {
return asset
.map((arrayItem) => {
return getPreservableFieldsFromAssets(arrayItem, keywordMappings);
})
.flat();
}
if (typeof asset === 'object') {
return Object.keys(asset)
.map((key: string): string[] => {
const value = asset[key];

if (value === undefined || value === null) return [];
return getPreservableFieldsFromAssets(value, keywordMappings);
})
.flat();
}
return [];
};
45 changes: 44 additions & 1 deletion test/keywordPreservation.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { shouldFieldBePreserved } from '../src/keywordPreservation';
import { shouldFieldBePreserved, getPreservableFieldsFromAssets } from '../src/keywordPreservation';

describe('#Keyword Preservation', () => {
describe('shouldFieldBePreserved', () => {
Expand Down Expand Up @@ -38,4 +38,47 @@ describe('#Keyword Preservation', () => {
).to.be.true;
});
});

describe('getPreservableFieldsFromAssets', () => {
it('should retrieve all preservable fields from assets tree', () => {
const fieldsToPreserve = getPreservableFieldsFromAssets(
{
object: {
friendly_name: 'Friendly name ##KEYWORD##',
notInKeywordMapping: '##NOT_IN_KEYWORD_MAPPING##',
number: 5,
boolean: true,
nested: {
nestedProperty: 'Nested property ##KEYWORD##',
},
},
array: [
{
nestedArray: ['Nested array value 1 ##KEYWORD##', 'Nested array value 2 ##KEYWORD##'],
notInKeywordMapping: '##NOT_IN_KEYWORD_MAPPING##',
nested: {
nestedProperty: 'Another nested array property ##KEYWORD##',
},
},
],
arrayReplace: '@@ARRAY_REPLACE_KEYWORD@@',
nullField: null,
undefinedField: undefined,
},
{
KEYWORD: 'Travel0',
ARRAY_REPLACE_KEYWORD: ['this value', 'that value'],
}
);

expect(fieldsToPreserve).to.have.members([
'Friendly name ##KEYWORD##',
'Nested property ##KEYWORD##',
'Nested array value 1 ##KEYWORD##',
'Nested array value 2 ##KEYWORD##',
'Another nested array property ##KEYWORD##',
'@@ARRAY_REPLACE_KEYWORD@@',
]);
});
});
});