Skip to content

Instantly share code, notes, and snippets.

@sperand-io
Forked from nopjia/splitPath.js
Last active April 3, 2018 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sperand-io/19dfc4fd4442ed7c02ca to your computer and use it in GitHub Desktop.
Save sperand-io/19dfc4fd4442ed7c02ca to your computer and use it in GitHub Desktop.
/**
* 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