-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (134 loc) · 3.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* node-name
* https://github.com/helpers/node-name
* Copyright (c) 2013 Jon Schlinkert
* Licensed under the MIT license.
*/
// Node dependencies.
var path = require('path');
// Export this module.
var name = module.exports = exports = {}
/**
* Directory path, excluding the filename
* @param {[type]} path [description]
* @return {[type]} [description]
*/
name.dir = function(str) {
return str.split(path.sep).pop().split('.')[0];
};
/**
* Get the name of the first dir in path
* @param {[type]} str [description]
* @return {[type]} [description]
*/
name.first = function(str) {
return str.split(path.sep).slice(0, 1)[0];
};
/**
* Filename
* @param {[type]} name [description]
* @return {[type]} [description]
*/
name.file = function(name) {
return path.basename(name).replace(/\.[^\.]+$/, '');
};
/**
* Filename
* @param {[type]} filepath [description]
* @return {[type]} [description]
*/
name.filename = function(filepath) {
return filepath.split(path.sep).pop().split('/').pop();
};
/**
* Fullname
* @param {[type]} filepath [description]
* @return {[type]} [description]
*/
name.fullname = function(filepath) {
return filepath.replace(/^.*[\\\/]/, '');
};
/**
* Returns the basename of a file by removing
* the last segment of the name
* @param {[type]} base [description]
* @param {[type]} ext [description]
* @return {[type]} [description]
* foo/bar.baz.quux => bar.baz
*/
name.basename = function(filepath) {
return path.basename(filepath, path.extname(filepath));
};
/**
* Returns the basename of a file
* @param {[type]} filepath [description]
* @return {[type]} [description]
* @example
* foo/bar.baz.quux => bar
*/
name.base = function(filepath) {
var base = filepath.split(path.sep).slice(-1)[0];
base = path.basename(filepath, path.extname(filepath));
if (base.lastIndexOf('.') > 0) {
base = base.substr(0, base.lastIndexOf('.'));
}
return base;
};
/**
* File extension
* @param {[type]} str [description]
* @return {[type]} [description]
*/
name.ext = function(str) {
var extname = path.extname(str);
if (extname) {
str = extname;
}
if (str[0] === ".") {
str = str.substring(1);
}
return str;
};
/**
* Returns the basename of a file, with dots and
* underscores transformed to dashes.
* @param {[type]} filepath [description]
* @return {[type]} [description]
*/
name.dashify = function(filepath) {
var base = filepath.split(path.sep).slice(-1)[0];
base = path.basename(filepath, path.extname(filepath));
if (base.lastIndexOf('.') > 0) {
base = base.substr(0, base.lastIndexOf('.'));
}
return base.replace(/\-|\_|\./g, '-');
};
// this/is/a/filepath/and-extensions.one.two.three/four/five/six.ext
// => this/is/a/filepath/and-extensions.one.two.three/four/five/six
name.removeLast = function (name) {
return path.basename(name).replace(/\.[^\.]+$/, '');
};
// this/is/a/filepath/and-extensions.one.two.three/four/five/six.ext
// => this/is/a/filepath/and-extensions.two.three/four/five/six.ext
name.removeFirst = function (name) {
return path.basename(name).replace(/\.[^\.]+/, '');
};
// this/is/a/filepath/and-extensions.one.two.three/four/five/six.ext
// => this/is/a/filepath/and-extensions
name.removeAll = function (name) {
return path.basename(name).replace(/\.[^\.]+/g, '');
};
/**
* Return an object with a `dirname` property and a `filename` property
* @param {String} path [description]
* @return {Object} [description]
*/
name.splitPath = function(path) {
var dirname;
var filename;
path.replace(/^(.*\/)?([^/]*)$/, function(_, dir, file) {
dirname = dir;
filename = file;
});
return { dirname: dirname, filename: filename };
};