Skip to content

Commit

Permalink
moving string.js to more code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray committed Sep 19, 2023
1 parent cb2ed9b commit 8a41d6d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 15 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,24 +320,24 @@ 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))
* **[split](#split)** ([code](lib/string.js#L547) | [tests](test/string.js#L278))
* **[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)
Expand Down
12 changes: 6 additions & 6 deletions lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}
* <!-- results in: 'z b a b a b' -->
* ```
* @param {String} `str`
Expand Down Expand Up @@ -695,9 +695,9 @@ helpers.truncate = function(str, limit, suffix) {
*
* ```handlebars
* truncateWords("foo bar baz", 1);
* <!-- results in: 'foo' -->
* <!-- results in: 'foo...' -->
* truncateWords("foo bar baz", 2);
* <!-- results in: 'foo bar' -->
* <!-- results in: 'foo bar...' -->
* truncateWords("foo bar baz", 3);
* <!-- results in: 'foo bar baz' -->
* ```
Expand All @@ -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);
}

Expand Down
71 changes: 71 additions & 0 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}}');
});
});
});

0 comments on commit 8a41d6d

Please sign in to comment.