diff --git a/README.md b/README.md index cd290a4..9ff1857 100644 --- a/README.md +++ b/README.md @@ -320,12 +320,12 @@ Visit the: [code](lib/string.js) | [unit tests](test/string.js) | [issues](https * **[pascalcase](#pascalcase)** ([code](lib/string.js#L303) | [tests](test/string.js#L173)) * **[pathcase](#pathcase)** ([code](lib/string.js#L323) | [tests](test/string.js#L188)) * **[plusify](#plusify)** ([code](lib/string.js#L343) | [tests](test/string.js#L203)) -* **[prepend](#prepend)** ([code](lib/string.js#L363) | [no tests]) -* **[raw](#raw)** ([code](lib/string.js#L385) | [no tests]) -* **[remove](#remove)** ([code](lib/string.js#L413) | [no tests]) -* **[removeFirst](#removeFirst)** ([code](lib/string.js#L432) | [no tests]) +* **[prepend](#prepend)** ([code](lib/string.js#L363) | [tests](test/string.js#L409)) +* **[raw](#raw)** ([code](lib/string.js#L385) | [tests](test/string.js#L465)) +* **[remove](#remove)** ([code](lib/string.js#L413) | [tests](test/string.js#L416) +* **[removeFirst](#removeFirst)** ([code](lib/string.js#L432) | [tests](test/string.js#L422)) * **[replace](#replace)** ([code](lib/string.js#L452) | [tests](test/string.js#L222)) -* **[replaceFirst](#replaceFirst)** ([code](lib/string.js#L473) | [no tests]) +* **[replaceFirst](#replaceFirst)** ([code](lib/string.js#L473) | [tests](test/string.js#L428)) * **[reverse](#reverse)** ([code](lib/string.js#L492) | [tests](test/string.js#L241)) * **[sentence](#sentence)** ([code](lib/string.js#L509) | [tests](test/string.js#L252)) * **[snakecase](#snakecase)** ([code](lib/string.js#L528) | [tests](test/string.js#L263)) @@ -333,11 +333,11 @@ Visit the: [code](lib/string.js) | [unit tests](test/string.js) | [issues](https * **[startsWith](#startsWith)** ([code](lib/string.js#L572) | [tests](test/string.js#L293)) * **[titleize](#titleize)** ([code](lib/string.js#L596) | [tests](test/string.js#L312)) * **[trim](#trim)** ([code](lib/string.js#L623) | [tests](test/string.js#L323)) -* **[trimLeft](#trimLeft)** ([code](lib/string.js#L639) | [no tests]) -* **[trimRight](#trimRight)** ([code](lib/string.js#L657) | [no tests]) +* **[trimLeft](#trimLeft)** ([code](lib/string.js#L639) | [tests](test/string.js#L436)) +* **[trimRight](#trimRight)** ([code](lib/string.js#L657) | [tests](test/string.js#L441)) * **[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]) +* **[truncateWords](#truncateWords)** ([code](lib/string.js#L712) | [tests](test/string.js#L447))) +* **[upcase](#upcase)** ([code](lib/string.js#L742) | [tests](test/string.js#458)) * **[uppercase](#uppercase)** ([code](lib/string.js#L763) | [tests](test/string.js#L362)) ### [url helpers](#url) diff --git a/lib/string.js b/lib/string.js index 5f07976..1b78636 100644 --- a/lib/string.js +++ b/lib/string.js @@ -460,7 +460,7 @@ helpers.replace = function(str, a, b) { * Replace the first occurrence of substring `a` with substring `b`. * * ```handlebars - * {{replace "a b a b a b" "a" "z"}} + * {{replaceFirst "a b a b a b" "a" "z"}} * * ``` * @param {String} `str` @@ -695,9 +695,9 @@ helpers.truncate = function(str, limit, suffix) { * * ```handlebars * truncateWords("foo bar baz", 1); - * + * * truncateWords("foo bar baz", 2); - * + * * truncateWords("foo bar baz", 3); * * ``` @@ -712,12 +712,12 @@ helpers.truncate = function(str, limit, suffix) { helpers.truncateWords = function(str, count, suffix) { if (util.isString(str) && isNumber(count)) { if (typeof suffix !== 'string') { - suffix = '…'; + suffix = '...'; } var num = Number(count); - var arr = str.split(/[ \t]/); - if (num > arr.length) { + var arr = str.split(' '); + if (num < arr.length) { arr = arr.slice(0, num); } diff --git a/test/string.js b/test/string.js index 73030d8..039c26f 100644 --- a/test/string.js +++ b/test/string.js @@ -405,5 +405,76 @@ describe('string', function() { assert.equal(result, 'foo bar baz'); }); }); + + describe('prepend', function() { + it('should do prepend', function() { + var fn = hbs.compile('{{prepend "bar" "foo-"}}'); + assert.equal(fn(), 'foo-bar'); + }); + }); + + describe('remove', function() { + it('should remove characters', function() { + var fn = hbs.compile('{{remove "a b a b a b" "a "}}'); + assert.equal(fn(), 'b b b'); + }); + + it('should remove characters first', function() { + var fn = hbs.compile('{{removeFirst "a b a b a b" "a "}}'); + assert.equal(fn(), 'b a b a b'); + }); + }); + + describe('replaceFirst', function() { + it('should do replaceFirst', function() { + var fn = hbs.compile('{{replaceFirst "a b a b a b" "a" "z"}}'); + assert.equal(fn(), 'z b a b a b'); + }); + }); + + describe('trimLeft and trimRight', function() { + it('should do trimLeft', function() { + var fn = hbs.compile('{{trimLeft " ABC "}}'); + assert.equal(fn(), 'ABC '); + }); + + it('should do trimRight', function() { + var fn = hbs.compile('{{trimRight " ABC "}}'); + assert.equal(fn(), ' ABC'); + }); + }); + + describe('truncateWords', function() { + it('should do a truncate', function() { + var fn = hbs.compile('{{truncateWords "foo bar baz" 1}}'); + assert.equal(fn(), 'foo...'); + }); + it('should do a truncate', function() { + var fn = hbs.compile('{{truncateWords "foo bar baz" 3}}'); + assert.equal(fn(), 'foo bar baz...'); + }); + }); + + describe('upcase', function() { + it('should do upcase', function() { + var fn = hbs.compile('{{upcase "abc"}}'); + assert.equal(fn(), 'ABC'); + }); + }); + + describe('raw', function() { + it('should do raw', function() { + var str = {}; + str.fn = function() { + return '{{foo}}'; + }; + var result = helpers.string().raw(str); + assert.equal(result, '\\{{foo}}'); + }); + it('should do raw via compile', function() { + var fn = hbs.compile('{{{{raw}}}}{{foo}}{{{{/raw}}}}'); + assert.equal(fn(), '\\{{foo}}'); + }); + }); });