Added hyphen pattern case preserve logic

This commit is contained in:
skprabhanjan 2019-08-20 23:04:41 +05:30
parent 78d48a09cd
commit aa7c5a44cc

View file

@ -12,7 +12,11 @@ export function buildReplaceStringWithCasePreserved(matches: string[] | null, pa
} else if (matches[0].toLowerCase() === matches[0]) {
return pattern.toLowerCase();
} else if (strings.containsUppercaseCharacter(matches[0][0])) {
return pattern[0].toUpperCase() + pattern.substr(1);
if (validateHyphenPattern(matches, pattern)) {
return buildReplaceStringForHyphenPatterns(matches, pattern);
} else {
return pattern[0].toUpperCase() + pattern.substr(1);
}
} else {
// we don't understand its pattern yet.
return pattern;
@ -21,3 +25,20 @@ export function buildReplaceStringWithCasePreserved(matches: string[] | null, pa
return pattern;
}
}
function validateHyphenPattern(matches: string[], pattern: string): boolean {
const doesConatinHyphen = matches[0].indexOf('-') !== -1 && pattern.indexOf('-') !== -1;
const doesConatinSameNumberOfHyphens = matches[0].split('-').length === pattern.split('-').length;
return doesConatinHyphen && doesConatinSameNumberOfHyphens;
}
function buildReplaceStringForHyphenPatterns(matches: string[], pattern: string): string {
const splitPatternAtHyphen = pattern.split('-');
const splitMatchAtHyphen = matches[0].split('-');
let replaceString: string = '';
splitPatternAtHyphen.forEach((splitValues, index) => {
replaceString += buildReplaceStringWithCasePreserved([splitMatchAtHyphen[index]], splitValues) + '-';
});
return replaceString.slice(0, -1);
}