mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-20 04:11:51 +00:00
feat(ngPluralize): add alternative mapping using attributes
Add an alternative way to define a mapping for ng:pluralize using attributes instead of the `when` attribute Closes #2454
This commit is contained in:
parent
1e649c5a81
commit
a170fc1a74
2 changed files with 70 additions and 6 deletions
|
|
@ -174,13 +174,20 @@ var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interp
|
||||||
restrict: 'EA',
|
restrict: 'EA',
|
||||||
link: function(scope, element, attr) {
|
link: function(scope, element, attr) {
|
||||||
var numberExp = attr.count,
|
var numberExp = attr.count,
|
||||||
whenExp = element.attr(attr.$attr.when), // this is because we have {{}} in attrs
|
whenExp = attr.$attr.when && element.attr(attr.$attr.when), // we have {{}} in attrs
|
||||||
offset = attr.offset || 0,
|
offset = attr.offset || 0,
|
||||||
whens = scope.$eval(whenExp),
|
whens = scope.$eval(whenExp) || {},
|
||||||
whensExpFns = {},
|
whensExpFns = {},
|
||||||
startSymbol = $interpolate.startSymbol(),
|
startSymbol = $interpolate.startSymbol(),
|
||||||
endSymbol = $interpolate.endSymbol();
|
endSymbol = $interpolate.endSymbol(),
|
||||||
|
isWhen = /^when(Minus)?(.+)$/;
|
||||||
|
|
||||||
|
forEach(attr, function(expression, attributeName) {
|
||||||
|
if (isWhen.test(attributeName)) {
|
||||||
|
whens[lowercase(attributeName.replace('when', '').replace('Minus', '-'))] =
|
||||||
|
element.attr(attr.$attr[attributeName]);
|
||||||
|
}
|
||||||
|
});
|
||||||
forEach(whens, function(expression, key) {
|
forEach(whens, function(expression, key) {
|
||||||
whensExpFns[key] =
|
whensExpFns[key] =
|
||||||
$interpolate(expression.replace(BRACE, startSymbol + numberExp + '-' +
|
$interpolate(expression.replace(BRACE, startSymbol + numberExp + '-' +
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
describe('ngPluralize', function() {
|
describe('ngPluralize', function() {
|
||||||
var element;
|
var element,
|
||||||
|
elementAlt;
|
||||||
|
|
||||||
|
|
||||||
afterEach(function(){
|
afterEach(function(){
|
||||||
dealoc(element);
|
dealoc(element);
|
||||||
|
dealoc(elementAlt);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -13,10 +15,18 @@ describe('ngPluralize', function() {
|
||||||
beforeEach(inject(function($rootScope, $compile) {
|
beforeEach(inject(function($rootScope, $compile) {
|
||||||
element = $compile(
|
element = $compile(
|
||||||
'<ng:pluralize count="email"' +
|
'<ng:pluralize count="email"' +
|
||||||
"when=\"{'0': 'You have no new email'," +
|
"when=\"{'-1': 'You have negative email. Whohoo!'," +
|
||||||
|
"'0': 'You have no new email'," +
|
||||||
"'one': 'You have one new email'," +
|
"'one': 'You have one new email'," +
|
||||||
"'other': 'You have {} new emails'}\">" +
|
"'other': 'You have {} new emails'}\">" +
|
||||||
'</ng:pluralize>')($rootScope);
|
'</ng:pluralize>')($rootScope);
|
||||||
|
elementAlt = $compile(
|
||||||
|
'<ng:pluralize count="email" ' +
|
||||||
|
"when-minus-1='You have negative email. Whohoo!' " +
|
||||||
|
"when-0='You have no new email' " +
|
||||||
|
"when-one='You have one new email' " +
|
||||||
|
"when-other='You have {} new emails'>" +
|
||||||
|
'</ng:pluralize>')($rootScope);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -24,38 +34,52 @@ describe('ngPluralize', function() {
|
||||||
$rootScope.email = 0;
|
$rootScope.email = 0;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have no new email');
|
expect(element.text()).toBe('You have no new email');
|
||||||
|
expect(elementAlt.text()).toBe('You have no new email');
|
||||||
|
|
||||||
$rootScope.email = '0';
|
$rootScope.email = '0';
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have no new email');
|
expect(element.text()).toBe('You have no new email');
|
||||||
|
expect(elementAlt.text()).toBe('You have no new email');
|
||||||
|
|
||||||
$rootScope.email = 1;
|
$rootScope.email = 1;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have one new email');
|
expect(element.text()).toBe('You have one new email');
|
||||||
|
expect(elementAlt.text()).toBe('You have one new email');
|
||||||
|
|
||||||
$rootScope.email = 0.01;
|
$rootScope.email = 0.01;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have 0.01 new emails');
|
expect(element.text()).toBe('You have 0.01 new emails');
|
||||||
|
expect(elementAlt.text()).toBe('You have 0.01 new emails');
|
||||||
|
|
||||||
$rootScope.email = '0.1';
|
$rootScope.email = '0.1';
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have 0.1 new emails');
|
expect(element.text()).toBe('You have 0.1 new emails');
|
||||||
|
expect(elementAlt.text()).toBe('You have 0.1 new emails');
|
||||||
|
|
||||||
$rootScope.email = 2;
|
$rootScope.email = 2;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have 2 new emails');
|
expect(element.text()).toBe('You have 2 new emails');
|
||||||
|
expect(elementAlt.text()).toBe('You have 2 new emails');
|
||||||
|
|
||||||
$rootScope.email = -0.1;
|
$rootScope.email = -0.1;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have -0.1 new emails');
|
expect(element.text()).toBe('You have -0.1 new emails');
|
||||||
|
expect(elementAlt.text()).toBe('You have -0.1 new emails');
|
||||||
|
|
||||||
$rootScope.email = '-0.01';
|
$rootScope.email = '-0.01';
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have -0.01 new emails');
|
expect(element.text()).toBe('You have -0.01 new emails');
|
||||||
|
expect(elementAlt.text()).toBe('You have -0.01 new emails');
|
||||||
|
|
||||||
$rootScope.email = -2;
|
$rootScope.email = -2;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have -2 new emails');
|
expect(element.text()).toBe('You have -2 new emails');
|
||||||
|
expect(elementAlt.text()).toBe('You have -2 new emails');
|
||||||
|
|
||||||
|
$rootScope.email = -1;
|
||||||
|
$rootScope.$digest();
|
||||||
|
expect(element.text()).toBe('You have negative email. Whohoo!');
|
||||||
|
expect(elementAlt.text()).toBe('You have negative email. Whohoo!');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -63,38 +87,47 @@ describe('ngPluralize', function() {
|
||||||
$rootScope.email = '';
|
$rootScope.email = '';
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('');
|
expect(element.text()).toBe('');
|
||||||
|
expect(elementAlt.text()).toBe('');
|
||||||
|
|
||||||
$rootScope.email = null;
|
$rootScope.email = null;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('');
|
expect(element.text()).toBe('');
|
||||||
|
expect(elementAlt.text()).toBe('');
|
||||||
|
|
||||||
$rootScope.email = undefined;
|
$rootScope.email = undefined;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('');
|
expect(element.text()).toBe('');
|
||||||
|
expect(elementAlt.text()).toBe('');
|
||||||
|
|
||||||
$rootScope.email = 'a3';
|
$rootScope.email = 'a3';
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('');
|
expect(element.text()).toBe('');
|
||||||
|
expect(elementAlt.text()).toBe('');
|
||||||
|
|
||||||
$rootScope.email = '011';
|
$rootScope.email = '011';
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have 11 new emails');
|
expect(element.text()).toBe('You have 11 new emails');
|
||||||
|
expect(elementAlt.text()).toBe('You have 11 new emails');
|
||||||
|
|
||||||
$rootScope.email = '-011';
|
$rootScope.email = '-011';
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have -11 new emails');
|
expect(element.text()).toBe('You have -11 new emails');
|
||||||
|
expect(elementAlt.text()).toBe('You have -11 new emails');
|
||||||
|
|
||||||
$rootScope.email = '1fff';
|
$rootScope.email = '1fff';
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have one new email');
|
expect(element.text()).toBe('You have one new email');
|
||||||
|
expect(elementAlt.text()).toBe('You have one new email');
|
||||||
|
|
||||||
$rootScope.email = '0aa22';
|
$rootScope.email = '0aa22';
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have no new email');
|
expect(element.text()).toBe('You have no new email');
|
||||||
|
expect(elementAlt.text()).toBe('You have no new email');
|
||||||
|
|
||||||
$rootScope.email = '000001';
|
$rootScope.email = '000001';
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('You have one new email');
|
expect(element.text()).toBe('You have one new email');
|
||||||
|
expect(elementAlt.text()).toBe('You have one new email');
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -117,35 +150,48 @@ describe('ngPluralize', function() {
|
||||||
describe('deal with pluralized strings with offset', function() {
|
describe('deal with pluralized strings with offset', function() {
|
||||||
it('should show single/plural strings with offset', inject(function($rootScope, $compile) {
|
it('should show single/plural strings with offset', inject(function($rootScope, $compile) {
|
||||||
element = $compile(
|
element = $compile(
|
||||||
"<ng:pluralize count=\"viewCount\" offset=2 " +
|
"<ng:pluralize count='viewCount' offset='2' " +
|
||||||
"when=\"{'0': 'Nobody is viewing.'," +
|
"when=\"{'0': 'Nobody is viewing.'," +
|
||||||
"'1': '{{p1}} is viewing.'," +
|
"'1': '{{p1}} is viewing.'," +
|
||||||
"'2': '{{p1}} and {{p2}} are viewing.'," +
|
"'2': '{{p1}} and {{p2}} are viewing.'," +
|
||||||
"'one': '{{p1}}, {{p2}} and one other person are viewing.'," +
|
"'one': '{{p1}}, {{p2}} and one other person are viewing.'," +
|
||||||
"'other': '{{p1}}, {{p2}} and {} other people are viewing.'}\">" +
|
"'other': '{{p1}}, {{p2}} and {} other people are viewing.'}\">" +
|
||||||
"</ng:pluralize>")($rootScope);
|
"</ng:pluralize>")($rootScope);
|
||||||
|
elementAlt = $compile(
|
||||||
|
"<ng:pluralize count='viewCount' offset='2' " +
|
||||||
|
"when-0='Nobody is viewing.'" +
|
||||||
|
"when-1='{{p1}} is viewing.'" +
|
||||||
|
"when-2='{{p1}} and {{p2}} are viewing.'" +
|
||||||
|
"when-one='{{p1}}, {{p2}} and one other person are viewing.'" +
|
||||||
|
"when-other='{{p1}}, {{p2}} and {} other people are viewing.'>" +
|
||||||
|
"</ng:pluralize>")($rootScope);
|
||||||
$rootScope.p1 = 'Igor';
|
$rootScope.p1 = 'Igor';
|
||||||
$rootScope.p2 = 'Misko';
|
$rootScope.p2 = 'Misko';
|
||||||
|
|
||||||
$rootScope.viewCount = 0;
|
$rootScope.viewCount = 0;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('Nobody is viewing.');
|
expect(element.text()).toBe('Nobody is viewing.');
|
||||||
|
expect(elementAlt.text()).toBe('Nobody is viewing.');
|
||||||
|
|
||||||
$rootScope.viewCount = 1;
|
$rootScope.viewCount = 1;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('Igor is viewing.');
|
expect(element.text()).toBe('Igor is viewing.');
|
||||||
|
expect(elementAlt.text()).toBe('Igor is viewing.');
|
||||||
|
|
||||||
$rootScope.viewCount = 2;
|
$rootScope.viewCount = 2;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('Igor and Misko are viewing.');
|
expect(element.text()).toBe('Igor and Misko are viewing.');
|
||||||
|
expect(elementAlt.text()).toBe('Igor and Misko are viewing.');
|
||||||
|
|
||||||
$rootScope.viewCount = 3;
|
$rootScope.viewCount = 3;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('Igor, Misko and one other person are viewing.');
|
expect(element.text()).toBe('Igor, Misko and one other person are viewing.');
|
||||||
|
expect(elementAlt.text()).toBe('Igor, Misko and one other person are viewing.');
|
||||||
|
|
||||||
$rootScope.viewCount = 4;
|
$rootScope.viewCount = 4;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('Igor, Misko and 2 other people are viewing.');
|
expect(element.text()).toBe('Igor, Misko and 2 other people are viewing.');
|
||||||
|
expect(elementAlt.text()).toBe('Igor, Misko and 2 other people are viewing.');
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -165,23 +211,34 @@ describe('ngPluralize', function() {
|
||||||
"'one': '[[p1%% and one other person are viewing.'," +
|
"'one': '[[p1%% and one other person are viewing.'," +
|
||||||
"'other': '[[p1%% and {} other people are viewing.'}\">" +
|
"'other': '[[p1%% and {} other people are viewing.'}\">" +
|
||||||
"</ng:pluralize>")($rootScope);
|
"</ng:pluralize>")($rootScope);
|
||||||
|
elementAlt = $compile(
|
||||||
|
"<ng:pluralize count='viewCount' offset='1'" +
|
||||||
|
"when-0='Nobody is viewing.'" +
|
||||||
|
"when-1='[[p1%% is viewing.'" +
|
||||||
|
"when-one='[[p1%% and one other person are viewing.'" +
|
||||||
|
"when-other='[[p1%% and {} other people are viewing.'>" +
|
||||||
|
"</ng:pluralize>")($rootScope);
|
||||||
$rootScope.p1 = 'Igor';
|
$rootScope.p1 = 'Igor';
|
||||||
|
|
||||||
$rootScope.viewCount = 0;
|
$rootScope.viewCount = 0;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('Nobody is viewing.');
|
expect(element.text()).toBe('Nobody is viewing.');
|
||||||
|
expect(elementAlt.text()).toBe('Nobody is viewing.');
|
||||||
|
|
||||||
$rootScope.viewCount = 1;
|
$rootScope.viewCount = 1;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('Igor is viewing.');
|
expect(element.text()).toBe('Igor is viewing.');
|
||||||
|
expect(elementAlt.text()).toBe('Igor is viewing.');
|
||||||
|
|
||||||
$rootScope.viewCount = 2;
|
$rootScope.viewCount = 2;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('Igor and one other person are viewing.');
|
expect(element.text()).toBe('Igor and one other person are viewing.');
|
||||||
|
expect(elementAlt.text()).toBe('Igor and one other person are viewing.');
|
||||||
|
|
||||||
$rootScope.viewCount = 3;
|
$rootScope.viewCount = 3;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(element.text()).toBe('Igor and 2 other people are viewing.');
|
expect(element.text()).toBe('Igor and 2 other people are viewing.');
|
||||||
|
expect(elementAlt.text()).toBe('Igor and 2 other people are viewing.');
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue