Skip to content

Commit

Permalink
Rename angular.foreach to angular.forEach to make the api consistent.
Browse files Browse the repository at this point in the history
camelcase is used for other angular functions and forEach is also
used by EcmaScript standard.

- rename the internal as well as the external function name
- tweak the implementation of the function so that it doesn't
  clober it self when we extend the angular object with an
  object that has a forEach property equal to this forEach function

Closes angular#85
  • Loading branch information
IgorMinar committed Jan 10, 2011
1 parent c79aba9 commit 0a6cf70
Show file tree
Hide file tree
Showing 32 changed files with 101 additions and 96 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@

- In the light of the `eager-published` change, to complete the cleanup we renamed `$creation`
property of services to `eager` with its value being a boolean.
To transition, please rename all `$creation: 'eager'` declarations to `$eager: true`
To transition, please rename all `$creation: 'eager'` declarations to `$eager: true`.

- `angular.foreach` was renamed to `angular.forEach` to make the api consistent.


# <angular/> 0.9.8 astral-projection (2010-12-23) #
Expand Down
24 changes: 13 additions & 11 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,21 @@ var _undefined = undefined,
/**
* @workInProgress
* @ngdoc function
* @name angular.foreach
* @name angular.forEach
* @function
*
* @description
* Invokes the `iterator` function once for each item in `obj` collection. The collection can either
* be an object or an array. The `iterator` function is invoked with `iterator(value, key)`, where
* `value` is the value of an object property or an array element and `key` is the object property
* key or array element index. Optionally, `context` can be specified for the iterator function.
*
* Note: this function was previously known as `angular.foreach`.
*
<pre>
var values = {name: 'misko', gender: 'male'};
var log = [];
angular.foreach(values, function(value, key){
angular.forEach(values, function(value, key){
this.push(key + ': ' + value);
}, log);
expect(log).toEqual(['name: misko', 'gender:male']);
Expand All @@ -138,7 +140,7 @@ var _undefined = undefined,
* @param {Object} context Object to become context (`this`) for the iterator function.
* @returns {Objet|Array} Reference to `obj`.
*/
function foreach(obj, iterator, context) {
function forEach(obj, iterator, context) {
var key;
if (obj) {
if (isFunction(obj)){
Expand All @@ -147,7 +149,7 @@ function foreach(obj, iterator, context) {
iterator.call(context, obj[key], key);
}
}
} else if (obj.forEach) {
} else if (obj.forEach && obj.forEach !== forEach) {
obj.forEach(iterator, context);
} else if (isObject(obj) && isNumber(obj.length)) {
for (key = 0; key < obj.length; key++)
Expand All @@ -160,7 +162,7 @@ function foreach(obj, iterator, context) {
return obj;
}

function foreachSorted(obj, iterator, context) {
function forEachSorted(obj, iterator, context) {
var keys = [];
for (var key in obj) keys.push(key);
keys.sort();
Expand Down Expand Up @@ -197,9 +199,9 @@ function formatError(arg) {
* @param {...Object} src The source object(s).
*/
function extend(dst) {
foreach(arguments, function(obj){
forEach(arguments, function(obj){
if (obj !== dst) {
foreach(obj, function(value, key){
forEach(obj, function(value, key){
dst[key] = value;
});
}
Expand Down Expand Up @@ -459,7 +461,7 @@ function isVisible(element) {

function map(obj, iterator, context) {
var results = [];
foreach(obj, function(value, index, list) {
forEach(obj, function(value, index, list) {
results.push(iterator.call(context, value, index, list));
});
return results;
Expand Down Expand Up @@ -580,7 +582,7 @@ function copy(source, destination){
destination.push(copy(source[i]));
}
} else {
foreach(destination, function(value, key){
forEach(destination, function(value, key){
delete destination[key];
});
for ( var key in source) {
Expand Down Expand Up @@ -778,7 +780,7 @@ function compile(element, parentScope) {
*/
function parseKeyValue(/**string*/keyValue) {
var obj = {}, key_value, key;
foreach((keyValue || "").split('&'), function(keyValue){
forEach((keyValue || "").split('&'), function(keyValue){
if (keyValue) {
key_value = keyValue.split('=');
key = unescape(key_value[0]);
Expand All @@ -790,7 +792,7 @@ function parseKeyValue(/**string*/keyValue) {

function toKeyValue(obj) {
var parts = [];
foreach(obj, function(value, key) {
forEach(obj, function(value, key) {
parts.push(escape(key) + (value === true ? '' : '=' + escape(value)));
});
return parts.length ? parts.join('&') : '';
Expand Down
2 changes: 1 addition & 1 deletion src/AngularPublic.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extend(angular, {
'copy': copy,
'extend': extend,
'equals': equals,
'foreach': foreach,
'forEach': forEach,
'injector': createInjector,
'noop':noop,
'bind':bind,
Expand Down
2 changes: 1 addition & 1 deletion src/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function Browser(window, document, body, XHR, $log) {
* @methodOf angular.service.$browser
*/
self.poll = function() {
foreach(pollFns, function(pollFn){ pollFn(); });
forEach(pollFns, function(pollFn){ pollFn(); });
};

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Template.prototype = {
init: function(element, scope) {
var inits = {};
this.collectInits(element, inits, scope);
foreachSorted(inits, function(queue){
foreach(queue, function(fn) {fn();});
forEachSorted(inits, function(queue){
forEach(queue, function(fn) {fn();});
});
},

Expand All @@ -32,7 +32,7 @@ Template.prototype = {
scope.$onEval(childScope.$eval);
element.data($$scope, childScope);
}
foreach(this.inits, function(fn) {
forEach(this.inits, function(fn) {
queue.push(function() {
childScope.$tryEval(function(){
return childScope.$service(fn, childScope, element);
Expand Down Expand Up @@ -232,7 +232,7 @@ Compiler.prototype = {
for(var i=0, child=element[0].childNodes;
i<child.length; i++) {
if (isTextNode(child[i])) {
foreach(self.markup, function(markup){
forEach(self.markup, function(markup){
if (i<child.length) {
var textNode = jqLite(child[i]);
markup.call(selfApi, textNode.text(), textNode, element);
Expand All @@ -245,7 +245,7 @@ Compiler.prototype = {
if (directives) {
// Process attributes/directives
eachAttribute(element, function(value, name){
foreach(self.attrMarkup, function(markup){
forEach(self.attrMarkup, function(markup){
markup.call(selfApi, value, name, element);
});
});
Expand Down Expand Up @@ -287,6 +287,6 @@ function eachAttribute(element, fn){
}
attrValue[name] = value;
}
foreachSorted(attrValue, fn);
forEachSorted(attrValue, fn);
}

4 changes: 2 additions & 2 deletions src/Injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ function createInjector(providerScope, providers, cache) {
returnValue = cache[value];
} else if (isArray(value)) {
returnValue = [];
foreach(value, function(name) {
forEach(value, function(name) {
returnValue.push(inject(name));
});
} else if (isFunction(value)) {
returnValue = inject(value.$inject || []);
returnValue = value.apply(scope, concat(returnValue, arguments, 2));
} else if (isObject(value)) {
foreach(providers, function(provider, name){
forEach(providers, function(provider, name){
if (provider.$eager)
inject(name);

Expand Down
4 changes: 2 additions & 2 deletions src/JSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ function fromJson(json, useNative) {
throw e;
}

// TODO make foreach optionally recursive and remove this function
// TODO make forEach optionally recursive and remove this function
function transformDates(obj) {
if (isString(obj) && obj.length === DATE_ISOSTRING_LN) {
return angularString.toDate(obj);
} else if (isArray(obj) || isObject(obj)) {
foreach(obj, function(val, name) {
forEach(obj, function(val, name) {
obj[name] = transformDates(val);
});
}
Expand Down
12 changes: 6 additions & 6 deletions src/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function Route(template, defaults) {
this.template = template = template + '#';
this.defaults = defaults || {};
var urlParams = this.urlParams = {};
foreach(template.split(/\W/), function(param){
forEach(template.split(/\W/), function(param){
if (param && template.match(new RegExp(":" + param + "\\W"))) {
urlParams[param] = true;
}
Expand All @@ -17,13 +17,13 @@ Route.prototype = {
var self = this;
var url = this.template;
params = params || {};
foreach(this.urlParams, function(_, urlParam){
forEach(this.urlParams, function(_, urlParam){
var value = params[urlParam] || self.defaults[urlParam] || "";
url = url.replace(new RegExp(":" + urlParam + "(\\W)"), value + "$1");
});
url = url.replace(/\/?#$/, '');
var query = [];
foreachSorted(params, function(value, key){
forEachSorted(params, function(value, key){
if (!self.urlParams[key]) {
query.push(encodeURI(key) + '=' + encodeURI(value));
}
Expand Down Expand Up @@ -52,7 +52,7 @@ ResourceFactory.prototype = {
actions = extend({}, ResourceFactory.DEFAULT_ACTIONS, actions);
function extractParams(data){
var ids = {};
foreach(paramDefaults || {}, function(value, key){
forEach(paramDefaults || {}, function(value, key){
ids[key] = value.charAt && value.charAt(0) == '@' ? getter(data, value.substr(1)) : value;
});
return ids;
Expand All @@ -62,7 +62,7 @@ ResourceFactory.prototype = {
copy(value || {}, this);
}

foreach(actions, function(action, name){
forEach(actions, function(action, name){
var isPostOrPut = action.method == 'POST' || action.method == 'PUT';
Resource[name] = function (a1, a2, a3) {
var params = {};
Expand Down Expand Up @@ -97,7 +97,7 @@ ResourceFactory.prototype = {
if (status == 200) {
if (action.isArray) {
value.length = 0;
foreach(response, function(item){
forEach(response, function(item){
value.push(new Resource(item));
});
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/Scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var scopeId = 0,
getterFnCache = {},
compileCache = {},
JS_KEYWORDS = {};
foreach(
forEach(
("abstract,boolean,break,byte,case,catch,char,class,const,continue,debugger,default," +
"delete,do,double,else,enum,export,extends,false,final,finally,float,for,function,goto," +
"if,implements,import,ininstanceof,intinterface,long,native,new,null,package,private," +
Expand All @@ -61,7 +61,7 @@ function getterFn(path){
if (fn) return fn;

var code = 'var l, fn, t;\n';
foreach(path.split('.'), function(key) {
forEach(path.split('.'), function(key) {
key = (JS_KEYWORDS[key]) ? '["' + key + '"]' : '.' + key;
code += 'if(!s) return s;\n' +
'l=s;\n' +
Expand Down Expand Up @@ -575,7 +575,7 @@ function createScope(parent, providers, instanceCache) {
$become: function(Class) {
if (isFunction(Class)) {
instance.constructor = Class;
foreach(Class.prototype, function(fn, name){
forEach(Class.prototype, function(fn, name){
instance[name] = bind(instance, fn);
});
instance.$service.apply(instance, concat([Class, instance], arguments, 1));
Expand Down
4 changes: 2 additions & 2 deletions src/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ var angularArray = {
'count':function(array, condition) {
if (!condition) return array.length;
var fn = angular['Function']['compile'](condition), count = 0;
foreach(array, function(value){
forEach(array, function(value){
if (fn(value)) {
count ++;
}
Expand Down Expand Up @@ -747,7 +747,7 @@ var angularFunction = {

function defineApi(dst, chain){
angular[dst] = angular[dst] || {};
foreach(chain, function(parent){
forEach(chain, function(parent){
extend(angular[dst], parent);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function compileBindTemplate(template){
var fn = bindTemplateCache[template];
if (!fn) {
var bindings = [];
foreach(parseBindings(template), function(text){
forEach(parseBindings(template), function(text){
var exp = binding(text);
bindings.push(exp ? function(element){
var error, value = this.$tryEval(exp, function(e){
Expand Down
2 changes: 1 addition & 1 deletion src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ angularFilter.date = function(date, format) {
parts = concat(parts, DATE_FORMATS_SPLIT.exec(format), 1);
format = parts.pop();
}
foreach(parts, function(value){
forEach(parts, function(value){
fn = DATE_FORMATS[value];
text += fn ? fn(date) : value;
});
Expand Down
2 changes: 1 addition & 1 deletion src/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ angularFormatter.list = formatter(
function(obj) { return obj ? obj.join(", ") : obj; },
function(value) {
var list = [];
foreach((value || '').split(','), function(item){
forEach((value || '').split(','), function(item){
item = trim(item);
if (item) list.push(item);
});
Expand Down
10 changes: 5 additions & 5 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function jqClearData(element) {
var cacheId = element[jqName],
cache = jqCache[cacheId];
if (cache) {
foreach(cache.bind || {}, function(fn, type){
forEach(cache.bind || {}, function(fn, type){
removeEventListenerFn(element, type, fn);
});
delete jqCache[cacheId];
Expand Down Expand Up @@ -106,7 +106,7 @@ JQLite.prototype = {
bind = self.data('bind'),
eventHandler;
if (!bind) this.data('bind', bind = {});
foreach(type.split(' '), function(type){
forEach(type.split(' '), function(type){
eventHandler = bind[type];
if (!eventHandler) {
bind[type] = eventHandler = function(event) {
Expand All @@ -120,7 +120,7 @@ JQLite.prototype = {
event.cancelBubble = true; //ie
};
}
foreach(eventHandler.fns, function(fn){
forEach(eventHandler.fns, function(fn){
fn.call(self, event);
});
};
Expand All @@ -142,7 +142,7 @@ JQLite.prototype = {
append: function(node) {
var self = this[0];
node = jqLite(node);
foreach(node, function(child){
forEach(node, function(child){
self.appendChild(child);
});
},
Expand Down Expand Up @@ -200,7 +200,7 @@ JQLite.prototype = {
attr: function(name, value){
var e = this[0];
if (isObject(name)) {
foreach(name, function(value, name){
forEach(name, function(value, name){
e.setAttribute(name, value);
});
} else if (isDefined(value)) {
Expand Down
2 changes: 1 addition & 1 deletion src/markups.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ angularTextMarkup('{{}}', function(text, textNode, parentElement) {
parentElement.attr('ng:bind-template', text);
} else {
var cursor = textNode, newElement;
foreach(parseBindings(text), function(text){
forEach(parseBindings(text), function(text){
var exp = binding(text);
if (exp) {
newElement = self.element('span');
Expand Down
2 changes: 1 addition & 1 deletion src/sanitizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ function htmlSanitizeWriter(buf){
if (!ignore && validElements[tag] == true) {
out('<');
out(tag);
foreach(attrs, function(value, key){
forEach(attrs, function(value, key){
var lkey=lowercase(key);
if (validAttrs[lkey]==true && (uriAttrs[lkey]!==true || value.match(URI_REGEXP))) {
out(' ');
Expand Down
Loading

0 comments on commit 0a6cf70

Please sign in to comment.