Skip to content

Commit

Permalink
Fixed null issue with truncateWords fixes HubSpot#84, added tests for…
Browse files Browse the repository at this point in the history
… truncateWords
  • Loading branch information
blakeyc committed Jan 3, 2017
1 parent 40fc981 commit 5ac5c13
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 46 deletions.
8 changes: 7 additions & 1 deletion __tests__/humanize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,19 @@ describe('Truncating objects to shorter versions', () => {
const objs = {
str: 'abcdefghijklmnopqrstuvwxyz',
num: 1234567890,
arr: [1, 2, 3, 4, 5]
arr: [1, 2, 3, 4, 5],
wrd: 'hello world'
};

it('should truncate a long string with ellipsis', () => {
expect(Humanize.truncate(objs.str, 14)).toEqual('abcdefghijk...');
expect(Humanize.truncate(objs.str, 14, '...kidding')).toEqual('abcd...kidding');
});

it('should truncate a string of words with ellipsis', () => {
expect(Humanize.truncateWords(objs.wrd, 1)).toEqual('hello ...');
});

it('should truncate a number to an upper bound', () => {
expect(Humanize.truncatenumber(objs.num, 500)).toEqual('500+');
expect(Humanize.boundedNumber(objs.num, 500)).toEqual('500+');
Expand All @@ -210,6 +215,7 @@ describe('Truncating objects to shorter versions', () => {
expect(Humanize.truncate(objs.str, objs.str.length + 1)).toEqual(objs.str);
expect(Humanize.truncatenumber(objs.num, objs.num + 1)).toEqual(String(objs.num));
expect(Humanize.boundedNumber(objs.num, objs.num + 1)).toEqual(String(objs.num));
expect(Humanize.truncateWords(objs.wrd, 3)).toEqual('hello world');
});
});

Expand Down
63 changes: 30 additions & 33 deletions dist/humanize.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* humanize.js - v1.8.2 */
'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

/**
* Copyright 2013-2016 HubSpotDev
Expand Down Expand Up @@ -77,9 +77,8 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
var Humanize = {

// Converts a large integer to a friendly text representation.

intword: function intword(number, charWidth) {
var decimals = arguments.length <= 2 || arguments[2] === undefined ? 2 : arguments[2];
var decimals = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2;

/*
* This method is deprecated. Please use compactInteger instead.
Expand All @@ -91,7 +90,7 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol

// Converts an integer into its most compact representation
compactInteger: function compactInteger(input) {
var decimals = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
var decimals = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;

decimals = Math.max(decimals, 0);
var number = parseInt(input, 10);
Expand Down Expand Up @@ -150,7 +149,7 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol

// Converts an integer to a string containing commas every three digits.
intComma: function intComma(number) {
var decimals = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
var decimals = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;

return Humanize.formatNumber(number, decimals);
},
Expand All @@ -161,7 +160,7 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol

// Formats the value like a 'human-readable' file size (i.e. '13 KB', '4.1 MB', '102 bytes', etc).
fileSize: function fileSize(filesize) {
var precision = arguments.length <= 1 || arguments[1] === undefined ? 2 : arguments[1];
var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;

for (var label in LABELS_FOR_POWERS_OF_KILO) {
if (LABELS_FOR_POWERS_OF_KILO.hasOwnProperty(label)) {
Expand All @@ -185,9 +184,9 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
// Formats a number to a human-readable string.
// Localize by overriding the precision, thousand and decimal arguments.
formatNumber: function formatNumber(number) {
var precision = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
var thousand = arguments.length <= 2 || arguments[2] === undefined ? ',' : arguments[2];
var decimal = arguments.length <= 3 || arguments[3] === undefined ? '.' : arguments[3];
var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var thousand = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ',';
var decimal = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '.';

// Create some private utility functions to make the computational
// code that follows much easier to read.
Expand Down Expand Up @@ -268,7 +267,7 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol

// Interprets numbers as occurences. Also accepts an optional array/map of overrides.
times: function times(value) {
var overrides = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

if (isFiniteNumber(value) && value >= 0) {
var number = parseFloat(value);
Expand Down Expand Up @@ -299,8 +298,8 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
// Truncates a string if it is longer than the specified number of characters (inclusive).
// Truncated strings will end with a translatable ellipsis sequence ("…").
truncate: function truncate(str) {
var length = arguments.length <= 1 || arguments[1] === undefined ? 100 : arguments[1];
var ending = arguments.length <= 2 || arguments[2] === undefined ? '...' : arguments[2];
var length = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100;
var ending = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '...';

if (str.length > length) {
return str.substring(0, length - ending.length) + ending;
Expand All @@ -312,21 +311,19 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
// Truncates a string after a certain number of words.
truncateWords: function truncateWords(string, length) {
var array = string.split(' ');
var result = '';
var i = 0;

while (i < length) {
if (exists(array[i])) {
result += array[i] + ' ';
}
i++;
}

if (array.length > length) {
var result = '';
var i = 0;
while (i < length) {
if (exists(array[i])) {
result += array[i] + ' ';
}
i++;
}
return result + '...';
}

return null;
return string;
},
truncatewords: function truncatewords() {
return Humanize.truncateWords.apply(Humanize, arguments);
Expand All @@ -335,8 +332,8 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol

// Truncates a number to an upper bound.
boundedNumber: function boundedNumber(num) {
var bound = arguments.length <= 1 || arguments[1] === undefined ? 100 : arguments[1];
var ending = arguments.length <= 2 || arguments[2] === undefined ? '+' : arguments[2];
var bound = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100;
var ending = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '+';

var result = void 0;

Expand Down Expand Up @@ -377,8 +374,8 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol

// Converts an object to a definition-like string
dictionary: function dictionary(object) {
var joiner = arguments.length <= 1 || arguments[1] === undefined ? ' is ' : arguments[1];
var separator = arguments.length <= 2 || arguments[2] === undefined ? ', ' : arguments[2];
var joiner = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ' is ';
var separator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ', ';

var result = '';

Expand Down Expand Up @@ -414,7 +411,7 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
return verb + ' ' + times;
},
pace: function pace(value, intervalMs) {
var unit = arguments.length <= 2 || arguments[2] === undefined ? 'time' : arguments[2];
var unit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'time';

if (value === 0 || intervalMs === 0) {
// Needs a better string than this...
Expand Down Expand Up @@ -453,23 +450,23 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol

// Converts newlines to <br/> tags
nl2br: function nl2br(string) {
var replacement = arguments.length <= 1 || arguments[1] === undefined ? '<br/>' : arguments[1];
var replacement = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '<br/>';

return string.replace(/\n/g, replacement);
},


// Converts <br/> tags to newlines
br2nl: function br2nl(string) {
var replacement = arguments.length <= 1 || arguments[1] === undefined ? '\r\n' : arguments[1];
var replacement = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '\r\n';

return string.replace(/\<br\s*\/?\>/g, replacement);
},


// Capitalizes first letter in a string
capitalize: function capitalize(string) {
var downCaseTail = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
var downCaseTail = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

return '' + string.charAt(0).toUpperCase() + (downCaseTail ? string.slice(1).toLowerCase() : string.slice(1));
},
Expand All @@ -492,8 +489,8 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol

var _doTitleCase = void 0;
_doTitleCase = function doTitleCase(_string) {
var hyphenated = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
var firstOrLast = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
var hyphenated = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var firstOrLast = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;

var titleCasedArray = [];
var stringArray = _string.split(hyphenated ? splitOnHyphensRegex : splitOnWhiteSpaceRegex);
Expand Down
Loading

0 comments on commit 5ac5c13

Please sign in to comment.