Skip to content

Commit f1e1048

Browse files
authored
Merge pull request #242 from Marchrius/master
'split' filter implementation
2 parents 43a4a04 + d18b75c commit f1e1048

File tree

7 files changed

+165
-15
lines changed

7 files changed

+165
-15
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Bunch of useful filters for AngularJS *(with no external dependencies!)*
6363
- [ltrim](#ltrim)
6464
- [rtrim](#rtrim)
6565
- [truncate](#truncate)
66+
- [split](#split)
6667
- [ucfirst](#ucfirst)
6768
- [uriEncode](#uriencode)
6869
- [uriComponentEncode](#uricomponentencode)
@@ -1099,6 +1100,22 @@ lorem ipsum...
10991100
lorem ipsum d...
11001101
lorem ipsum dolor sit amet
11011102
```
1103+
###split
1104+
truncates a string given a specified length, providing a custom string to denote an omission.<br/>
1105+
usage: ``` | split: [delimiter]: [skip-optional]```<br/>
1106+
```js
1107+
$scope.text = 'lorem ipsum dolor sit amet';
1108+
```
1109+
```html
1110+
1111+
<p>{{ text | split: ' ' }}</p>
1112+
1113+
<p>{{ text | split: ' ': 2}}</p>
1114+
1115+
<!--result:
1116+
['lorem', 'ipsum', 'dolor', 'sit', 'amet']
1117+
['lorem ipsum dolor', 'sit', 'amet']
1118+
```
11021119
###reverse
11031120
Reverses a string
11041121
```js

dist/angular-filter.js

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Bunch of useful filters for angularJS(with no external dependencies!)
3-
* @version v0.5.14 - 2016-12-06 * @link https://github.com/a8m/angular-filter
3+
* @version v0.5.14 - 2017-01-16 * @link https://github.com/a8m/angular-filter
44
* @author Ariel Mashraki <ariel@mashraki.co.il>
55
* @license MIT License, http://www.opensource.org/licenses/MIT
66
*/
@@ -1798,6 +1798,23 @@ angular.module('a8m.match', [])
17981798
}
17991799
});
18001800

1801+
/**
1802+
* @ngdoc filter
1803+
* @name phone-us
1804+
* @kind function
1805+
*
1806+
* @description
1807+
* format a string or a number into a us-style
1808+
* phone number in the form (***) ***-****
1809+
*/
1810+
angular.module('a8m.phoneUS', [])
1811+
.filter('phoneUS', function () {
1812+
return function(num) {
1813+
num += '';
1814+
return '(' + num.slice(0, 3) + ') ' + num.slice(3, 6) + '-' + num.slice(6);
1815+
}
1816+
});
1817+
18011818
/**
18021819
* @ngdoc filter
18031820
* @name repeat
@@ -1875,6 +1892,47 @@ angular.module('a8m.slugify', [])
18751892
}
18761893
}]);
18771894

1895+
/**
1896+
* @ngdoc filter
1897+
* @name split
1898+
* @kind function
1899+
*
1900+
* @description
1901+
* split a string by a provided delimiter (none '' by default) and skip first n-delimiters
1902+
*/
1903+
angular.module('a8m.split', [])
1904+
.filter('split', function () {
1905+
function escapeRegExp(str) {
1906+
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
1907+
}
1908+
1909+
return function (input, delimiter, skip) {
1910+
var _regexp, _matches, _splitted, _temp;
1911+
1912+
if (isUndefined(input) || !isString(input)) {
1913+
return null;
1914+
}
1915+
if (isUndefined(delimiter)) delimiter = '';
1916+
if (isNaN(skip)) skip = 0;
1917+
1918+
_regexp = new RegExp(escapeRegExp(delimiter), 'g');
1919+
_matches = input.match(_regexp);
1920+
1921+
if (isNull(_matches) || skip >= _matches.length) {
1922+
return [input];
1923+
}
1924+
1925+
if (skip === 0) return input.split(delimiter);
1926+
1927+
_splitted = input.split(delimiter);
1928+
_temp = _splitted.splice(0, skip + 1);
1929+
_splitted.unshift(_temp.join(delimiter));
1930+
1931+
return _splitted;
1932+
};
1933+
})
1934+
;
1935+
18781936
/**
18791937
* @ngdoc filter
18801938
* @name startWith
@@ -2009,18 +2067,23 @@ angular.module('a8m.truncate', [])
20092067
* ucfirst
20102068
*/
20112069
angular.module('a8m.ucfirst', [])
2012-
.filter('ucfirst', [function() {
2013-
return function(input) {
2014-
return isString(input)
2015-
? input
2016-
.split(' ')
2017-
.map(function (ch) {
2018-
return ch.charAt(0).toUpperCase() + ch.substring(1);
2019-
})
2020-
.join(' ')
2021-
: input;
2022-
}
2023-
}]);
2070+
.filter({
2071+
ucfirst: ucfirstFilter,
2072+
titleize: ucfirstFilter
2073+
});
2074+
2075+
function ucfirstFilter() {
2076+
return function (input) {
2077+
return isString(input)
2078+
? input
2079+
.split(' ')
2080+
.map(function (ch) {
2081+
return ch.charAt(0).toUpperCase() + ch.substring(1);
2082+
})
2083+
.join(' ')
2084+
: input;
2085+
}
2086+
}
20242087

20252088
/**
20262089
* @ngdoc filter
@@ -2248,6 +2311,7 @@ angular.module('angular.filter', [
22482311
'a8m.repeat',
22492312
'a8m.test',
22502313
'a8m.match',
2314+
'a8m.split',
22512315

22522316
'a8m.to-array',
22532317
'a8m.concat',

dist/angular-filter.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-filter.zip

1.92 KB
Binary file not shown.

src/_filter/string/split.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @ngdoc filter
3+
* @name split
4+
* @kind function
5+
*
6+
* @description
7+
* split a string by a provided delimiter (none '' by default) and skip first n-delimiters
8+
*/
9+
angular.module('a8m.split', [])
10+
.filter('split', function () {
11+
function escapeRegExp(str) {
12+
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
13+
}
14+
15+
return function (input, delimiter, skip) {
16+
var _regexp, _matches, _splitted, _temp;
17+
18+
if (isUndefined(input) || !isString(input)) {
19+
return null;
20+
}
21+
if (isUndefined(delimiter)) delimiter = '';
22+
if (isNaN(skip)) skip = 0;
23+
24+
_regexp = new RegExp(escapeRegExp(delimiter), 'g');
25+
_matches = input.match(_regexp);
26+
27+
if (isNull(_matches) || skip >= _matches.length) {
28+
return [input];
29+
}
30+
31+
if (skip === 0) return input.split(delimiter);
32+
33+
_splitted = input.split(delimiter);
34+
_temp = _splitted.splice(0, skip + 1);
35+
_splitted.unshift(_temp.join(delimiter));
36+
37+
return _splitted;
38+
};
39+
})
40+
;

src/filters.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ angular.module('angular.filter', [
2424
'a8m.repeat',
2525
'a8m.test',
2626
'a8m.match',
27+
'a8m.split',
2728

2829
'a8m.to-array',
2930
'a8m.concat',

test/spec/filter/string/split.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
describe('splitFilter', function () {
4+
5+
var filter, sentence = "Today is a beautiful and sunny day!";
6+
7+
beforeEach(module('a8m.split'));
8+
9+
beforeEach(inject(function ($filter) {
10+
filter = $filter('split');
11+
}));
12+
13+
it('should test a string with given pattern', function() {
14+
15+
expect(filter(sentence, ' ', 3)).toEqual(['Today is a beautiful', 'and', 'sunny', 'day!']);
16+
expect(angular.equals(filter(sentence, '.'), [sentence])).toBeTruthy();
17+
expect(filter(sentence, ' ')).toEqual(['Today', 'is', 'a', 'beautiful', 'and', 'sunny', 'day!']);
18+
19+
});
20+
21+
it('should get a !string and return null', function() {
22+
expect(filter({})).toEqual(null);
23+
expect(filter([])).toEqual(null);
24+
expect(filter(1)).toEqual(null);
25+
expect(filter(!1)).toBeFalsy(null);
26+
});
27+
28+
});

0 commit comments

Comments
 (0)