ui: Make sure right trim doesn't try to overtrim (#8171)

This commit is contained in:
John Cowen 2020-06-23 18:34:21 +01:00 committed by hashicorp-ci
parent deb77f31cb
commit b2b2cedb06
2 changed files with 20 additions and 1 deletions

View File

@ -1,4 +1,7 @@
export default function rightTrim(str = '', search = '') {
const pos = str.length - search.length;
return str.lastIndexOf(search) === pos ? str.substr(0, pos) : str;
if (pos >= 0) {
return str.lastIndexOf(search) === pos ? str.substr(0, pos) : str;
}
return str;
}

View File

@ -37,6 +37,22 @@ module('Unit | Utility | right trim', function() {
args: ['/a/folder/here/', '/a/folder/here/'],
expected: '',
},
{
args: ['/a/folder/here/', '-'],
expected: '/a/folder/here/',
},
{
args: ['/a/folder/here/', 'here'],
expected: '/a/folder/here/',
},
{
args: ['here', '/here'],
expected: 'here',
},
{
args: ['/here', '/here'],
expected: '',
},
].forEach(function(item) {
const actual = rightTrim(...item.args);
assert.equal(actual, item.expected);