Skip to content

Commit 70381d3

Browse files
Merge pull request #297 from syedfaizan/master
added unique helper
2 parents 268e800 + 03a930c commit 70381d3

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ Visit the: [code](lib/array.js) | [unit tests](test/array.js) | [issues](https:/
138138
* **[withGroup](#withGroup)** ([code](lib/array.js#L692) | [tests](test/array.js#L418))
139139
* **[withLast](#withLast)** ([code](lib/array.js#L727) | [tests](test/array.js#L428))
140140
* **[withSort](#withSort)** ([code](lib/array.js#L766) | [tests](test/array.js#L442))
141+
* **[unique](#unique)** ([code](lib/array.js#L801) | [tests](test/array.js#L483))
141142

142143
### [code helpers](#code)
143144

@@ -927,6 +928,25 @@ Block helper that sorts a collection and exposes the sorted collection as contex
927928

928929
## code
929930

931+
### [{{unique}}](lib/array.js#L801)
932+
933+
Block helper that return an array with all duplicate values removed. Best used along with a #each helper
934+
935+
**Params**
936+
937+
* `array` **{Array}**
938+
* `returns` **{Array}**: Array with No duplicate values
939+
940+
**Example**
941+
942+
```handlebars
943+
<!-- array: ['a', 'a', 'c', 'b', 'e', 'e'] -->
944+
{{#each (unique array)}}{{this}}{{/each}}
945+
<!-- results in: 'acbe' -->
946+
```
947+
948+
## code
949+
930950
### [{{embed}}](lib/code.js#L24)
931951

932952
Embed code from an external file as preformatted text.

lib/array.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,3 +797,13 @@ helpers.withSort = function(array, prop, options) {
797797
}
798798
return result;
799799
};
800+
801+
helpers.unique = function(array, options) {
802+
if (util.isUndefined(array)) return '';
803+
804+
return array.filter(function(item, index, self) {
805+
return self.indexOf(item) === index;
806+
});
807+
808+
};
809+

test/array.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var helpers = require('..');
77
helpers.array({handlebars: hbs});
88
helpers.string({handlebars: hbs});
99

10-
var context = {array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']};
10+
var context = {array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], duplicate: [ 'a', 'b', 'b', 'c', 'd', 'b', 'f', 'a', 'g']};
1111

1212
describe('array', function() {
1313
describe('after', function() {
@@ -479,4 +479,16 @@ describe('array', function() {
479479
assert.equal(res, 'f: 8021 <br>b: 239 <br>d: -12 <br>');
480480
});
481481
});
482+
483+
describe('unique', function() {
484+
it('should return empty string when the array is null', function() {
485+
var fn = hbs.compile('{{#unique}}{{this}}{{/unique}}');
486+
assert.equal(fn(context), '');
487+
});
488+
it('should return array with unique items', function() {
489+
var fn = hbs.compile('{{#unique duplicate}}{{this}}{{/unique}}');
490+
assert.equal(fn(context).toString(), 'a,b,c,d,f,g');
491+
});
492+
});
493+
482494
});

0 commit comments

Comments
 (0)