From 982f460a5a53f07428302a20490326697897d570 Mon Sep 17 00:00:00 2001 From: eliorb Date: Tue, 30 Dec 2014 13:50:25 +0200 Subject: [PATCH 1/2] feat(joinFilter): add a filter to join collections into a string --- src/_filter/collection/join.js | 23 +++++++++ test/spec/filter/collection/join.js | 78 +++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/_filter/collection/join.js create mode 100644 test/spec/filter/collection/join.js diff --git a/src/_filter/collection/join.js b/src/_filter/collection/join.js new file mode 100644 index 0000000..1f292f6 --- /dev/null +++ b/src/_filter/collection/join.js @@ -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) { + delimiter = ' '; + } + + return input.join(delimiter); + }; + }) +; diff --git a/test/spec/filter/collection/join.js b/test/spec/filter/collection/join.js new file mode 100644 index 0000000..b3454c1 --- /dev/null +++ b/test/spec/filter/collection/join.js @@ -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); + }); + }); +}); From d96807bc3204bea4b344b6196b39e0e248b44dae Mon Sep 17 00:00:00 2001 From: eliorb Date: Wed, 31 Dec 2014 11:09:56 +0200 Subject: [PATCH 2/2] feat(joinFilter): update README --- README.md | 20 ++++++++++++++++++++ src/filters.js | 1 + 2 files changed, 21 insertions(+) diff --git a/README.md b/README.md index bb5a38a..f075a79 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ - [fuzzyBy](#fuzzyby) - [groupBy](#groupby) - [isEmpty](#isempty) + - [join] (#join) - [last](#last) - [map](#map) - [omit](#omit) @@ -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.
+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 +

{{ names | join:', ' }}

+ + +``` + + ###fuzzy fuzzy string searching(approximate string matching). [Read more](http://en.wikipedia.org/wiki/Approximate_string_matching)
**note:** use fuzzyBy to filter by one property to improve performance
diff --git a/src/filters.js b/src/filters.js index 7d69e27..6174e30 100644 --- a/src/filters.js +++ b/src/filters.js @@ -50,6 +50,7 @@ angular.module('angular.filter', [ 'a8m.first', 'a8m.last', 'a8m.flatten', + 'a8m.join', 'a8m.math', 'a8m.math.max',