Created
December 15, 2017 19:21
-
-
Save rsms/cf455d8cea2a0decd1e8b701b56a3dce 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 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