Skip to content
This repository was archived by the owner on Apr 11, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
node_modules/*
.idea
34 changes: 22 additions & 12 deletions lib/filters/truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,32 @@ var each = require('../utils').each;
*
* @param {*} input
* @param {number} len Number of characters to truncate to.
* @param {boolean} [entireEndingWord=true] Text should end with an entire word
* @param {string} [end="..."] Text that will be appended if the string was truncated
* @return {*}
*/
module.exports = function (input, len, end) {
end = (typeof end === 'undefined') ? '...' : end;
module.exports = function (input, len, entireEndingWord, end) {
entireEndingWord = (typeof entireEndingWord === 'undefined') ? true : entireEndingWord;
end = (typeof end === 'undefined') ? '...' : end;

if (typeof input === 'object') {
each(input, function (value, key) {
input[key] = module.exports(value, len, end);
});
return input;
}
if (typeof input === 'object') {
each(input, function (value, key) {
input[key] = module.exports(value, len, entireEndingWord, end);
});
return input;
}

if (typeof input === 'string' && input.length > len) {
input = input.substring(0, len);

if (typeof input === 'string') {
return input.substring(0, len) + ((input.length > len) ? end : '');
}
if (entireEndingWord) {
var lastSpace = input.lastIndexOf(' ');
len = lastSpace !== -1 ? lastSpace : len;
input = input.substring(0, len);
}

return input;
return input + ((input.length >= len) ? end : '');
}

return input;
};