ellipsize
Advanced tools
Comparing version 0.0.1 to 0.0.2
16
index.js
@@ -6,6 +6,7 @@ 'use strict'; | ||
chars: [' ', '-'], | ||
max: 140 | ||
max: 140, | ||
truncate: true | ||
}; | ||
function ellipsize(str, max, ellipse, chars) { | ||
function ellipsize(str, max, ellipse, chars, truncate) { | ||
var last = 0, | ||
@@ -24,3 +25,6 @@ c = ''; | ||
if (i < max) continue; | ||
if (last === 0) return str.substring(0, max - 1) + ellipse; | ||
if (last === 0) { | ||
return !truncate ? '' : str.substring(0, max - 1) + ellipse; | ||
} | ||
return str.substring(0, last) + ellipse; | ||
@@ -39,3 +43,5 @@ } | ||
for (var key in defaults) { | ||
opts[key] = opts[key] || defaults[key]; | ||
if (opts[key] === null || typeof opts[key] === 'undefined') { | ||
opts[key] = defaults[key]; | ||
} | ||
} | ||
@@ -45,3 +51,3 @@ | ||
return ellipsize(str, opts.max, opts.ellipse, opts.chars); | ||
return ellipsize(str, opts.max, opts.ellipse, opts.chars, opts.truncate); | ||
}; |
{ | ||
"name": "ellipsize", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Ellipsizes a string at the nearest whitespace character near the end of allowed length", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -23,3 +23,4 @@ # ellipsize | ||
It simply loops over all the characters using a single function call, storing the | ||
last location of an allowed break point, if any. Otherwise just truncates the string. | ||
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). | ||
@@ -58,7 +59,39 @@ ## Examples | ||
Also you may provide a setting to `truncate` words: | ||
```javascript | ||
var ellipsize = require('ellipsize'); | ||
ellipsize( '123456789ABCDEF', 8, { truncate: false }); | ||
// '' | ||
// its default settings | ||
ellipsize( '123456789ABCDEF', 8, { truncate: true }); | ||
// '1234567…' | ||
``` | ||
## Copyright | ||
Copyright 2014 Matthijs van Henten. | ||
Released under the GPLv3. | ||
The MIT License (MIT) | ||
Copyright (c) 2014 Matthijs van Henten | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
@@ -73,2 +73,44 @@ 'use strict'; | ||
}); | ||
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 | ||
} | ||
]; | ||
cases.forEach(function(testCase) { | ||
var result = ellipsize(testCase.string, testCase.len, { | ||
truncate: testCase.truncate | ||
}); | ||
assert.equal(result, testCase.expect); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Copyleft License
License(Experimental) Copyleft license information was found
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found
Found 1 instance in 1 package
9
142
96
11021