From ec2187fdd30a71ada7ea7faf857cb5f96122b4d1 Mon Sep 17 00:00:00 2001 From: jonschlinkert Date: Mon, 3 Jul 2017 23:05:54 -0400 Subject: [PATCH] use handlebars-utils, lint --- CHANGELOG | 4 + lib/array.js | 262 +++++++++++++++++++++----------------------- lib/collection.js | 9 +- lib/comparison.js | 63 ++++++----- lib/html.js | 11 +- lib/i18n.js | 3 +- lib/inflection.js | 5 +- lib/object.js | 13 ++- lib/string.js | 197 +++++++++++++-------------------- lib/utils/html.js | 3 +- lib/utils/index.js | 207 +++++------------------------------ lib/utils/utils.js | 1 - package.json | 67 +++++++----- test/array.js | 266 +++++++++++++++++++++------------------------ test/code.js | 18 +-- test/helpers.js | 1 - test/html.js | 70 ++++++------ test/string.js | 10 +- 18 files changed, 501 insertions(+), 709 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 48a5c58c..3db30878 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +v0.9.0: + date: "2017-07-03" + changes: + - remove `fileSize` helper in favor of new `bytes` helper, which does the same thing with minor differences. v0.8.4: date: "2017-07-03" changes: diff --git a/lib/array.js b/lib/array.js index c8e8e9d6..56224050 100644 --- a/lib/array.js +++ b/lib/array.js @@ -1,6 +1,7 @@ 'use strict'; var utils = require('./utils'); +var util = require('handlebars-utils'); /** * Expose `helpers` @@ -12,9 +13,8 @@ var helpers = module.exports; * Returns all of the items in an array after the specified index. * Opposite of [before](#before). * - * Given the array `['a', 'b', 'c']`: - * * ```handlebars + * * {{after array 1}} * //=> '["c"]' * ``` @@ -26,7 +26,7 @@ var helpers = module.exports; */ helpers.after = function(array, n) { - if (utils.isUndefined(array)) return ''; + if (util.isUndefined(array)) return ''; return array.slice(n); }; @@ -50,9 +50,8 @@ helpers.arrayify = function(value) { * Return all of the items in the collection before the specified * count. Opposite of [after](#after). * - * Given the array `['a', 'b', 'c']`: - * * ```handlebars + * * {{before array 2}} * //=> '["a", "b"]' * ``` @@ -64,13 +63,14 @@ helpers.arrayify = function(value) { */ helpers.before = function(array, n) { - if (utils.isUndefined(array)) return ''; + if (util.isUndefined(array)) return ''; return array.slice(0, -n); }; /** * ```handlebars - * {{#eachIndex collection}} + * + * {{#eachIndex array}} * {{item}} is {{index}} * {{/eachIndex}} * ``` @@ -93,9 +93,8 @@ helpers.eachIndex = function(array, options) { * Block helper that filters the given array and renders the block for values that * evaluate to `true`, otherwise the inverse block is returned. * - * Given the array `['a', 'b', 'c']`: - * * ```handlebars + * * {{#filter array "foo"}}AAA{{else}}BBB{{/filter}} * //=> 'BBB' * ``` @@ -139,9 +138,8 @@ helpers.filter = function(array, value, options) { /** * Returns the first item, or first `n` items of an array. * - * Given the array `['a', 'b', 'c', 'd', 'e']`: - * * ```handlebars + * * {{first array 2}} * //=> '["a", "b"]' * ``` @@ -153,7 +151,7 @@ helpers.filter = function(array, value, options) { */ helpers.first = function(array, n) { - if (utils.isUndefined(array)) return ''; + if (util.isUndefined(array)) return ''; if (!utils.isNumber(n)) { return array[0]; } @@ -174,19 +172,18 @@ helpers.first = function(array, n) { * Also, `@index` is exposed as a private variable, and additional * private variables may be defined as hash arguments. * - * ```js - * var accounts = [ + * ```handlebars + * + * + * {{#forEach accounts}} + * + * {{ name }} + * {{#unless isLast}}, {{/unless}} + * {{/forEach}} * ``` * @source * @param {Array} `array` @@ -218,9 +215,8 @@ helpers.forEach = function(array, options) { * given `value`. Optionally specify an inverse block to render * when the array does not have the given value. * - * Given the array `['a', 'b', 'c']`: - * * ```handlebars + * * {{#inArray array "d"}} * foo * {{else}} @@ -239,7 +235,7 @@ helpers.forEach = function(array, options) { */ helpers.inArray = function(array, value, options) { - if (utils.indexOf(array, value) > -1) { + if (Array.isArray(array) && util.indexOf(array, value) > -1) { return options.fn(this); } return options.inverse(this); @@ -250,7 +246,11 @@ helpers.inArray = function(array, value, options) { * * ```handlebars * {{isArray "abc"}} - * //=> 'false' + * //=> false + * + * + * {{isArray array}} + * //=> true * ``` * * @param {any} `value` The value to test. @@ -265,9 +265,8 @@ helpers.isArray = function(value) { /** * Block helper that returns the item with specified index. * - * Given the array `['a', 'b', 'c']`: - * * ```handlebars + * * {{itemAt array 1}} * //=> 'b' * ``` @@ -281,8 +280,7 @@ helpers.isArray = function(value) { */ helpers.itemAt = function(array, idx) { - if (utils.isUndefined(array)) return; - array = utils.result(array); + array = util.result(array); if (Array.isArray(array)) { idx = utils.isNumber(idx) ? +idx : 0; if (idx < 0) { @@ -298,9 +296,8 @@ helpers.itemAt = function(array, idx) { * Join all elements of array into a string, optionally using a * given separator. * - * Given the array `['a', 'b', 'c']`: - * * ```handlebars + * * {{join array}} * //=> 'a, b, c' * @@ -315,32 +312,35 @@ helpers.itemAt = function(array, idx) { */ helpers.join = function(array, sep) { - if (utils.isUndefined(array)) return ''; - sep = utils.isString(sep) ? sep : ', '; + if (typeof array === 'string') return array; + if (!Array.isArray(array)) return ''; + sep = util.isString(sep) ? sep : ', '; return array.join(sep); }; /** - * Returns the last item, or last `n` items of an array. + * Returns the last item, or last `n` items of an array or string. * Opposite of [first](#first). * - * Given the array `['a', 'b', 'c', 'd', 'e']`: - * * ```handlebars + * * {{last array 2}} * //=> '["d", "e"]' * ``` - * @param {Array} `array` - * @param {Number} `n` Number of items to return, starting with the last item. + * @param {Array|String} `val` Array or string. + * @param {Number} `n` Number of items to return from the end of the array. * @return {Array} * @api public */ -helpers.last = function(array, n) { +helpers.last = function(val, n) { + if (!Array.isArray(val) && typeof val !== 'string') { + return ''; + } if (!utils.isNumber(n)) { - return array[array.length - 1]; + return val[val.length - 1]; } - return array.slice(-n); + return val.slice(-n); }; /** @@ -349,9 +349,8 @@ helpers.last = function(array, n) { * is equal to the given `length`, the block is returned, * otherwise an inverse block may optionally be returned. * - * Given the array `['a', 'b', 'c', 'd', 'e']`: - * * ```handlebars + * * {{#lengthEqual array 10}}AAA{{else}}BBB{{/lengthEqual}} * //=> 'BBB' * ``` @@ -366,25 +365,20 @@ helpers.last = function(array, n) { */ helpers.lengthEqual = function(array, length, options) { - if (array.length === length) { + if (Array.isArray(array) && array.length === length) { return options.fn(this); } return options.inverse(this); }; /** - * Returns a new array, created by calling `function` - * on each element of the given `array`. - * - * Given an array `['a', 'b', 'c']`: - * - * ```js - * // register `double` as a helper - * function double(str) { - * return str + str; - * } - * // then used like this: - * // {{map array double}} + * Returns a new array, created by calling `function` on each + * element of the given `array`. For example, + * + * ```handlebars + * + * {{map array double}} * //=> '["aa", "bb", "cc"]' * ``` * @@ -394,17 +388,15 @@ helpers.lengthEqual = function(array, length, options) { * @api public */ -helpers.map = function(array, fn) { - if (utils.isUndefined(array)) return ''; - if (typeof array === 'string' && /[[]/.test(array)) { - array = utils.tryParse(array) || []; - } +helpers.map = function(array, iter) { + if (!Array.isArray(array)) return ''; var len = array.length; var res = new Array(len); var i = -1; + iter = utils.iterator(iter, this); while (++i < len) { - res[i] = fn(array[i], i, array); + res[i] = iter(array[i], i, array); } return res; }; @@ -413,9 +405,8 @@ helpers.map = function(array, fn) { * Block helper that returns the block if the callback returns true * for some value in the given array. * - * Given the array `[1, 'b', 3]`: - * * ```handlebars + * * {{#some array isString}} * Render me if the array has a string. * {{else}} @@ -425,22 +416,20 @@ helpers.map = function(array, fn) { * ``` * @name .some * @param {Array} `array` - * @param {Function} `cb` callback function + * @param {Function} `iter` Iteratee * @param {Options} Handlebars provided options object - * @return {Array} + * @return {String} * @block * @api public */ -helpers.some = function(arr, cb, options) { - cb = utils.iterator(cb, this); - if (arr == null) { - return options.inverse(this); - } - var len = arr.length, i = -1; - while (++i < len) { - if (cb(arr[i], i, arr)) { - return options.fn(this); +helpers.some = function(array, iter, options) { + if (Array.isArray(array)) { + iter = utils.iterator(iter, this); + for (var i = 0; i < array.length; i++) { + if (iter(array[i], i, array)) { + return options.fn(this); + } } } return options.inverse(this); @@ -452,9 +441,8 @@ helpers.some = function(arr, cb, options) { * argument. You may alternatively pass a sorting function as * the second argument. * - * Given an array `['b', 'a', 'c']`: - * * ```handlebars + * * {{sort array}} * //=> '["a", "b", "c"]' * ``` @@ -464,12 +452,12 @@ helpers.some = function(arr, cb, options) { * @api public */ -helpers.sort = function(arr, options) { - if (utils.isUndefined(arr)) return ''; +helpers.sort = function(array, options) { + if (!Array.isArray(array)) return ''; if (utils.get(options, 'hash.reverse')) { - return arr.sort().reverse(); + return array.sort().reverse(); } - return arr.sort(); + return array.sort(); }; /** @@ -478,9 +466,8 @@ helpers.sort = function(arr, options) { * argument. You may alternatively pass a sorting function as * the second argument. * - * Given an array `[{a: 'zzz'}, {a: 'aaa'}]`: - * * ```handlebars + * * {{sortBy array "a"}} * //=> '[{"a":"aaa"}, {"a":"zzz"}]' * ``` @@ -490,16 +477,14 @@ helpers.sort = function(arr, options) { * @api public */ -helpers.sortBy = function(arr/*, prop*/) { - if (utils.isUndefined(arr)) return ''; +helpers.sortBy = function(array, prop, options) { + if (!Array.isArray(array)) return ''; var args = [].slice.call(arguments); - args.pop(); // remove hbs options object + // remove handlebars options + args.pop(); - if (typeof args[0] === 'string' && /[[]/.test(args[0])) { - args[0] = utils.tryParse(args[0]) || []; - } - if (utils.isUndefined(args[1])) { - return args[0].sort(); + if (!util.isString(prop) && typeof prop !== 'function') { + return array.sort(); } return utils.sortBy.apply(null, args); }; @@ -508,9 +493,8 @@ helpers.sortBy = function(arr/*, prop*/) { * Use the items in the array _after_ the specified index * as context inside a block. Opposite of [withBefore](#withBefore). * - * Given the array `['a', 'b', 'c', 'd', 'e']`: - * * ```handlebars + * * {{#withAfter array 3}} * {{this}} * {{/withAfter}} @@ -525,11 +509,11 @@ helpers.sortBy = function(arr/*, prop*/) { */ helpers.withAfter = function(array, idx, options) { + if (!Array.isArray(array)) return ''; array = array.slice(idx); var result = ''; - var len = array.length, i = -1; - while (++i < len) { + for (var i = 0; i < array.length; i++) { result += options.fn(array[i]); } return result; @@ -539,9 +523,8 @@ helpers.withAfter = function(array, idx, options) { * Use the items in the array _before_ the specified index * as context inside a block. Opposite of [withAfter](#withAfter). * - * Given the array `['a', 'b', 'c', 'd', 'e']`: - * * ```handlebars + * * {{#withBefore array 3}} * {{this}} * {{/withBefore}} @@ -556,11 +539,11 @@ helpers.withAfter = function(array, idx, options) { */ helpers.withBefore = function(array, idx, options) { + if (!Array.isArray(array)) return ''; array = array.slice(0, -idx); var result = ''; - var len = array.length, i = -1; - while (++i < len) { + for (var i = 0; i < array.length; i++) { result += options.fn(array[i]); } return result; @@ -570,9 +553,8 @@ helpers.withBefore = function(array, idx, options) { * Use the first item in a collection inside a handlebars * block expression. Opposite of [withLast](#withLast). * - * Given the array `['a', 'b', 'c']`: - * * ```handlebars + * * {{#withFirst array}} * {{this}} * {{/withFirst}} @@ -586,79 +568,78 @@ helpers.withBefore = function(array, idx, options) { * @api public */ -helpers.withFirst = function(arr, idx, options) { - if (utils.isUndefined(arr)) return ''; - arr = utils.result(arr); +helpers.withFirst = function(array, idx, options) { + if (util.isUndefined(array)) return ''; + array = util.result(array); - if (!utils.isUndefined(idx)) { - idx = parseFloat(utils.result(idx)); + if (!util.isUndefined(idx)) { + idx = parseFloat(util.result(idx)); } - if (utils.isUndefined(idx)) { + if (util.isUndefined(idx)) { options = idx; - return options.fn(arr[0]); + return options.fn(array[0]); } - arr = arr.slice(0, idx); - var len = arr.length, i = -1; + array = array.slice(0, idx); var result = ''; - while (++i < len) { - result += options.fn(arr[i]); + for (var i = 0; i < array.length; i++) { + result += options.fn(array[i]); } return result; }; /** - * Block helper that groups array elements by given `value`. - * - * Given the array `['a', 'b', 'c', 'd','e','f','g','h']`: + * Block helper that groups array elements by given group `size`. * * ```handlebars + * * {{#withGroup array 4}} * {{#each this}} * {{.}} * {{each}} *
* {{/withGroup}} + * + * + * * ``` - * //=> 'a','b','c','d'
- * //=> 'e','f','g','h'
* * @name .withGroup - * @param {Array} `array` - * @param {Number} `every` - * @param {Object} `options` + * @param {Array} `array` The array to iterate over + * @param {Number} `size` The desired length of each array "group" + * @param {Object} `options` Handlebars options * @return {String} * @block * @api public */ -helpers.withGroup = function(array, every, options) { - var out = '', subcontext = [], i; - if (array && array.length > 0) { - for (i = 0; i < array.length; i++) { - if (i > 0 && i % every === 0) { - out += options.fn(subcontext); +helpers.withGroup = function(array, size, options) { + var result = ''; + if (Array.isArray(array) && array.length > 0) { + var subcontext = []; + for (var i = 0; i < array.length; i++) { + if (i > 0 && (i % size) === 0) { + result += options.fn(subcontext); subcontext = []; } subcontext.push(array[i]); } - out += options.fn(subcontext); + result += options.fn(subcontext); } - return out; + return result; }; /** * Use the last item or `n` items in an array as context inside a block. * Opposite of [withFirst](#withFirst). * - * Given the array `['a', 'b', 'c']`: - * * ```handlebars + * * {{#withLast array}} * {{this}} * {{/withLast}} - * //=> 'c' + * * ``` * @param {Array} `array` * @param {Number} `idx` The starting index. @@ -669,14 +650,14 @@ helpers.withGroup = function(array, every, options) { */ helpers.withLast = function(array, idx, options) { - if (utils.isUndefined(array)) return ''; - array = utils.result(array); + if (util.isUndefined(array)) return ''; + array = util.result(array); - if (!utils.isUndefined(idx)) { - idx = parseFloat(utils.result(idx)); + if (!util.isUndefined(idx)) { + idx = parseFloat(util.result(idx)); } - if (utils.isUndefined(idx)) { + if (util.isUndefined(idx)) { options = idx; return options.fn(array[array.length - 1]); } @@ -694,9 +675,8 @@ helpers.withLast = function(array, idx, options) { * Block helper that sorts a collection and exposes the sorted * collection as context inside the block. * - * Given the array `['b', 'a', 'c']`: - * * ```handlebars + * * {{#withSort array}}{{this}}{{/withSort}} * //=> 'abc' * ``` @@ -710,10 +690,10 @@ helpers.withLast = function(array, idx, options) { */ helpers.withSort = function(array, prop, options) { - if (utils.isUndefined(array)) return ''; + if (util.isUndefined(array)) return ''; var result = ''; - if (utils.isUndefined(prop)) { + if (util.isUndefined(prop)) { options = prop; array = array.sort(); diff --git a/lib/collection.js b/lib/collection.js index 3f3308ce..da6c23ab 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -1,5 +1,6 @@ 'use strict'; +var util = require('handlebars-utils'); var array = require('./array'); var object = require('./object'); var utils = require('./utils'); @@ -56,7 +57,7 @@ helpers.isEmpty = function(collection, options) { helpers.iterate = function(context, options) { if (Array.isArray(context)) { return forEach.apply(forEach, arguments); - } else if (utils.isObject(context)) { + } else if (util.isObject(context)) { return forOwn.apply(forOwn, arguments); } return options.inverse(this); @@ -85,11 +86,11 @@ helpers.iterate = function(context, options) { */ helpers.length = function(value) { - if (utils.isUndefined(value)) return ''; + if (util.isUndefined(value)) return ''; if (typeof value === 'string' && /[[]/.test(value)) { - value = utils.tryParse(value) || []; + value = util.tryParse(value) || []; } - if (utils.isObject(value)) { + if (util.isObject(value)) { value = Object.keys(value); } return value.length; diff --git a/lib/comparison.js b/lib/comparison.js index 9b8726fa..657e88d6 100644 --- a/lib/comparison.js +++ b/lib/comparison.js @@ -4,6 +4,14 @@ var isObject = require('./object').isObject; var isString = require('./string').isString; var utils = require('./utils'); +/** + * TODO + * - contains + * - equals + * - strictEquals + */ + + /** * Expose `helpers` */ @@ -99,9 +107,8 @@ helpers.compare = function(a, operator, b, options) { * `startIndex` is specified and is negative, it is used as the * offset from the end of the collection. * - * Given the array `['a', 'b', 'c']`: - * * ```handlebars + * * {{#contains array "d"}} * This will not be rendered. * {{else}} @@ -127,6 +134,32 @@ helpers.contains = function(collection, value, startIndex, options) { return options.inverse(this); }; +/** + * Block helper that renders a block if `a` is **equal to** `b`. + * If an inverse block is specified it will be rendered when falsy. + * You may optionally use the `compare=""` hash argument for the + * second value. + * + * @name .eq + * @param {String} `a` + * @param {String} `b` + * @param {Object} `options` Handlebars provided options object + * @return {String} Block, or inverse block if specified and falsey. + * @block + * @api public + */ + +helpers.eq = function(a, b, options) { + if (arguments.length === 2) { + options = b; + b = options.hash.compare; + } + if (a === b) { + return options.fn(this); + } + return options.inverse(this); +}; + /** * Block helper that renders a block if `a` is **greater than** `b`. * @@ -214,32 +247,6 @@ helpers.has = function(value, pattern, options) { return options.inverse(this); }; -/** - * Block helper that renders a block if `a` is **equal to** `b`. - * If an inverse block is specified it will be rendered when falsy. - * You may optionally use the `compare=""` hash argument for the - * second value. - * - * @name .eq - * @param {String} `a` - * @param {String} `b` - * @param {Object} `options` Handlebars provided options object - * @return {String} Block, or inverse block if specified and falsey. - * @block - * @api public - */ - -helpers.eq = function(a, b, options) { - if (arguments.length === 2) { - options = b; - b = options.hash.compare; - } - if (a === b) { - return options.fn(this); - } - return options.inverse(this); -}; - /** * Return true if the given value is an even number. * diff --git a/lib/html.js b/lib/html.js index 70d50f46..3d6a1f9e 100644 --- a/lib/html.js +++ b/lib/html.js @@ -1,6 +1,7 @@ 'use strict'; var path = require('path'); +var util = require('handlebars-utils'); var html = require('./utils/html'); var utils = require('./utils'); var parseAttr = html.parseAttributes; @@ -26,7 +27,7 @@ helpers.css = function(array, options) { array = []; } - var styles = utils.arrayify(array); + var styles = util.arrayify(array); var assets = ''; if (this && this.options) { @@ -34,7 +35,7 @@ helpers.css = function(array, options) { } if (options.hash.href) { - styles = utils.arrayify(options.hash.href); + styles = util.arrayify(options.hash.href); } return styles.map(function(item) { @@ -64,7 +65,7 @@ helpers.css = function(array, options) { */ helpers.ellipsis = function(str, limit) { - if (utils.isString(str)) { + if (util.isString(str)) { if (!str) { return str; } @@ -98,7 +99,7 @@ helpers.js = function(context) { return ''; } - context = utils.arrayify(context); + context = util.arrayify(context); return context.map(function(fp) { return (path.extname(fp) === '.coffee') ? utils.tag('script', {type: 'text/coffeescript', src: fp}) @@ -143,7 +144,7 @@ helpers.sanitize = function(str) { */ helpers.truncate = function(str, limit, suffix) { - if (utils.isString(str)) { + if (util.isString(str)) { if (!str) { return str; } diff --git a/lib/i18n.js b/lib/i18n.js index ffa59e35..3edb9f6b 100644 --- a/lib/i18n.js +++ b/lib/i18n.js @@ -1,5 +1,6 @@ 'use strict'; +var util = require('handlebars-utils'); var utils = require('./utils'); /** @@ -20,7 +21,7 @@ var helpers = module.exports; */ helpers.i18n = function(prop, context, options) { - if (utils.isOptions(context)) { + if (util.isOptions(context)) { options = context; context = {}; } diff --git a/lib/inflection.js b/lib/inflection.js index 68317c82..1bf22803 100644 --- a/lib/inflection.js +++ b/lib/inflection.js @@ -1,5 +1,6 @@ 'use strict'; +var util = require('handlebars-utils'); var utils = require('./utils'); /** @@ -21,7 +22,7 @@ var helpers = module.exports; helpers.inflect = function(count, singular, plural, include) { var word = (count > 1 || count === 0) ? plural : singular; - if (utils.isUndefined(include) || include === false) { + if (util.isUndefined(include) || include === false) { return word; } else { return String(count) + ' ' + word; @@ -51,7 +52,7 @@ helpers.ordinalize = function(val) { var num = Math.abs(Math.round(val)); var res = num % 100; - if (utils.indexOf([11, 12, 13], res) >= 0) { + if (util.indexOf([11, 12, 13], res) >= 0) { return '' + val + 'th'; } diff --git a/lib/object.js b/lib/object.js index 4db5fa39..09ca0613 100644 --- a/lib/object.js +++ b/lib/object.js @@ -1,6 +1,7 @@ 'use strict'; var hasOwn = Object.hasOwnProperty; +var util = require('handlebars-utils'); var array = require('./array'); var utils = require('./utils/'); @@ -23,7 +24,7 @@ helpers.extend = function(/*objects*/) { var args = [].slice.call(arguments); var last = args[args.length - 1]; - if (last && utils.isObject(last) && last.hash) { + if (last && util.isObject(last) && last.hash) { last = last.hash; args.pop(); // remove handlebars options object args.push(last); @@ -35,7 +36,7 @@ helpers.extend = function(/*objects*/) { while (++i < len) { var obj = args[i]; - if (utils.isObject(obj)) { + if (util.isObject(obj)) { for (var key in obj) { if (obj.hasOwnProperty(key)) { context[key] = obj[key]; @@ -59,7 +60,7 @@ helpers.extend = function(/*objects*/) { */ helpers.forIn = function(obj, options) { - if (!utils.isOptions(options)) { + if (!util.isOptions(options)) { return obj.inverse(this); } @@ -86,7 +87,7 @@ helpers.forIn = function(obj, options) { */ helpers.forOwn = function(obj, options) { - if (!utils.isOptions(options)) { + if (!util.isOptions(options)) { return obj.inverse(this); } @@ -110,10 +111,10 @@ helpers.forOwn = function(obj, options) { * @return {String} * @api public */ -helpers.toPath = function (/*prop*/) { +helpers.toPath = function(/*prop*/) { var prop = []; for (var i = 0; i < arguments.length; i++) { - if (typeof arguments[i] === "string" || typeof arguments[i] === "number") { + if (typeof arguments[i] === 'string' || typeof arguments[i] === 'number') { prop.push(arguments[i]); } } diff --git a/lib/string.js b/lib/string.js index d60c5ba9..484e524a 100644 --- a/lib/string.js +++ b/lib/string.js @@ -1,5 +1,6 @@ 'use strict'; +var util = require('handlebars-utils'); var utils = require('./utils'); /** @@ -23,6 +24,7 @@ var helpers = module.exports; */ helpers.camelcase = function(str) { + if (!util.isString(str)) return ''; return utils.changecase(str, function(ch) { return ch.toUpperCase(); }); @@ -41,13 +43,8 @@ helpers.camelcase = function(str) { */ helpers.capitalize = function(str) { - if (utils.isString(str)) { - if (!str) { - return str; - } - return str.charAt(0).toUpperCase() - + str.slice(1); - } + if (!util.isString(str)) return ''; + return str.charAt(0).toUpperCase() + str.slice(1); }; /** @@ -63,14 +60,10 @@ helpers.capitalize = function(str) { */ helpers.capitalizeAll = function(str) { - if (utils.isString(str)) { - if (!str) { - return str; - } - return str.replace(/\w\S*/g, function(word) { - return exports.capitalize(word); - }); - } + if (!util.isString(str)) return ''; + return str.replace(/\w\S*/g, function(word) { + return exports.capitalize(word); + }); }; /** @@ -83,18 +76,14 @@ helpers.capitalizeAll = function(str) { */ helpers.center = function(str, spaces) { - if (utils.isString(str)) { - if (!str) { - return str; - } - var space = ''; - var i = 0; - while (i < spaces) { - space += ' '; - i++; - } - return space + str + space; + if (!util.isString(str)) return ''; + var space = ''; + var i = 0; + while (i < spaces) { + space += ' '; + i++; } + return space + str + space; }; /** @@ -119,6 +108,7 @@ helpers.center = function(str, spaces) { */ helpers.chop = function(str) { + if (!util.isString(str)) return ''; return utils.chop(str); }; @@ -137,6 +127,7 @@ helpers.chop = function(str) { */ helpers.dashcase = function(str) { + if (!util.isString(str)) return ''; return utils.changecase(str, function(ch) { return '-' + ch; }); @@ -156,6 +147,7 @@ helpers.dashcase = function(str) { */ helpers.dotcase = function(str) { + if (!util.isString(str)) return ''; return utils.changecase(str, function(ch) { return '.' + ch; }); @@ -174,12 +166,8 @@ helpers.dotcase = function(str) { */ helpers.hyphenate = function(str) { - if (utils.isString(str)) { - if (!str) { - return str; - } - return str.split(' ').join('-'); - } + if (!util.isString(str)) return ''; + return str.split(' ').join('-'); }; /** @@ -195,7 +183,7 @@ helpers.hyphenate = function(str) { */ helpers.isString = function(value) { - return utils.isString(value); + return typeof value === 'string'; }; /** @@ -211,12 +199,11 @@ helpers.isString = function(value) { */ helpers.lowercase = function(str) { - if (utils.isString(str)) { - if (!str) { - return str; - } - return str.toLowerCase(); + if (util.isObject(str) && str.fn) { + return str.fn(this).toLowerCase(); } + if (!util.isString(str)) return ''; + return str.toLowerCase(); }; /** @@ -234,20 +221,16 @@ helpers.lowercase = function(str) { */ helpers.occurrences = function(str, substring) { - if (utils.isString(str)) { - if (!str) { - return str; - } - var len = substring.length; - var pos = 0; - var n = 0; - - while ((pos = str.indexOf(substring, pos)) > -1) { - n++; - pos += len; - } - return n; + if (!util.isString(str)) return ''; + var len = substring.length; + var pos = 0; + var n = 0; + + while ((pos = str.indexOf(substring, pos)) > -1) { + n++; + pos += len; } + return n; }; /** @@ -265,11 +248,11 @@ helpers.occurrences = function(str, substring) { */ helpers.pascalcase = function(str) { + if (!util.isString(str)) return ''; str = utils.changecase(str, function(ch) { return ch.toUpperCase(); }); - return str.charAt(0).toUpperCase() - + str.slice(1); + return str.charAt(0).toUpperCase() + str.slice(1); }; /** @@ -286,6 +269,7 @@ helpers.pascalcase = function(str) { */ helpers.pathcase = function(str) { + if (!util.isString(str)) return ''; return utils.changecase(str, function(ch) { return '/' + ch; }); @@ -304,13 +288,10 @@ helpers.pathcase = function(str) { * @api public */ -helpers.plusify = function(str) { - if (utils.isString(str)) { - if (!str) { - return str; - } - return str.split(' ').join('+'); - } +helpers.plusify = function(str, ch) { + if (!util.isString(str)) return ''; + if (!util.isString(ch)) ch = ' '; + return str.split(ch).join('+'); }; /** @@ -329,8 +310,8 @@ helpers.plusify = function(str) { helpers.raw = function(options) { var str = options.fn(); - - if (this.escape !== false) { + var opts = Object.assign({}, this.options, options.hash); + if (opts.escape !== false) { var idx = 0; while (((idx = str.indexOf('{{', idx)) !== -1)) { if (str[idx - 1] !== '\\') { @@ -356,12 +337,8 @@ helpers.raw = function(options) { */ helpers.reverse = function(str) { - if (utils.isString(str)) { - if (!str) { - return str; - } - return str.split('').reverse().join(''); - } + if (!util.isString(str)) return ''; + return str.split('').reverse().join(''); }; /** @@ -379,14 +356,10 @@ helpers.reverse = function(str) { */ helpers.replace = function(str, a, b) { - if (utils.isString(str)) { - if (!str) { - return str; - } - if (!a || typeof a !== 'string') return str; - if (!b || typeof b !== 'string') b = ''; - return str.split(a).join(b); - } + if (!util.isString(str)) return ''; + if (!util.isString(a)) return str; + if (!util.isString(b)) b = ''; + return str.split(a).join(b); }; /** @@ -402,16 +375,10 @@ helpers.replace = function(str, a, b) { */ helpers.sentence = function(str) { - if (utils.isString(str)) { - if (!str) { - return str; - } - var re = /((?:\S[^\.\?\!]*)[\.\?\!]*)/g; - return str.replace(re, function(txt) { - return txt.charAt(0).toUpperCase() - + txt.substr(1).toLowerCase(); - }); - } + if (!util.isString(str)) return ''; + return str.replace(/((?:\S[^\.\?\!]*)[\.\?\!]*)/g, function(txt) { + return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); + }); }; /** @@ -428,6 +395,7 @@ helpers.sentence = function(str) { */ helpers.snakecase = function(str) { + if (!util.isString(str)) return ''; return utils.changecase(str, function(ch) { return '_' + ch; }); @@ -447,8 +415,8 @@ helpers.snakecase = function(str) { */ helpers.split = function(str, ch) { - if (!helpers.isString(str)) return ''; - if (typeof ch !== 'string') ch = ','; + if (!util.isString(str)) return ''; + if (!util.isString(ch)) ch = ','; return str.split(ch); }; @@ -474,13 +442,8 @@ helpers.split = function(str, ch) { helpers.startsWith = function(prefix, str, options) { var args = [].slice.call(arguments); options = args.pop(); - if (utils.isString(str)) { - if (!str) { - return str; - } - if (str.indexOf(prefix) === 0) { - return options.fn(this); - } + if (util.isString(str) && str.indexOf(prefix) === 0) { + return options.fn(this); } if (typeof options.inverse === 'function') { return options.inverse(this); @@ -501,21 +464,17 @@ helpers.startsWith = function(prefix, str, options) { */ helpers.titleize = function(str) { - if (utils.isString(str)) { - if (!str) { - return str; - } - var title = str.replace(/[ \-_]+/g, ' '); - var words = title.match(/\w+/g); - var len = words.length; - var res = []; - var i = 0; - while (len--) { - var word = words[i++]; - res.push(exports.capitalize(word)); - } - return res.join(' '); + if (!util.isString(str)) return ''; + var title = str.replace(/[ \-_]+/g, ' '); + var words = title.match(/\w+/g); + var len = words.length; + var res = []; + var i = 0; + while (len--) { + var word = words[i++]; + res.push(exports.capitalize(word)); } + return res.join(' '); }; /** @@ -534,8 +493,7 @@ helpers.titleize = function(str) { */ helpers.trim = function(str) { - if (!helpers.isString(str)) return ''; - return str.trim(); + return typeof str === 'string' ? str.trim() : ''; }; /** @@ -552,17 +510,10 @@ helpers.trim = function(str) { * @api public */ -helpers.uppercase = function(str, options) { - if (utils.isString(str)) { - if (!str) { - return str; - } - return str.toUpperCase(); - } else { - options = str; - } - if (typeof options === 'object' && options.fn) { - return options.fn(this).toUpperCase(); +helpers.uppercase = function(str) { + if (util.isObject(str) && str.fn) { + return str.fn(this).toUpperCase(); } - return ''; + if (!util.isString(str)) return ''; + return str.toUpperCase(); }; diff --git a/lib/utils/html.js b/lib/utils/html.js index 9681ac09..4f920480 100644 --- a/lib/utils/html.js +++ b/lib/utils/html.js @@ -1,5 +1,6 @@ 'use strict'; +var util = require('handlebars-utils'); var striptags = require('striptags'); var typeOf = require('kind-of'); var utils = require('./'); @@ -63,7 +64,7 @@ html.parseAttributes = function parseAttributes(hash) { */ html.sanitize = function(str) { - if (!utils.isString(str)) return ''; + if (!util.isString(str)) return ''; return striptags(str).trim(); }; diff --git a/lib/utils/index.js b/lib/utils/index.js index e9bb4e49..437569f5 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -1,7 +1,28 @@ 'use strict'; +var util = require('handlebars-utils'); var utils = require('./utils'); +/** + * Returns true if the given value appears to be a + * regular expression. + * + * @param {Object} `value` + * @return {Boolean} + * @api public + */ + +utils.isRegex = function(val) { + if (utils.typeOf(val) === 'regexp') { + return true; + } + if (typeof val !== 'string') { + return false; + } + return val.charAt(0) === '/' + && val.slice(-1) === '\/'; +}; + /** * Returns true if the given value contains the given * `object`, optionally passing a starting index. @@ -13,32 +34,10 @@ var utils = require('./utils'); */ utils.contains = function(val, obj, start) { - var len = val ? val.length : 0; - var idx = start < 0 - ? Math.max(0, len + start) - : start; - - var res = false; - var i = 0; - - start = idx || 0; - - if (Array.isArray(val)) { - res = utils.indexOf(val, obj, start) > -1; - - } else if (utils.isNumber(len)) { - res = (typeof val === 'string' - ? val.indexOf(obj, start) - : utils.indexOf(val, obj, start)) > -1; - - } else { - utils.iterator(val, function(ele) { - if (start < i++) { - return !(res = (ele === obj)); - } - }); + if (val == null || obj == null || !utils.isNumber(val.length)) { + return false; } - return res; + return val.indexOf(obj, start) !== -1; }; /** @@ -57,26 +56,6 @@ utils.toRegex = function(val) { return new RegExp(val.replace(/^\/|\/$/g, '')); }; -/** - * Returns true if the given value appears to be a - * regular expression. - * - * @param {Object} `value` - * @return {Boolean} - * @api public - */ - -utils.isRegex = function(val) { - if (utils.typeOf(val) === 'regexp') { - return true; - } - if (typeof val !== 'string') { - return false; - } - return val.charAt(0) === '/' - && val.slice(-1) === '\/'; -}; - /** * Remove leading and trailing whitespace and non-word * characters from the given string. @@ -86,7 +65,7 @@ utils.isRegex = function(val) { */ utils.chop = function(str) { - if (!utils.isString(str)) return ''; + if (!util.isString(str)) return ''; var re = /^[-_.\W\s]+|[-_.\W\s]+$/g; return str.trim().replace(re, ''); }; @@ -109,7 +88,7 @@ utils.chop = function(str) { */ utils.changecase = function(str, fn) { - if (!utils.isString(str)) return ''; + if (!util.isString(str)) return ''; if (str.length === 1) { return str.toLowerCase(); } @@ -138,32 +117,6 @@ utils.random = function(min, max) { return min + Math.floor(Math.random() * (max - min + 1)); }; -/** - * Returns true if the given value is `undefined` or - * is a handlebars options hash. - * - * @param {any} `value` - * @return {Boolean} - * @api public - */ - -utils.isUndefined = function(val) { - return typeof val === 'undefined' - || (val.hash != null); -}; - -/** - * Returns true if the given value appears to be an **options** object. - * - * @param {Object} `value` - * @return {Boolean} - * @api public - */ - -utils.isOptions = function(val) { - return utils.isObject(val) && val.hasOwnProperty('hash'); -}; - /** * Get options from the options hash and `this`. * @@ -181,14 +134,14 @@ utils.getArgs = function(app, args) { var last = args[args.length - 1]; // merge `options.hash` into the options - if (utils.isOptions(last)) { + if (util.isOptions(last)) { var hbsOptions = args.pop(); opts = utils.get(opts, hbsOptions.name) || opts; opts = utils.merge({}, opts, hbsOptions.hash); // if the last arg is an object, merge it // into the options - } else if (utils.isObject(last)) { + } else if (util.isObject(last)) { opts = utils.merge({}, opts, args.pop()); } @@ -196,110 +149,6 @@ utils.getArgs = function(app, args) { return args; }; -/** - * Returns true if the given value is an object - * and not an array. - * - * @param {any} `value` - * @return {Boolean} - * @api public - */ - -utils.isObject = function(val) { - return val && typeof val === 'object' - && !Array.isArray(val); -}; - -/** - * Returns true if the given value is empty. - * - * @param {any} `value` - * @return {Boolean} - * @api public - */ - -utils.isEmpty = function(val) { - if (val === 0 || val === '0') { - return false; - } - if (!val || (Array.isArray(val) && val.length === 0)) { - return true; - } - if (typeof val === 'object' && !Object.keys(val).length) { - return true; - } - return false; -}; - -/** - * Try to parse the given `string` as JSON. Fails - * gracefully if the value cannot be parsed. - * - * @name .tryParse - * @param {String} `string` - * @return {Object} - * @api public - */ - -utils.tryParse = function(str) { - try { - return JSON.parse(str); - } catch (err) {} - return null; -}; - -/** - * Return the given value. If the value is a function - * it will be called, and the result is returned. - * - * @param {any} `val` - * @return {any} - * @api public - */ - -utils.result = function(value) { - if (typeof value === 'function') { - return value(); - } - return value; -}; - -/** - * Return the given value, unchanged. - * - * @param {any} `val` - * @return {any} - * @api public - */ - -utils.identity = function(val) { - return val; -}; - -/** - * Return true if `val` is a string. - * - * @param {any} `val` The value to check - * @return {Boolean} - * @api public - */ - -utils.isString = function(val) { - return typeof val === 'string'; -}; - -/** - * Cast `val` to an array. - * - * @param {any} `val` The value to arrayify. - * @return {Array} - * @api public - */ - -utils.arrayify = function(val) { - return val ? (Array.isArray(val) ? val : [val]) : []; -}; - /** * Expose `utils` */ diff --git a/lib/utils/utils.js b/lib/utils/utils.js index f8bc1089..d2b60214 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -8,7 +8,6 @@ require = utils; require('array-sort', 'sortBy'); require('arr-filter', 'filter'); require('arr-flatten', 'flatten'); -require('index-of'); // Collection utils require('make-iterator', 'iterator'); diff --git a/package.json b/package.json index 02ceb8d5..6038fb68 100644 --- a/package.json +++ b/package.json @@ -65,58 +65,58 @@ "test": "mocha" }, "dependencies": { - "arr-filter": "^1.1.1", - "arr-flatten": "^1.0.1", + "arr-filter": "^1.1.2", + "arr-flatten": "^1.0.3", "array-sort": "^0.1.2", "create-frame": "^1.0.0", - "define-property": "^0.2.5", - "for-in": "^0.1.6", - "for-own": "^0.1.4", + "define-property": "^1.0.0", + "for-in": "^1.0.2", + "for-own": "^1.0.0", "get-object": "^0.2.0", "get-value": "^2.0.6", - "handlebars": "^4.0.6", + "handlebars": "^4.0.10", + "handlebars-utils": "^1.0.0", "helper-date": "^0.2.3", - "helper-markdown": "^0.2.1", + "helper-markdown": "^0.2.2", "helper-md": "^0.2.2", "html-tag": "^1.0.0", - "index-of": "^0.2.0", - "is-even": "^0.1.1", + "is-even": "^1.0.0", "is-glob": "^3.1.0", "is-number": "^3.0.0", - "is-odd": "^0.1.1", - "kind-of": "^3.1.0", + "is-odd": "^1.0.0", + "kind-of": "^5.0.0", "lazy-cache": "^2.0.2", - "logging-helpers": "^0.4.0", - "make-iterator": "^0.3.0", - "micromatch": "^2.3.11", - "mixin-deep": "^1.1.3", - "normalize-path": "^2.0.1", + "logging-helpers": "^1.0.0", + "make-iterator": "^1.0.0", + "micromatch": "^3.0.3", + "mixin-deep": "^1.2.0", + "normalize-path": "^2.1.1", "relative": "^3.0.2", - "striptags": "^2.1.1", + "striptags": "^3.0.1", "to-gfm-code-block": "^0.1.1" }, "devDependencies": { - "engine-handlebars": "^0.8.0", + "engine-handlebars": "^0.8.2", "fs-exists-sync": "^0.1.0", - "global-modules": "^0.2.3", + "global-modules": "^1.0.0", "gulp": "^3.9.1", - "gulp-eslint": "^3.0.1", + "gulp-eslint": "^4.0.0", "gulp-format-md": "^1.0.0", - "gulp-istanbul": "^1.1.1", + "gulp-istanbul": "^1.1.2", "gulp-mocha": "^3.0.1", - "gulp-unused": "^0.2.0", + "gulp-unused": "^0.2.1", "helper-changelog": "^0.3.0", "helper-coverage": "^0.1.3", - "is-valid-app": "^0.2.1", - "js-yaml": "^3.7.0", + "is-valid-app": "^0.3.0", + "js-yaml": "^3.8.4", "markdown-link": "^0.1.1", - "mocha": "^3.2.0", - "should": "^11.1.2", + "mocha": "^3.4.2", + "should": "^11.2.1", "template-helpers": "^0.6.7", - "templates": "^0.25.3", + "templates": "^1.2.8", "through2": "^2.0.3", "verb-generate-readme": "^0.6.0", - "vinyl": "^2.0.1" + "vinyl": "^2.1.0" }, "keywords": [ "assemble", @@ -189,5 +189,16 @@ "lint": { "reflinks": true } + }, + "lintDeps": { + "devDependencies": { + "files": { + "patterns": [ + "test/integration/*.js", + "test/support/*.js", + "test/utils/*.js" + ] + } + } } } diff --git a/test/array.js b/test/array.js index c2e3cd46..07893048 100644 --- a/test/array.js +++ b/test/array.js @@ -1,6 +1,6 @@ 'use strict'; -require('should'); +var assert = require('assert'); var hbs = require('handlebars'); var helpers = require('..'); helpers.array({handlebars: hbs}); @@ -9,95 +9,95 @@ var context = {array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']}; describe('array', function() { describe('after', function() { - it('should return an empty string when undefined.', function() { - hbs.compile('{{after}}')().should.equal(''); + it('should return an empty string when undefined', function() { + assert.equal(hbs.compile('{{after}}')(), ''); }); - it('should return all of the items in an array after the given index.', function() { + it('should return all of the items in an array after the given index', function() { var fn = hbs.compile('{{after array 5}}'); - fn(context).should.eql(['f', 'g', 'h'].toString()); + assert.equal(fn(context), 'f,g,h'); }); - it('should return all of the items in an array after the specified count.', function() { + it('should return all of the items in an array after the specified count', function() { var fn = hbs.compile('{{after array 5}}'); - fn(context).should.eql(['f', 'g', 'h'].toString()); + assert.equal(fn(context), 'f,g,h'); }); }); describe('arrayify', function() { - it('should arrayify a value.', function() { - hbs.compile('{{#each (arrayify .)}}{{.}}{{/each}}')('foo').should.equal('foo'); - hbs.compile('{{#each (arrayify .)}}{{.}}{{/each}}')(['foo']).should.equal('foo'); + it('should arrayify a value', function() { + assert.equal(hbs.compile('{{#each (arrayify .)}}{{.}}{{/each}}')('foo'), 'foo'); + assert.equal(hbs.compile('{{#each (arrayify .)}}{{.}}{{/each}}')(['foo']), 'foo'); }); }); describe('before', function() { - it('should return an empty string when undefined.', function() { - hbs.compile('{{before}}')().should.equal(''); + it('should return an empty string when undefined', function() { + assert.equal(hbs.compile('{{before}}')(), ''); }); - it('should return all of the items in an array before the given index.', function() { + it('should return all of the items in an array before the given index', function() { var fn = hbs.compile('{{before array 5}}'); - fn(context).should.eql(['a', 'b', 'c'].toString()); + assert.equal(fn(context), 'a,b,c'); }); - it('should return all of the items in an array before the specified count.', function() { + it('should return all of the items in an array before the specified count', function() { var fn = hbs.compile('{{before array 5}}'); - fn(context).should.eql(['a', 'b', 'c'].toString()); + assert.equal(fn(context), 'a,b,c'); }); }); describe('each', function() { - it('should use the key and value of each property in an object inside a block.', function() { + it('should use the key and value of each property in an object inside a block', function() { var fn = hbs.compile('{{#each obj}}{{@key}}: {{this}} {{/each}}'); - fn({obj: {fry: 3, bender: 120 }}).should.equal('fry: 3 bender: 120 '); + assert.equal(fn({obj: {fry: 3, bender: 120 }}), 'fry: 3 bender: 120 '); }); }); describe('eachIndex', function() { - it('should render the block using the array and each item\'s index.', function() { + it('should render the block using the array and each item\'s index', function() { var fn = hbs.compile('{{#eachIndex array}} {{item}} is {{index}} {{/eachIndex}}'); - fn(context).should.equal(' a is 0 b is 1 c is 2 d is 3 e is 4 f is 5 g is 6 h is 7 '); + assert.equal(fn(context), ' a is 0 b is 1 c is 2 d is 3 e is 4 f is 5 g is 6 h is 7 '); }); }); describe('first', function() { - it('should return the first item in a collection.', function() { + it('should return the first item in a collection', function() { var fn = hbs.compile('{{first foo}}'); - fn({foo: ['a', 'b', 'c']}).should.equal('a'); + assert.equal(fn({foo: ['a', 'b', 'c']}), 'a'); }); - it('should return an array with the first two items in a collection.', function() { + it('should return an array with the first two items in a collection', function() { var fn = hbs.compile('{{first foo 2}}'); - fn({foo: ['a', 'b', 'c']}).should.eql(['a', 'b'].toString()); + assert.equal(fn({foo: ['a', 'b', 'c']}), ['a', 'b'].toString()); }); - it('should return an empty string when undefined.', function() { - hbs.compile('{{first}}')().should.equal(''); + it('should return an empty string when undefined', function() { + assert.equal(hbs.compile('{{first}}')(), ''); }); - it('should return the first item in an array.', function() { + it('should return the first item in an array', function() { var fn = hbs.compile('{{first foo}}'); - fn({foo: ['a', 'b', 'c']}).should.equal('a'); + assert.equal(fn({foo: ['a', 'b', 'c']}), 'a'); }); - it('should return an array with the first two items in an array.', function() { + it('should return an array with the first two items in an array', function() { var fn = hbs.compile('{{first foo 2}}'); - fn({foo: ['a', 'b', 'c']}).should.eql(['a', 'b'].toString()); + assert.equal(fn({foo: ['a', 'b', 'c']}), ['a', 'b'].toString()); }); }); describe('filter', function() { - it('should render the block if the given string is in the array.', function() { + it('should render the block if the given string is in the array', function() { var source = '{{#filter array "d"}}AAA{{else}}BBB{{/filter}}'; - hbs.compile(source)(context).should.equal('AAA'); + assert.equal(hbs.compile(source)(context), 'AAA'); }); it('should render the inverse block if the string is not in the array:', function() { var source = '{{#filter array "foo"}}AAA{{else}}BBB{{/filter}}'; - hbs.compile(source)(context).should.equal('BBB'); + assert.equal(hbs.compile(source)(context), 'BBB'); }); - it('should render a block for each object that has a "first" property with the value "d".', function() { + it('should render a block for each object that has a "first" property with the value "d"', function() { var ctx = { collection: [ @@ -114,179 +114,170 @@ describe('array', function() { var source = '{{#filter collection "d" property="first"}}{{this.first}}{{else}}ZZZ{{/filter}}'; var fn = hbs.compile(source); - fn(ctx).should.equal('d'); + assert.equal(fn(ctx), 'd'); }); }); describe('forEach', function() { - it('should iterate over an array, exposing objects as context.', function() { + it('should iterate over an array, exposing objects as context', function() { var arr = [{name: 'a'}, {name: 'b'}, {name: 'c'}]; var fn = hbs.compile('{{#forEach arr}}{{name}}{{/forEach}}'); - fn({arr: arr}).should.equal('abc'); + assert.equal(fn({arr: arr}), 'abc'); }); it('should expose `index`', function() { var arr = [{name: 'a'}, {name: 'b'}, {name: 'c'}]; var fn = hbs.compile('{{#forEach arr}}{{index}}{{/forEach}}'); - fn({arr: arr}).should.equal('123'); + assert.equal(fn({arr: arr}), '123'); }); it('should expose `total`', function() { var arr = [{name: 'a'}, {name: 'b'}, {name: 'c'}]; var fn = hbs.compile('{{#forEach arr}}{{total}}{{/forEach}}'); - fn({arr: arr}).should.equal('333'); + assert.equal(fn({arr: arr}), '333'); }); it('should expose `isFirst`', function() { var arr = [{name: 'a'}, {name: 'b'}, {name: 'c'}]; var fn = hbs.compile('{{#forEach arr}}{{isFirst}}{{/forEach}}'); - fn({arr: arr}).should.equal('truefalsefalse'); + assert.equal(fn({arr: arr}), 'truefalsefalse'); }); it('should expose `isLast`', function() { var arr = [{name: 'a'}, {name: 'b'}, {name: 'c'}]; var fn = hbs.compile('{{#forEach arr}}{{isLast}}{{/forEach}}'); - fn({arr: arr}).should.equal('falsefalsetrue'); + assert.equal(fn({arr: arr}), 'falsefalsetrue'); }); }); describe('inArray', function() { - it('should render the first block when a value exists in the array.', function() { + it('should render the first block when a value exists in the array', function() { var fn = hbs.compile('{{#inArray array "d"}}AAA{{else}}BBB{{/inArray}}'); - fn(context).should.equal('AAA'); + assert.equal(fn(context), 'AAA'); }); - it('should render the inverse block when a value does not exist.', function() { + it('should render the inverse block when a value does not exist', function() { var fn = hbs.compile('{{#inArray array "foo"}}AAA{{else}}BBB{{/inArray}}'); - fn(context).should.equal('BBB'); + assert.equal(fn(context), 'BBB'); }); }); describe('isArray', function() { - it('should return true if the value is an array.', function() { - hbs.compile('{{isArray "foo"}}')().should.eql('false'); - hbs.compile('{{isArray \'["foo"]\'}}')().should.eql('false'); - hbs.compile('{{isArray foo}}')({foo: ['foo']}).should.equal('true'); - hbs.compile('{{isArray (arrayify "foo")}}')().should.equal('true'); - hbs.compile('{{isArray (arrayify ["foo"])}}')().should.equal('true'); + it('should return true if the value is an array', function() { + assert.equal(hbs.compile('{{isArray "foo"}}')(), 'false'); + assert.equal(hbs.compile('{{isArray \'["foo"]\'}}')(), 'false'); + assert.equal(hbs.compile('{{isArray foo}}')({foo: ['foo']}), 'true'); + assert.equal(hbs.compile('{{isArray (arrayify "foo")}}')(), 'true'); + assert.equal(hbs.compile('{{isArray (arrayify ["foo"])}}')(), 'true'); }); }); describe('itemAt', function() { var ctx = {array: ['foo', 'bar', 'baz']}; - it('should return a null value for undefined array.', function() { - hbs.compile('{{#if (itemAt)}}exists{{else}}notfound{{/if}}')().should.equal('notfound'); + it('should return a null value for undefined array', function() { + assert.equal(hbs.compile('{{#if (itemAt)}}exists{{else}}notfound{{/if}}')(), 'notfound'); }); - it('should return a null value for empty array.', function() { + it('should return a null value for empty array', function() { var fn = hbs.compile('{{#if (itemAt array)}}exists{{else}}notfound{{/if}}'); - fn({array: []}).should.equal('notfound'); + assert.equal(fn({array: []}), 'notfound'); }); - it('should return a null value for exceed bound.', function() { + it('should return a null value for exceed bound', function() { var fn = hbs.compile('{{#if (itemAt array 999)}}exists{{else}}notfound{{/if}}'); - fn(ctx).should.equal('notfound'); + assert.equal(fn(ctx), 'notfound'); }); - it('should return a first value of array for undefined index.', function() { + it('should return a first value of array for undefined index', function() { var fn = hbs.compile('{{itemAt array}}'); - fn(ctx).should.equal('foo'); + assert.equal(fn(ctx), 'foo'); }); - it('should return a first value of array for zero index.', function() { + it('should return a first value of array for zero index', function() { var fn = hbs.compile('{{itemAt array 0}}'); - fn(ctx).should.equal('foo'); + assert.equal(fn(ctx), 'foo'); }); - it('should return a second value of array.', function() { + it('should return a second value of array', function() { var fn = hbs.compile('{{itemAt array 1}}'); - fn(ctx).should.equal('bar'); + assert.equal(fn(ctx), 'bar'); }); - it('should return a last value of array.', function() { + it('should return a last value of array', function() { var fn = hbs.compile('{{itemAt array -1}}'); - fn(ctx).should.equal('baz'); + assert.equal(fn(ctx), 'baz'); }); - it('should return a last before value of array.', function() { + it('should return a last before value of array', function() { var fn = hbs.compile('{{itemAt array -2}}'); - fn(ctx).should.equal('bar'); + assert.equal(fn(ctx), 'bar'); }); }); describe('join', function() { - it('should return an empty string when undefined.', function() { - hbs.compile('{{join}}')().should.equal(''); + it('should return an empty string when undefined', function() { + assert.equal(hbs.compile('{{join}}')(), ''); }); - it('should join items by the default separator.', function() { - hbs.compile('{{join array}}')(context).should.equal('a, b, c, d, e, f, g, h'); + it('should join items by the default separator', function() { + assert.equal(hbs.compile('{{join array}}')(context), 'a, b, c, d, e, f, g, h'); }); - it('should join by a custom separator.', function() { + it('should join by a custom separator', function() { var fn = hbs.compile('{{join array " | "}}'); - fn(context).should.equal('a | b | c | d | e | f | g | h'); + assert.equal(fn(context), 'a | b | c | d | e | f | g | h'); }); }); describe('last', function() { - it('should return an empty string when undefined.', function() { - hbs.compile('{{last}}')().should.equal(''); + it('should return an empty string when undefined', function() { + assert.equal(hbs.compile('{{last}}')(), ''); }); - it('should return the last item in an array.', function() { - hbs.compile('{{last array}}')(context).should.equal('h'); + it('should return the last item in an array', function() { + assert.equal(hbs.compile('{{last array}}')(context), 'h'); }); - it('should return an array with the last two items in an array.', function() { - hbs.compile('{{last array 2}}')(context).should.eql(['g', 'h'].toString()); + it('should return an array with the last two items in an array', function() { + assert.equal(hbs.compile('{{last array 2}}')(context), ['g', 'h'].toString()); }); }); describe('lengthEqual', function() { it('should render the first block if length is the given number', function() { var fn = hbs.compile('{{#lengthEqual array 8}}AAA{{else}}BBB{{/lengthEqual}}'); - fn(context).should.equal('AAA'); + assert.equal(fn(context), 'AAA'); }); it('should render the inverse block if length is not the given number', function() { var fn = hbs.compile('{{#lengthEqual array 3}}AAA{{else}}BBB{{/lengthEqual}}'); - fn(context).should.equal('BBB'); + assert.equal(fn(context), 'BBB'); }); }); describe('map', function() { - it('should return an empty string when undefined.', function() { - hbs.compile('{{map}}')().should.equal(''); + it('should return an empty string when undefined', function() { + assert.equal(hbs.compile('{{map}}')(), ''); }); - it('should map the items in the array and return new values.', function() { + it('should map the items in the array and return new values', function() { var o = {array: ['a', 'b', 'c']}; o.double = function(str) { return str + str; }; var fn = hbs.compile('{{map array double}}'); - fn(o).should.equal('aa,bb,cc'); + assert.equal(fn(o), 'aa,bb,cc'); }); - it('should work with a string value:', function() { - var o = {}; - o.double = function(str) { - return str + str; - }; - var fn = hbs.compile('{{map \'["a","b","c"]\' double}}'); - fn(o).should.equal('aa,bb,cc'); - }); - - it('should return an empty string when the array syntax is invalid:', function() { + it('should return an empty string when value is not an array', function() { var fn = hbs.compile('{{map \'["b", "c", "a"\'}}'); - fn(context).should.equal(''); + assert.equal(fn(context), ''); }); }); @@ -297,12 +288,12 @@ describe('array', function() { return typeof val === 'string'; }; var fn = hbs.compile('{{#some array isString}}AAA{{else}}BBB{{/some}}'); - fn(ctx).should.equal('AAA'); + assert.equal(fn(ctx), 'AAA'); }); it('should render the inverse block if the array is undefined', function() { var fn = hbs.compile('{{#some array isString}}AAA{{else}}BBB{{/some}}'); - fn().should.equal('BBB'); + assert.equal(fn(), 'BBB'); }); it('should render the inverse block if falsey', function() { @@ -311,7 +302,7 @@ describe('array', function() { return typeof val === 'string'; }; var fn = hbs.compile('{{#some array isString}}AAA{{else}}BBB{{/some}}'); - fn(ctx).should.equal('BBB'); + assert.equal(fn(ctx), 'BBB'); }); }); @@ -319,84 +310,79 @@ describe('array', function() { it('should return an empty string when an invalid value is passed:', function() { var fn = hbs.compile('{{sort}}'); var res = fn(); - res.should.equal(''); + assert.equal(res, ''); }); it('should sort the items in the array', function() { var fn = hbs.compile('{{sort array}}'); var res = fn({array: ['c', 'a', 'b']}); - res.should.equal('a,b,c'); + assert.equal(res, 'a,b,c'); }); - it('should return all items in an array sorted in lexicographical order.', function() { + it('should return all items in an array sorted in lexicographical order', function() { var fn = hbs.compile('{{sort array}}'); - fn(context).should.eql(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'].toString()); + assert.equal(fn(context), ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'].toString()); }); it('should sort the items in the array in reverse order:', function() { var fn = hbs.compile('{{sort array reverse="true"}}'); var res = fn({array: ['c', 'a', 'b']}); - res.should.equal('c,b,a'); + assert.equal(res, 'c,b,a'); }); }); describe('sortBy', function() { - it('should return an empty string when undefined.', function() { - hbs.compile('{{sortBy}}')().should.equal(''); - }); - - it('should sort the items in an array.', function() { - var fn = hbs.compile('{{sortBy \'["b", "c", "a"]\'}}'); - fn(context).should.equal('a,b,c'); + it('should return an empty string when undefined', function() { + assert.equal(hbs.compile('{{sortBy}}')(), ''); }); - it('should return an empty string when the array is invalid:', function() { + it('should return an empty string when not an array:', function() { var fn = hbs.compile('{{sortBy \'["b", "c", "a"\'}}'); - fn(context).should.equal(''); + assert.equal(fn(context), ''); }); - it('should take a compare function.', function() { - var o = {}; - o.compare = function(a, b) { + it('should take a compare function', function() { + var ctx = {array: ['b', 'a', 'c']}; + ctx.compare = function(a, b) { return b.localeCompare(a); }; - var fn = hbs.compile('{{sortBy \'["b", "c", "a"]\' compare}}'); - fn(o).should.equal('c,b,a'); + var fn = hbs.compile('{{sortBy array compare}}'); + assert.equal(fn(ctx), 'c,b,a'); }); it('should sort based on object key:', function() { var ctx = {arr: [{a: 'zzz'}, {a: 'aaa'}]}; hbs.registerHelper(helpers.object()); var fn = hbs.compile('{{{stringify (sortBy arr "a") 0}}}'); - fn(ctx).should.equal('[{"a":"aaa"},{"a":"zzz"}]'); + assert.equal(fn(ctx), '[{"a":"aaa"},{"a":"zzz"}]'); }); }); describe('withAfter', function() { - it('should use all of the items in an array after the specified count.', function() { + it('should use all of the items in an array after the specified count', function() { var fn = hbs.compile('{{#withAfter array 5}}<{{this}}>{{/withAfter}}'); - fn(context).should.equal(''); + assert.equal(fn(context), ''); }); }); describe('withBefore', function() { - it('should use all of the items in an array before the specified count.', function() { + it('should use all of the items in an array before the specified count', function() { var fn = hbs.compile('{{#withBefore array 5}}<{{this}}>{{/withBefore}}'); - fn(context).should.equal(''); + assert.equal(fn(context), ''); }); }); describe('withFirst', function() { - it('should use the first item in an array.', function() { + it('should use the first item in an array', function() { var fn = hbs.compile('{{#withFirst array}}{{this}} is smart.{{/withFirst}}'); - fn(context).should.equal('a is smart.'); + assert.equal(fn(context), 'a is smart.'); }); it('should return an empty string when no array is passed:', function() { - hbs.compile('{{#withFirst}}{{/withFirst}}')().should.equal(''); + assert.equal(hbs.compile('{{#withFirst}}{{/withFirst}}')(), ''); }); - it('should use the first two items in an array.', function() { + it('should use the first two items in an array', function() { var fn = hbs.compile('{{#withFirst array 2}}{{this}} is smart.{{/withFirst}}'); - fn(context).should.equal('a is smart.b is smart.'); + assert.equal(fn(context), 'a is smart.b is smart.'); }); }); @@ -406,38 +392,38 @@ describe('array', function() { var res = fn({ collection: [ {name: 'a'}, {name: 'b'}, {name: 'c'}, {name: 'd'}, {name: 'e'}, {name: 'f'}, {name: 'g'}, {name: 'h'}] }); - res.should.equal('abcd
efgh
'); + assert.equal(res, 'abcd
efgh
'); }); }); describe('withLast', function() { - it('should return an empty string when undefined.', function() { - hbs.compile('{{withLast}}')().should.equal(''); + it('should return an empty string when undefined', function() { + assert.equal(hbs.compile('{{withLast}}')(), ''); }); - it('should use the last item in an array.', function() { + it('should use the last item in an array', function() { var fn = hbs.compile('{{#withLast array}}{{this}} is dumb.{{/withLast}}'); - fn(context).should.equal('h is dumb.'); + assert.equal(fn(context), 'h is dumb.'); }); - it('should use the last two items in an array.', function() { + it('should use the last two items in an array', function() { var fn = hbs.compile('{{#withLast array 2}}{{this}} is dumb.{{/withLast}}'); - fn(context).should.equal('g is dumb.h is dumb.'); + assert.equal(fn(context), 'g is dumb.h is dumb.'); }); }); describe('withSort', function() { it('should return an empty string when array is undefined', function() { var fn = hbs.compile('{{#withSort}}{{this}}{{/withSort}}'); - fn(context).should.equal(''); + assert.equal(fn(context), ''); }); it('should sort the array in lexicographical order', function() { var fn = hbs.compile('{{#withSort array}}{{this}}{{/withSort}}'); - fn(context).should.equal('abcdefgh'); + assert.equal(fn(context), 'abcdefgh'); }); it('should sort the array in reverse order', function() { var fn = hbs.compile('{{#withSort array reverse="true"}}{{this}}{{/withSort}}'); - fn(context).should.equal('hgfedcba'); + assert.equal(fn(context), 'hgfedcba'); }); it('should sort the array by deliveries', function() { @@ -449,7 +435,7 @@ describe('array', function() { {name: 'd', deliveries: -12 } ] }); - res.should.equal('d: -12
b: 239
f: 8021
'); + assert.equal(res, 'd: -12
b: 239
f: 8021
'); }); it('should sort the array by deliveries in reverse order', function() { @@ -461,7 +447,7 @@ describe('array', function() { {name: 'd', deliveries: -12 } ] }); - res.should.equal('f: 8021
b: 239
d: -12
'); + assert.equal(res, 'f: 8021
b: 239
d: -12
'); }); }); }); diff --git a/test/code.js b/test/code.js index 7bb7e0f4..ae7a8ab8 100644 --- a/test/code.js +++ b/test/code.js @@ -1,6 +1,6 @@ 'use strict'; -require('should'); +var assert = require('assert'); var hbs = require('handlebars'); var helpers = require('..'); helpers.code({handlebars: hbs}); @@ -8,7 +8,7 @@ helpers.code({handlebars: hbs}); describe('code', function() { describe('embed', function() { it('should embed markdown:', function() { - hbs.compile('{{{embed "test/fixtures/simple.md"}}}')().should.equal([ + assert.equal(hbs.compile('{{{embed "test/fixtures/simple.md"}}}')(), [ '```markdown', '## Some Markdown\n', ' - one', @@ -20,7 +20,7 @@ describe('code', function() { }); it('should determine the language from the file extension', function() { - hbs.compile('{{{embed "test/fixtures/embedded.md"}}}')().should.equal([ + assert.equal(hbs.compile('{{{embed "test/fixtures/embedded.md"}}}')(), [ '```markdown', '## Markdown', '', @@ -40,7 +40,7 @@ describe('code', function() { it('should use the language defined in the last argument', function() { var template = hbs.compile('{{{embed "test/fixtures/index.html" "hbs"}}}'); - template().should.equal([ + assert.equal(template(), [ '```hbs', '', ' ', @@ -61,7 +61,7 @@ describe('code', function() { describe('gist', function() { it('should return a gist script tag', function() { var fn = hbs.compile('{{{gist "abcdefg"}}}'); - fn().should.equal(''); + assert.equal(fn(), ''); }); }); @@ -69,19 +69,19 @@ describe('code', function() { it('should return a jsfiddle embed link, with default tabs assigned', function() { var source = '{{{jsfiddle id="UXbas"}}}'; var fn = hbs.compile(source); - fn().should.equal(''); + assert.equal(fn(), ''); }); it('should throw an error if id is missing', function() { - (function() { + assert.throws(function() { hbs.compile('{{jsfiddle}}')(); - }).should.throw('jsfiddle helper expects an `id`'); + }); }); it('should return a jsfiddle embed link, with custom tabs assigned', function() { var source = '{{{jsfiddle id="UXbas" tabs="html,css"}}}'; var fn = hbs.compile(source); - fn().should.equal(''); + assert.equal(fn(), ''); }); }); }); diff --git a/test/helpers.js b/test/helpers.js index 5691dc8a..3ee2a2f4 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -1,7 +1,6 @@ 'use strict'; require('mocha'); -require('should'); var assert = require('assert'); var hbs = require('handlebars'); var helpers = require('..'); diff --git a/test/html.js b/test/html.js index e359a587..01e7d122 100644 --- a/test/html.js +++ b/test/html.js @@ -1,6 +1,6 @@ 'use strict'; -require('should'); +var assert = require('assert'); var hbs = require('handlebars'); var helpers = require('..'); helpers.html({handlebars: hbs}); @@ -11,35 +11,35 @@ var actual; describe('html', function() { describe('css', function() { it('should return an empty string when no context is passed:', function() { - hbs.compile('{{{css}}}')().should.equal(''); + assert.equal(hbs.compile('{{{css}}}')(), ''); }); it('should use a path passed as a string', function() { var actual = hbs.compile('{{{css "abc.css"}}}')(); - actual.should.equal(''); + assert.equal(actual, ''); }); it('should use options.assets', function() { var actual = hbs.compile('{{{css "abc.css"}}}')({options: {assets: 'foo'}}); - actual.should.equal(''); + assert.equal(actual, ''); }); it('should ensure that options.assets is a string', function() { var actual = hbs.compile('{{{css "abc.css"}}}')({options: {assets: null}}); - actual.should.equal(''); + assert.equal(actual, ''); }); it('should use the `href` attribute on the hash', function() { actual = hbs.compile('{{{css href=""}}}')(); - actual.should.equal(''); + assert.equal(actual, ''); actual = hbs.compile('{{{css href="abc.css"}}}')(); - actual.should.equal(''); + assert.equal(actual, ''); }); it('should create multiple tags from an array passed on the context:', function() { var ctx = {styles: ['a.css', 'bcss', 'c.css'] }; - hbs.compile('{{{css styles}}}')(ctx).should.equal([ + assert.equal(hbs.compile('{{{css styles}}}')(ctx), [ '', '', '', @@ -48,48 +48,48 @@ describe('html', function() { it('should create a less tag (TODO: only works with array format)', function() { var ctx = {styles: ['a.less'] }; - hbs.compile('{{{css styles}}}')(ctx).should.equal(''); + assert.equal(hbs.compile('{{{css styles}}}')(ctx), ''); }); }); describe('ellipsis', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{ellipsis}}'); - fn().should.equal(''); + assert.equal(fn(), ''); }); it('should return then string truncated by a specified length.', function() { var fn = hbs.compile('{{ellipsis "Bender should not be allowed on tv." 31}}'); - fn().should.equal('Bender should not be allowed on…'); + assert.equal(fn(), 'Bender should not be allowed on…'); }); it('should return the string if shorter than the specified length.', function() { var fn = hbs.compile('{{ellipsis "Bender should not be allowed on tv." 100}}'); - fn().should.equal('Bender should not be allowed on tv.'); + assert.equal(fn(), 'Bender should not be allowed on tv.'); }); it('should return a string if empty', function() { - hbs.compile('{{isString (ellipsis "")}}')().should.equal('true'); + assert(hbs.compile('{{isString (ellipsis "")}}')()); }); }); describe('js', function() { it('should create an empty script tag', function() { - hbs.compile('{{{js}}}')().should.equal(''); + assert.equal(hbs.compile('{{{js}}}')(), ''); }); it('should use a path passed as a string', function() { - hbs.compile('{{{js "abc.js"}}}')().should.equal(''); + assert.equal(hbs.compile('{{{js "abc.js"}}}')(), ''); }); it('should use the `src` attribute on the hash', function() { - hbs.compile('{{{js src=""}}}')().should.equal(''); - hbs.compile('{{{js src="abc.js"}}}')().should.equal(''); + assert.equal(hbs.compile('{{{js src=""}}}')(), ''); + assert.equal(hbs.compile('{{{js src="abc.js"}}}')(), ''); }); it('should create multiple tags from an array passed on the context:', function() { var ctx = {scripts: ['a.js', 'bjs', 'c.js'] }; - hbs.compile('{{{js scripts}}}')(ctx).should.equal([ + assert.equal(hbs.compile('{{{js scripts}}}')(ctx), [ '', '', '', @@ -98,62 +98,62 @@ describe('html', function() { it('should create a coffeescript tag (TODO: only works with array format)', function() { var ctx = {scripts: ['a.coffee'] }; - hbs.compile('{{{js scripts}}}')(ctx).should.equal(''); + assert.equal(hbs.compile('{{{js scripts}}}')(ctx), ''); }); }); describe('sanitize', function() { it('should return an empty string when undefined.', function() { - hbs.compile('{{sanitize}}')().should.equal(''); + assert.equal(hbs.compile('{{sanitize}}')(), ''); }); it('should strip html from a string.', function() { var actual = hbs.compile('{{sanitize "foo"}}')(); - actual.should.equal('foo'); + assert.equal(actual, 'foo'); }); }); describe('truncate', function() { it('should return an empty string if undefined', function() { var fn = hbs.compile('{{truncate}}'); - fn().should.equal(''); + assert.equal(fn(), ''); }); it('should return the string truncated by a specified length.', function() { var fn = hbs.compile('{{truncate "Bender should not be allowed on tv." 31}}'); - fn().should.equal('Bender should not be allowed on'); + assert.equal(fn(), 'Bender should not be allowed on'); }); it('should return the string if shorter than the specified length.', function() { var fn = hbs.compile('{{truncate "Bender should not be allowed on tv." 100}}'); - fn().should.equal('Bender should not be allowed on tv.'); + assert.equal(fn(), 'Bender should not be allowed on tv.'); }); it('should return then string truncated by a specified length', function() { var fn = hbs.compile('{{truncate "foo bar baz qux" 7}}...'); - fn().should.equal('foo bar...'); + assert.equal(fn(), 'foo bar...'); }); it('should return then string truncated by a specified length, providing a custom string to denote an omission.', function() { var fn = hbs.compile('{{truncate "foo bar baz qux" 7 "…"}}'); - fn().should.equal('foo ba…'); + assert.equal(fn(), 'foo ba…'); }); it('should return a string if empty', function() { - hbs.compile('{{isString (truncate "")}}')().should.equal('true'); + assert(hbs.compile('{{isString (truncate "")}}')()); }); }); describe('ul', function() { it('should should return an unordered list', function() { var fn = hbs.compile('{{#ul data class="names"}}{{aaa}} {{bbb}}{{/ul}}'); - fn(locals).should.equal('
  • AAA BBB
  • \n
  • CCC DDD
'); + assert.equal(fn(locals), '
  • AAA BBB
  • \n
  • CCC DDD
'); }); }); describe('ol', function() { it('should should return an ordered list', function() { var fn = hbs.compile('{{#ol data class="names"}}{{aaa}} {{bbb}}{{/ol}}'); - fn(locals).should.equal('
  1. AAA BBB
  2. \n
  3. CCC DDD
'); + assert.equal(fn(locals), '
  1. AAA BBB
  2. \n
  3. CCC DDD
'); }); }); @@ -182,7 +182,7 @@ describe('html', function() { '
My new caption!
', '', ].join('\n'); - fn(context).should.equal(comparison); + assert.equal(fn(context), comparison); }); it('should return figure with extra class "test"', function() { @@ -213,7 +213,7 @@ describe('html', function() { '
My new caption!
', '' ].join('\n'); - fn(context).should.equal(comparison); + assert.equal(fn(context), comparison); }); it('should return figure with image that has class "test"', function() { @@ -243,7 +243,7 @@ describe('html', function() { '
My new caption!
', '' ].join('\n'); - fn(context).should.equal(comparison); + assert.equal(fn(context), comparison); }); it('should return figure with link that has class "test"', function() { @@ -273,7 +273,7 @@ describe('html', function() { '
My new caption!
', '', ].join('\n'); - fn(context).should.equal(comparison); + assert.equal(fn(context), comparison); }); it('should return figure without link', function() { @@ -297,7 +297,7 @@ describe('html', function() { '
My new caption!
', '' ].join('\n'); - fn(context).should.equal(comparison); + assert.equal(fn(context), comparison); }); it('should return figure without caption', function() { @@ -322,7 +322,7 @@ describe('html', function() { '
', '' ].join('\n'); - fn(context).should.equal(comparison); + assert.equal(fn(context), comparison); }); }); }); diff --git a/test/string.js b/test/string.js index 5961cdc7..91766039 100644 --- a/test/string.js +++ b/test/string.js @@ -112,23 +112,23 @@ describe('string', function() { describe('isString', function() { it('should return true for string', function() { - hbs.compile('{{isString "foo"}}')().should.equal('true'); + assert.equal(hbs.compile('{{isString "foo"}}')(), 'true'); }); it('should return true for empty string', function() { - hbs.compile('{{isString ""}}')().should.equal('true'); + assert.equal(hbs.compile('{{isString ""}}')(), 'true'); }); it('should return false for number', function() { - hbs.compile('{{isString 123}}')().should.equal('false'); + assert.equal(hbs.compile('{{isString 123}}')(), 'false'); }); it('should return false for null', function() { - hbs.compile('{{isString null}}')().should.equal('false'); + assert.equal(hbs.compile('{{isString null}}')(), 'false'); }); it('should return false when undefined', function() { - hbs.compile('{{isString}}')().should.equal('false'); + assert.equal(hbs.compile('{{isString}}')(), 'false'); }); });