Comparing version 0.2.0 to 0.3.0
{ | ||
"name": "ellipsize", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Ellipsizes a string at the nearest whitespace character near the end of allowed length", | ||
"main": "index.js", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "tape test/*", | ||
"beautify": "js-beautify -r -f test/index.js -f index.js" | ||
"test": "eslint && prettier -c **/*.js && tape test/*" | ||
}, | ||
@@ -28,9 +27,6 @@ "repository": { | ||
"devDependencies": { | ||
"js-beautify": "~1.4.2", | ||
"jshint": "^2.13.0", | ||
"tape": "^4.9.0" | ||
}, | ||
"dependencies": { | ||
"tape": "^4.9.0" | ||
"eslint": "^8.7.0", | ||
"prettier": "^2.5.1", | ||
"tape": "^5.4.1" | ||
} | ||
} |
@@ -11,21 +11,4 @@ # ellipsize | ||
## 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 | ||
Off by one errors. | ||
I've written a couple of ellipsize functions, and got it wrong on edge cases | ||
several times. It's not rocket science, but something as simple as this you should | ||
write in five minutes right? Never mind the unit test. | ||
This ellipsize function is robust and tested against a couple of edge cases. | ||
It's written to be fast, work in any browser and have no dependencies at all. | ||
It simply loops over all the characters using a single function call, storing the | ||
last location of an allowed break point, if any. Otherwise it just truncates the string | ||
or return empty string if `truncate` options set up to `false` (in some cases its just better). | ||
## Examples | ||
@@ -68,5 +51,11 @@ | ||
// only ellipsize if word boundarys found | ||
ellipsize( '123456789ABCDEF', 8, { truncate: false }); | ||
// '' | ||
// ellipsize in the middle | ||
ellipsize( '1234…CDEF', 9, { truncate: "middle" }); | ||
// '1234…CDEF' | ||
// its default settings | ||
@@ -73,0 +62,0 @@ ellipsize( '123456789ABCDEF', 8, { truncate: true }); |
@@ -1,59 +0,85 @@ | ||
'use strict'; | ||
"use strict"; | ||
var test = require('tape'); | ||
var ellipsize = require('../index'); | ||
var test = require("tape"); | ||
var ellipsize = require("../src/index"); | ||
var ELLIPSE = '…'; | ||
var ELLIPSE = "…"; | ||
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 (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: "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: "multibyte characters", | ||
len: 5, | ||
string: "审核未通过", | ||
expect: "审核未通过", | ||
}, | ||
{ | ||
label: "multibyte characters ellipsised", | ||
len: 5, | ||
string: "审核未通过过", | ||
expect: "审核未通" + ELLIPSE, | ||
}, | ||
{ | ||
label: "length has a default", | ||
len: undefined, | ||
string: "xia3blpfgw9skc40k8k8808cw0cwk4wg88c4cwcokw88ggss40wo080so044og00gc4o40s88sowk8k4k0sswg0k84gws4ksg8so44gwcg0gkcwgc0wwcog08cwc0ogogsgkgcccko48w", | ||
expect: | ||
"xia3blpfgw9skc40k8k8808cw0cwk4wg88c4cwcokw88ggss40wo080so044og00gc4o40s88sowk8k4k0sswg0k84gws4ksg8so44gwcg0gkcwgc0wwcog08cwc0ogogsgkgcccko48w".slice( | ||
0, | ||
139 | ||
) + 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) { | ||
cases.forEach(function (testCase) { | ||
var result = ellipsize(testCase.string, testCase.len); | ||
assert.equal(result, testCase.expect); | ||
assert.equal(result, testCase.expect, testCase.label); | ||
assert.ok(result.length <= testCase.len || 140); | ||
@@ -65,88 +91,74 @@ }); | ||
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' | ||
}]; | ||
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: "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 + "DEF", | ||
truncate: "middle", | ||
}, | ||
]; | ||
cases.forEach(function(testCase) { | ||
cases.forEach(function (testCase) { | ||
var result = ellipsize(testCase.string, testCase.len, { | ||
truncate: testCase.truncate | ||
truncate: testCase.truncate, | ||
}); | ||
assert.equal(result, testCase.expect); | ||
assert.equal(result, testCase.expect, testCase.label); | ||
}); | ||
assert.end(); | ||
}); | ||
test('ellipsize truncate words', function(assert) { | ||
test("ellipsize truncate middle", 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', | ||
label: "truncate words settings middle short", | ||
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' | ||
} | ||
string: "the quick brown fox", | ||
expect: "the" + ELLIPSE + " fox", | ||
truncate: "middle", | ||
}, | ||
{ | ||
label: "truncate words settings middle longer", | ||
len: 37, | ||
string: "These are a few of my favourite things", | ||
expect: "These are a few" + ELLIPSE + " favourite things", | ||
truncate: "middle", | ||
}, | ||
]; | ||
cases.forEach(function(testCase) { | ||
cases.forEach(function (testCase) { | ||
var result = ellipsize(testCase.string, testCase.len, { | ||
truncate: testCase.truncate | ||
truncate: testCase.truncate, | ||
}); | ||
assert.equal(result, testCase.expect); | ||
assert.equal(result, testCase.expect, testCase.label); | ||
}); | ||
@@ -157,35 +169,38 @@ | ||
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: "--", | ||
}]; | ||
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; | ||
cases.forEach(function (testCase) { | ||
const { len, string, expect, ellipse } = testCase; | ||
const result = ellipsize(string, len, { | ||
ellipse | ||
ellipse, | ||
}); | ||
@@ -192,0 +207,0 @@ assert.equal(result, expect, "ellipsized as expected"); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
17125
0
16
297
89
1
- Removedtape@^4.9.0
- Removed@ljharb/resumer@0.0.1(transitive)
- Removed@ljharb/through@2.3.13(transitive)
- Removedarray-buffer-byte-length@1.0.1(transitive)
- Removedarraybuffer.prototype.slice@1.0.3(transitive)
- Removedavailable-typed-arrays@1.0.7(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removeddata-view-buffer@1.0.1(transitive)
- Removeddata-view-byte-length@1.0.1(transitive)
- Removeddata-view-byte-offset@1.0.0(transitive)
- Removeddeep-equal@1.1.2(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removeddefined@1.0.1(transitive)
- Removeddotignore@0.1.2(transitive)
- Removedes-abstract@1.23.3(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-object-atoms@1.0.0(transitive)
- Removedes-set-tostringtag@2.0.3(transitive)
- Removedes-to-primitive@1.2.1(transitive)
- Removedfor-each@0.3.3(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedfunction.prototype.name@1.1.6(transitive)
- Removedfunctions-have-names@1.2.3(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedget-symbol-description@1.0.2(transitive)
- Removedglob@7.2.3(transitive)
- Removedglobalthis@1.0.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas@1.0.4(transitive)
- Removedhas-bigints@1.0.2(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedinternal-slot@1.0.7(transitive)
- Removedis-arguments@1.1.1(transitive)
- Removedis-array-buffer@3.0.4(transitive)
- Removedis-bigint@1.0.4(transitive)
- Removedis-boolean-object@1.1.2(transitive)
- Removedis-callable@1.2.7(transitive)
- Removedis-core-module@2.15.1(transitive)
- Removedis-data-view@1.0.1(transitive)
- Removedis-date-object@1.0.5(transitive)
- Removedis-negative-zero@2.0.3(transitive)
- Removedis-number-object@1.0.7(transitive)
- Removedis-regex@1.1.4(transitive)
- Removedis-shared-array-buffer@1.0.3(transitive)
- Removedis-string@1.0.7(transitive)
- Removedis-symbol@1.0.4(transitive)
- Removedis-typed-array@1.1.13(transitive)
- Removedis-weakref@1.0.2(transitive)
- Removedisarray@2.0.5(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmock-property@1.0.3(transitive)
- Removedobject-inspect@1.12.31.13.2(transitive)
- Removedobject-is@1.1.6(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedobject.assign@4.1.5(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedpossible-typed-array-names@1.0.0(transitive)
- Removedregexp.prototype.flags@1.5.3(transitive)
- Removedresolve@1.22.8(transitive)
- Removedsafe-array-concat@1.1.2(transitive)
- Removedsafe-regex-test@1.0.3(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedset-function-name@2.0.2(transitive)
- Removedside-channel@1.0.6(transitive)
- Removedstring.prototype.trim@1.2.9(transitive)
- Removedstring.prototype.trimend@1.0.8(transitive)
- Removedstring.prototype.trimstart@1.0.8(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)
- Removedtape@4.17.0(transitive)
- Removedtyped-array-buffer@1.0.2(transitive)
- Removedtyped-array-byte-length@1.0.1(transitive)
- Removedtyped-array-byte-offset@1.0.2(transitive)
- Removedtyped-array-length@1.0.6(transitive)
- Removedunbox-primitive@1.0.2(transitive)
- Removedwhich-boxed-primitive@1.0.2(transitive)
- Removedwhich-typed-array@1.1.15(transitive)
- Removedwrappy@1.0.2(transitive)