Skip to content

Commit 71ee990

Browse files
committed
Added testing, and readme update with instructions on usage
1 parent d5e6e23 commit 71ee990

10 files changed

+274
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bower_components
2+
node_modules

Gruntfile.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports = function(grunt) {
2+
grunt.loadNpmTasks('grunt-contrib-uglify');
3+
grunt.loadNpmTasks('grunt-karma');
4+
5+
grunt.initConfig({
6+
7+
pkg: grunt.file.readJSON('package.json'),
8+
9+
uglify: {
10+
my_target: {
11+
options: {
12+
sourceMap: 'dist/angularjs-string-humanize.map.js',
13+
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
14+
'<%= grunt.template.today("yyyy-mm-dd") %> - ' +
15+
'<%= pkg.repository.url %> */'
16+
},
17+
files: {
18+
'dist/angularjs-string-humanize.min.js': ['src/angularjs-string-humanize.js']
19+
}
20+
}
21+
},
22+
23+
karma: {
24+
unit: {
25+
configFile: 'test/karma.conf.js',
26+
autoWatch: false,
27+
singleRun: true
28+
}
29+
}
30+
31+
});
32+
33+
grunt.registerTask('test', ['karma:unit']);
34+
grunt.registerTask('build', ['uglify']);
35+
36+
grunt.registerTask('default', ['karma:unit', 'uglify']);
37+
38+
};

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,62 @@
1-
# angularjs-string-humanize
2-
AngularJS filter wrapping the 'string-humanize' library
1+
# AngularJS String Humanize (angularjs-string-humanize)
2+
3+
## Description
4+
AngularJS filter wrapping the 'string-humanize' library to handle unknown string format and convert to human readable.
5+
6+
## Usage
7+
8+
Apply filter to string in HTML or JS to convert formatted string to human readable.
9+
10+
### In HTML Template Binding
11+
`{{ someString | stringHumanize}}`
12+
### In JavaScript
13+
`$filter('stringHumanize')(someString)`
14+
### Returns
15+
16+
String in human readable form. The first letter will be capitalized in the word or phrase.
17+
18+
### Examples
19+
Javascript
20+
```
21+
$filter('stringHumanize')('snake_case_string') // 'Snake case string'
22+
$filter('stringHumanize')('camelCaseString') // 'Camel case string'
23+
$filter('stringHumanize')('hyphen-case-string-with-ext.html') // 'Hyphen case string with ext'
24+
$filter('stringHumanize')(undefined) // ''
25+
```
26+
27+
HTML
28+
```
29+
{{ 'hyphen-case-string' | stringHumanize}} // 'Hyphen case string'
30+
{{ 'lower cased phrase' | stringHumanize}} // 'Lower cased phrase'
31+
{{ ' remove extra spaces ' | stringHumanize}} //'Remove extra spaces'
32+
{{ null | stringHumanize}} // ''
33+
```
34+
35+
## Install
36+
37+
### Via bower
38+
39+
```
40+
bower install --save angularjs-string-humanize
41+
```
42+
43+
Include `src/angularjs-string-humanize.js` or `dist/angularjs-string-humanize.min.js` to your project or index file.
44+
45+
```
46+
<script src="/bower_components/angularjs-string-humanize/dist/angularjs-string-humanize.min.js"></script>
47+
```
48+
49+
Add it as a dependency to your angularjs project:
50+
51+
```
52+
var app = angular.module("myApp", ['angularStringHumanize']);
53+
```
54+
55+
56+
## Compatibility
57+
58+
Functionality verified with unit test with angular versions from `v1.6.0`+.
59+
60+
## Credits
61+
62+
All credits go to [string-humanize](https://github.com/jxson/string-humanize) and [string-capitalize](https://github.com/jxson/string-capitalize). This is just packaged and tested version of their code.

bower.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
{
22
"name": "angularjs-string-humanize",
33
"version": "0.0.1",
4-
"main": "src/angular-string-humanize.js",
4+
"main": "src/angularjs-string-humanize.js",
55
"description": "Angular 1.x filter for rendering strings to human-readable data",
66
"license": "MIT",
7+
"dependencies": {
8+
"angular": "~1.6.0"
9+
},
710
"ignore": [
811
"**/.*",
9-
"*.md",
10-
"package.json"
12+
"node_modules",
13+
"bower_components",
14+
"test"
1115
],
12-
"dependencies": {
13-
"angular": "~1.6.0"
16+
"devDependencies": {
17+
"angular": "~1.6.0",
18+
"angular-mocks": "~1.6.0"
1419
}
1520
}

dist/angular-string-humanize.min.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-string-humanize.min.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,38 @@
11
{
22
"name": "angularjs-string-humanize",
33
"version": "0.0.1",
4-
"main": "src/angular-string-humanize.js",
5-
"description": "Angular 1.x filter for rendering strings to human-readable data",
4+
"main": "src/angularjs-string-humanize.js",
5+
"description": "Angular 1.x filter for rendering unknown formatted strings to human-readable data",
66
"repository": {
77
"type": "git",
88
"url": "https://github.com/apat183/angularjs-string-humanize"
9-
}
9+
},
10+
"directories": {
11+
"test": "test"
12+
},
13+
"dependencies": {},
14+
"devDependencies": {
15+
"grunt": "0.4.4",
16+
"grunt-contrib-uglify": "0.4.0",
17+
"grunt-karma": "0.8.2",
18+
"karma": "0.12.0",
19+
"karma-jasmine": "0.1.5",
20+
"karma-chrome-launcher": "0.1.2",
21+
"karma-firefox-launcher": "0.1.3",
22+
"karma-phantomjs-launcher": "0.1.2",
23+
"karma-script-launcher": "0.1.0"
24+
},
25+
"scripts": {
26+
"test": "grunt karma"
27+
},
28+
"keywords": [
29+
"angularjs",
30+
"string",
31+
"filter",
32+
"humanize"
33+
],
34+
"author": "Anand Patel",
35+
"contributors": [
36+
"Anand Patel (https://github.com/apat183)"
37+
]
1038
}

src/angularjs-string-humanize.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
(function (angular) {
2+
'use strict';
3+
4+
angular.module('angularStringHumanize', []).filter('stringHumanize', function () {
5+
6+
function humanize(string) {
7+
string = string || '';
8+
string = string.toString();
9+
string = string.trim();
10+
string = string.replace(extname(string), '');
11+
string = underscore(string);
12+
string = string.replace(/[\W_]+/g, ' ');
13+
14+
return capitalize(string);
15+
}
16+
17+
function underscore(string) {
18+
string = string || '';
19+
string = string.toString();
20+
string = string.trim();
21+
string = string.replace(/([a-z\d])([A-Z]+)/g, '$1_$2');
22+
string = string.replace(/[-\s]+/g, '_').toLowerCase();
23+
24+
return string;
25+
}
26+
27+
function extname(string) {
28+
var index = string.lastIndexOf('.');
29+
var ext = string.substring(index, string.length);
30+
31+
return (index === -1) ? '' : ext;
32+
}
33+
34+
function capitalize(string) {
35+
string = string || '';
36+
string = string.trim();
37+
38+
if (string[0]) {
39+
string = string[0].toUpperCase() + string.substr(1).toLowerCase();
40+
}
41+
42+
return string;
43+
}
44+
45+
return function (input) {
46+
return humanize(input);
47+
};
48+
49+
});
50+
51+
}(angular));

test/karma.conf.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*global module */
2+
module.exports = function ( config ) {
3+
'use strict';
4+
5+
var files = [
6+
'../bower_components/angular/angular.js',
7+
'../bower_components/angular-mocks/angular-mocks.js',
8+
'../src/angularjs-string-humanize.js',
9+
'../test/unit/angularjs-string-humanize.spec.js'
10+
];
11+
12+
config.set({
13+
files : files,
14+
basePath: '',
15+
frameworks: ['jasmine'],
16+
reporters: ['progress'],
17+
browsers: ['Chrome'],
18+
autoWatch: true,
19+
singleRun: false,
20+
colors: true
21+
});
22+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*global beforeEach, describe, it, inject, expect, module */
2+
'use strict';
3+
4+
describe('String Humanize', function () {
5+
6+
var humanize;
7+
8+
beforeEach(module('angularStringHumanize'));
9+
10+
beforeEach(inject(function ($filter) {
11+
humanize = $filter('stringHumanize');
12+
}));
13+
14+
describe('default functionality', function () {
15+
16+
it('should format snake case strings', function () {
17+
expect(humanize('snake_case_string')).toEqual('Snake case string');
18+
});
19+
20+
it('should format camel case string', function () {
21+
expect(humanize('camelCaseString')).toEqual('Camel case string');
22+
});
23+
24+
it('should format hyphen case string', function () {
25+
expect(humanize('hyphen-case-string')).toEqual('Hyphen case string');
26+
});
27+
28+
it('should remove extensions and format strings any case', function () {
29+
expect(humanize('snake_case_string.js')).toEqual('Snake case string');
30+
expect(humanize('camelCaseString.md')).toEqual('Camel case string');
31+
expect(humanize('hyphen-case-string.html')).toEqual('Hyphen case string');
32+
});
33+
34+
it('should format lower cased phrase string', function () {
35+
expect(humanize('lower cased phrase')).toEqual('Lower cased phrase');
36+
});
37+
38+
it('should format and remove all excess spaces in string', function () {
39+
expect(humanize(' remove extra spaces ')).toEqual('Remove extra spaces');
40+
});
41+
42+
it('should format numbers to string', function () {
43+
expect(humanize('123')).toEqual('123');
44+
});
45+
46+
it('should be able to handle blank null and undefined', function () {
47+
expect(humanize('')).toEqual('');
48+
expect(humanize(null)).toEqual('');
49+
expect(humanize(undefined)).toEqual('');
50+
});
51+
52+
});
53+
54+
});

0 commit comments

Comments
 (0)