Skip to content

Commit 982f460

Browse files
author
eliorb
committed
feat(joinFilter): add a filter to join collections into a string
1 parent 6879c65 commit 982f460

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

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+
;
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)