Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a170fc1

Browse files
lgalfasopkozlowski-opensource
authored andcommitted
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
1 parent 1e649c5 commit a170fc1

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

src/ng/directive/ngPluralize.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,20 @@ var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interp
174174
restrict: 'EA',
175175
link: function(scope, element, attr) {
176176
var numberExp = attr.count,
177-
whenExp = element.attr(attr.$attr.when), // this is because we have {{}} in attrs
177+
whenExp = attr.$attr.when && element.attr(attr.$attr.when), // we have {{}} in attrs
178178
offset = attr.offset || 0,
179-
whens = scope.$eval(whenExp),
179+
whens = scope.$eval(whenExp) || {},
180180
whensExpFns = {},
181181
startSymbol = $interpolate.startSymbol(),
182-
endSymbol = $interpolate.endSymbol();
182+
endSymbol = $interpolate.endSymbol(),
183+
isWhen = /^when(Minus)?(.+)$/;
183184

185+
forEach(attr, function(expression, attributeName) {
186+
if (isWhen.test(attributeName)) {
187+
whens[lowercase(attributeName.replace('when', '').replace('Minus', '-'))] =
188+
element.attr(attr.$attr[attributeName]);
189+
}
190+
});
184191
forEach(whens, function(expression, key) {
185192
whensExpFns[key] =
186193
$interpolate(expression.replace(BRACE, startSymbol + numberExp + '-' +

test/ng/directive/ngPluralizeSpec.js

+60-3
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,133 @@
11
'use strict';
22

33
describe('ngPluralize', function() {
4-
var element;
4+
var element,
5+
elementAlt;
56

67

78
afterEach(function(){
89
dealoc(element);
10+
dealoc(elementAlt);
911
});
1012

1113

1214
describe('deal with pluralized strings without offset', function() {
1315
beforeEach(inject(function($rootScope, $compile) {
1416
element = $compile(
1517
'<ng:pluralize count="email"' +
16-
"when=\"{'0': 'You have no new email'," +
18+
"when=\"{'-1': 'You have negative email. Whohoo!'," +
19+
"'0': 'You have no new email'," +
1720
"'one': 'You have one new email'," +
1821
"'other': 'You have {} new emails'}\">" +
1922
'</ng:pluralize>')($rootScope);
23+
elementAlt = $compile(
24+
'<ng:pluralize count="email" ' +
25+
"when-minus-1='You have negative email. Whohoo!' " +
26+
"when-0='You have no new email' " +
27+
"when-one='You have one new email' " +
28+
"when-other='You have {} new emails'>" +
29+
'</ng:pluralize>')($rootScope);
2030
}));
2131

2232

2333
it('should show single/plural strings', inject(function($rootScope) {
2434
$rootScope.email = 0;
2535
$rootScope.$digest();
2636
expect(element.text()).toBe('You have no new email');
37+
expect(elementAlt.text()).toBe('You have no new email');
2738

2839
$rootScope.email = '0';
2940
$rootScope.$digest();
3041
expect(element.text()).toBe('You have no new email');
42+
expect(elementAlt.text()).toBe('You have no new email');
3143

3244
$rootScope.email = 1;
3345
$rootScope.$digest();
3446
expect(element.text()).toBe('You have one new email');
47+
expect(elementAlt.text()).toBe('You have one new email');
3548

3649
$rootScope.email = 0.01;
3750
$rootScope.$digest();
3851
expect(element.text()).toBe('You have 0.01 new emails');
52+
expect(elementAlt.text()).toBe('You have 0.01 new emails');
3953

4054
$rootScope.email = '0.1';
4155
$rootScope.$digest();
4256
expect(element.text()).toBe('You have 0.1 new emails');
57+
expect(elementAlt.text()).toBe('You have 0.1 new emails');
4358

4459
$rootScope.email = 2;
4560
$rootScope.$digest();
4661
expect(element.text()).toBe('You have 2 new emails');
62+
expect(elementAlt.text()).toBe('You have 2 new emails');
4763

4864
$rootScope.email = -0.1;
4965
$rootScope.$digest();
5066
expect(element.text()).toBe('You have -0.1 new emails');
67+
expect(elementAlt.text()).toBe('You have -0.1 new emails');
5168

5269
$rootScope.email = '-0.01';
5370
$rootScope.$digest();
5471
expect(element.text()).toBe('You have -0.01 new emails');
72+
expect(elementAlt.text()).toBe('You have -0.01 new emails');
5573

5674
$rootScope.email = -2;
5775
$rootScope.$digest();
5876
expect(element.text()).toBe('You have -2 new emails');
77+
expect(elementAlt.text()).toBe('You have -2 new emails');
78+
79+
$rootScope.email = -1;
80+
$rootScope.$digest();
81+
expect(element.text()).toBe('You have negative email. Whohoo!');
82+
expect(elementAlt.text()).toBe('You have negative email. Whohoo!');
5983
}));
6084

6185

6286
it('should show single/plural strings with mal-formed inputs', inject(function($rootScope) {
6387
$rootScope.email = '';
6488
$rootScope.$digest();
6589
expect(element.text()).toBe('');
90+
expect(elementAlt.text()).toBe('');
6691

6792
$rootScope.email = null;
6893
$rootScope.$digest();
6994
expect(element.text()).toBe('');
95+
expect(elementAlt.text()).toBe('');
7096

7197
$rootScope.email = undefined;
7298
$rootScope.$digest();
7399
expect(element.text()).toBe('');
100+
expect(elementAlt.text()).toBe('');
74101

75102
$rootScope.email = 'a3';
76103
$rootScope.$digest();
77104
expect(element.text()).toBe('');
105+
expect(elementAlt.text()).toBe('');
78106

79107
$rootScope.email = '011';
80108
$rootScope.$digest();
81109
expect(element.text()).toBe('You have 11 new emails');
110+
expect(elementAlt.text()).toBe('You have 11 new emails');
82111

83112
$rootScope.email = '-011';
84113
$rootScope.$digest();
85114
expect(element.text()).toBe('You have -11 new emails');
115+
expect(elementAlt.text()).toBe('You have -11 new emails');
86116

87117
$rootScope.email = '1fff';
88118
$rootScope.$digest();
89119
expect(element.text()).toBe('You have one new email');
120+
expect(elementAlt.text()).toBe('You have one new email');
90121

91122
$rootScope.email = '0aa22';
92123
$rootScope.$digest();
93124
expect(element.text()).toBe('You have no new email');
125+
expect(elementAlt.text()).toBe('You have no new email');
94126

95127
$rootScope.email = '000001';
96128
$rootScope.$digest();
97129
expect(element.text()).toBe('You have one new email');
130+
expect(elementAlt.text()).toBe('You have one new email');
98131
}));
99132
});
100133

@@ -117,35 +150,48 @@ describe('ngPluralize', function() {
117150
describe('deal with pluralized strings with offset', function() {
118151
it('should show single/plural strings with offset', inject(function($rootScope, $compile) {
119152
element = $compile(
120-
"<ng:pluralize count=\"viewCount\" offset=2 " +
153+
"<ng:pluralize count='viewCount' offset='2' " +
121154
"when=\"{'0': 'Nobody is viewing.'," +
122155
"'1': '{{p1}} is viewing.'," +
123156
"'2': '{{p1}} and {{p2}} are viewing.'," +
124157
"'one': '{{p1}}, {{p2}} and one other person are viewing.'," +
125158
"'other': '{{p1}}, {{p2}} and {} other people are viewing.'}\">" +
126159
"</ng:pluralize>")($rootScope);
160+
elementAlt = $compile(
161+
"<ng:pluralize count='viewCount' offset='2' " +
162+
"when-0='Nobody is viewing.'" +
163+
"when-1='{{p1}} is viewing.'" +
164+
"when-2='{{p1}} and {{p2}} are viewing.'" +
165+
"when-one='{{p1}}, {{p2}} and one other person are viewing.'" +
166+
"when-other='{{p1}}, {{p2}} and {} other people are viewing.'>" +
167+
"</ng:pluralize>")($rootScope);
127168
$rootScope.p1 = 'Igor';
128169
$rootScope.p2 = 'Misko';
129170

130171
$rootScope.viewCount = 0;
131172
$rootScope.$digest();
132173
expect(element.text()).toBe('Nobody is viewing.');
174+
expect(elementAlt.text()).toBe('Nobody is viewing.');
133175

134176
$rootScope.viewCount = 1;
135177
$rootScope.$digest();
136178
expect(element.text()).toBe('Igor is viewing.');
179+
expect(elementAlt.text()).toBe('Igor is viewing.');
137180

138181
$rootScope.viewCount = 2;
139182
$rootScope.$digest();
140183
expect(element.text()).toBe('Igor and Misko are viewing.');
184+
expect(elementAlt.text()).toBe('Igor and Misko are viewing.');
141185

142186
$rootScope.viewCount = 3;
143187
$rootScope.$digest();
144188
expect(element.text()).toBe('Igor, Misko and one other person are viewing.');
189+
expect(elementAlt.text()).toBe('Igor, Misko and one other person are viewing.');
145190

146191
$rootScope.viewCount = 4;
147192
$rootScope.$digest();
148193
expect(element.text()).toBe('Igor, Misko and 2 other people are viewing.');
194+
expect(elementAlt.text()).toBe('Igor, Misko and 2 other people are viewing.');
149195
}));
150196
});
151197

@@ -165,23 +211,34 @@ describe('ngPluralize', function() {
165211
"'one': '[[p1%% and one other person are viewing.'," +
166212
"'other': '[[p1%% and {} other people are viewing.'}\">" +
167213
"</ng:pluralize>")($rootScope);
214+
elementAlt = $compile(
215+
"<ng:pluralize count='viewCount' offset='1'" +
216+
"when-0='Nobody is viewing.'" +
217+
"when-1='[[p1%% is viewing.'" +
218+
"when-one='[[p1%% and one other person are viewing.'" +
219+
"when-other='[[p1%% and {} other people are viewing.'>" +
220+
"</ng:pluralize>")($rootScope);
168221
$rootScope.p1 = 'Igor';
169222

170223
$rootScope.viewCount = 0;
171224
$rootScope.$digest();
172225
expect(element.text()).toBe('Nobody is viewing.');
226+
expect(elementAlt.text()).toBe('Nobody is viewing.');
173227

174228
$rootScope.viewCount = 1;
175229
$rootScope.$digest();
176230
expect(element.text()).toBe('Igor is viewing.');
231+
expect(elementAlt.text()).toBe('Igor is viewing.');
177232

178233
$rootScope.viewCount = 2;
179234
$rootScope.$digest();
180235
expect(element.text()).toBe('Igor and one other person are viewing.');
236+
expect(elementAlt.text()).toBe('Igor and one other person are viewing.');
181237

182238
$rootScope.viewCount = 3;
183239
$rootScope.$digest();
184240
expect(element.text()).toBe('Igor and 2 other people are viewing.');
241+
expect(elementAlt.text()).toBe('Igor and 2 other people are viewing.');
185242
});
186243
})
187244
});

0 commit comments

Comments
 (0)