From a170fc1a749effa98bfd1c2e1b30297ed47b451b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Galfas=C3=B3?= Date: Fri, 12 Jul 2013 17:20:40 -0300 Subject: [PATCH] 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 --- src/ng/directive/ngPluralize.js | 13 ++++-- test/ng/directive/ngPluralizeSpec.js | 63 ++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/src/ng/directive/ngPluralize.js b/src/ng/directive/ngPluralize.js index 6ab1e182f5f8..1cefc47682eb 100644 --- a/src/ng/directive/ngPluralize.js +++ b/src/ng/directive/ngPluralize.js @@ -174,13 +174,20 @@ var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interp restrict: 'EA', link: function(scope, element, attr) { 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, - whens = scope.$eval(whenExp), + whens = scope.$eval(whenExp) || {}, whensExpFns = {}, 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) { whensExpFns[key] = $interpolate(expression.replace(BRACE, startSymbol + numberExp + '-' + diff --git a/test/ng/directive/ngPluralizeSpec.js b/test/ng/directive/ngPluralizeSpec.js index 0ca2d23e952c..4d596efd0fa6 100644 --- a/test/ng/directive/ngPluralizeSpec.js +++ b/test/ng/directive/ngPluralizeSpec.js @@ -1,11 +1,13 @@ 'use strict'; describe('ngPluralize', function() { - var element; + var element, + elementAlt; afterEach(function(){ dealoc(element); + dealoc(elementAlt); }); @@ -13,10 +15,18 @@ describe('ngPluralize', function() { beforeEach(inject(function($rootScope, $compile) { element = $compile( '" + '')($rootScope); + elementAlt = $compile( + '" + + '')($rootScope); })); @@ -24,38 +34,52 @@ describe('ngPluralize', function() { $rootScope.email = 0; $rootScope.$digest(); expect(element.text()).toBe('You have no new email'); + expect(elementAlt.text()).toBe('You have no new email'); $rootScope.email = '0'; $rootScope.$digest(); expect(element.text()).toBe('You have no new email'); + expect(elementAlt.text()).toBe('You have no new email'); $rootScope.email = 1; $rootScope.$digest(); expect(element.text()).toBe('You have one new email'); + expect(elementAlt.text()).toBe('You have one new email'); $rootScope.email = 0.01; $rootScope.$digest(); 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.$digest(); expect(element.text()).toBe('You have 0.1 new emails'); + expect(elementAlt.text()).toBe('You have 0.1 new emails'); $rootScope.email = 2; $rootScope.$digest(); expect(element.text()).toBe('You have 2 new emails'); + expect(elementAlt.text()).toBe('You have 2 new emails'); $rootScope.email = -0.1; $rootScope.$digest(); 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.$digest(); expect(element.text()).toBe('You have -0.01 new emails'); + expect(elementAlt.text()).toBe('You have -0.01 new emails'); $rootScope.email = -2; $rootScope.$digest(); 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.$digest(); expect(element.text()).toBe(''); + expect(elementAlt.text()).toBe(''); $rootScope.email = null; $rootScope.$digest(); expect(element.text()).toBe(''); + expect(elementAlt.text()).toBe(''); $rootScope.email = undefined; $rootScope.$digest(); expect(element.text()).toBe(''); + expect(elementAlt.text()).toBe(''); $rootScope.email = 'a3'; $rootScope.$digest(); expect(element.text()).toBe(''); + expect(elementAlt.text()).toBe(''); $rootScope.email = '011'; $rootScope.$digest(); expect(element.text()).toBe('You have 11 new emails'); + expect(elementAlt.text()).toBe('You have 11 new emails'); $rootScope.email = '-011'; $rootScope.$digest(); expect(element.text()).toBe('You have -11 new emails'); + expect(elementAlt.text()).toBe('You have -11 new emails'); $rootScope.email = '1fff'; $rootScope.$digest(); expect(element.text()).toBe('You have one new email'); + expect(elementAlt.text()).toBe('You have one new email'); $rootScope.email = '0aa22'; $rootScope.$digest(); expect(element.text()).toBe('You have no new email'); + expect(elementAlt.text()).toBe('You have no new email'); $rootScope.email = '000001'; $rootScope.$digest(); 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() { it('should show single/plural strings with offset', inject(function($rootScope, $compile) { element = $compile( - "" + "")($rootScope); + elementAlt = $compile( + "" + + "")($rootScope); $rootScope.p1 = 'Igor'; $rootScope.p2 = 'Misko'; $rootScope.viewCount = 0; $rootScope.$digest(); expect(element.text()).toBe('Nobody is viewing.'); + expect(elementAlt.text()).toBe('Nobody is viewing.'); $rootScope.viewCount = 1; $rootScope.$digest(); expect(element.text()).toBe('Igor is viewing.'); + expect(elementAlt.text()).toBe('Igor is viewing.'); $rootScope.viewCount = 2; $rootScope.$digest(); expect(element.text()).toBe('Igor and Misko are viewing.'); + expect(elementAlt.text()).toBe('Igor and Misko are viewing.'); $rootScope.viewCount = 3; $rootScope.$digest(); 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.$digest(); 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.'," + "'other': '[[p1%% and {} other people are viewing.'}\">" + "")($rootScope); + elementAlt = $compile( + "" + + "")($rootScope); $rootScope.p1 = 'Igor'; $rootScope.viewCount = 0; $rootScope.$digest(); expect(element.text()).toBe('Nobody is viewing.'); + expect(elementAlt.text()).toBe('Nobody is viewing.'); $rootScope.viewCount = 1; $rootScope.$digest(); expect(element.text()).toBe('Igor is viewing.'); + expect(elementAlt.text()).toBe('Igor is viewing.'); $rootScope.viewCount = 2; $rootScope.$digest(); 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.$digest(); expect(element.text()).toBe('Igor and 2 other people are viewing.'); + expect(elementAlt.text()).toBe('Igor and 2 other people are viewing.'); }); }) });