Created
January 22, 2025 18:30
-
-
Save DavidWells/ca67d5c06b7b958da4190ffbd3e5c3ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function testFilename(filename) { | |
const match = filename.match(/^(?:(\d+)[.-])?([^.]+)(?:\.([^.]+))?(?:\.([^.]+))?$/) | |
if (match) { | |
const order = match[1] | |
const base = match[2] ?? filename | |
const modifier = match[4] ? match[3] : undefined | |
const extension = match[4] ?? match[3] | |
console.log({ | |
filename, | |
order, | |
base, | |
modifier, | |
extension | |
}) | |
} | |
} | |
// Examples with parsed values: | |
testFilename('01-hello.md') // {order: '01', base: 'hello', modifier: undefined, extension: 'md'} | |
testFilename('hello.md') | |
testFilename('hello.test.js') // {order: undefined, base: 'hello', modifier: 'test', extension: 'js'} | |
testFilename('01.page.test.jsx') | |
testFilename('hello') | |
testFilename('wow/hello.md') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment