├── .editorconfig
├── .gitignore
├── .jshintrc
├── .npmignore
├── LICENSE
├── README.md
├── bower.json
├── capitalize.js
├── capitalize.min.js
├── index.js
├── karma.conf.js
├── package.json
└── test
└── capitalize.spec.js
/.editorconfig:
--------------------------------------------------------------------------------
1 | # http://editorconfig.org
2 | root = true
3 |
4 | [*]
5 | indent_style = space
6 | indent_size = 2
7 | end_of_line = lf
8 | charset = utf-8
9 | trim_trailing_whitespace = true
10 | insert_final_newline = true
11 |
12 | [*.md]
13 | trim_trailing_whitespace = false
14 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | bower_components
--------------------------------------------------------------------------------
/.jshintrc:
--------------------------------------------------------------------------------
1 | {
2 | "bitwise": true,
3 | "curly": true,
4 | "eqeqeq": true,
5 | "latedef": "nofunc",
6 | "noarg": true,
7 | "strict": "global",
8 | "undef": true,
9 | "unused": true,
10 | "browser": true,
11 | "jasmine": true,
12 | "globals": {
13 | "angular": true,
14 | "module": true,
15 | "inject": true
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | bower_components/
2 | test/
3 | .editorconfig
4 | .jshintrc
5 | .npmignore
6 | bower.json
7 | karma.conf.js
8 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Pablo Villoslada Puigcerber
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # angular-capitalize-filter
2 |
3 | AngularJS filter to capitalize sentences and specially team names.
4 |
5 | ## Installation
6 |
7 | You can install the filter using [Bower](http://bower.io/):
8 |
9 | ```bash
10 | $ bower install angular-capitalize-filter
11 | ```
12 |
13 | Or [npm](https://www.npmjs.com/):
14 |
15 | ```bash
16 | $ npm install angular-capitalize-filter
17 | ```
18 |
19 | Then you have to include it in your HTML:
20 |
21 | ```html
22 |
23 |
24 | ```
25 |
26 | And inject the module `puigcerber.capitalize` as a dependency in your application:
27 |
28 | ```js
29 | angular.module('webApp', ['puigcerber.capitalize']);
30 | ```
31 |
32 | ## Usage
33 |
34 | You can use it like any other AngularJS filter:
35 |
36 | ```html
37 |
{{ input | capitalize:format:separator }}
38 | ```
39 |
40 | The format and separator are optional parameters. If not specified all the words are capitalized.
41 |
42 | ### Format
43 |
44 | Available formats:
45 |
46 | * [all](#all)
47 | * [first](#first)
48 | * [team](#team)
49 |
50 | #### All
51 |
52 | It capitalizes all the words of a given sentence. As it's the default format you can omit the parameter.
53 |
54 | ```html
55 | {{ sentence | capitalize:'all' }}
56 | ```
57 |
58 | #### First
59 |
60 | It capitalizes just the first letter of the given sentence.
61 |
62 | ```html
63 | {{ sentence | capitalize:'first' }}
64 | ```
65 |
66 | #### Team
67 |
68 | Specially adapted for team names, with uppercase abbreviation.
69 |
70 | ```html
71 | {{ teamName | capitalize:'team' }}
72 | ```
73 |
74 | It formats the team name as CD Logroñés, FC Barcelona or Valencia CF.
75 |
76 | ### Separator
77 |
78 | By default the words in a sentence are separated by the space character.
79 | But any other character can be specified as a separator so we can humanize our output.
80 |
81 | ```html
82 | {{ underscored_sentence | capitalize:'all':'_' }}
83 | ```
84 |
85 | ## Related
86 |
87 | * [camelize](https://github.com/Puigcerber/angular-camelize-filter):
88 | AngularJS filter to convert strings to lower camel case replacing non-alphanumeric characters.
89 | * [lowerize](https://github.com/Puigcerber/angular-lowerize-filter):
90 | AngularJS filter to convert strings to lower case replacing non-alphanumeric characters.
91 |
92 | ## License
93 |
94 | MIT © [Pablo Villoslada Puigcerber](http://pablovilloslada.com)
95 |
--------------------------------------------------------------------------------
/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "angular-capitalize-filter",
3 | "version": "3.0.0",
4 | "homepage": "https://github.com/Puigcerber/angular-capitalize-filter",
5 | "repository": {
6 | "type": "git",
7 | "url": "git@github.com:Puigcerber/angular-capitalize-filter.git"
8 | },
9 | "authors": [
10 | "Pablo Villoslada Puigcerber (http://pablovilloslada.com/)"
11 | ],
12 | "description": "AngularJS filter to capitalize sentences and specially team names.",
13 | "main": "capitalize.js",
14 | "keywords": [
15 | "angularjs",
16 | "capitalize",
17 | "filter",
18 | "humanize"
19 | ],
20 | "license": "MIT",
21 | "ignore": [
22 | "**/.*",
23 | "node_modules",
24 | "bower_components",
25 | "test",
26 | "karma.conf.js",
27 | "**/*.json",
28 | "index.js"
29 | ],
30 | "dependencies": {
31 | "angular": "^1.2.15"
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/capitalize.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 | /**
3 | * Capitalize Filter
4 | * Capitalizes all the words of a given sentence.
5 | * If the format parameter is set to 'team', uppercase the team abbreviation.
6 | * i.e. CLUB DEPORTIVO LOGROÑÉS => Club Deportivo Logroñés
7 | * i.e. sd logroñés => SD Logroñés
8 | * @author Pablo Villoslada Puigcerber
9 | *
10 | * @param {string} input The string to be formatted.
11 | * @param {string} [format] The format to be applied being the options 'all', 'first' or 'team'.
12 | * If not specified, 'all' is used.
13 | * @param {string} [separator] The character(s) to be used for separating the string.
14 | * If not specified, space is used.
15 | * @returns {string} Formatted string.
16 | */
17 | angular.module('puigcerber.capitalize',[])
18 | .filter('capitalize', function () {
19 | return function (input, format, separator) {
20 | if (!input) {
21 | return input;
22 | }
23 | format = format || 'all';
24 | separator = separator || ' ';
25 | if (format === 'first') {
26 | // Capitalize the first letter of a sentence
27 | var output = input.charAt(0).toUpperCase() + input.slice(1).toLowerCase();
28 | if (separator === ' ') {
29 | return output;
30 | } else {
31 | return output.split(separator).join(' ');
32 | }
33 | } else {
34 | return input.split(separator).map(function(word) {
35 | if (word.length === 2 && format === 'team') {
36 | // Uppercase team abbreviations like FC, CD, SD
37 | return word.toUpperCase();
38 | } else {
39 | return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
40 | }
41 | }).join(' ');
42 | }
43 | };
44 | });
45 |
--------------------------------------------------------------------------------
/capitalize.min.js:
--------------------------------------------------------------------------------
1 | "use strict";angular.module("puigcerber.capitalize",[]).filter("capitalize",function(){return function(e,r,t){if(!e){return e}r=r||"all";t=t||" ";if(r==="first"){var i=e.charAt(0).toUpperCase()+e.slice(1).toLowerCase();if(t===" "){return i}else{return i.split(t).join(" ")}}else{return e.split(t).map(function(e){if(e.length===2&&r==="team"){return e.toUpperCase()}else{return e.charAt(0).toUpperCase()+e.slice(1).toLowerCase()}}).join(" ")}}});
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | require('angular');
2 | require('./capitalize');
3 | module.exports = 'puigcerber.capitalize';
4 |
--------------------------------------------------------------------------------
/karma.conf.js:
--------------------------------------------------------------------------------
1 | // Karma configuration
2 | // Generated on Fri Mar 28 2014 15:42:21 GMT+0100 (W. Europe Standard Time)
3 |
4 | module.exports = function(config) {
5 | config.set({
6 |
7 | // base path that will be used to resolve all patterns (eg. files, exclude)
8 | basePath: '',
9 |
10 |
11 | // frameworks to use
12 | // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
13 | frameworks: ['jasmine'],
14 |
15 |
16 | // list of files / patterns to load in the browser
17 | files: [
18 | 'node_modules/angular/angular.js',
19 | 'node_modules/angular-mocks/angular-mocks.js',
20 | 'capitalize.js',
21 | 'test/**/*.spec.js'
22 | ],
23 |
24 |
25 | // list of files to exclude
26 | exclude: [
27 |
28 | ],
29 |
30 |
31 | // preprocess matching files before serving them to the browser
32 | // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
33 | preprocessors: {
34 |
35 | },
36 |
37 |
38 | // test results reporter to use
39 | // possible values: 'dots', 'progress'
40 | // available reporters: https://npmjs.org/browse/keyword/karma-reporter
41 | reporters: ['progress'],
42 |
43 |
44 | // web server port
45 | port: 9876,
46 |
47 |
48 | // enable / disable colors in the output (reporters and logs)
49 | colors: true,
50 |
51 |
52 | // level of logging
53 | // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
54 | logLevel: config.LOG_INFO,
55 |
56 |
57 | // enable / disable watching file and executing tests whenever any file changes
58 | autoWatch: true,
59 |
60 |
61 | // start these browsers
62 | // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
63 | browsers: ['PhantomJS', 'Chrome'],
64 |
65 |
66 | // Continuous Integration mode
67 | // if true, Karma captures browsers, runs the tests and exits
68 | singleRun: true
69 | });
70 | };
71 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "angular-capitalize-filter",
3 | "version": "3.0.0",
4 | "description": "AngularJS filter to capitalize sentences and specially team names.",
5 | "author": "Pablo Villoslada Puigcerber (http://pablovilloslada.com/)",
6 | "main": "index.js",
7 | "scripts": {
8 | "build": "uglifyjs capitalize.js -m -o capitalize.min.js",
9 | "lint": "jshint capitalize.js test; true",
10 | "test": "karma start",
11 | "pretest": "npm run lint"
12 | },
13 | "repository": {
14 | "type": "git",
15 | "url": "https://github.com/Puigcerber/angular-capitalize-filter"
16 | },
17 | "bugs": {
18 | "url": "https://github.com/Puigcerber/angular-capitalize-filter/issues"
19 | },
20 | "keywords": [
21 | "angularjs",
22 | "capitalize",
23 | "filter",
24 | "humanize"
25 | ],
26 | "dependencies": {
27 | "angular": "^1.2.15"
28 | },
29 | "devDependencies": {
30 | "angular-mocks": "^1.5.0",
31 | "jshint": "^2.9.1",
32 | "karma": "~0.12.1",
33 | "karma-chrome-launcher": "^0.2.2",
34 | "karma-jasmine": "~0.1.5",
35 | "karma-phantomjs-launcher": "^1.0.0",
36 | "phantomjs-prebuilt": "^2.1.4",
37 | "uglify-js": "^2.6.2"
38 | },
39 | "license": "MIT"
40 | }
41 |
--------------------------------------------------------------------------------
/test/capitalize.spec.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | describe('Filter: capitalize', function () {
4 |
5 | // load the filter's module
6 | beforeEach(module('puigcerber.capitalize'));
7 |
8 | // initialize a new instance of the filter before each test
9 | var capitalize;
10 | beforeEach(inject(function ($filter) {
11 | capitalize = $filter('capitalize');
12 | }));
13 |
14 | describe('Format: team', function() {
15 | it('should return the uppercase input with uppercase abbreviation and the rest of the words capitalized', function () {
16 | var text = 'FC BARCELONA';
17 | expect(capitalize(text, 'team')).toBe('FC Barcelona');
18 | });
19 |
20 | it('should return the lowercase input with uppercase abbreviation and the rest of the words capitalized', function () {
21 | var text = 'sd logroñés';
22 | expect(capitalize(text, 'team')).toBe('SD Logroñés');
23 | });
24 | });
25 |
26 | describe('Format: all', function() {
27 | it('should return the uppercase input with all the words capitalized', function () {
28 | var text = 'CLUB DEPORTIVO LOGROÑÉS';
29 | expect(capitalize(text)).toBe('Club Deportivo Logroñés');
30 | });
31 |
32 | it('should return the lowercase input with all the words capitalized', function () {
33 | var text = 'la tierra con nombre de vino';
34 | expect(capitalize(text)).toBe('La Tierra Con Nombre De Vino');
35 | });
36 | });
37 |
38 | describe('Format: first', function() {
39 | it('should return the uppercase input with the first word capitalized', function () {
40 | var text = 'LIFE IS TOO SHORT FOR MANUAL TESTING.';
41 | expect(capitalize(text, 'first')).toBe('Life is too short for manual testing.');
42 | });
43 |
44 | it('should return the lowercase input with the first word capitalized', function () {
45 | var text = 'a code that cannot be tested is flawed.';
46 | expect(capitalize(text, 'first')).toBe('A code that cannot be tested is flawed.');
47 | });
48 | });
49 |
50 | describe('Separator', function () {
51 | it('should return the underscored sentence humanized with the desired capitalization', function() {
52 | var text = 'COPY_and_PASTE_is_a_design_ERROR';
53 | expect(capitalize(text, 'all', '_')).toBe('Copy And Paste Is A Design Error');
54 | expect(capitalize(text, 'first', '_')).toBe('Copy and paste is a design error');
55 | });
56 | });
57 |
58 | });
59 |
--------------------------------------------------------------------------------