diff --git a/.travis.yml b/.travis.yml
index efd82de1..ee8bff0b 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -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'
diff --git a/CHANGELOG b/CHANGELOG
index 15e5a1b7..04a1771e 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -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:
diff --git a/README.md b/README.md
index 7c708747..0ea898bd 100644
--- a/README.md
+++ b/README.md
@@ -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)
@@ -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}}
-truncate(sanitize("foo bar baz", 7));
+{{truncate (sanitize("foo bar baz", 7)}}
```
@@ -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}}
-truncateWords("foo bar baz", 2);
+{{truncateWords "foo bar baz" 2}}
-truncateWords("foo bar baz", 3);
+{{truncateWords "foo bar baz" 3}}
```
-### [{{upcase}}](lib/string.js#L742)
+### [{{upcase}}](lib/string.js#L746)
Uppercase all of the characters in the given string. Alias for [uppercase](#uppercase).
@@ -2938,7 +2938,7 @@ Uppercase all of the characters in the given string. Alias for [uppercase](#uppe
```
-### [{{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.
@@ -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**
@@ -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._
\ No newline at end of file
+_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on March 06, 2019._
\ No newline at end of file
diff --git a/appveyor.yml b/appveyor.yml
index ebbad0a7..e0321d2a 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -2,10 +2,13 @@
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:
@@ -13,15 +16,12 @@ install:
- 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
diff --git a/lib/string.js b/lib/string.js
index 5f079766..768d4c91 100644
--- a/lib/string.js
+++ b/lib/string.js
@@ -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}}
*
- * truncate(sanitize("foo bar baz", 7));
+ * {{truncate (sanitize("foo bar baz", 7)}}
*
* ```
* @param {String} `str`
@@ -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}}
*
- * truncateWords("foo bar baz", 2);
+ * {{truncateWords "foo bar baz" 2}}
*
- * truncateWords("foo bar baz", 3);
+ * {{truncateWords "foo bar baz" 3}}
*
* ```
* @param {String} `str`
@@ -710,6 +710,7 @@ helpers.truncate = function(str, limit, suffix) {
*/
helpers.truncateWords = function(str, count, suffix) {
+
if (util.isString(str) && isNumber(count)) {
if (typeof suffix !== 'string') {
suffix = '…';
@@ -717,12 +718,15 @@ helpers.truncateWords = function(str, count, 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;
}
};
diff --git a/package.json b/package.json
index b51f0d11..a70893f1 100644
--- a/package.json
+++ b/package.json
@@ -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": [
@@ -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": {
diff --git a/test/string.js b/test/string.js
index c18388d2..50723002 100644
--- a/test/string.js
+++ b/test/string.js
@@ -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}}');