forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.js
134 lines (117 loc) · 2.42 KB
/
path.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
'use strict';
var path = require('path');
var utils = require('./utils');
/**
* Expose `helpers`
*/
var helpers = module.exports;
/**
* Get the directory path segment from the given `filepath`.
*
* ```handlebars
* {{absolute "docs/toc.md"}}
* //=> 'docs'
* ```
* @param {String} `ext`
* @return {String}
* @api public
*/
helpers.absolute = function(filepath, options) {
var context = utils.merge({}, this, options);
var ctx = utils.merge({}, context.root, context, context._parent, context.hash);
var cwd = ctx.cwd || process.cwd();
return path.resolve(cwd, filepath);
};
/**
* Get the directory path segment from the given `filepath`.
*
* ```handlebars
* {{dirname "docs/toc.md"}}
* //=> 'docs'
* ```
* @param {String} `ext`
* @return {String}
* @api public
*/
helpers.dirname = function(filepath) {
return path.dirname(filepath);
};
/**
* Get the relative filepath from `a` to `b`.
*
* ```handlebars
* {{relative a b}}
* ```
*
* @param {String} `a`
* @param {String} `b`
* @return {String}
* @api public
*/
helpers.relative = function(a, b) {
return utils.relative(a, b);
};
/**
* Get the file extension from the given `filepath`.
*
* ```handlebars
* {{basename "docs/toc.md"}}
* //=> 'toc.md'
* ```
* @param {String} `ext`
* @return {String}
* @api public
*/
helpers.basename = function(filepath) {
return path.basename(filepath);
};
/**
* Get the "stem" from the given `filepath`.
*
* ```handlebars
* {{stem "docs/toc.md"}}
* //=> 'toc'
* ```
* @param {String} `filepath`
* @return {String}
* @api public
*/
helpers.stem = function(filepath) {
return path.basename(filepath, path.extname(filepath));
};
/**
* Get the file extension from the given `filepath`.
*
* ```handlebars
* {{extname "docs/toc.md"}}
* //=> '.md'
* ```
* @param {String} `filepath`
* @return {String}
* @api public
*/
helpers.extname = function(filepath) {
return path.extname(filepath);
};
/**
* Get specific (joined) segments of a file path by passing a
* range of array indices.
*
* ```js
* {{segments "a/b/c/d" "2" "3"}}
* //=> 'c/d'
*
* {{segments "a/b/c/d" "1" "3"}}
* //=> 'b/c/d'
*
* {{segments "a/b/c/d" "1" "2"}}
* //=> 'b/c'
* ```
*
* @param {String} `filepath` The file path to split into segments.
* @return {String} Returns a single, joined file path.
* @api public
*/
helpers.segments = function(fp, a, b) {
return utils.normalize(fp).split('/').slice(a, b).join('/');
};