Skip to content

feat: Add new filters - String case filters #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Bunch of useful filters for AngularJS *(with no external dependencies!)*
- [where](#where)
- [xor](#xor)
- [String](#string)
- [camelize](#camelize)
- [endsWith](#endswith)
- [latinize](#latinize)
- [repeat](#repeat)
Expand All @@ -66,6 +67,7 @@ Bunch of useful filters for AngularJS *(with no external dependencies!)*
- [rtrim](#rtrim)
- [truncate](#truncate)
- [ucfirst](#ucfirst)
- [underscore](#underscore)
- [uriEncode](#uriencode)
- [uriComponentEncode](#uricomponentencode)
- [wrap](#wrap)
Expand Down Expand Up @@ -983,13 +985,18 @@ $scope.double = function(i) {
###ucfirst

ucfirstFilter get string as parameter and return it capitalized
usage: ```string | ucfirst: [lettersOnly:boolean|optional]```<br/>
**aliases:** titleize


```html
<p> {{ 'foo bar baz' | ucfirst }}</p>
<p> {{ 'foo bar baz' | ucfirst: true }}</p>

<!--
result:
Foo Bar Baz
FooBarBaz
-->
```

Expand Down Expand Up @@ -1083,7 +1090,7 @@ get string with {n} and replace match with enumeration values
```

### phoneUS
Format a string or a number into a us-style phone number
Format a string or a number into a us-style phone number
```html
<p>{{ 1234567890 | phoneUS }}</p>

Expand Down Expand Up @@ -1200,6 +1207,44 @@ Return an array of matched element in a string<br/>
['15', '12', '2003']
```

###underscore

Converts a string to underscore(snake_case)
**Usage:** ```string | underscore```<br/>
**aliases:** snakeCase

```html
<p>{{ 'fooBarBaz' | underscore }}</p>
<p>{{ 'ANUPPERCASEDWORD' | underscore }}</p>

<!--
result:
foo_bar_baz
a_n_u_p_p_e_r_c_a_s_e_d_w_o_r_d
-->
```

###camelize

Converts a string to camelCase<br/>
**Usage:** ```string | camelize```<br/>
**aliases:** camelCase

```html
<p>{{ 'a_simple_word' | camelize }}</p>
<p>{{ 'ANUPPERCASEDWORD' | camelize }}</p>
<p>{{ 'angular-js' | camelize }}</p>
<p>{{ 'a sentence' | camelize }}</p>

<!--
result:
aSimpleWord
angularJs
anuppercasedword
aSentence
-->
```

## Math

###max
Expand Down
29 changes: 29 additions & 0 deletions src/_filter/string/camelize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @ngdoc filter
* @name camelize
* @kind function
*
* @description
* Converts a string to camelCase
*/
angular.module('a8m.camelize', [])
.filter({
camelize: camelizeFilter,
camelCase: camelizeFilter
});

function camelizeFilter() {
return function(input) {
if (!isString(input)) return input;

return input.toLowerCase()
.split(/[-_\s]+/g)
.filter(function(value) {
return value !== '';
})
.map(function(value, index) {
return index === 0 ? value : value.slice(0, 1).toUpperCase() + value.slice(1);
})
.join('');
}
}
21 changes: 16 additions & 5 deletions src/_filter/string/ucfirst.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@ angular.module('a8m.ucfirst', [])
});

function ucfirstFilter() {
return function (input) {
return isString(input)
? input
return function (input, lettersOnly) {
if (!isString(input)) {
return input;
}

var delimiter = ' ';
var newWord = input;

if (!!lettersOnly) {
delimiter = '';
newWord = input.replace(/[^a-zA-Z ]/g, '');
}

return newWord
.toLowerCase()
.split(' ')
.map(function (ch) {
return ch.charAt(0).toUpperCase() + ch.substring(1);
})
.join(' ')
: input;
.join(delimiter);
}
}
22 changes: 22 additions & 0 deletions src/_filter/string/underscore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @ngdoc filter
* @name underscore
* @kind function
*
* @description
* Converts a string to underscore
*/
angular.module('a8m.underscore', [])
.filter({
underscore: underscoreFilter,
snakeCase: underscoreFilter
});

function underscoreFilter() {
return function(input) {
if (!isString(input)) return input;

return input.replace(/\W/g, '').replace(
/[A-Z]/g, function(value, index) { return index === 0 ? value.toLowerCase() : '_' + value.toLowerCase() });
}
}
2 changes: 2 additions & 0 deletions src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ angular.module('angular.filter', [
'a8m.test',
'a8m.match',
'a8m.split',
'a8m.underscore',
'a8m.camelize',

'a8m.to-array',
'a8m.concat',
Expand Down
31 changes: 31 additions & 0 deletions test/spec/filter/string/camelize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

describe('camelizeFilter', function () {

var filter;

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

beforeEach(inject(function ($filter) {
filter = $filter('camelize');
}));

it('should camelize valid strings', function() {
expect(filter('a_simple_word')).toEqual('aSimpleWord');
expect(filter('a_medium_word_here')).toEqual('aMediumWordHere');
expect(filter('a sentence')).toEqual('aSentence');
expect(filter('ANUPPERCASEDWORDHERE')).toEqual('anuppercasedwordhere');
expect(filter('alowercasedword')).toEqual('alowercasedword');
expect(filter(' s')).toEqual('s');
expect(filter(' SOME WHITE SPACES ')).toEqual('someWhiteSpaces');
expect(filter(' SOME-WHITE-SPACES ')).toEqual('someWhiteSpaces');
expect(filter('-1 SOME-WHITE-SPACES0 2-')).toEqual('1SomeWhiteSpaces02');
});

it('should not camelize invalid strings', function() {
expect(filter(null)).not.toEqual('null');
expect(filter(undefined)).not.toEqual('undefined');
expect(filter('ANUPPERCASEDWORDHERe')).not.toEqual('aNUPPERCASEDWORDHERE');
});

});
2 changes: 2 additions & 0 deletions test/spec/filter/string/ucfirst.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ describe('ucfirstFilter', function () {
it('should get a string and return it uppercase each first letter', function() {
expect(filter('a')).toEqual('A');
expect(filter('foo bar baz')).toEqual('Foo Bar Baz');
expect(filter('foO baR bAz')).toEqual('Foo Bar Baz');
expect(filter('lorem ipsum is simply dummy.... industry.')).toEqual('Lorem Ipsum Is Simply Dummy.... Industry.');
expect(filter('a medium senTence.', true)).toEqual('AMediumSentence');
});

it('should get a !string and not touch it', function() {
Expand Down
29 changes: 29 additions & 0 deletions test/spec/filter/string/underscore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

describe('underscoreFilter', function () {

var filter;

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

beforeEach(inject(function ($filter) {
filter = $filter('underscore');
}));

it('should underscore valid strings', function() {
expect(filter('ASimpleWord')).toEqual('a_simple_word');
expect(filter('aMediumWordHere')).toEqual('a_medium_word_here');
expect(filter('ANUPPERCASEDWORDHERE')).toEqual('a_n_u_p_p_e_r_c_a_s_e_d_w_o_r_d_h_e_r_e');
expect(filter('alowercasedword')).toEqual('alowercasedword');
expect(filter(' SOME WHITE SPACES ')).toEqual('s_o_m_e_w_h_i_t_e_s_p_a_c_e_s');
expect(filter(' SOME-WHITE-SPACES ')).toEqual('s_o_m_e_w_h_i_t_e_s_p_a_c_e_s');
expect(filter('1 SOME-WHITE-SPACES0 2')).toEqual('1_s_o_m_e_w_h_i_t_e_s_p_a_c_e_s02');

});

it('should not underscore invalid strings', function() {
expect(filter(null)).not.toEqual('null');
expect(filter(undefined)).not.toEqual('undefined');
});

});