UI - fix bug in policy creation from files (#5864)

* fix bug in trim-right util where the last ending wasn't trimming from the end of the string

* simplify based on feedback
This commit is contained in:
Matthew Irish 2018-11-28 10:35:55 -06:00 committed by GitHub
parent 37e75a95c7
commit aba957660f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -1,4 +1,8 @@
export default function(fileName, toTrimArray = []) {
const extension = new RegExp(toTrimArray.join('$|'));
return fileName.replace(extension, '');
// will trim a given set of endings from the end of a string
// if isExtension is true, the first char of that string will be escaped
// in the regex
export default function(str, endings = [], isExtension = true) {
let prefix = isExtension ? '\\' : '';
let trimRegex = new RegExp(endings.map(ext => `${prefix}${ext}$`).join('|'));
return str.replace(trimRegex, '');
}

View File

@ -25,4 +25,23 @@ module('Unit | Util | trim right', function() {
assert.equal(trimmedName, 'my-file-name-is-cool.json');
});
test('it allows the last extension to also be part of the file name', function(assert) {
const trimmedName = trimRight('my-policy.hcl', ['.json', '.txt', '.hcl', '.policy']);
assert.equal(trimmedName, 'my-policy');
});
test('it allows the last extension to also be part of the file name and the extenstion', function(assert) {
const trimmedName = trimRight('my-policy.policy', ['.json', '.txt', '.hcl', '.policy']);
assert.equal(trimmedName, 'my-policy');
});
test('it passes endings into the regex unescaped when passing false as the third arg', function(assert) {
const trimmedName = trimRight('my-policypolicy', ['.json', '.txt', '.hcl', '.policy'], false);
// the . gets interpreted as regex wildcard so it also trims the y character
assert.equal(trimmedName, 'my-polic');
});
});