-
Notifications
You must be signed in to change notification settings - Fork 331
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
c23f890
428f27d
48715de
a3dbbab
e3de068
ef17415
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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> | ||
|
||
|
@@ -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 | ||
|
||
Converts a string to camelCase<br/> | ||
usage: ```string | camelize: [upperFirst:boolean|optional]```<br/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The camelCase filter with |
||
|
||
```html | ||
<p>{{ 'a_simple_word' | camelize }}</p> | ||
<p>{{ 'ANUPPERCASEDWORD' | camelize }}</p> | ||
<p>{{ 'angular-js' | camelize: true }}</p> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also mention what happens to |
||
|
||
<!-- | ||
result: | ||
aSimpleWord | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
AngularJs | ||
--> | ||
``` | ||
|
||
## Math | ||
|
||
###max | ||
|
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(''); | ||
} | ||
}); |
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() }); | ||
} | ||
}); |
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'); | ||
}); | ||
|
||
}); |
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'); | ||
}); | ||
|
||
}); |
There was a problem hiding this comment.
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?