Comparing version 0.0.3 to 0.1.0
20
index.js
@@ -11,17 +11,25 @@ 'use strict'; | ||
function ellipsize(str, max, ellipse, chars, truncate) { | ||
if (str.length < max) return str; | ||
var last = 0, | ||
c = ''; | ||
c = '', | ||
midMax = Math.floor(max / 2), | ||
computedMax = truncate === 'middle' ? midMax : max; | ||
if (str.length < max) return str; | ||
for (var i = 0, len = str.length; i < len; i++) { | ||
c = str.charAt(i); | ||
if (chars.indexOf(c) !== -1) { | ||
if (chars.indexOf(c) !== -1 && truncate !== 'middle') { | ||
last = i; | ||
} | ||
if (i < max) continue; | ||
if (i < computedMax) continue; | ||
if (last === 0) { | ||
return !truncate ? '' : str.substring(0, max - 1) + ellipse; | ||
return !truncate ? | ||
'' : | ||
str.substring(0, computedMax - 1) + ellipse + ( | ||
truncate === 'middle' ? | ||
str.substring(str.length - midMax, str.length) : | ||
'' | ||
); | ||
} | ||
@@ -28,0 +36,0 @@ |
{ | ||
"name": "ellipsize", | ||
"version": "0.0.3", | ||
"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": [ | ||
"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", | ||
@@ -20,13 +20,13 @@ "ellipsis", | ||
], | ||
"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" | ||
} | ||
"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" | ||
} | ||
} |
# ellipsize | ||
[![Build Status](https://drone.io/github.com/mvhenten/ellipsize/status.png)](https://drone.io/github.com/mvhenten/ellipsize/latest) | ||
[![Build Status](https://travis-ci.org/mvhenten/ellipsize.svg?branch=master)](https://travis-ci.org/mvhenten/ellipsize) | ||
@@ -5,0 +5,0 @@ Ellipsizes a string near a word boundary. |
@@ -103,2 +103,9 @@ 'use strict'; | ||
truncate: null | ||
}, | ||
{ | ||
label: 'truncate settings middle', | ||
len: 8, | ||
string: '123456789ABCDEF', | ||
expect: '123' + ELLIPSE + 'CDEF', | ||
truncate: 'middle' | ||
} | ||
@@ -116,2 +123,50 @@ ]; | ||
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' | ||
} | ||
]; | ||
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
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
203
13266
12