From af393508941816688c5fb3abd002fe89a3fb7d25 Mon Sep 17 00:00:00 2001 From: Jeremy Ho Date: Thu, 31 Aug 2023 14:06:17 -0700 Subject: [PATCH] Bugfix isAtPath to accept identical prefix/path values The previous fix checking for length being less than 2 was not accurate because it was letting paths with just a trailing slash to pass through. We roll it back to checking if the length is exactly 1 in difference, and also do an earlier check for prefix and path being identical in order to better handle the exception cases. Signed-off-by: Jeremy Ho --- app/src/components/utils.js | 3 ++- app/tests/unit/components/utils.spec.js | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/src/components/utils.js b/app/src/components/utils.js index ec688ecf..fd550221 100644 --- a/app/src/components/utils.js +++ b/app/src/components/utils.js @@ -306,11 +306,12 @@ const utils = { */ isAtPath(prefix, path) { if (typeof prefix !== 'string' || typeof path !== 'string') return false; + if (prefix === path) return true; // Matching strings are always at the at the path const pathParts = path.split(DELIMITER).filter(part => part); const prefixParts = prefix.split(DELIMITER).filter(part => part); return prefixParts.every((part, i) => pathParts[i] === part) - && pathParts.filter(part => !prefixParts.includes(part)).length < 2; + && pathParts.filter(part => !prefixParts.includes(part)).length === 1; }, /** diff --git a/app/tests/unit/components/utils.spec.js b/app/tests/unit/components/utils.spec.js index 2ecc7208..f6234083 100644 --- a/app/tests/unit/components/utils.spec.js +++ b/app/tests/unit/components/utils.spec.js @@ -455,20 +455,22 @@ describe('isAtPath', () => { it.each([ [false, undefined, undefined], [false, null, null], - [true, '', ''], // Root level empty string identies should technically be true + [true, '', ''], // Root level empty string identities should technically be true [true, '', 'file'], [false, '', 'file/bleep'], [true, '/', 'file'], [false, '/', 'file/bleep'], - [true, 'foo', 'foo'], // Root level file identies should be true + [true, 'foo', 'foo'], // Root level file identities should be true [false, 'foo', 'bar'], // Non-matching root level path and prefix should be false [true, 'foo', 'foo/bar'], [true, 'foo', '/foo/bar'], [true, '/foo', 'foo/bar'], [true, '/foo', '/foo/bar'], [true, 'a/b', 'a/b/foo.jpg'], + [false, 'a/b', 'a/b/'], // Trailing slashes references the folder and should be excluded [false, 'a/b', 'a/b/z/deep.jpg'], [false, 'a/b', 'a/b/y/z/deep.jpg'], + [false, 'a/b/c', 'a/b/c/'], // Trailing slashes references the folder and should be excluded [false, 'a/b/c', 'a/bar.png'], [false, 'c/b/a', 'a/b/c/bar.png'], [false, 'c/a/b', 'a/b/c/bar.png'],