Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- [fuzzyBy](#fuzzyby)
- [groupBy](#groupby)
- [isEmpty](#isempty)
- [join] (#join)
- [last](#last)
- [map](#map)
- [omit](#omit)
Expand Down Expand Up @@ -341,6 +342,25 @@ $scope.weirdArray = [[], 1, 2, 3, [4, 5, 6, [7, 8, 9, [10, 11, [12, [[[[[13], [[
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
```

### join
Joins the contents of a collection into a string.<br/>
By default, it will join elements with a *single space*, but you can provide your own delimiter.

**Usage:** ```collection | join:', '```

Example:

```js
$scope.names = ['John', 'Sebastian', 'Will', 'James'];
```

```html
<p>{{ names | join:', ' }}</p>
<!-- Will print "John, Sebastian, Will, James" -->

```


###fuzzy
fuzzy string searching(approximate string matching). [Read more](http://en.wikipedia.org/wiki/Approximate_string_matching)<br/>
**note:** use fuzzyBy to filter by one property to improve performance<br/>
Expand Down
23 changes: 23 additions & 0 deletions src/_filter/collection/join.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @ngdoc filter
* @name join
* @kind function
*
* @description
* join a collection by a provided delimiter (space by default)
*/
angular.module('a8m.join', [])
.filter('join', function () {
'use strict';
return function (input, delimiter) {
if (isUndefined(input) || !isArray(input)) {
return input;
}
if (!delimiter) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use isUndefined, cuz !0, !false, !'' etc.. it's also true

delimiter = ' ';
}

return input.join(delimiter);
};
})
;
1 change: 1 addition & 0 deletions src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ angular.module('angular.filter', [
'a8m.first',
'a8m.last',
'a8m.flatten',
'a8m.join',

'a8m.math',
'a8m.math.max',
Expand Down
78 changes: 78 additions & 0 deletions test/spec/filter/collection/join.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

describe('isJoinFilter', function () {
var joinFilter;

beforeEach(module('a8m.join'));

beforeEach(inject(function (_joinFilter_) {
joinFilter = _joinFilter_;
}));

describe('given a collection', function () {
var arr;

describe('which is empty', function () {
beforeEach(function () {
arr = [];
});

it('should return an empty string', function () {
expect(joinFilter(arr)).toEqual('');
});

});

describe('of strings', function () {
beforeEach(function () {
arr = ['hello', 'world'];
});

describe('with no delimiter', function () {
it('should join elements with a space', function () {
expect(joinFilter(arr)).toEqual('hello world');
});
});

describe('with a custom delimiter', function () {
var delim;

describe('which is not a string', function () {
it('should join elements with a toString representation of the delimiter', function () {
delim = true;
expect(joinFilter(arr, delim)).toEqual('hellotrueworld');

delim = 10;
expect(joinFilter(arr, delim)).toEqual('hello10world');

delim = {toString: function () { return ' - ' }}
expect(joinFilter(arr, delim)).toEqual('hello - world');
});
});

it('should join elements with the given delimiter', function () {
delim = ', '
expect(joinFilter(arr, delim)).toEqual('hello, world');
});
});
});

});

describe('given something that is not a collection', function () {
var str, obj, bool, num;
beforeEach(function () {
str = 'string';
obj = {'a': 'b'};
bool = true;
num = 5;
});

it('should return the input as is', function () {
expect(joinFilter(str)).toEqual(str);
expect(joinFilter(obj)).toEqual(obj);
expect(joinFilter(bool)).toEqual(bool);
expect(joinFilter(num)).toEqual(num);
});
});
});