forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.js
129 lines (117 loc) · 4.23 KB
/
fs.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
'use strict';
require('mocha');
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var hbs = require('handlebars').create();
require('..')({handlebars: hbs});
var libFiles = fs.readdirSync(path.join(__dirname, '../lib'))
.map(function(fp) {
return path.join('lib', fp);
});
describe('fs', function() {
describe('read', function() {
it('should read a file from the file system', function() {
var fn = hbs.compile('{{read filepath}}');
assert.equal(fn({filepath: 'test/fixtures/read/a.txt'}), 'abc');
});
});
describe('readdir', function() {
it('should return an array of files', function() {
var fn = hbs.compile('{{readdir dir}}');
assert.deepEqual(fn({dir: 'lib'}).split(','), libFiles);
});
it('should work as a subexpression', function() {
var fn = hbs.compile('{{match (readdir dir) "**/[a-c]*.js"}}');
assert.deepEqual(fn({dir: 'lib'}).split(','), [
'lib/array.js',
'lib/code.js',
'lib/collection.js',
'lib/comparison.js'
]);
});
it('should filter using a custom filter function', function() {
var fn = hbs.compile('{{readdir dir filter}}');
function filter(arr) {
return arr.filter(function(fp) {
return /\.js$/.test(fp);
});
}
assert.deepEqual(fn({dir: 'lib', filter: filter}).split(','), [
path.join('lib', 'array.js'),
path.join('lib', 'code.js'),
path.join('lib', 'collection.js'),
path.join('lib', 'comparison.js'),
path.join('lib', 'date.js'),
path.join('lib', 'fs.js'),
path.join('lib', 'html.js'),
path.join('lib', 'i18n.js'),
path.join('lib', 'index.js'),
path.join('lib', 'inflection.js'),
path.join('lib', 'logging.js'),
path.join('lib', 'markdown.js'),
path.join('lib', 'match.js'),
path.join('lib', 'math.js'),
path.join('lib', 'misc.js'),
path.join('lib', 'number.js'),
path.join('lib', 'object.js'),
path.join('lib', 'path.js'),
path.join('lib', 'regex.js'),
path.join('lib', 'string.js'),
path.join('lib', 'url.js')
]);
});
it('should filter using a regex', function() {
var fn = hbs.compile('{{readdir dir (toRegex "\\.js$")}}');
assert.deepEqual(fn({dir: 'lib'}).split(','), [
path.join('lib', 'array.js'),
path.join('lib', 'code.js'),
path.join('lib', 'collection.js'),
path.join('lib', 'comparison.js'),
path.join('lib', 'date.js'),
path.join('lib', 'fs.js'),
path.join('lib', 'html.js'),
path.join('lib', 'i18n.js'),
path.join('lib', 'index.js'),
path.join('lib', 'inflection.js'),
path.join('lib', 'logging.js'),
path.join('lib', 'markdown.js'),
path.join('lib', 'match.js'),
path.join('lib', 'math.js'),
path.join('lib', 'misc.js'),
path.join('lib', 'number.js'),
path.join('lib', 'object.js'),
path.join('lib', 'path.js'),
path.join('lib', 'regex.js'),
path.join('lib', 'string.js'),
path.join('lib', 'url.js')
]);
});
it('should filter using a glob pattern', function() {
var fn = hbs.compile('{{readdir dir "lib/[a-d]*.js"}}');
assert.deepEqual(fn({dir: 'lib'}).split(','), [
path.join('lib', 'array.js'),
path.join('lib', 'code.js'),
path.join('lib', 'collection.js'),
path.join('lib', 'comparison.js'),
path.join('lib', 'date.js')
]);
});
it('should filter by fs.stat (files)', function() {
var fn = hbs.compile('{{readdir dir "isFile"}}');
assert.deepEqual(fn({dir: 'lib'}).split(','), libFiles.filter(function(fp) {
return fp.indexOf(path.join('lib', 'util')) !== 0;
}));
});
it('should filter by fs.stat (dirs)', function() {
var fn = hbs.compile('{{readdir dir "isDirectory"}}');
assert.deepEqual(fn({dir: 'lib'}).split(','), [
path.join('lib', 'utils')
]);
});
it('should return the whole array when the filter is invalid', function() {
var fn = hbs.compile('{{readdir dir "foo"}}');
assert.deepEqual(fn({dir: 'lib'}).split(','), libFiles);
});
});
});