Skip to content

Commit fe7e666

Browse files
committed
lint, fix code comments
1 parent 3ba7036 commit fe7e666

File tree

7 files changed

+79
-25
lines changed

7 files changed

+79
-25
lines changed

lib/array.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ helpers.after = function(array, n) {
2929
*
3030
* ```handlebars
3131
* {{arrayify "foo"}}
32-
* <!-- results in: '["foo"]' -->
32+
* <!-- results in: [ "foo" ] -->
3333
* ```
3434
* @param {any} `value`
3535
* @return {Array}
@@ -458,10 +458,10 @@ helpers.pluck = function(arr, prop) {
458458
* Reverse the elements in an array, or the characters in a string.
459459
*
460460
* ```handlebars
461-
* <!-- value = 'abcd' -->
461+
* <!-- value: 'abcd' -->
462462
* {{reverse value}}
463463
* <!-- results in: 'dcba' -->
464-
* <!-- value = ['a', 'b', 'c', 'd'] -->
464+
* <!-- value: ['a', 'b', 'c', 'd'] -->
465465
* {{reverse value}}
466466
* <!-- results in: ['d', 'c', 'b', 'a'] -->
467467
* ```
@@ -798,12 +798,25 @@ helpers.withSort = function(array, prop, options) {
798798
return result;
799799
};
800800

801+
/**
802+
* Block helper that return an array with all duplicate
803+
* values removed. Best used along with a [each](#each) helper.
804+
*
805+
* ```handlebars
806+
* <!-- array: ['a', 'a', 'c', 'b', 'e', 'e'] -->
807+
* {{#each (unique array)}}{{.}}{{/each}}
808+
* <!-- results in: 'acbe' -->
809+
* ```
810+
* @param {Array} `array`
811+
* @param {Object} `options`
812+
* @return {Array}
813+
* @api public
814+
*/
815+
801816
helpers.unique = function(array, options) {
802817
if (util.isUndefined(array)) return '';
803818

804-
return array.filter(function(item, index, self) {
805-
return self.indexOf(item) === index;
819+
return array.filter(function(item, index, arr) {
820+
return arr.indexOf(item) === index;
806821
});
807-
808822
};
809-

lib/code.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ var helpers = module.exports;
1010
*
1111
* ```handlebars
1212
* {{embed 'path/to/file.js'}}
13-
*
14-
* <!-- specify the language to use -->
13+
* <!-- optionally specify the language to use -->
1514
* {{embed 'path/to/file.hbs' 'html')}}
1615
* ```
1716
*

lib/collection.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ var forOwn = object.forOwn;
88
var helpers = module.exports;
99

1010
/**
11-
* Block helper that returns a block if the given collection is
12-
* empty. If the collection is not empty the inverse block is returned
13-
* (if supplied).
11+
* Inline, subexpression, or block helper that returns true (or the block)
12+
* if the given collection is empty, or false (or the inverse block, if
13+
* supplied) if the colleciton is not empty.
1414
*
15+
* ```handlebars
16+
* <!-- array: [] -->
17+
* {{#isEmpty array}}AAA{{else}}BBB{{/isEmpty}}
18+
* <!-- results in: 'AAA' -->
19+
*
20+
* <!-- array: [] -->
21+
* {{isEmpty array}}
22+
* <!-- results in: true -->
23+
* ```
1524
* @param {Object} `collection`
1625
* @param {Object} `options`
1726
* @return {String}
@@ -35,9 +44,10 @@ helpers.isEmpty = function(collection, options) {
3544
};
3645

3746
/**
38-
* Iterate over an array or object. If `collection` is an array,
39-
* `.forEach` is called, else if `collection` is an object, `.forOwn`
40-
* is called, otherwise the inverse block is returned.
47+
* Block helper that iterates over an array or object. If
48+
* an array is given, `.forEach` is called, or if an object
49+
* is given, `.forOwn` is called, otherwise the inverse block
50+
* is returned.
4151
*
4252
* @param {Object|Array} `collection` The collection to iterate over
4353
* @param {Object} `options`
@@ -48,10 +58,10 @@ helpers.isEmpty = function(collection, options) {
4858

4959
helpers.iterate = function(collection, options) {
5060
if (Array.isArray(collection)) {
51-
return forEach.apply(forEach, arguments);
52-
} else if (util.isObject(collection)) {
53-
return forOwn.apply(forOwn, arguments);
61+
return forEach.apply(null, arguments);
62+
}
63+
if (util.isObject(collection)) {
64+
return forOwn.apply(null, arguments);
5465
}
5566
return options.inverse(this);
5667
};
57-

lib/comparison.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ var utils = require('./utils');
66
var helpers = module.exports;
77

88
/**
9-
* Block helper that renders the block if **both** of the given values
9+
* Helper that renders the block if **both** of the given values
1010
* are truthy. If an inverse block is specified it will be rendered
11-
* when falsy.
11+
* when falsy. Works as a block helper, inline helper or
12+
* subexpression.
1213
*
14+
* ```handlebars
15+
* <!-- {great: true, magnificent: true} -->
16+
* {{#and great magnificent}}A{{else}}B{{/and}}
17+
* <!-- results in: 'A' -->
18+
* ```
1319
* @param {any} `a`
1420
* @param {any} `b`
1521
* @param {Object} `options` Handlebars provided options object

test/collection.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ helpers.string({handlebars: hbs});
1111
var context = {array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']};
1212

1313
describe('collection', function() {
14-
describe('isEmpty', function() {
14+
describe('isEmpty block helper', function() {
1515
it('should render the first block when an array is empty', function() {
1616
var fn = hbs.compile('{{#isEmpty array}}AAA{{else}}BBB{{/isEmpty}}');
1717
assert.equal(fn({array: []}), 'AAA');
@@ -38,6 +38,33 @@ describe('collection', function() {
3838
});
3939
});
4040

41+
describe('isEmpty inline helper', function() {
42+
it('should render the first block when an array is empty', function() {
43+
var fn = hbs.compile('{{isEmpty array}}');
44+
assert.equal(fn({array: []}), 'true');
45+
});
46+
47+
it('should render the first block when the value is null', function() {
48+
var fn = hbs.compile('{{isEmpty}}');
49+
assert.equal(fn({array: []}), 'true');
50+
});
51+
52+
it('should render the second block when an array is not empty', function() {
53+
var fn = hbs.compile('{{isEmpty array}}');
54+
assert.equal(fn(context), 'false');
55+
});
56+
57+
it('should render the second block when an object is not empty', function() {
58+
var fn = hbs.compile('{{isEmpty object}}');
59+
assert.equal(fn({object: {foo: 'bar'}}), 'false');
60+
});
61+
62+
it('should render the first block when an object is empty', function() {
63+
var fn = hbs.compile('{{isEmpty object}}');
64+
assert.equal(fn({object: {}}), 'true');
65+
});
66+
});
67+
4168
describe('iterate', function() {
4269
describe('object', function() {
4370
it('should iterate over a plain object:', function() {

test/comparison.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('comparison', function() {
2020
});
2121
});
2222

23-
describe('inline', function() {
23+
describe('inline or subexpression', function() {
2424
it('should render a block if both values are truthy.', function() {
2525
var fn = hbs.compile('{{and great magnificent}}');
2626
assert.equal(fn({great: true, magnificent: true}), 'true');

test/integration/templates.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
var os = require('os');
44
var path = require('path');
55
var assert = require('assert');
6-
var engine = require('engine-handlebars');
7-
var hbs = require('handlebars').create();
86
var gm = require('global-modules');
7+
var engine = require('engine-handlebars');
98
var templates = require('templates');
109
var helpers = require('../..');
1110
var compile;

0 commit comments

Comments
 (0)