Skip to content

Commit

Permalink
linting, test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Oct 14, 2015
1 parent 3270fc7 commit 1c335e9
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 75 deletions.
2 changes: 0 additions & 2 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 0 additions & 1 deletion lib/comparison.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
71 changes: 70 additions & 1 deletion lib/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 "<span>foo bar baz</span>", 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 `<script></script>` tags with paths/urls to
* javascript or coffeescript files.
Expand Down Expand Up @@ -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 "<span>foo</span>"}}
* //=> '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("<span>foo bar baz</span>", 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 (`<ul></ul>`)
*
Expand Down
25 changes: 25 additions & 0 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var path = require('path');
var relative = require('relative');
var normalize = require('normalize-path');

/**
* Expose `helpers`
Expand Down Expand Up @@ -73,3 +74,27 @@ helpers.extname = function(filepath) {
helpers.relative = function(a, b) {
return relative(a, b);
};

/**
* Get specific (joined) segments of a file path by passing a
* range of array indices.
*
* ```js
* {{segments "a/b/c/d" "2" "3"}}
* //=> 'c/d'
*
* {{segments "a/b/c/d" "1" "3"}}
* //=> 'b/c/d'
*
* {{segments "a/b/c/d" "1" "2"}}
* //=> 'b/c'
* ```
*
* @param {String} `filepath` The file path to split into segments.
* @return {String} Returns a single, joined file path.
* @api public
*/

helpers.segments = function(fp, a, b) {
return normalize(fp).split('/').slice(a, b).join('/');
};
1 change: 0 additions & 1 deletion lib/string.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var html = require('./utils/html');
var utils = require('./utils');

/**
Expand Down
30 changes: 25 additions & 5 deletions lib/utils/html.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use strict';

var striptags = require('striptags');
var typeOf = require('kind-of');
var utils = require('./');

/**
* Expose `utils`
*/

var utils = module.exports;
var html = module.exports;

/**
* Remove extra newlines from HTML, respect indentation.
Expand All @@ -16,7 +18,7 @@ var utils = module.exports;
* @api public
*/

utils.condense = function(str) {
html.condense = function(str) {
return str.replace(/(\n|\r){2,}/g, '\n');
};

Expand All @@ -28,7 +30,7 @@ utils.condense = function(str) {
* @api public
*/

utils.padcomments = function(str) {
html.padcomments = function(str) {
return str.replace(/(\s*<!--)/g, '\n$1');
};

Expand All @@ -40,14 +42,32 @@ utils.padcomments = function(str) {
* @api public
*/

utils.parseAttributes = function parseAttributes(hash) {
html.parseAttributes = function parseAttributes(hash) {
return Object.keys(hash).map(function(key) {
return key + '="' + hash[key] + '"';
}).join(' ');
};

/**
* Strip HTML tags from a string, so that only the text nodes
* are preserved.
*
* ```js
* {{sanitize "<span>foo</span>"}}
* //=> 'foo'
* ```
*
* @param {String} `str` The string of HTML to sanitize.
* @return {String}
* @api public
*/

html.sanitize = function(str) {
if (!utils.isString(str)) return '';
return striptags(str).trim();
};

utils.toAttributes = function toAttributes(hash) {
html.toAttributes = function toAttributes(hash) {
var res = '';
for (var key in hash) {
if (hash.hasOwnProperty(key) && typeOf(hash[key] === 'string')) {
Expand Down
72 changes: 72 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,63 @@

var utils = require('export-files')(__dirname);

/**
* Remove leading and trailing whitespace and non-word
* characters from the given string.
*
* @param {String} `str`
* @return {String}
*/

utils.chop = function(str) {
if (!utils.isString(str)) return '';
var re = /^[-_.\W\s]+|[-_.\W\s]+$/g;
return str.trim().replace(re, '');
};

/**
* Change casing on the given `string`, optionally
* passing a delimiter to use between words in the
* returned string.
*
* ```js
* utils.changecase('fooBarBaz');
* //=> 'foo bar baz'
*
* utils.changecase('fooBarBaz' '-');
* //=> 'foo-bar-baz'
* ```
*
* @name .changecase
* @param {String} `string` The string to change.
* @return {String}
* @api public
*/

utils.changecase = function(str, fn) {
if (!utils.isString(str)) return '';
if (str.length === 1) {
return str.toLowerCase();
}

str = utils.chop(str).toLowerCase();
if (typeof fn !== 'function') {
fn = utils.identity;
}

var re = /[-_.\W\s]+(\w|$)/g;
return str.replace(re, function (_, ch) {
return fn(ch);
});
};

/**
* Generate a random number
*
* @param {Number} `min`
* @param {Number} `max`
* @return {Number}
* @api public
*/

utils.random = function(min, max) {
Expand Down Expand Up @@ -52,6 +103,27 @@ utils.result = function(value) {
return value;
};

/**
* Return the given value, unchanged.
*
* @name .identity
* @param {any} `val`
* @return {any}
* @api public
*/

utils.identity = function(val) {
return val;
};

/**
* Return true if `val` is a string.
*/

utils.isString = function(val) {
return val && typeof val === 'string';
};

/**
* Cast `val` to an array.
*
Expand Down
49 changes: 49 additions & 0 deletions test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ describe('html', function() {
});
});

describe('ellipsis', function() {
it('should return an empty string if undefined', function() {
var fn = hbs.compile('{{ellipsis}}');
fn().should.equal('');
});
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…');
});
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.');
});
});

describe('js', function() {
it('should create an empty script tag', function() {
hbs.compile('{{{js}}}')().should.equal('<script></script>');
Expand Down Expand Up @@ -81,6 +96,40 @@ describe('html', function() {
});
});

describe('sanitize', function() {
it('should return an empty string when undefined.', function() {
hbs.compile('{{sanitize}}')().should.equal('');
});
it('should strip html from a string.', function() {
var actual = hbs.compile('{{sanitize "<span>foo</span>"}}')();
actual.should.equal('foo');
});
});

describe('truncate', function() {
it('should return an empty string if undefined', function() {
var fn = hbs.compile('{{truncate}}');
fn().should.equal('');
});
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');
});
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.');
});
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...');
});

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…');
});
});

describe('ul', function() {
it('should should return an unordered list', function() {
var fn = hbs.compile('{{#ul data class="names"}}{{aaa}} {{bbb}}{{/ul}}');
Expand Down
Loading

0 comments on commit 1c335e9

Please sign in to comment.