Skip to content

Commit c8b9f04

Browse files
authored
DXCDT-380: shouldFieldBePreserved function (#738)
Adding shouldFieldBePreserved function Co-authored-by: Will Vedder <[email protected]>
1 parent e750263 commit c8b9f04

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

src/keywordPreservation.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { KeywordMappings } from './types';
2+
import { keywordReplaceArrayRegExp, keywordReplaceStringRegExp } from './tools/utils';
3+
4+
export const shouldFieldBePreserved = (
5+
string: string,
6+
keywordMappings: KeywordMappings
7+
): boolean => {
8+
return !Object.keys(keywordMappings).every((keyword) => {
9+
const hasArrayMarker = keywordReplaceArrayRegExp(keyword).test(string);
10+
const hasStringMarker = keywordReplaceStringRegExp(keyword).test(string);
11+
12+
return !hasArrayMarker && !hasStringMarker;
13+
});
14+
};

src/tools/utils.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,30 @@ import log from '../logger';
66
import { Asset, Assets, CalculatedChanges, KeywordMappings } from '../types';
77
import constants from './constants';
88

9+
export const keywordReplaceArrayRegExp = (key) => {
10+
const pattern = `@@${key}@@`;
11+
const patternWithQuotes = `"${pattern}"`;
12+
13+
return new RegExp(`${patternWithQuotes}|${pattern}`, 'g');
14+
};
15+
16+
export const keywordReplaceStringRegExp = (key) => {
17+
return new RegExp(`##${key}##`, 'g');
18+
};
19+
920
export function keywordArrayReplace(input: string, mappings: KeywordMappings): string {
1021
Object.keys(mappings).forEach(function (key) {
1122
// Matching against two sets of patterns because a developer may provide their array replacement keyword with or without wrapping quotes. It is not obvious to the developer which to do depending if they're operating in YAML or JSON.
12-
const pattern = `@@${key}@@`;
13-
const patternWithQuotes = `"${pattern}"`;
23+
const regex = keywordReplaceArrayRegExp(key);
1424

15-
const regex = new RegExp(`${patternWithQuotes}|${pattern}`, 'g');
1625
input = input.replace(regex, JSON.stringify(mappings[key]));
1726
});
1827
return input;
1928
}
2029

2130
export function keywordStringReplace(input: string, mappings: KeywordMappings): string {
2231
Object.keys(mappings).forEach(function (key) {
23-
const regex = new RegExp(`##${key}##`, 'g');
32+
const regex = keywordReplaceStringRegExp(key);
2433
// @ts-ignore TODO: come back and distinguish strings vs array replacement.
2534
input = input.replace(regex, mappings[key]);
2635
});

test/keywordPreservation.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { expect } from 'chai';
2+
import { shouldFieldBePreserved } from '../src/keywordPreservation';
3+
4+
describe('#Keyword Preservation', () => {
5+
describe('shouldFieldBePreserved', () => {
6+
it('should return false when field does not contain keyword markers', () => {
7+
const keywordMappings = {
8+
BAR: 'bar',
9+
};
10+
expect(shouldFieldBePreserved('', keywordMappings)).to.be.false;
11+
expect(shouldFieldBePreserved('this is a field without a keyword marker', keywordMappings)).to
12+
.be.false;
13+
expect(shouldFieldBePreserved('this field has an invalid @keyword@ marker', keywordMappings))
14+
.to.be.false;
15+
});
16+
17+
it('should return false when field contain keyword markers but are absent in keyword mappings', () => {
18+
const keywordMappings = {
19+
BAR: 'bar',
20+
};
21+
expect(shouldFieldBePreserved('##FOO##', keywordMappings)).to.be.false;
22+
expect(shouldFieldBePreserved('@@FOO@@', keywordMappings)).to.be.false;
23+
expect(shouldFieldBePreserved('this field has a @@FOO@@ marker', keywordMappings)).to.be
24+
.false;
25+
});
26+
27+
it('should return true when field contain keyword markers that exist in keyword mappings', () => {
28+
const keywordMappings = {
29+
FOO: 'foo keyword',
30+
BAR: 'bar keyword',
31+
ARRAY: ['foo', 'bar'],
32+
};
33+
expect(shouldFieldBePreserved('##FOO##', keywordMappings)).to.be.true;
34+
expect(shouldFieldBePreserved('@@FOO@@', keywordMappings)).to.be.true;
35+
expect(shouldFieldBePreserved('this field has a ##FOO## marker', keywordMappings)).to.be.true;
36+
expect(
37+
shouldFieldBePreserved('this field has both a ##FOO## and ##BAR## marker', keywordMappings)
38+
).to.be.true;
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)