feat(archive): add double-extension-aware filename parsing helper
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
import path from 'node:path';
|
||||||
|
|
||||||
|
const DOUBLE_EXTENSIONS = ['tar.gz', 'tar.bz2', 'tar.7z'];
|
||||||
|
|
||||||
|
export function extractExtension(filename) {
|
||||||
|
const lower = filename.toLowerCase();
|
||||||
|
const match = DOUBLE_EXTENSIONS.find((ext) => lower.endsWith(`.${ext}`));
|
||||||
|
return match ?? path.extname(filename).slice(1).toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stripExtension(filename) {
|
||||||
|
const ext = extractExtension(filename);
|
||||||
|
return filename.slice(0, filename.length - ext.length - 1);
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { extractExtension, stripExtension } from '../src/archiveExtensions.js';
|
||||||
|
|
||||||
|
describe('extractExtension', () => {
|
||||||
|
it('returns the simple extension for a normal filename', () => {
|
||||||
|
expect(extractExtension('photo.PNG')).toBe('png');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('recognizes tar.gz as a single compound extension', () => {
|
||||||
|
expect(extractExtension('backup.TAR.GZ')).toBe('tar.gz');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('recognizes tar.bz2 as a single compound extension', () => {
|
||||||
|
expect(extractExtension('backup.tar.bz2')).toBe('tar.bz2');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('recognizes tar.7z as a single compound extension', () => {
|
||||||
|
expect(extractExtension('backup.tar.7z')).toBe('tar.7z');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not treat an unrelated double extension as compound', () => {
|
||||||
|
expect(extractExtension('archive.zip.bak')).toBe('bak');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('stripExtension', () => {
|
||||||
|
it('strips a simple extension', () => {
|
||||||
|
expect(stripExtension('photo.png')).toBe('photo');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('strips a compound tar.gz extension, keeping the rest of the name intact', () => {
|
||||||
|
expect(stripExtension('my.backup.tar.gz')).toBe('my.backup');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user