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

Commit c66b4b6

Browse files
marclavallgalfaso
authored andcommitted
fix(ngPluralize): generate a warning when using a not defined rule
When using `ng-pluralize` and a rule is missing, then generate a warning Fix #10207
1 parent 66ceecc commit c66b4b6

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

src/ng/directive/ngPluralize.js

+12-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,14 @@ 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 (isUndefined(whenExpFn)) {
224+
$log.debug("ngPluralize: no rule defined for '" + count + "' in " + whenExp);
225+
watchRemover = noop;
226+
updateElementText();
227+
} else {
228+
watchRemover = scope.$watch(whenExpFn, updateElementText);
229+
}
220230
lastCount = count;
221231
}
222232
});

test/ng/directive/ngPluralizeSpec.js

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

149+
describe('undefined rule cases', function() {
150+
var $locale, $log;
151+
beforeEach(inject(function(_$locale_, _$log_) {
152+
$locale = _$locale_;
153+
$log = _$log_;
154+
}));
155+
afterEach(inject(function($log) {
156+
$log.reset();
157+
}));
158+
159+
it('should generate a warning when being asked to use a rule that is not defined',
160+
inject(function($rootScope, $compile) {
161+
element = $compile(
162+
'<ng:pluralize count="email"' +
163+
"when=\"{'0': 'Zero'," +
164+
"'one': 'Some text'," +
165+
"'other': 'Some text'}\">" +
166+
'</ng:pluralize>')($rootScope);
167+
$locale.pluralCat = function() {return "few";};
168+
169+
$rootScope.email = '3';
170+
expect($log.debug.logs).toEqual([]);
171+
$rootScope.$digest();
172+
expect(element.text()).toBe('');
173+
expect($log.debug.logs.shift())
174+
.toEqual(["ngPluralize: no rule defined for 'few' in {'0': 'Zero','one': 'Some text','other': 'Some text'}"]);
175+
}));
176+
177+
it('should empty the element content when using a rule that is not defined',
178+
inject(function($rootScope, $compile) {
179+
element = $compile(
180+
'<ng:pluralize count="email"' +
181+
"when=\"{'0': 'Zero'," +
182+
"'one': 'Some text'," +
183+
"'other': 'Some text'}\">" +
184+
'</ng:pluralize>')($rootScope);
185+
$locale.pluralCat = function(count) {return count === 1 ? "one" : "few";};
186+
187+
$rootScope.email = '0';
188+
$rootScope.$digest();
189+
expect(element.text()).toBe('Zero');
190+
191+
$rootScope.email = '3';
192+
$rootScope.$digest();
193+
expect(element.text()).toBe('');
194+
195+
$rootScope.email = '1';
196+
$rootScope.$digest();
197+
expect(element.text()).toBe('Some text');
198+
}));
199+
});
149200

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

0 commit comments

Comments
 (0)