forked from oneflow/handlebars-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.js
88 lines (81 loc) · 3.44 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
const assert = require('assert');
const hbs = require('handlebars').create();
const helpers = require('..');
helpers.path({ handlebars: hbs });
describe('assemble', function() {
describe('dirname', function() {
it('should get the dirname of a file path', function() {
assert.equal(hbs.compile('{{dirname "a/b/c/package.json"}}')(), 'a/b/c');
assert.equal(hbs.compile('{{dirname "a/b/c/docs/toc.md"}}')(), 'a/b/c/docs');
});
it('should return an empty string for a non-string', function() {
const fn = hbs.compile('{{dirname value}}');
assert.equal(fn({ value: {} }), '');
});
});
describe('basename', function() {
it('should get the basename of a file path', function() {
assert.equal(hbs.compile('{{basename "a/b/c/package.json"}}')(), 'package.json');
assert.equal(hbs.compile('{{basename "a/b/c/docs/toc.md"}}')(), 'toc.md');
});
it('should get the basename when a path has no extension', function() {
const fn = hbs.compile('{{basename "a/b/c/CHANGELOG"}}');
assert.equal(fn(), 'CHANGELOG');
});
it('should return an empty string for a non-string', function() {
const fn = hbs.compile('{{basename value}}');
assert.equal(fn({ value: {} }), '');
});
});
describe('stem', function() {
it('should get the stem of a file path', function() {
assert.equal(hbs.compile('{{stem "a/b/c/package.json"}}')(), 'package');
assert.equal(hbs.compile('{{stem "a/b/c/docs/toc.md"}}')(), 'toc');
});
it('should get the stem when a path has no extension', function() {
const fn = hbs.compile('{{stem "CHANGELOG"}}');
assert.equal(fn(), 'CHANGELOG');
});
it('should return an empty string for a non-string', function() {
const fn = hbs.compile('{{stem value}}');
assert.equal(fn({ value: {} }), '');
});
});
describe('extname', function() {
it('should get the extname of a file path', function() {
assert.equal(hbs.compile('{{extname "a/b/c/package.json"}}')(), '.json');
assert.equal(hbs.compile('{{extname "a/b/c/docs/toc.md"}}')(), '.md');
});
it('should not blow up when a path has no extension', function() {
const fn = hbs.compile('{{extname "a/b/c/CHANGELOG"}}');
assert.equal(fn(), '');
});
it('should return an empty string for a non-string', function() {
const fn = hbs.compile('{{extname value}}');
assert.equal(fn({ value: {} }), '');
});
});
describe('segments', function() {
it('should return specified path segments:', function() {
assert.equal(hbs.compile('{{segments "a/b/c/e.js" 1 3}}')(), 'b/c');
assert.equal(hbs.compile('{{segments "a/b/c/e.js" 1 2}}')(), 'b');
assert.equal(hbs.compile('{{segments "a/b/c/e.js" 0 3}}')(), 'a/b/c');
assert.equal(hbs.compile('{{segments "a/b/c/e.js" 2 3}}')(), 'c');
assert.equal(hbs.compile('{{segments "a/b/c/e.js" 0 3}}')(), 'a/b/c');
});
it('should return an empty string for a non-string', function() {
const fn = hbs.compile('{{segments value}}');
assert.equal(fn({ value: {} }), '');
});
});
describe('linkToUNC', () => {
it('returns blank string if no link supplied', () => {
const fn = hbs.compile('{{linkToUNC null}}');
assert.equal(fn({}), '');
});
it('changes file:// to \\\\ and reverse the slashes', () => {
const fn = hbs.compile('{{linkToUNC "file://path.to/here"}}');
assert.equal(fn({}), '\\\\path.to\\here');
});
});
});