Skip to content

Add dotName filter #133

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 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -63,6 +63,7 @@
- [uriEncode](#uriencode)
- [uriComponentEncode](#uricomponentencode)
- [wrap](#wrap)
- [dotName](#dotname)
- [Math](#math)
- [min](#min)
- [max](#max)
@@ -1087,6 +1088,16 @@ usage: ```string | wrap: string: string[optional]```
/foo/
{{foo}}
```
###dotName
Replace firstname and all middle names with a dot notation<br/>
usage: ```string | dotName```
```html
<p>{{ 'Gale Lewin' | dotName }}</p>
<p>{{ 'Sonny Teresa Hopson' | dotName }}</p>
<!--result:
G. Lewin
S. T. Hopson
```
###trim
Strip whitespace (or other characters) from the beginning and end of a string<br/>
usage: ```string | trim: chars[optional]```
38 changes: 38 additions & 0 deletions src/_filter/string/dot-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @ngdoc filter
* @name dotName
* @kind function
*
* @description
* replace firstname and all middle names with a dot notation.
*/
angular.module('a8m.dot-name', [])

.filter('dotName', function () {
return function (input) {

if(!isString(input)) {
return input;
}

var parts = input.split(' ')
.filter(function (part) {
return part.charAt(part.length - 1) !== '.';
}),
len = parts.length,
hasComma = input.indexOf(',') > -1,
larstNameIdx = hasComma ? 0 : len - 1;

if (parts.length < 2) {
return input;
}

return parts.map(function(part, idx){
if (idx === larstNameIdx) {
return part;
}

return part.charAt(0) + '.';
}).join(' ');
}
});
1 change: 1 addition & 0 deletions src/filters.js
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ angular.module('angular.filter', [
'a8m.repeat',
'a8m.test',
'a8m.match',
'a8m.dot-name',

'a8m.to-array',
'a8m.concat',
59 changes: 59 additions & 0 deletions test/spec/filter/string/dot-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

describe('dotNameFilter', function () {

var filter;

beforeEach(module('a8m.dot-name'));

beforeEach(inject(function ($filter) {
filter = $filter('dotName');
}));

it('should convert first and all middle names to dot', function() {

expect(filter('Gale Lewin')).toEqual('G. Lewin');
expect(filter('Mandi Baines')).toEqual('M. Baines');
expect(filter('Ariel Washington')).toEqual('A. Washington');

expect(filter('Sonny Teresa Hopson')).toEqual('S. T. Hopson');
expect(filter('Dannie Royston Stacie Ford')).toEqual('D. R. S. Ford');
expect(filter('Beth Storm Braith Terrence Swindlehurst')).toEqual('B. S. B. T. Swindlehurst');

});

it('should handle hyphens', function() {

expect(filter('Dannie-Royston Stacie Ford')).toEqual('D. S. Ford');
expect(filter('Dannie Royston-Stacie Ford')).toEqual('D. R. Ford');
expect(filter('Dannie Royston Stacie-Ford')).toEqual('D. R. Stacie-Ford');

});

it('should handle comma', function() {

expect(filter('Lewin, Gale')).toEqual('Lewin, G.');
expect(filter('Hopson, Sonny Teresa')).toEqual('Hopson, S. T.');

});

it('should remove title', function() {

expect(filter('Mr. Gale Lewin')).toEqual('G. Lewin');

});

it('should ignore single string', function() {

expect(filter('Ford')).toEqual('Ford');

});

it('should get a !string and not touch it', function() {
expect(filter({})).toEqual({});
expect(filter([])).toEqual([]);
expect(filter(1)).toEqual(1);
expect(filter(!1)).toBeFalsy();
});

});