Socket
Socket
Sign inDemoInstall

ellipsize

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ellipsize - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

.github/workflows/node.js.yml

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc