3 var path = require('path');
4 var isAbsolute = require('is-absolute');
5 var pathRoot = require('path-root');
6 var MapCache = require('map-cache');
7 var cache = new MapCache();
9 module.exports = function(filepath) {
10 if (typeof filepath !== 'string') {
11 throw new TypeError('parse-filepath expects a string');
14 if (cache.has(filepath)) {
15 return cache.get(filepath);
19 if (typeof path.parse === 'function') {
20 obj = path.parse(filepath);
21 obj.extname = obj.ext;
22 obj.basename = obj.base;
23 obj.dirname = obj.dir;
27 define(obj, 'root', function() {
28 return pathRoot(this.path);
31 define(obj, 'extname', function() {
32 return path.extname(filepath);
35 define(obj, 'ext', function() {
39 define(obj, 'name', function() {
40 return path.basename(filepath, this.ext);
43 define(obj, 'stem', function() {
47 define(obj, 'base', function() {
48 return this.name + this.ext;
51 define(obj, 'basename', function() {
55 define(obj, 'dir', function() {
56 return path.dirname(filepath);
59 define(obj, 'dirname', function() {
66 define(obj, 'absolute', function() {
67 return path.resolve(this.path);
70 define(obj, 'isAbsolute', function() {
71 return isAbsolute(this.path);
74 cache.set(filepath, obj);
78 function define(obj, prop, fn) {
80 Object.defineProperty(obj, prop, {
87 return cached || (cached = fn.call(obj));