Skip to content

Commit 937a52c

Browse files
authoredJul 21, 2018
Merge pull request #35 from mdvorscak/master
Fix #25, all falsy pattern values and empty arrays are now treated the same
2 parents 9131829 + 66ca400 commit 937a52c

File tree

8 files changed

+59
-1
lines changed

8 files changed

+59
-1
lines changed
 

‎lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ function replace(pattern, data, options) {
201201

202202
for (var i = 0, key; (key = keys[i++]); ) {
203203
var val = data[key];
204-
if (val == null) return null;
204+
if (!val || Array.isArray(val) && val.length === 0) return null;
205205
if (val instanceof Date) {
206206
ret[key] = options.date(val);
207207
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
array: []
3+
title: test
4+
---
5+
test
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
falsy: ''
3+
title: test
4+
---
5+
test

‎test/fixtures/falsy/src/false.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
falsy: false
3+
title: test
4+
---
5+
test

‎test/fixtures/falsy/src/nan.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
falsy: .NAN
3+
title: test
4+
---
5+
test

‎test/fixtures/falsy/src/null.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
falsy: null
3+
title: test
4+
---
5+
test
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: test
3+
---
4+
test

‎test/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,33 @@ describe('metalsmith-permalinks', function() {
183183
done();
184184
});
185185
});
186+
187+
it('should use the resolve path for false values (not root)', function (done) {
188+
Metalsmith('test/fixtures/falsy')
189+
.use(permalinks(':falsy/:title'))
190+
.use(function (files, metalsmith, pluginDone) {
191+
Object.keys(files).forEach(function (file) {
192+
assert.notEqual(files[file].path.charAt(0), '/');
193+
});
194+
done();
195+
})
196+
.build(function (err) {
197+
if (err) return done(err);
198+
});
199+
});
200+
201+
202+
it('should use the resolve path for empty arrays (not root)', function (done) {
203+
Metalsmith('test/fixtures/empty-array')
204+
.use(permalinks(':array/:title'))
205+
.use(function (files, metalsmith, pluginDone) {
206+
Object.keys(files).forEach(function (file) {
207+
assert.notEqual(files[file].path.charAt(0), '/');
208+
});
209+
done();
210+
})
211+
.build(function (err) {
212+
if (err) return done(err);
213+
});
214+
});
186215
});

0 commit comments

Comments
 (0)
Please sign in to comment.