Skip to content

Commit

Permalink
Merge pull request #8 from millerized/version-1
Browse files Browse the repository at this point in the history
Fix: #7 off-by-one issue, tons of hygiene improvements including transpilation
  • Loading branch information
millerized committed Jan 6, 2018
2 parents 27f1b4e + 8261d54 commit 1c47900
Show file tree
Hide file tree
Showing 6 changed files with 2,204 additions and 25 deletions.
12 changes: 12 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"env",
{
"targets": {
"browsers": ["last 2 versions", "IE >= 10"]
}
}
]
]
}
41 changes: 41 additions & 0 deletions dist/smart-truncate.es5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

/**
* smartTruncate - Smartly™ truncate a given string.
*
* @param {String} string A string with a minimum lenght of 4 chars.
* @param {Number} length The length of the truncated result.
* @param {Number} [position] The index of the ellipsis (zero based). Default is the end.
* @return {String} Return a truncated string w/ ellipsis.
*
* Example: smartTruncate('Steve Miller', 8) === 'Steve M…'.
* Example: smartTruncate('Steve Miller', 9, 4) === 'Stev…ller'.
*/
var smartTruncate = function smartTruncate(string, length) {
var position = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : length;

var ellipsisOffset = 1;
var minLength = 4;

var str = string;

if (typeof str === 'string') {
str = str.trim();
}

var invalid = typeof str !== 'string' || str.length < minLength || typeof length !== 'number' || length <= minLength || length >= str.length - ellipsisOffset;

if (invalid) return string;

if (position >= length - ellipsisOffset) {
var _start = str.substring(0, length - ellipsisOffset);
return _start + '\u2026';
}

var start = str.substring(0, position);
var end = str.slice(position + ellipsisOffset - length);

return start + '\u2026' + end;
};

