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 GitHub
parent 90758e8100
commit aa938190c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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);