-
-
Save sperand-io/19dfc4fd4442ed7c02ca 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
/** | |
* The node posix path parser implemented in ES2015 | |
* | |
* regex from: https://github.com/nodejs/node/blob/master/lib/path.js#L406 | |
* | |
* @param {String} path | |
* @return {Object} result | |
* | |
* let { name } = parse('/foo/bar/baz.html') | |
* // => name: baz | |
*/ | |
const pattern = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/ | |
const splitPath = (file='') => { | |
const [ _ , root='', tempDir='', base='', ext=''] = pattern.exec(file) | |
const name = base.slice(0, base.length - ext.length) | |
const dir = root + tempDir.slice(0, -1) | |
return { name, dir, root, base, ext } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment