Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a469b46

Browse files
committedNov 12, 2014
refactor(ngPluralize): minor refactoring to avoid repetition
Move regular expression outside of the `link` function (so it doesn't get created for every instance of `ngPluralize`. Replace `RegExp.test + String.replace + String.replace` with `RegExp.exec + string concatenation`.
1 parent 58cba58 commit a469b46

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed
 

‎src/ng/directive/ngPluralize.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -173,24 +173,27 @@
173173
</example>
174174
*/
175175
var ngPluralizeDirective = ['$locale', '$interpolate', '$parse', function($locale, $interpolate, $parse) {
176-
var BRACE = /{}/g;
176+
var BRACE = /{}/g,
177+
IS_WHEN = /^when(Minus)?(.+)$/;
178+
177179
return {
178180
restrict: 'EA',
179181
link: function(scope, element, attr) {
180182
var numberExp = attr.count,
181183
oneTimeCount = (numberExp.charAt(0) === ':') && (numberExp.charAt(1) === ':'),
182184
whenExp = attr.$attr.when && element.attr(attr.$attr.when), // we have {{}} in attrs
183185
offset = attr.offset || 0,
184-
whens = scope.$eval(whenExp) || {},
185-
whensExpFns = {},
186+
whens = scope.$eval(whenExp) || createMap(),
187+
whensExpFns = createMap(),
186188
startSymbol = $interpolate.startSymbol(),
187189
endSymbol = $interpolate.endSymbol(),
188-
isWhen = /^when(Minus)?(.+)$/;
190+
tmpMatch;
189191

190192
forEach(attr, function(expression, attributeName) {
191-
if (isWhen.test(attributeName)) {
192-
whens[lowercase(attributeName.replace('when', '').replace('Minus', '-'))] =
193-
element.attr(attr.$attr[attributeName]);
193+
tmpMatch = IS_WHEN.exec(attributeName);
194+
if (tmpMatch) {
195+
var whenKey = lowercase((tmpMatch[1] ? '-' : '') + tmpMatch[2]);
196+
whens[whenKey] = element.attr(attr.$attr[attributeName]);
194197
}
195198
});
196199
forEach(whens, function(expression, key) {

0 commit comments

Comments
 (0)
This repository has been archived.