Skip to content

Commit 7b7cfbd

Browse files
committed
fix (ngPluralize): generate a warning when being asked to use a rule that is not defined
Fix angular#10207
1 parent 30694c8 commit 7b7cfbd

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/ng/directive/ngPluralize.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
* <span ng-non-bindable>`{{personCount}}`</span>. The closed braces `{}` is a placeholder
5555
* for <span ng-non-bindable>{{numberExpression}}</span>.
5656
*
57+
* If no rule is defined for a category, then an empty string is displayed and a warning is generated.
58+
* Note that some locales define more categories than `one` and `other`. For example, fr-fr defines `few` and `many`.
59+
*
5760
* # Configuring ngPluralize with offset
5861
* The `offset` attribute allows further customization of pluralized text, which can result in
5962
* a better user experience. For example, instead of the message "4 people are viewing this document",
@@ -172,7 +175,7 @@
172175
</file>
173176
</example>
174177
*/
175-
var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interpolate) {
178+
var ngPluralizeDirective = ['$locale', '$interpolate', '$log', function($locale, $interpolate, $log) {
176179
var BRACE = /{}/g,
177180
IS_WHEN = /^when(Minus)?(.+)$/;
178181

@@ -216,7 +219,13 @@ var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interp
216219
// In JS `NaN !== NaN`, so we have to exlicitly check.
217220
if ((count !== lastCount) && !(countIsNaN && isNaN(lastCount))) {
218221
watchRemover();
219-
watchRemover = scope.$watch(whensExpFns[count], updateElementText);
222+
var whenExpFn = whensExpFns[count];
223+
if (angular.isUndefined(whenExpFn)) {
224+
$log.debug("ngPluralize: no rule defined for '" + count + "' in " + whenExp);
225+
}
226+
else {
227+
watchRemover = scope.$watch(whenExpFn, updateElementText);
228+
}
220229
lastCount = count;
221230
}
222231
});

test/ng/directive/ngPluralizeSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,30 @@ describe('ngPluralize', function() {
146146
}));
147147
});
148148

149+
describe('error cases', function() {
150+
var $locale, $log;
151+
beforeEach(inject(function (_$locale_, _$log_) {
152+
$locale = _$locale_;
153+
$log = _$log_;
154+
}));
155+
156+
it('should generate a warning when being asked to use a rule that is not defined',
157+
inject(function($rootScope, $compile) {
158+
element = $compile(
159+
'<ng:pluralize count="email"' +
160+
"when=\"{'0': 'Zero'," +
161+
"'one': 'Some text'," +
162+
"'other': 'Some text'}\">" +
163+
'</ng:pluralize>')($rootScope);
164+
$locale.pluralCat = function() {return "few";};
165+
$rootScope.email = '3';
166+
expect($log.debug.logs).toEqual([]);
167+
$rootScope.$digest();
168+
expect(element.text()).toBe('');
169+
expect($log.debug.logs.shift())
170+
.toEqual(["ngPluralize: no rule defined for 'few' in {'0': 'Zero','one': 'Some text','other': 'Some text'}"]);
171+
}));
172+
});
149173

150174
describe('deal with pluralized strings with offset', function() {
151175
it('should show single/plural strings with offset', inject(function($rootScope, $compile) {

0 commit comments

Comments
 (0)