Created
June 17, 2015 11:27
-
-
Save ivan-kleshnin/edb33afd4ce5c3991d8f 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
import Path from "path"; | |
const splitPathRe = | |
/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; | |
function assertPath(path) { | |
if (typeof path !== 'string') { | |
throw new TypeError('Path must be a string. Received ' + | |
util.inspect(path)); | |
} | |
} | |
function posixSplitPath(filename) { | |
return splitPathRe.exec(filename).slice(1); | |
} | |
function parse(pathString) { | |
assertPath(pathString); | |
var allParts = posixSplitPath(pathString); | |
if (!allParts || allParts.length !== 4) { | |
throw new TypeError("Invalid path '" + pathString + "'"); | |
} | |
allParts[1] = allParts[1] || ''; | |
allParts[2] = allParts[2] || ''; | |
allParts[3] = allParts[3] || ''; | |
var parsed = { | |
root: allParts[0], | |
dir: allParts[0] + allParts[1].slice(0, allParts[1].length - 1), | |
ext: allParts[3], | |
name: allParts[2].slice(0, allParts[2].length - allParts[3].length) | |
}; | |
Object.defineProperty(parsed, "base", { | |
enumerable: true, | |
configurable: false, | |
get: function () { | |
return this.name + this.ext; | |
}, | |
set: function (value) { | |
if (value.startsWith(".") || !value.includes(".")) { | |
this.name = value; | |
this.ext = ""; | |
} else { | |
let [name, ext] = value.split("."); | |
this.name = name; | |
this.ext = "." + ext; | |
} | |
} | |
}); | |
return parsed; | |
} | |
// CURRENT BEHAVIOR =============================================================================== | |
console.log("== current =="); | |
let path0 = "/Users/ivankleshnin/Projects/demo/config.yml"; | |
let parsed0 = Path.parse(path0); | |
console.log(Path.format(parsed0)); // /Users/ivankleshnin/Projects/demo/config.yml | |
parsed0.name = "xxx"; | |
console.log(Path.format(parsed0)); // /Users/ivankleshnin/Projects/demo/config.yml | |
parsed0.ext = ".json"; | |
console.log(Path.format(parsed0)); // /Users/ivankleshnin/Projects/demo/config.yml | |
parsed0.base = "test.html"; | |
console.log(Path.format(parsed0)); // /Users/ivankleshnin/Projects/demo/test.html | |
// NEW BEHAVIOR ==================================================================================== | |
console.log("== new =="); | |
let path1 = "/Users/ivankleshnin/Projects/demo/config.yml"; | |
let parsed1 = parse(path1); | |
console.log(Path.format(parsed1)); // /Users/ivankleshnin/Projects/demo/config.yml | |
parsed1.name = "xxx"; | |
console.log(Path.format(parsed1)); // /Users/ivankleshnin/Projects/demo/xxx.yml | |
parsed1.ext = ".json"; | |
console.log(Path.format(parsed1)); // /Users/ivankleshnin/Projects/demo/xxx.json | |
parsed1.base = "test.html"; | |
console.log(Path.format(parsed1)); // /Users/ivankleshnin/Projects/demo/test.html | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment