diff --git a/README.md b/README.md index a72e75a..5eeb6e2 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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) @@ -983,13 +985,18 @@ $scope.double = function(i) { ###ucfirst ucfirstFilter get string as parameter and return it capitalized +usage: ```string | ucfirst: [lettersOnly:boolean|optional]```
+**aliases:** titleize + ```html

{{ 'foo bar baz' | ucfirst }}

+

{{ 'foo bar baz' | ucfirst: true }}

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

{{ 1234567890 | phoneUS }}

@@ -1200,6 +1207,44 @@ Return an array of matched element in a string
['15', '12', '2003'] ``` +###underscore + +Converts a string to underscore(snake_case) +**Usage:** ```string | underscore```
+**aliases:** snakeCase + +```html +

{{ 'fooBarBaz' | underscore }}

+

{{ 'ANUPPERCASEDWORD' | underscore }}

+ + +``` + +###camelize + +Converts a string to camelCase
+**Usage:** ```string | camelize```
+**aliases:** camelCase + +```html +

{{ 'a_simple_word' | camelize }}

+

{{ 'ANUPPERCASEDWORD' | camelize }}

+

{{ 'angular-js' | camelize }}

+

{{ 'a sentence' | camelize }}

+ + +``` + ## Math ###max diff --git a/src/_filter/string/camelize.js b/src/_filter/string/camelize.js new file mode 100644 index 0000000..364b104 --- /dev/null +++ b/src/_filter/string/camelize.js @@ -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(''); + } +} diff --git a/src/_filter/string/ucfirst.js b/src/_filter/string/ucfirst.js index d81fa61..e6d7be9 100644 --- a/src/_filter/string/ucfirst.js +++ b/src/_filter/string/ucfirst.js @@ -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); } } diff --git a/src/_filter/string/underscore.js b/src/_filter/string/underscore.js new file mode 100644 index 0000000..21253ba --- /dev/null +++ b/src/_filter/string/underscore.js @@ -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() }); + } +} diff --git a/src/filters.js b/src/filters.js index f757f8d..8052d39 100644 --- a/src/filters.js +++ b/src/filters.js @@ -25,6 +25,8 @@ angular.module('angular.filter', [ 'a8m.test', 'a8m.match', 'a8m.split', + 'a8m.underscore', + 'a8m.camelize', 'a8m.to-array', 'a8m.concat', diff --git a/test/spec/filter/string/camelize.js b/test/spec/filter/string/camelize.js new file mode 100644 index 0000000..b3afde1 --- /dev/null +++ b/test/spec/filter/string/camelize.js @@ -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'); + }); + +}); diff --git a/test/spec/filter/string/ucfirst.js b/test/spec/filter/string/ucfirst.js index 80f1e2d..823b16b 100644 --- a/test/spec/filter/string/ucfirst.js +++ b/test/spec/filter/string/ucfirst.js @@ -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() { diff --git a/test/spec/filter/string/underscore.js b/test/spec/filter/string/underscore.js new file mode 100644 index 0000000..cf472f3 --- /dev/null +++ b/test/spec/filter/string/underscore.js @@ -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'); + }); + +});