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 1 commit
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
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ Return an array of matched element in a string<br/>

###underscore

underscoreFilter get string as parameter and return it capitalized
Converts a string to snake_case (underscore)

```html
<p>{{ 'fooBarBaz' | underscore }}</p>
Expand All @@ -1216,6 +1216,23 @@ 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('');
}
});
1 change: 1 addition & 0 deletions src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ angular.module('angular.filter', [
'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');
});

});