module.exports = smartTruncate;
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
"name": "smart-truncate",
"version": "0.0.5",
"description": "A small library that truncates a string. It can insert or append an ellipsis at any desired position of the truncated result.",
"main": "smart-truncate.js",
"main": "dist/smart-truncate.es5.js",
"scripts": {
"test": "node_modules/.bin/mocha --reporter spec",
"cover": "node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- -R spec test/*"
"test": "node_modules/.bin/mocha -R spec src/*.spec.js",
"cover": "node_modules/istanbul/lib/cli.js cover node_modules/.bin/_mocha -- -R spec src/*.spec.js",
"cover:dev": "npm run compile && npm run cover",
"compile": "./node_modules/.bin/babel src/smart-truncate.js --out-file dist/smart-truncate.es5.js",
"build": "npm run compile"
},
"repository": {
"type": "git",
Expand All @@ -24,9 +27,11 @@
},
"homepage": "https://github.com/millerized/smart-truncate#readme",
"devDependencies": {
"chai": "^3.5.0",
"coveralls": "^2.11.14",
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1",
"chai": "^4.1.2",
"coveralls": "^3.0.0",
"istanbul": "^0.4.5",
"mocha": "^3.1.2"
"mocha": "^4.1.0"
}
}
9 changes: 6 additions & 3 deletions smart-truncate.js → src/smart-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
* @return {String} Return a truncated string w/ ellipsis.
*
* Example: smartTruncate('Steve Miller', 8) === 'Steve M…'.
* Example: smartTruncate('Steve Miller', 9, 5) === 'Stev…ller'.
* Example: smartTruncate('Steve Miller', 9, 4) === 'Stev…ller'.
*/
module.exports = function smartTruncate(string, length, position = length) {
const smartTruncate = (string, length, position = length) => {
const ellipsisOffset = 1;
const minLength = 4;

let str = string;

if (typeof str === 'string') {
Expand All @@ -26,7 +27,7 @@ module.exports = function smartTruncate(string, length, position = length) {

if (invalid) return string;

if (position >= length) {
if (position >= (length - ellipsisOffset)) {
const start = str.substring(0, length - ellipsisOffset);
return `${start}…`;
}
Expand All @@ -36,3 +37,5 @@ module.exports = function smartTruncate(string, length, position = length) {

return `${start}${end}`;
}

module.exports = smartTruncate;
51 changes: 35 additions & 16 deletions test/test.js → src/smart-truncate.spec.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,83 @@
'use strict';

const expect = require('chai').expect;
const smartTruncate = require('../smart-truncate');
const {expect} = require('chai');
const smartTruncate = require('../dist/smart-truncate.es5');

describe('smartTruncate(string, length[, position])', function() {
it('should return a smart truncated string w/ an ellipsis at the 4th index position of the given string', function() {
describe('smartTruncate(string, length[, position])', () => {
it('should return a smart truncated string w/ an ellipsis at the 4th index position of the given string', () => {
expect(smartTruncate('Steve Miller', 9, 4)).to.equal('Stev…ller');
});

it('should assert that the length of the truncated string is equal to the given length w/ an ellipsis at the 4th index position', function() {
it('should assert that the length of the truncated string is equal to the given length w/ an ellipsis at the 4th index position', () => {
const length = 9;
const truncated = smartTruncate('Steve Miller', length, 4);
expect(truncated.length).to.equal(length);
});

it('should append an ellipsis to the end of the truncated string (default) when given an undefined position', function() {
it('should append an ellipsis to the end of the truncated string (default) when given an undefined position', () => {
expect(smartTruncate('Steve Miller', 8)).to.equal('Steve M…');
});

it('should assert that the length of the truncated string is equal to the given length w/ an ellipsis at the default end of the string', function() {
it('should assert that the length of the truncated string is equal to the given length w/ an ellipsis at the default end of the string', () => {
const length = 8;
const truncated = smartTruncate('Steve Miller', length);
expect(truncated.length).to.equal(length);
});

it('should return a smart truncated string w/ an ellipsis at the 5th index position of the given string', function() {
it('should return a smart truncated string w/ an ellipsis at the 5th index position of the given string', () => {
expect(smartTruncate('Steve Miller', 9, 5)).to.equal('Steve…ler');
});

it('should return a smart truncated string w/ an ellipsis at the 8th index position of the given string', function() {
it('should return a smart truncated string w/ an ellipsis at the 8th index position of the given string', () => {
expect(smartTruncate('Not a good fit', 12, 8)).to.equal('Not a go…fit');
});

it('should return the given string when given a length that is larger than the given string', function() {
it('should return the given string when given a length that is larger than the given string', () => {
expect(smartTruncate('Steve Miller', 14)).to.equal('Steve Miller');
});

it('should return the given string when given a length that will end up replacing 1 char with an ellipsis (noop)', function() {
it('should return the given string when given a length that will end up replacing 1 char with an ellipsis (noop)', () => {
expect(smartTruncate('Steve Miller', 11)).to.equal('Steve Miller');
});

it('should append an ellipsis to the end of the truncated string when given a position that is larger than the given string and length', function() {
it('should append an ellipsis to the end of the truncated string when given a position that is larger than the given string and length', () => {
expect(smartTruncate('Steve Miller', 10, 14)).to.equal('Steve Mil…');
});

it('should return the given string when given an undefined length', function() {
it('should return the given string when given an undefined length', () => {
expect(smartTruncate('Steve Miller')).to.equal('Steve Miller');
});

it('should return the given string when given a String that has a length of less than the minimum 4 chars', function() {
it('should return the given string when given a String that has a length of less than the minimum 4 chars', () => {
expect(smartTruncate('Ste', 2)).to.equal('Ste');
});

it('should return the given string when given a String that has trailing whitespace and becomes trimmed to less than the minimum 4 chars in length', function() {
it('should return the given string when given a String that has trailing whitespace and becomes trimmed to less than the minimum 4 chars in length', () => {
expect(smartTruncate('Ste ', 2)).to.equal('Ste ');
});

it('should return the original, given value when given a non-String value', function() {
it('should return the original, given value when given a non-String value', () => {
expect(smartTruncate(1000, 3)).to.equal(1000);
expect(smartTruncate(undefined, 3)).to.equal(undefined);
expect(smartTruncate(null, 3)).to.equal(null);
});

// ref: https://github.com/millerized/smart-truncate/issues/7
it('should assert the correct index of the given position and length of truncated result', () => {
const str = 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz';
const length = 50;
const ellipsisOffset = 1;

for (var i = length; i > -1; i--) {
const expectedIndex = (i >= (length - ellipsisOffset))
? (length - 1)
: i;

const truncated = smartTruncate(str, length, i);
const resultIndex = truncated.indexOf('…');

expect(resultIndex).to.equal(expectedIndex);
expect(truncated.length).to.equal(length)
}
});
});

0 comments on commit 1c47900

Please sign in to comment.