Skip to content

Fixes truncateWords helper #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ node_js:
- '5'
- '4'
- '0.12'
before_script:
- npm install -g gulp-cli
script: gulp
allowed_failures:
node_js: '0.12'
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v0.10.1:
date: "2019-03-06"
changes:
- fixes truncating in truncateWords helper.
v0.10.0:
date: "2017-11-17"
changes:
Expand Down
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,9 @@ Visit the: [code](lib/string.js) | [unit tests](test/string.js) | [issues](https
* **[trimLeft](#trimLeft)** ([code](lib/string.js#L639) | [no tests])
* **[trimRight](#trimRight)** ([code](lib/string.js#L657) | [no tests])
* **[truncate](#truncate)** ([code](lib/string.js#L680) | [tests](test/string.js#L338))
* **[truncateWords](#truncateWords)** ([code](lib/string.js#L712) | [no tests])
* **[upcase](#upcase)** ([code](lib/string.js#L742) | [no tests])
* **[uppercase](#uppercase)** ([code](lib/string.js#L763) | [tests](test/string.js#L362))
* **[truncateWords](#truncateWords)** ([code](lib/string.js#L712) | [tests](test/string.js#L362))
* **[upcase](#upcase)** ([code](lib/string.js#L746) | [no tests])
* **[uppercase](#uppercase)** ([code](lib/string.js#L767) | [tests](test/string.js#L386))

### [url helpers](#url)

Expand Down Expand Up @@ -2894,9 +2894,9 @@ Truncate a string to the specified `length`. Also see [ellipsis](#ellipsis).
**Example**

```handlebars
truncate("foo bar baz", 7);
{{truncate "foo bar baz" 7}}
<!-- results in: 'foo bar' -->
truncate(sanitize("<span>foo bar baz</span>", 7));
{{truncate (sanitize("<span>foo bar baz</span>", 7)}}
<!-- results in: 'foo bar' -->
```

Expand All @@ -2914,15 +2914,15 @@ Truncate a string to have the specified number of words. Also see [truncate](#tr
**Example**

```handlebars
truncateWords("foo bar baz", 1);
{{truncateWords "foo bar baz" 1}}
<!-- results in: 'foo…' -->
truncateWords("foo bar baz", 2);
{{truncateWords "foo bar baz" 2}}
<!-- results in: 'foo bar…' -->
truncateWords("foo bar baz", 3);
{{truncateWords "foo bar baz" 3}}
<!-- results in: 'foo bar baz' -->
```

### [{{upcase}}](lib/string.js#L742)
### [{{upcase}}](lib/string.js#L746)

Uppercase all of the characters in the given string. Alias for [uppercase](#uppercase).

Expand All @@ -2938,7 +2938,7 @@ Uppercase all of the characters in the given string. Alias for [uppercase](#uppe
<!-- results in: 'ABCDEF' -->
```

### [{{uppercase}}](lib/string.js#L763)
### [{{uppercase}}](lib/string.js#L767)

Uppercase all of the characters in the given string. If used as a block helper it will uppercase the entire block. This helper does not support inverse blocks.

Expand Down Expand Up @@ -3081,6 +3081,12 @@ Generate a random number

## History

## [v0.10.1](https://github.com/helpers/handlebars-helpers/compare/v0.10.0...v0.10.1) - 2019-03-06

**changes**

* fixes truncating in truncateWords helper.

## [v0.10.0](https://github.com/helpers/handlebars-helpers/compare/v0.9.0...v0.10.0) - 2017-11-17

**changes**
Expand Down Expand Up @@ -3339,10 +3345,10 @@ $ npm install && npm test

### License

Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
Copyright © 2019, [Jon Schlinkert](https://github.com/jonschlinkert).
When this project was created some helpers were sourced from [Swag, by Elving Rodriguez](http://elving.github.com/swag/).
Released under the [MIT License](LICENSE).

***

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on November 17, 2017._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on March 06, 2019._
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
environment:
matrix:
# node.js
- nodejs_version: "9.0"
- nodejs_version: "8.0"
- nodejs_version: "7.0"
- nodejs_version: "6.0"
- nodejs_version: "5.0"
- nodejs_version: "4.0"
- nodejs_version: "0.12"

# Install scripts. (runs after repo cloning)
install:
# Get the latest stable version of Node.js or io.js
- ps: Install-Product node $env:nodejs_version
# install modules
- npm install
- npm install -g gulp-cli

# Post-install test scripts.
test_script:
# Output useful info for debugging.
- node --version
- npm --version
# run tests
- gulp

# Don't actually build.
build: off
20 changes: 12 additions & 8 deletions lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,9 @@ helpers.trimRight = function(str) {
* Truncate a string to the specified `length`. Also see [ellipsis](#ellipsis).
*
* ```handlebars
* truncate("foo bar baz", 7);
* {{truncate "foo bar baz" 7}}
* <!-- results in: 'foo bar' -->
* truncate(sanitize("<span>foo bar baz</span>", 7));
* {{truncate (sanitize("<span>foo bar baz</span>", 7)}}
* <!-- results in: 'foo bar' -->
* ```
* @param {String} `str`
Expand Down Expand Up @@ -694,11 +694,11 @@ helpers.truncate = function(str, limit, suffix) {
* Also see [truncate](#truncate).
*
* ```handlebars
* truncateWords("foo bar baz", 1);
* {{truncateWords "foo bar baz" 1}}
* <!-- results in: 'foo…' -->
* truncateWords("foo bar baz", 2);
* {{truncateWords "foo bar baz" 2}}
* <!-- results in: 'foo bar…' -->
* truncateWords("foo bar baz", 3);
* {{truncateWords "foo bar baz" 3}}
* <!-- results in: 'foo bar baz' -->
* ```
* @param {String} `str`
Expand All @@ -710,19 +710,23 @@ helpers.truncate = function(str, limit, suffix) {
*/

helpers.truncateWords = function(str, count, suffix) {

if (util.isString(str) && isNumber(count)) {
if (typeof suffix !== 'string') {
suffix = '…';
}

var num = Number(count);
var arr = str.split(/[ \t]/);
if (num > arr.length) {
var val;
if (num < arr.length) {
arr = arr.slice(0, num);
val = arr.join(' ').trim() + suffix;
} else {
val = str;
}

var val = arr.join(' ').trim();
return val + suffix;
return val;
}
};

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "handlebars-helpers",
"description": "More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate, Verb, Ghost, gulp-handlebars, grunt-handlebars, consolidate, or any node.js/Handlebars project.",
"version": "0.10.0",
"version": "0.10.1",
"homepage": "https://github.com/helpers/handlebars-helpers",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"contributors": [
Expand Down Expand Up @@ -52,7 +52,8 @@
"Stephen Way (http://stephenway.net)",
"Thomas Jaggi (http://responsive.ch)",
"Tim Douglas (https://github.com/timdouglas)",
"(https://github.com/homersimpsons)"
"(https://github.com/homersimpsons)",
"Lauren Hamel (https://github.com/laurenhamel)"
],
"repository": "helpers/handlebars-helpers",
"bugs": {
Expand Down
28 changes: 26 additions & 2 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,41 @@ describe('string', function() {
var fn = hbs.compile('{{truncate "Bender should not be allowed on tv." 100}}');
assert.equal(fn(), 'Bender should not be allowed on tv.');
});
it('should return then string truncated by a specified length', function() {
it('should return the string truncated by a specified length.', function() {
var fn = hbs.compile('{{truncate "foo bar baz qux" 7}}...');
assert.equal(fn(), 'foo bar...');
});

it('should return then string truncated by a specified length, providing a custom string to denote an omission.', function() {
it('should return the string truncated by a specified length, providing a custom string to denote an omission.', function() {
var fn = hbs.compile('{{truncate "foo bar baz qux" 7 "…"}}');
assert.equal(fn(), 'foo ba…');
});
});

describe('truncateWords', function() {
it('should return an empty string if undefined', function() {
var fn = hbs.compile('{{truncateWords}}');
assert.equal(fn(), '');
});
it('should return the string truncated by a specified number of words.', function() {
var fn = hbs.compile('{{truncateWords "Bender should not be allowed on tv." 3}}');
assert.equal(fn(), 'Bender should not…');
});
it('should return the string if shorter than the specified number of words.', function() {
var fn = hbs.compile('{{truncateWords "Bender should not be allowed on tv." 100}}');
assert.equal(fn(), 'Bender should not be allowed on tv.');
});
it('should return the string truncated by a specified number of words with the custom suffix.', function() {
var fn = hbs.compile('{{truncateWords "foo bar baz qux" 3 ""}}');
assert.equal(fn(), 'foo bar baz');
});

it('should return the string truncated by a specified number of words with the custom suffix.', function() {
var fn = hbs.compile('{{truncateWords "foo bar baz qux" 2 " [see more]"}}');
assert.equal(fn(), 'foo bar [see more]');
});
});

describe('uppercase', function() {
it('should return an empty string if undefined', function() {
var fn = hbs.compile('{{uppercase}}');
Expand Down