Comparing version
{ | ||
"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"); |
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
17125
8.47%0
-100%16
23.08%297
32.59%89
-11%1
Infinity%- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed