Comparing version 0.1.0 to 0.2.0
12
index.js
@@ -16,3 +16,3 @@ 'use strict'; | ||
midMax = Math.floor(max / 2), | ||
computedMax = truncate === 'middle' ? midMax : max; | ||
computedMax = truncate === 'middle' ? midMax : max - ellipse.length; | ||
@@ -28,9 +28,9 @@ for (var i = 0, len = str.length; i < len; i++) { | ||
if (last === 0) { | ||
return !truncate ? | ||
'' : | ||
return !truncate ? | ||
'' : | ||
str.substring(0, computedMax - 1) + ellipse + ( | ||
truncate === 'middle' ? | ||
str.substring(str.length - midMax, str.length) : | ||
truncate === 'middle' ? | ||
str.substring(str.length - midMax, str.length) : | ||
'' | ||
); | ||
); | ||
} | ||
@@ -37,0 +37,0 @@ |
{ | ||
"name": "ellipsize", | ||
"version": "0.1.0", | ||
"description": "Ellipsizes a string at the nearest whitespace character near the end of allowed length", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/mocha --recursive -u tdd -R spec test/*" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:mvhenten/ellipsize.git" | ||
}, | ||
"keywords": [ | ||
"ellipsize", | ||
"ellipsis", | ||
"truncate", | ||
"shorten", | ||
"string" | ||
], | ||
"author": "Matthijs van Henten", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mvhenten/ellipsize/issues" | ||
}, | ||
"homepage": "https://github.com/mvhenten/ellipsize", | ||
"devDependencies": { | ||
"mocha": "~1.17.1", | ||
"js-beautify": "~1.4.2", | ||
"jshint": "~2.4.4" | ||
} | ||
"name": "ellipsize", | ||
"version": "0.2.0", | ||
"description": "Ellipsizes a string at the nearest whitespace character near the end of allowed length", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tape test/*", | ||
"beautify": "js-beautify -r -f test/index.js -f index.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:mvhenten/ellipsize.git" | ||
}, | ||
"keywords": [ | ||
"ellipsize", | ||
"ellipsis", | ||
"truncate", | ||
"shorten", | ||
"string" | ||
], | ||
"author": "Matthijs van Henten", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mvhenten/ellipsize/issues" | ||
}, | ||
"homepage": "https://github.com/mvhenten/ellipsize", | ||
"devDependencies": { | ||
"js-beautify": "~1.4.2", | ||
"jshint": "^2.13.0", | ||
"tape": "^4.9.0" | ||
}, | ||
"dependencies": { | ||
"tape": "^4.9.0" | ||
} | ||
} |
# ellipsize | ||
[![Build Status](https://travis-ci.org/mvhenten/ellipsize.svg?branch=master)](https://travis-ci.org/mvhenten/ellipsize) | ||
[![Node.js CI](https://github.com/mvhenten/ellipsize/actions/workflows/node.js.yml/badge.svg)](https://github.com/mvhenten/ellipsize/actions/workflows/node.js.yml) | ||
@@ -11,2 +11,6 @@ Ellipsizes a string near a word boundary. | ||
## N.B. Major version change | ||
As of 1.x, ellipsize honors the max length including the ellipsize char. This means you get exactly _n_ characters, including the ellipse. | ||
## Why make a module for something sooo simple | ||
@@ -13,0 +17,0 @@ |
'use strict'; | ||
var assert = require('assert'), | ||
ellipsize = require('../index'); | ||
var test = require('tape'); | ||
var ellipsize = require('../index'); | ||
var ELLIPSE = '…'; | ||
suite('ellipsize', function() { | ||
test('ellipsize simple cases', function(assert) { | ||
var cases = [{ | ||
label: 'zero length string', | ||
len: 100, | ||
string: '', | ||
expect: '' | ||
}, { | ||
label: 'simple string', | ||
len: 8, | ||
string: 'one two three four', | ||
expect: 'one two' + ELLIPSE | ||
}, { | ||
label: 'long string gets truncated', | ||
len: 8, | ||
string: '12345678910', | ||
expect: '123456' + ELLIPSE | ||
}, { | ||
label: 'dashes are also a "word boundary"', | ||
len: 8, | ||
string: 'one two-three four', | ||
expect: 'one two' + ELLIPSE | ||
}, { | ||
label: 'dont ellipsize short strings', | ||
len: 100, | ||
string: 'one two three four', | ||
expect: 'one two three four' | ||
}, { | ||
label: 'length has a silly default', | ||
len: undefined, | ||
string: 'xia3blpfgw9skc40k8k8808cw0cwk4wg88c4cwcokw88ggss40wo080so044og00gc4o40s88sowk8k4k0sswg0k84gws4ksg8so44gwcg0gkcwgc0wwcog08cwc0ogogsgkgcccko48w', | ||
expect: 'xia3blpfgw9skc40k8k8808cw0cwk4wg88c4cwcokw88ggss40wo080so044og00gc4o40s88sowk8k4k0sswg0k84gws4ksg8so44gwcg0gkcwgc0wwcog08cwc0ogogsgkgcccko' + ELLIPSE | ||
}, { | ||
label: 'zero length returns an empty string', | ||
len: 0, | ||
string: 'gc4o40s88sowk8k4k0ssw', | ||
expect: '' | ||
}, { | ||
label: 'bogus input', | ||
len: 0, | ||
string: null, | ||
expect: '' | ||
}, { | ||
label: 'bogus input', | ||
len: 0, | ||
string: undefined, | ||
expect: '' | ||
}, ]; | ||
test('ellipsize simple cases', function() { | ||
var cases = [ | ||
{ | ||
label: 'zero length string', | ||
len: 100, | ||
string: '', | ||
expect: '' | ||
}, | ||
{ | ||
label: 'simple string', | ||
len: 8, | ||
string: 'one two three four', | ||
expect: 'one two' + ELLIPSE | ||
}, | ||
{ | ||
label: 'long string gets truncated', | ||
len: 8, | ||
string: '12345678910', | ||
expect: '1234567' + ELLIPSE | ||
}, | ||
{ | ||
label: 'dashes are also a "word boundary"', | ||
len: 8, | ||
string: 'one two-three four', | ||
expect: 'one two' + ELLIPSE | ||
}, | ||
{ | ||
label: 'dont ellipsize short strings', | ||
len: 100, | ||
string: 'one two three four', | ||
expect: 'one two three four' | ||
}, | ||
{ | ||
label: 'length has a silly default', | ||
len: undefined, | ||
string: 'xia3blpfgw9skc40k8k8808cw0cwk4wg88c4cwcokw88ggss40wo080so044og00gc4o40s88sowk8k4k0sswg0k84gws4ksg8so44gwcg0gkcwgc0wwcog08cwc0ogogsgkgcccko48w', | ||
expect: 'xia3blpfgw9skc40k8k8808cw0cwk4wg88c4cwcokw88ggss40wo080so044og00gc4o40s88sowk8k4k0sswg0k84gws4ksg8so44gwcg0gkcwgc0wwcog08cwc0ogogsgkgcccko4' + ELLIPSE | ||
}, | ||
{ | ||
label: 'zero length returns an empty string', | ||
len: 0, | ||
string: 'gc4o40s88sowk8k4k0ssw', | ||
expect: '' | ||
}, | ||
{ | ||
label: 'bogus input', | ||
len: 0, | ||
string: null, | ||
expect: '' | ||
}, | ||
{ | ||
label: 'bogus input', | ||
len: 0, | ||
string: undefined, | ||
expect: '' | ||
}, | ||
]; | ||
cases.forEach(function(testCase) { | ||
var result = ellipsize(testCase.string, testCase.len); | ||
assert.equal(result, testCase.expect); | ||
assert.ok(result.length <= testCase.len || 140); | ||
}); | ||
cases.forEach(function(testCase) { | ||
var result = ellipsize(testCase.string, testCase.len); | ||
assert.equal(result, testCase.expect); | ||
assert.end(); | ||
}); | ||
test('ellipsize truncate settings', function(assert) { | ||
var cases = [{ | ||
label: 'truncate settings off', | ||
len: 8, | ||
string: '123456789ABCDEF', | ||
expect: '', | ||
truncate: false | ||
}, { | ||
label: 'truncate settings on', | ||
len: 8, | ||
string: '123456789ABCDEF', | ||
expect: '123456' + ELLIPSE, | ||
truncate: true | ||
}, { | ||
label: 'truncate settings default', | ||
len: 8, | ||
string: '123456789ABCDEF', | ||
expect: '123456' + ELLIPSE, | ||
truncate: undefined | ||
}, { | ||
label: 'truncate settings default', | ||
len: 8, | ||
string: '123456789ABCDEF', | ||
expect: '123456' + ELLIPSE, | ||
truncate: null | ||
}, { | ||
label: 'truncate settings middle', | ||
len: 8, | ||
string: '123456789ABCDEF', | ||
expect: '123' + ELLIPSE + 'CDEF', | ||
truncate: 'middle' | ||
}]; | ||
cases.forEach(function(testCase) { | ||
var result = ellipsize(testCase.string, testCase.len, { | ||
truncate: testCase.truncate | ||
}); | ||
assert.equal(result, testCase.expect); | ||
}); | ||
test('ellipsize truncate settings', function() { | ||
var cases = [ | ||
{ | ||
label: 'truncate settings off', | ||
len: 8, | ||
string: '123456789ABCDEF', | ||
expect: '', | ||
truncate: false | ||
}, | ||
{ | ||
label: 'truncate settings on', | ||
len: 8, | ||
string: '123456789ABCDEF', | ||
expect: '1234567' + ELLIPSE, | ||
truncate: true | ||
}, | ||
{ | ||
label: 'truncate settings default', | ||
len: 8, | ||
string: '123456789ABCDEF', | ||
expect: '1234567' + ELLIPSE, | ||
truncate: undefined | ||
}, | ||
{ | ||
label: 'truncate settings default', | ||
len: 8, | ||
string: '123456789ABCDEF', | ||
expect: '1234567' + ELLIPSE, | ||
truncate: null | ||
}, | ||
{ | ||
label: 'truncate settings middle', | ||
len: 8, | ||
string: '123456789ABCDEF', | ||
expect: '123' + ELLIPSE + 'CDEF', | ||
truncate: 'middle' | ||
} | ||
]; | ||
assert.end(); | ||
cases.forEach(function(testCase) { | ||
var result = ellipsize(testCase.string, testCase.len, { | ||
truncate: testCase.truncate | ||
}); | ||
assert.equal(result, testCase.expect); | ||
}); | ||
test('ellipsize truncate words', function(assert) { | ||
var cases = [ | ||
// XXX I'm unsure what the expected behavior should actually be, here | ||
// { | ||
// label: 'truncate words settings off', | ||
// len: 12, | ||
// string: 'the quick brown fox', | ||
// expect: '', | ||
// truncate: false | ||
// }, | ||
{ | ||
label: 'truncate words settings on', | ||
len: 16, | ||
string: 'the quick brown box', | ||
expect: 'the quick brown' + ELLIPSE, | ||
truncate: true | ||
}, { | ||
label: 'truncate words settings default', | ||
len: 16, | ||
string: 'the quick brown fox', | ||
expect: 'the quick brown' + ELLIPSE, | ||
truncate: undefined | ||
}, { | ||
label: 'truncate word settings default', | ||
len: 16, | ||
string: 'the quick brown fox', | ||
expect: 'the quick brown' + ELLIPSE, | ||
truncate: null | ||
}, { | ||
label: 'truncate words settings middle', | ||
len: 16, | ||
string: 'the quick brown fox', | ||
expect: 'the qui' + ELLIPSE + 'rown fox', | ||
truncate: 'middle' | ||
} | ||
]; | ||
cases.forEach(function(testCase) { | ||
var result = ellipsize(testCase.string, testCase.len, { | ||
truncate: testCase.truncate | ||
}); | ||
assert.equal(result, testCase.expect); | ||
}); | ||
test('ellipsize truncate words', function() { | ||
var cases = [ | ||
// XXX I'm unsure what the expected behavior should actually be, here | ||
// { | ||
// label: 'truncate words settings off', | ||
// len: 12, | ||
// string: 'the quick brown fox', | ||
// expect: '', | ||
// truncate: false | ||
// }, | ||
{ | ||
label: 'truncate words settings on', | ||
len: 16, | ||
string: 'the quick brown box', | ||
expect: 'the quick brown' + ELLIPSE, | ||
truncate: true | ||
}, | ||
{ | ||
label: 'truncate words settings default', | ||
len: 16, | ||
string: 'the quick brown fox', | ||
expect: 'the quick brown' + ELLIPSE, | ||
truncate: undefined | ||
}, | ||
{ | ||
label: 'truncate word settings default', | ||
len: 16, | ||
string: 'the quick brown fox', | ||
expect: 'the quick brown' + ELLIPSE, | ||
truncate: null | ||
}, | ||
{ | ||
label: 'truncate words settings middle', | ||
len: 16, | ||
string: 'the quick brown fox', | ||
expect: 'the qui' + ELLIPSE + 'rown fox', | ||
truncate: 'middle' | ||
} | ||
]; | ||
assert.end(); | ||
}); | ||
cases.forEach(function(testCase) { | ||
var result = ellipsize(testCase.string, testCase.len, { | ||
truncate: testCase.truncate | ||
}); | ||
assert.equal(result, testCase.expect); | ||
test('ellipsize custom ellipsize', function(assert) { | ||
var cases = [{ | ||
label: 'zero length string', | ||
len: 100, | ||
string: '', | ||
expect: '', | ||
ellipse: "--", | ||
}, { | ||
label: 'two character ellipse', | ||
len: 9, | ||
string: 'one two three four', | ||
expect: 'one two--', | ||
ellipse: "--", | ||
}, { | ||
label: 'unicode character ellipse', | ||
len: 8, | ||
string: 'one two three four', | ||
expect: 'one two☃', | ||
ellipse: "☃", | ||
}, { | ||
label: 'off by one string', | ||
len: 8, | ||
string: 'one two three four', | ||
expect: 'one--', | ||
ellipse: "--", | ||
}]; | ||
cases.forEach(function(testCase) { | ||
const { | ||
len, string, expect, ellipse | ||
} = testCase; | ||
const result = ellipsize(string, len, { | ||
ellipse | ||
}); | ||
assert.equal(result, expect, "ellipsized as expected"); | ||
assert.ok(result.length <= len, "length does not exceed maxLen"); | ||
}); | ||
}); | ||
assert.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15788
13
224
100
1
+ Addedtape@^4.9.0
+ Added@ljharb/resumer@0.0.1(transitive)
+ Added@ljharb/through@2.3.13(transitive)
+ Addedarray-buffer-byte-length@1.0.1(transitive)
+ Addedarraybuffer.prototype.slice@1.0.3(transitive)
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addeddata-view-buffer@1.0.1(transitive)
+ Addeddata-view-byte-length@1.0.1(transitive)
+ Addeddata-view-byte-offset@1.0.0(transitive)
+ Addeddeep-equal@1.1.2(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addeddefined@1.0.1(transitive)
+ Addeddotignore@0.1.2(transitive)
+ Addedes-abstract@1.23.3(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.0.0(transitive)
+ Addedes-set-tostringtag@2.0.3(transitive)
+ Addedes-to-primitive@1.2.1(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedfunction.prototype.name@1.1.6(transitive)
+ Addedfunctions-have-names@1.2.3(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedget-symbol-description@1.0.2(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedglobalthis@1.0.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas@1.0.4(transitive)
+ Addedhas-bigints@1.0.2(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedinternal-slot@1.0.7(transitive)
+ Addedis-arguments@1.1.1(transitive)
+ Addedis-array-buffer@3.0.4(transitive)
+ Addedis-bigint@1.0.4(transitive)
+ Addedis-boolean-object@1.1.2(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedis-data-view@1.0.1(transitive)
+ Addedis-date-object@1.0.5(transitive)
+ Addedis-negative-zero@2.0.3(transitive)
+ Addedis-number-object@1.0.7(transitive)
+ Addedis-regex@1.1.4(transitive)
+ Addedis-shared-array-buffer@1.0.3(transitive)
+ Addedis-string@1.0.7(transitive)
+ Addedis-symbol@1.0.4(transitive)
+ Addedis-typed-array@1.1.13(transitive)
+ Addedis-weakref@1.0.2(transitive)
+ Addedisarray@2.0.5(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmock-property@1.0.3(transitive)
+ Addedobject-inspect@1.12.31.13.2(transitive)
+ Addedobject-is@1.1.6(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.5(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedregexp.prototype.flags@1.5.3(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedsafe-array-concat@1.1.2(transitive)
+ Addedsafe-regex-test@1.0.3(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedset-function-name@2.0.2(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedstring.prototype.trim@1.2.9(transitive)
+ Addedstring.prototype.trimend@1.0.8(transitive)
+ Addedstring.prototype.trimstart@1.0.8(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedtape@4.17.0(transitive)
+ Addedtyped-array-buffer@1.0.2(transitive)
+ Addedtyped-array-byte-length@1.0.1(transitive)
+ Addedtyped-array-byte-offset@1.0.2(transitive)
+ Addedtyped-array-length@1.0.6(transitive)
+ Addedunbox-primitive@1.0.2(transitive)
+ Addedwhich-boxed-primitive@1.0.2(transitive)
+ Addedwhich-typed-array@1.1.15(transitive)
+ Addedwrappy@1.0.2(transitive)