Skip to content

Instantly share code, notes, and snippets.

@rsms
Created December 15, 2017 19:21
Show Gist options
  • Select an option

  • Save rsms/cf455d8cea2a0decd1e8b701b56a3dce to your computer and use it in GitHub Desktop.

Select an option

Save rsms/cf455d8cea2a0decd1e8b701b56a3dce to your computer and use it in GitHub Desktop.
function stripext(fn) {
const di = fn.lastIndexOf('.')
if (di < 1) {
return fn
}
const si = fn.lastIndexOf('/')
return (si == -1 || di > si+1) ? fn.substr(0, di) : fn
}
for (const sample of [
['/foo/bar/baz.txt.lol', '/foo/bar/baz.txt'],
['/foo/bar.baz/cat', '/foo/bar.baz/cat'],
['/foo/bar.baz/cat.lol', '/foo/bar.baz/cat'],
['/foo/bar/baz', '/foo/bar/baz'],
['/foo/bar/.baz', '/foo/bar/.baz'],
['bob.cat', 'bob'],
['bob', 'bob'],
['.bob', '.bob'],
]) {
const res = stripext(sample[0])
if (res == sample[1]) {
console.log('OK', sample[0], '=>', res)
} else {
console.log(
'FAIL', sample[0], '=>', res, `(expected "${res}" == "${sample[1]}"`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment