Skip to content

Commit ad9e0db

Browse files
committed
Merge pull request #65 from mallowigi/feature/join
feat(joinFilter): add a filter to join collections into a string
2 parents 52fab20 + d96807b commit ad9e0db

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- [fuzzyBy](#fuzzyby)
2727
- [groupBy](#groupby)
2828
- [isEmpty](#isempty)
29+
- [join] (#join)
2930
- [last](#last)
3031
- [map](#map)
3132
- [omit](#omit)
@@ -341,6 +342,25 @@ $scope.weirdArray = [[], 1, 2, 3, [4, 5, 6, [7, 8, 9, [10, 11, [12, [[[[[13], [[
341342
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
342343
```
343344
345+
### join
346+
Joins the contents of a collection into a string.<br/>
347+
By default, it will join elements with a *single space*, but you can provide your own delimiter.
348+
349+
**Usage:** ```collection | join:', '```
350+
351+
Example:
352+
353+
```js
354+
$scope.names = ['John', 'Sebastian', 'Will', 'James'];
355+
```
356+
357+
```html
358+
<p>{{ names | join:', ' }}</p>
359+
<!-- Will print "John, Sebastian, Will, James" -->
360+
361+
```
362+
363+
344364
###fuzzy
345365
fuzzy string searching(approximate string matching). [Read more](http://en.wikipedia.org/wiki/Approximate_string_matching)<br/>
346366
**note:** use fuzzyBy to filter by one property to improve performance<br/>

src/_filter/collection/join.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @ngdoc filter
3+
* @name join
4+
* @kind function
5+
*
6+
* @description
7+
* join a collection by a provided delimiter (space by default)
8+
*/
9+
angular.module('a8m.join', [])
10+
.filter('join', function () {
11+
'use strict';
12+
return function (input, delimiter) {
13+
if (isUndefined(input) || !isArray(input)) {
14+
return input;
15+
}
16+
if (!delimiter) {
17+
delimiter = ' ';
18+
}
19+
20+
return input.join(delimiter);
21+
};
22+
})
23+
;

src/filters.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ angular.module('angular.filter', [
5050
'a8m.first',
5151
'a8m.last',
5252
'a8m.flatten',
53+
'a8m.join',
5354

5455
'a8m.math',
5556
'a8m.math.max',
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict';
2+
3+
describe('isJoinFilter', function () {
4+
var joinFilter;
5+
6+
beforeEach(module('a8m.join'));
7+
8+
beforeEach(inject(function (_joinFilter_) {
9+
joinFilter = _joinFilter_;
10+
}));
11+
12+
describe('given a collection', function () {
13+
var arr;
14+
15+
describe('which is empty', function () {
16+
beforeEach(function () {
17+
arr = [];
18+
});
19+
20+
it('should return an empty string', function () {
21+
expect(joinFilter(arr)).toEqual('');
22+
});
23+
24+
});
25+
26+
describe('of strings', function () {
27+
beforeEach(function () {
28+
arr = ['hello', 'world'];
29+
});
30+
31+
describe('with no delimiter', function () {
32+
it('should join elements with a space', function () {
33+
expect(joinFilter(arr)).toEqual('hello world');
34+
});
35+
});
36+
37+
describe('with a custom delimiter', function () {
38+
var delim;
39+
40+
describe('which is not a string', function () {
41+
it('should join elements with a toString representation of the delimiter', function () {
42+
delim = true;
43+
expect(joinFilter(arr, delim)).toEqual('hellotrueworld');
44+
45+
delim = 10;
46+
expect(joinFilter(arr, delim)).toEqual('hello10world');
47+
48+
delim = {toString: function () { return ' - ' }}
49+
expect(joinFilter(arr, delim)).toEqual('hello - world');
50+
});
51+
});
52+
53+
it('should join elements with the given delimiter', function () {
54+
delim = ', '
55+
expect(joinFilter(arr, delim)).toEqual('hello, world');
56+
});
57+
});
58+
});
59+
60+
});
61+
62+
describe('given something that is not a collection', function () {
63+
var str, obj, bool, num;
64+
beforeEach(function () {
65+
str = 'string';
66+
obj = {'a': 'b'};
67+
bool = true;
68+
num = 5;
69+
});
70+
71+
it('should return the input as is', function () {
72+
expect(joinFilter(str)).toEqual(str);
73+
expect(joinFilter(obj)).toEqual(obj);
74+
expect(joinFilter(bool)).toEqual(bool);
75+
expect(joinFilter(num)).toEqual(num);
76+
});
77+
});
78+
});

0 commit comments

Comments
 (0)