Skip to content

Commit

Permalink
lint, fix code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Nov 17, 2017
1 parent 3ba7036 commit fe7e666
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 25 deletions.
27 changes: 20 additions & 7 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ helpers.after = function(array, n) {
*
* ```handlebars
* {{arrayify "foo"}}
* <!-- results in: '["foo"]' -->
* <!-- results in: [ "foo" ] -->
* ```
* @param {any} `value`
* @return {Array}
Expand Down Expand Up @@ -458,10 +458,10 @@ helpers.pluck = function(arr, prop) {
* Reverse the elements in an array, or the characters in a string.
*
* ```handlebars
* <!-- value = 'abcd' -->
* <!-- value: 'abcd' -->
* {{reverse value}}
* <!-- results in: 'dcba' -->
* <!-- value = ['a', 'b', 'c', 'd'] -->
* <!-- value: ['a', 'b', 'c', 'd'] -->
* {{reverse value}}
* <!-- results in: ['d', 'c', 'b', 'a'] -->
* ```
Expand Down Expand Up @@ -798,12 +798,25 @@ helpers.withSort = function(array, prop, options) {
return result;
};

/**
* Block helper that return an array with all duplicate
* values removed. Best used along with a [each](#each) helper.
*
* ```handlebars
* <!-- array: ['a', 'a', 'c', 'b', 'e', 'e'] -->
* {{#each (unique array)}}{{.}}{{/each}}
* <!-- results in: 'acbe' -->
* ```
* @param {Array} `array`
* @param {Object} `options`
* @return {Array}
* @api public
*/

helpers.unique = function(array, options) {
if (util.isUndefined(array)) return '';

return array.filter(function(item, index, self) {
return self.indexOf(item) === index;
return array.filter(function(item, index, arr) {
return arr.indexOf(item) === index;
});

};

3 changes: 1 addition & 2 deletions lib/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ var helpers = module.exports;
*
* ```handlebars
* {{embed 'path/to/file.js'}}
*
* <!-- specify the language to use -->
* <!-- optionally specify the language to use -->
* {{embed 'path/to/file.hbs' 'html')}}
* ```
*
Expand Down
30 changes: 20 additions & 10 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ var forOwn = object.forOwn;
var helpers = module.exports;

/**
* Block helper that returns a block if the given collection is
* empty. If the collection is not empty the inverse block is returned
* (if supplied).
* Inline, subexpression, or block helper that returns true (or the block)
* if the given collection is empty, or false (or the inverse block, if
* supplied) if the colleciton is not empty.
*
* ```handlebars
* <!-- array: [] -->
* {{#isEmpty array}}AAA{{else}}BBB{{/isEmpty}}
* <!-- results in: 'AAA' -->
*
* <!-- array: [] -->
* {{isEmpty array}}
* <!-- results in: true -->
* ```
* @param {Object} `collection`
* @param {Object} `options`
* @return {String}
Expand All @@ -35,9 +44,10 @@ helpers.isEmpty = function(collection, options) {
};

/**
* Iterate over an array or object. If `collection` is an array,
* `.forEach` is called, else if `collection` is an object, `.forOwn`
* is called, otherwise the inverse block is returned.
* Block helper that iterates over an array or object. If
* an array is given, `.forEach` is called, or if an object
* is given, `.forOwn` is called, otherwise the inverse block
* is returned.
*
* @param {Object|Array} `collection` The collection to iterate over
* @param {Object} `options`
Expand All @@ -48,10 +58,10 @@ helpers.isEmpty = function(collection, options) {

helpers.iterate = function(collection, options) {
if (Array.isArray(collection)) {
return forEach.apply(forEach, arguments);
} else if (util.isObject(collection)) {
return forOwn.apply(forOwn, arguments);
return forEach.apply(null, arguments);
}
if (util.isObject(collection)) {
return forOwn.apply(null, arguments);
}
return options.inverse(this);
};

10 changes: 8 additions & 2 deletions lib/comparison.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ var utils = require('./utils');
var helpers = module.exports;

/**
* Block helper that renders the block if **both** of the given values
* Helper that renders the block if **both** of the given values
* are truthy. If an inverse block is specified it will be rendered
* when falsy.
* when falsy. Works as a block helper, inline helper or
* subexpression.
*
* ```handlebars
* <!-- {great: true, magnificent: true} -->
* {{#and great magnificent}}A{{else}}B{{/and}}
* <!-- results in: 'A' -->
* ```
* @param {any} `a`
* @param {any} `b`
* @param {Object} `options` Handlebars provided options object
Expand Down
29 changes: 28 additions & 1 deletion test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ helpers.string({handlebars: hbs});
var context = {array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']};

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

describe('isEmpty inline helper', function() {
it('should render the first block when an array is empty', function() {
var fn = hbs.compile('{{isEmpty array}}');
assert.equal(fn({array: []}), 'true');
});

it('should render the first block when the value is null', function() {
var fn = hbs.compile('{{isEmpty}}');
assert.equal(fn({array: []}), 'true');
});

it('should render the second block when an array is not empty', function() {
var fn = hbs.compile('{{isEmpty array}}');
assert.equal(fn(context), 'false');
});

it('should render the second block when an object is not empty', function() {
var fn = hbs.compile('{{isEmpty object}}');
assert.equal(fn({object: {foo: 'bar'}}), 'false');
});

it('should render the first block when an object is empty', function() {
var fn = hbs.compile('{{isEmpty object}}');
assert.equal(fn({object: {}}), 'true');
});
});

describe('iterate', function() {
describe('object', function() {
it('should iterate over a plain object:', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/comparison.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('comparison', function() {
});
});

describe('inline', function() {
describe('inline or subexpression', function() {
it('should render a block if both values are truthy.', function() {
var fn = hbs.compile('{{and great magnificent}}');
assert.equal(fn({great: true, magnificent: true}), 'true');
Expand Down
3 changes: 1 addition & 2 deletions test/integration/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
var os = require('os');
var path = require('path');
var assert = require('assert');
var engine = require('engine-handlebars');
var hbs = require('handlebars').create();
var gm = require('global-modules');
var engine = require('engine-handlebars');
var templates = require('templates');
var helpers = require('../..');
var compile;
Expand Down

0 comments on commit fe7e666

Please sign in to comment.