From 0a6cf70debc6440685af3f9ea96a66450e4f4ed7 Mon Sep 17 00:00:00 2001
From: Igor Minar
Date: Fri, 7 Jan 2011 22:02:23 -0800
Subject: Rename angular.foreach to angular.forEach to make the api consistent.
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 #85
---
CHANGELOG.md | 4 +++-
src/Angular.js | 24 +++++++++++++-----------
src/AngularPublic.js | 2 +-
src/Browser.js | 2 +-
src/Compiler.js | 12 ++++++------
src/Injector.js | 4 ++--
src/JSON.js | 4 ++--
src/Resource.js | 12 ++++++------
src/Scope.js | 6 +++---
src/apis.js | 4 ++--
src/directives.js | 2 +-
src/filters.js | 2 +-
src/formatters.js | 2 +-
src/jqLite.js | 10 +++++-----
src/markups.js | 2 +-
src/sanitizer.js | 2 +-
src/scenario/Describe.js | 10 +++++-----
src/scenario/ObjectModel.js | 2 +-
src/scenario/Runner.js | 10 +++++-----
src/scenario/Scenario.js | 4 ++--
src/scenario/SpecRunner.js | 2 +-
src/scenario/dsl.js | 4 ++--
src/scenario/output/Html.js | 2 +-
src/scenario/output/Xml.js | 6 +++---
src/services.js | 20 ++++++++++----------
src/widgets.js | 12 ++++++------
test/BrowserSpecs.js | 2 +-
test/angular-mocks.js | 4 ++--
test/scenario/RunnerSpec.js | 2 +-
test/scenario/dslSpec.js | 6 +++---
test/scenario/mocks.js | 2 +-
test/testabilityPatch.js | 15 ++++++++-------
32 files changed, 101 insertions(+), 96 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a72281dc..0b965c4f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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.
#
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']);
@@ -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)){
@@ -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++)
@@ -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();
@@ -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;
});
}
@@ -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;
@@ -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) {
@@ -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]);
@@ -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('&') : '';
diff --git a/src/AngularPublic.js b/src/AngularPublic.js
index ab37a772..ec41d0d9 100644
--- a/src/AngularPublic.js
+++ b/src/AngularPublic.js
@@ -30,7 +30,7 @@ extend(angular, {
'copy': copy,
'extend': extend,
'equals': equals,
- 'foreach': foreach,
+ 'forEach': forEach,
'injector': createInjector,
'noop':noop,
'bind':bind,
diff --git a/src/Browser.js b/src/Browser.js
index 377c740c..eb6afb3c 100644
--- a/src/Browser.js
+++ b/src/Browser.js
@@ -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(); });
};
/**
diff --git a/src/Compiler.js b/src/Compiler.js
index a18341f4..fdce334c 100644
--- a/src/Compiler.js
+++ b/src/Compiler.js
@@ -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();});
});
},
@@ -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);
@@ -232,7 +232,7 @@ Compiler.prototype = {
for(var i=0, child=element[0].childNodes;
i').find('div:last');
context.attr('id', name);
diff --git a/src/scenario/SpecRunner.js b/src/scenario/SpecRunner.js
index f3bbe2e6..0e068f95 100644
--- a/src/scenario/SpecRunner.js
+++ b/src/scenario/SpecRunner.js
@@ -112,7 +112,7 @@ angular.scenario.SpecRunner.prototype.addFutureAction = function(name, behavior,
var args = Array.prototype.slice.call(arguments, 1);
selector = (self.selector || '') + ' ' + (selector || '');
selector = _jQuery.trim(selector) || '*';
- angular.foreach(args, function(value, index) {
+ angular.forEach(args, function(value, index) {
selector = selector.replace('$' + (index + 1), value);
});
var result = $document.find(selector);
diff --git a/src/scenario/dsl.js b/src/scenario/dsl.js
index 2ba61a52..4f967772 100644
--- a/src/scenario/dsl.js
+++ b/src/scenario/dsl.js
@@ -331,7 +331,7 @@ angular.scenario.dsl('element', function() {
});
};
- angular.foreach(KEY_VALUE_METHODS, function(methodName) {
+ angular.forEach(KEY_VALUE_METHODS, function(methodName) {
chain[methodName] = function(name, value) {
var futureName = "element '" + this.label + "' get " + methodName + " '" + name + "'";
if (angular.isDefined(value)) {
@@ -344,7 +344,7 @@ angular.scenario.dsl('element', function() {
};
});
- angular.foreach(VALUE_METHODS, function(methodName) {
+ angular.forEach(VALUE_METHODS, function(methodName) {
chain[methodName] = function(value) {
var futureName = "element '" + this.label + "' " + methodName;
if (angular.isDefined(value)) {
diff --git a/src/scenario/output/Html.js b/src/scenario/output/Html.js
index 4a682b9a..6e2e20f3 100644
--- a/src/scenario/output/Html.js
+++ b/src/scenario/output/Html.js
@@ -121,7 +121,7 @@ angular.scenario.output('html', function(context, runner) {
*/
function findContext(spec) {
var currentContext = context.find('#specs');
- angular.foreach(model.getDefinitionPath(spec), function(defn) {
+ angular.forEach(model.getDefinitionPath(spec), function(defn) {
var id = 'describe-' + defn.id;
if (!context.find('#' + id).length) {
currentContext.find('> .test-children').append(
diff --git a/src/scenario/output/Xml.js b/src/scenario/output/Xml.js
index cbc55ee7..e8c7f0e3 100644
--- a/src/scenario/output/Xml.js
+++ b/src/scenario/output/Xml.js
@@ -17,7 +17,7 @@ angular.scenario.output('xml', function(context, runner) {
* @param {Object} tree node to serialize
*/
function serializeXml(context, tree) {
- angular.foreach(tree.children, function(child) {
+ angular.forEach(tree.children, function(child) {
var describeContext = $(' ');
describeContext.attr('id', child.id);
describeContext.attr('name', child.name);
@@ -26,14 +26,14 @@ angular.scenario.output('xml', function(context, runner) {
});
var its = $(' ');
context.append(its);
- angular.foreach(tree.specs, function(spec) {
+ angular.forEach(tree.specs, function(spec) {
var it = $(' ');
it.attr('id', spec.id);
it.attr('name', spec.name);
it.attr('duration', spec.duration);
it.attr('status', spec.status);
its.append(it);
- angular.foreach(spec.steps, function(step) {
+ angular.forEach(spec.steps, function(step) {
var stepContext = $(' ');
stepContext.attr('name', step.name);
stepContext.attr('duration', step.duration);
diff --git a/src/services.js b/src/services.js
index 42d0622f..2e5c1893 100644
--- a/src/services.js
+++ b/src/services.js
@@ -369,7 +369,7 @@ angularServiceInject("$log", function($window){
if (logFn.apply) {
return function(){
var args = [];
- foreach(arguments, function(arg){
+ forEach(arguments, function(arg){
args.push(formatError(arg));
});
return logFn.apply(console, args);
@@ -556,7 +556,7 @@ angularServiceInject("$invalidWidgets", function(){
/** Return count of all invalid widgets that are currently visible */
invalidWidgets.visible = function() {
var count = 0;
- foreach(invalidWidgets, function(widget){
+ forEach(invalidWidgets, function(widget){
count = count + (isVisible(widget) ? 1 : 0);
});
return count;
@@ -596,7 +596,7 @@ function switchRouteMatcher(on, when, dstName) {
var regex = '^' + when.replace(/[\.\\\(\)\^\$]/g, "\$1") + '$',
params = [],
dst = {};
- foreach(when.split(/\W/), function(param){
+ forEach(when.split(/\W/), function(param){
if (param) {
var paramRegExp = new RegExp(":" + param + "([\\W])");
if (regex.match(paramRegExp)) {
@@ -607,7 +607,7 @@ function switchRouteMatcher(on, when, dstName) {
});
var match = on.match(new RegExp(regex));
if (match) {
- foreach(params, function(name, index){
+ forEach(params, function(name, index){
dst[name] = match[index + 1];
});
if (dstName) this.$set(dstName, dst);
@@ -716,7 +716,7 @@ angularServiceInject('$route', function(location) {
function updateRoute(){
var childScope;
$route.current = _null;
- angular.foreach(routes, function(routeParams, route) {
+ angular.forEach(routes, function(routeParams, route) {
if (!childScope) {
var pathParams = matcher(location.hashPath, route);
if (pathParams) {
@@ -728,7 +728,7 @@ angularServiceInject('$route', function(location) {
}
}
});
- angular.foreach(onChange, parentScope.$tryEval);
+ angular.forEach(onChange, parentScope.$tryEval);
if (childScope) {
childScope.$become($route.current.controller);
}
@@ -817,7 +817,7 @@ angularServiceInject('$xhr.bulk', function($xhr, $error, $log){
post = _null;
}
var currentQueue;
- foreach(bulkXHR.urls, function(queue){
+ forEach(bulkXHR.urls, function(queue){
if (isFunction(queue.match) ? queue.match(url) : queue.match.exec(url)) {
currentQueue = queue;
}
@@ -831,13 +831,13 @@ angularServiceInject('$xhr.bulk', function($xhr, $error, $log){
}
bulkXHR.urls = {};
bulkXHR.flush = function(callback){
- foreach(bulkXHR.urls, function(queue, url){
+ forEach(bulkXHR.urls, function(queue, url){
var currentRequests = queue.requests;
if (currentRequests && currentRequests.length) {
queue.requests = [];
queue.callbacks = [];
$xhr('POST', url, {requests:currentRequests}, function(code, response){
- foreach(response, function(response, i){
+ forEach(response, function(response, i){
try {
if (response.status == 200) {
(currentRequests[i].callback || noop)(response.status, response.response);
@@ -926,7 +926,7 @@ angularServiceInject('$xhr.cache', function($xhr, $defer){
cache.data[url] = { value: response };
var callbacks = inflight[url].callbacks;
delete inflight[url];
- foreach(callbacks, function(callback){
+ forEach(callbacks, function(callback){
try {
(callback||noop)(status, copy(response));
} catch(e) {
diff --git a/src/widgets.js b/src/widgets.js
index 5ff4a28f..5f159990 100644
--- a/src/widgets.js
+++ b/src/widgets.js
@@ -359,15 +359,15 @@ function optionsAccessor(scope, element) {
return {
get: function(){
var values = [];
- foreach(options, function(option){
+ forEach(options, function(option){
if (option.selected) values.push(option.value);
});
return values;
},
set: function(values){
var keys = {};
- foreach(values, function(value){ keys[value] = true; });
- foreach(options, function(option){
+ forEach(values, function(value){ keys[value] = true; });
+ forEach(options, function(option){
option.selected = keys[option.value];
});
}
@@ -698,7 +698,7 @@ var ngSwitch = angularWidget('ng:switch', function (element){
if (isString(when)) {
switchCase.when = function(scope, value){
var args = [value, when];
- foreach(usingExprParams, function(arg){
+ forEach(usingExprParams, function(arg){
args.push(arg);
});
return usingFn.apply(scope, args);
@@ -711,7 +711,7 @@ var ngSwitch = angularWidget('ng:switch', function (element){
});
// this needs to be here for IE
- foreach(cases, function(_case){
+ forEach(cases, function(_case){
_case.element.remove();
});
@@ -722,7 +722,7 @@ var ngSwitch = angularWidget('ng:switch', function (element){
var found = false;
element.html('');
childScope = createScope(scope);
- foreach(cases, function(switchCase){
+ forEach(cases, function(switchCase){
if (!found && switchCase.when(childScope, value)) {
found = true;
var caseElement = quickClone(switchCase.element);
diff --git a/test/BrowserSpecs.js b/test/BrowserSpecs.js
index a632f2bd..1e5512b0 100644
--- a/test/BrowserSpecs.js
+++ b/test/BrowserSpecs.js
@@ -7,7 +7,7 @@ describe('browser', function(){
}
fakeSetTimeout.flush = function() {
- foreach(setTimeoutQueue, function(fn) {
+ forEach(setTimeoutQueue, function(fn) {
fn();
});
};
diff --git a/test/angular-mocks.js b/test/angular-mocks.js
index 5a4e1de5..65dfb12f 100644
--- a/test/angular-mocks.js
+++ b/test/angular-mocks.js
@@ -141,7 +141,7 @@ function MockBrowser() {
MockBrowser.prototype = {
poll: function poll(){
- angular.foreach(this.pollFns, function(pollFn){
+ angular.forEach(this.pollFns, function(pollFn){
pollFn();
});
},
@@ -304,7 +304,7 @@ function TzDate(offset, timestamp) {
'setYear', 'toDateString', 'toJSON', 'toGMTString', 'toLocaleFormat', 'toLocaleString',
'toLocaleTimeString', 'toSource', 'toString', 'toTimeString', 'toUTCString', 'valueOf'];
- angular.foreach(unimplementedMethods, function(methodName) {
+ angular.forEach(unimplementedMethods, function(methodName) {
this[methodName] = function() {
throw {
name: "MethodNotImplemented",
diff --git a/test/scenario/RunnerSpec.js b/test/scenario/RunnerSpec.js
index 059dd874..5ebe5690 100644
--- a/test/scenario/RunnerSpec.js
+++ b/test/scenario/RunnerSpec.js
@@ -54,7 +54,7 @@ describe('angular.scenario.Runner', function() {
});
it('should publish the functions in the public API', function() {
- angular.foreach(runner.api, function(fn, name) {
+ angular.forEach(runner.api, function(fn, name) {
var func;
if (name in $window) {
func = $window[name];
diff --git a/test/scenario/dslSpec.js b/test/scenario/dslSpec.js
index 6551b6fc..71dfde3c 100644
--- a/test/scenario/dslSpec.js
+++ b/test/scenario/dslSpec.js
@@ -28,7 +28,7 @@ describe("angular.scenario.dsl", function() {
});
};
$root.dsl = {};
- angular.foreach(angular.scenario.dsl, function(fn, name) {
+ angular.forEach(angular.scenario.dsl, function(fn, name) {
$root.dsl[name] = function() {
return fn.call($root).apply($root, arguments);
};
@@ -281,7 +281,7 @@ describe("angular.scenario.dsl", function() {
it('should add all jQuery key/value methods', function() {
var METHODS = ['css', 'attr'];
var chain = $root.dsl.element('input');
- angular.foreach(METHODS, function(name) {
+ angular.forEach(METHODS, function(name) {
expect(angular.isFunction(chain[name])).toBeTruthy();
});
});
@@ -316,7 +316,7 @@ describe("angular.scenario.dsl", function() {
'innerWidth', 'outerWidth', 'position', 'scrollLeft', 'scrollTop', 'offset'
];
var chain = $root.dsl.element('input');
- angular.foreach(METHODS, function(name) {
+ angular.forEach(METHODS, function(name) {
expect(angular.isFunction(chain[name])).toBeTruthy();
});
});
diff --git a/test/scenario/mocks.js b/test/scenario/mocks.js
index 5cd2f30a..616e5d63 100644
--- a/test/scenario/mocks.js
+++ b/test/scenario/mocks.js
@@ -35,7 +35,7 @@ angular.scenario.testing.MockRunner.prototype.on = function(eventName, fn) {
angular.scenario.testing.MockRunner.prototype.emit = function(eventName) {
var args = Array.prototype.slice.call(arguments, 1);
- angular.foreach(this.listeners[eventName] || [], function(fn) {
+ angular.forEach(this.listeners[eventName] || [], function(fn) {
fn.apply(this, args);
});
};
diff --git a/test/testabilityPatch.js b/test/testabilityPatch.js
index 6cbf91e9..224a8915 100644
--- a/test/testabilityPatch.js
+++ b/test/testabilityPatch.js
@@ -56,10 +56,10 @@ afterEach(clearJqCache);
function clearJqCache(){
var count = 0;
- foreachSorted(jqCache, function(value, key){
+ forEachSorted(jqCache, function(value, key){
count ++;
delete jqCache[key];
- foreach(value, function(value, key){
+ forEach(value, function(value, key){
if (value.$element)
dump(key, sortedHtml(value.$element));
else
@@ -91,7 +91,7 @@ extend(angular, {
'copy': copy,
'extend': extend,
'equals': equals,
- 'foreach': foreach,
+ 'forEach': forEach,
'noop':noop,
'bind':bind,
'toJson': toJson,
@@ -103,13 +103,14 @@ extend(angular, {
'isFunction': isFunction,
'isObject': isObject,
'isNumber': isNumber,
- 'isArray': isArray
+ 'isArray': isArray,
+ 'forEach': forEach
});
function sortedHtml(element, showNgClass) {
var html = "";
- foreach(jqLite(element), function toString(node) {
+ forEach(jqLite(element), function toString(node) {
if (node.nodeName == "#text") {
html += node.nodeValue.
replace(/&(\w+[&;\W])?/g, function(match, entity){return entity?match:'&';}).
@@ -155,7 +156,7 @@ function sortedHtml(element, showNgClass) {
if (node.style) {
var style = [];
if (node.style.cssText) {
- foreach(node.style.cssText.split(';'), function(value){
+ forEach(node.style.cssText.split(';'), function(value){
value = trim(value);
if (value) {
style.push(lowercase(value));
@@ -174,7 +175,7 @@ function sortedHtml(element, showNgClass) {
style.sort();
var tmp = style;
style = [];
- foreach(tmp, function(value){
+ forEach(tmp, function(value){
if (!value.match(/^max[^\-]/))
style.push(value);
});
--
cgit v1.2.3