diff --git a/index.js b/index.js index baf1549..9229b86 100644 --- a/index.js +++ b/index.js @@ -92,13 +92,15 @@ const staticify = (root, options) => { const fileName = path.basename(p); const fileNameParts = fileName.split('.'); + const fileNameHashPosition = fileNameParts.length - 2; + const fileNameHash = fileNameParts[fileNameHashPosition]; const re = new RegExp(`^[0-9a-f]{${HASH_LEN}}$`, 'i'); + const reResult = re.exec(fileNameHash); - if (fileNameParts.length >= 3 && - fileNameParts[fileNameParts.length - 2].length === HASH_LEN && - re.exec(fileNameParts[fileNameParts.length - 2])[0] === fileNameParts[fileNameParts.length - 2] + if (fileNameParts.length >= 3 && fileNameHash.length === HASH_LEN && + (reResult && reResult[0] === fileNameHash) ) { - const stripped = fileNameParts.slice(0, fileNameParts.length - 2); + const stripped = fileNameParts.slice(0, fileNameHashPosition); stripped.push(fileNameParts[fileNameParts.length - 1]); diff --git a/test/index.js b/test/index.js index 0f3fdce..5a4de81 100644 --- a/test/index.js +++ b/test/index.js @@ -32,10 +32,18 @@ describe('.stripVersion', () => { staticify(ROOT).stripVersion(path.normalize('/script.js')).should.equal(path.normalize('/script.js')); }); + it('should not fail when the path contains a 7 character string that is not a hash', () => { + staticify(ROOT).stripVersion(path.normalize('/script.abcdefg.html')).should.equal(path.normalize('/script.abcdefg.html')); + }); + it('should strip the (long) hash from a path when necessary', () => { staticify(ROOT, {shortHash: false}).stripVersion(path.normalize('/script.4e2502b01a4c92b0a51b1a5a3271eab6.js')).should.equal(path.normalize('/script.js')); staticify(ROOT, {shortHash: false}).stripVersion(path.normalize('/script.js')).should.equal(path.normalize('/script.js')); }); + + it('should not fail when the path contains a 32 character string that is not a hash', () => { + staticify(ROOT).stripVersion(path.normalize('/script.abcdefgabcdefgabcdefgabcdefgabcd.html')).should.equal(path.normalize('/script.abcdefgabcdefgabcdefgabcdefgabcd.html')); + }); }); describe('.getVersionedPath', () => {