Skip to content
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

feat: Add new filters - String case filters #245

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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 @@ -1083,7 +1084,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 +1201,38 @@ Return an array of matched element in a string<br/>
['15', '12', '2003']
```

###underscore

Converts a string to snake_case (underscore)

```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

Choose a reason for hiding this comment

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

Why not call it camelCase?


Converts a string to camelCase<br/>
usage: ```string | camelize: [upperFirst:boolean|optional]```<br/>

Choose a reason for hiding this comment

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

The camelCase filter with upperFirst enabled is basically a PascalCase filter. Maybe it's better to split them up into two filters.


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

Choose a reason for hiding this comment

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

Maybe also mention what happens to a sentence -> aSentence


<!--
result:
aSimpleWord

Choose a reason for hiding this comment

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

anuppercaseword is missing

AngularJs
-->
```

## Math

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

return input.toLowerCase()
.split(/[-_\s]+/g)
.filter(function(value) {
return value !== '';
})
.map(function(value, index) {
return index === 0 && !!!upperFirst ? value : value.substring(0, 1).toUpperCase() + value.substring(1);
})
.join('');
}
});
17 changes: 17 additions & 0 deletions src/_filter/string/underscore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @ngdoc filter
* @name underscore
* @kind function
*
* @description
* Converts a string to underscore
*/
angular.module('a8m.underscore', [])
.filter('underscore', function () {
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
30 changes: 30 additions & 0 deletions test/spec/filter/string/camelize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'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('ANUPPERCASEDWORDHERE')).toEqual('anuppercasedwordhere');
expect(filter('alowercasedword')).toEqual('alowercasedword');
expect(filter(' s', true)).toEqual('S');
expect(filter(' SOME WHITE SPACES ')).toEqual('someWhiteSpaces');
expect(filter(' SOME-WHITE-SPACES ', true)).toEqual('SomeWhiteSpaces');
expect(filter('-1 SOME-WHITE-SPACES0 2-', true)).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');
});

});
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');
});

});