diff --git a/lib/array.js b/lib/array.js index 81db4134..118f7693 100644 --- a/lib/array.js +++ b/lib/array.js @@ -383,8 +383,6 @@ helpers.some = function(arr, cb, options) { if (arr == null) { return options.inverse(this); } - - var result = false; var len = arr.length, i = -1; while (++i < len) { if (cb(arr[i], i, arr)) { diff --git a/lib/comparison.js b/lib/comparison.js index 13b89d31..2dcf3186 100644 --- a/lib/comparison.js +++ b/lib/comparison.js @@ -2,7 +2,6 @@ var isOdd = require('is-odd'); var isEven = require('is-even'); -var typeOf = require('kind-of'); var contains = require('./utils/contains'); var isObject = require('./object').isObject; var isString = require('./string').isString; diff --git a/lib/html.js b/lib/html.js index 17042b57..90b4d151 100644 --- a/lib/html.js +++ b/lib/html.js @@ -3,7 +3,6 @@ var path = require('path'); var tag = require('html-tag'); var typeOf = require('kind-of'); -var merge = require('mixin-deep'); var html = require('./utils/html'); var utils = require('./utils'); var parseAttr = html.parseAttributes; @@ -51,6 +50,30 @@ helpers.css = function(array, options) { }).join('\n'); }; +/** + * Truncates a string to the specified `length`, and appends + * it with an elipsis, `…`. + * + * ```js + * {{ellipsis "foo bar baz", 7}} + * //=> 'foo bar…' + * ``` + * @name .ellipsis + * @param {String} `str` + * @param {Number} `length` The desired length of the returned string. + * @return {String} The truncated string. + * @api public + */ + +helpers.ellipsis = function(str, limit) { + if (str && typeof str === "string") { + if (str.length <= limit) { + return str; + } + return helpers.truncate(str, limit) + '…'; + } +}; + /** * Generate one or more `` tags with paths/urls to * javascript or coffeescript files. @@ -82,6 +105,52 @@ helpers.js = function(context) { }).join('\n'); }; +/** + * Strip HTML tags from a string, so that only the text nodes + * are preserved. + * + * ```js + * {{sanitize "foo"}} + * //=> 'foo' + * ``` + * + * @param {String} `str` The string of HTML to sanitize. + * @return {String} + * @api public + */ + +helpers.sanitize = function(str) { + return html.sanitize(str); +}; + +/** + * Truncate a string by removing all HTML tags and limiting the result + * to the specified `length`. Aslo see [ellipsis][]. + * + * ```js + * truncate("foo bar baz", 7); + * //=> 'foo bar' + * ``` + * + * @name .truncate + * @param {String} `str` + * @param {Number} `limit` The desired length of the returned string. + * @param {String} `suffix` Optionally supply a string to use as a suffix to + * denote when the string has been truncated. + * @return {String} The truncated string. + * @api public + */ + +helpers.truncate = function(str, limit, suffix) { + if (str && typeof str === "string") { + var ch = typeof suffix === 'string' ? suffix : ''; + if (str.length > limit) { + return html.sanitize(str).slice(0, limit - ch.length) + ch; + } + return str; + } +}; + /** * Block helper for creating unordered lists (`