From b2b2cedb06dea9deffbe1693be6be4913801788d Mon Sep 17 00:00:00 2001 From: John Cowen Date: Tue, 23 Jun 2020 18:34:21 +0100 Subject: [PATCH] ui: Make sure right trim doesn't try to overtrim (#8171) --- ui-v2/app/utils/right-trim.js | 5 ++++- ui-v2/tests/unit/utils/right-trim-test.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ui-v2/app/utils/right-trim.js b/ui-v2/app/utils/right-trim.js index 5e1b1d3c46..35af22815c 100644 --- a/ui-v2/app/utils/right-trim.js +++ b/ui-v2/app/utils/right-trim.js @@ -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; } diff --git a/ui-v2/tests/unit/utils/right-trim-test.js b/ui-v2/tests/unit/utils/right-trim-test.js index f8efb2cf6a..6898de8e9c 100644 --- a/ui-v2/tests/unit/utils/right-trim-test.js +++ b/ui-v2/tests/unit/utils/right-trim-test.js @@ -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);