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 --- 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 ++++++------ 25 files changed, 82 insertions(+), 80 deletions(-) (limited to 'src') diff --git a/src/Angular.js b/src/Angular.js index 060e08c1..59b031cc 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -115,7 +115,7 @@ var _undefined = undefined, /** * @workInProgress * @ngdoc function - * @name angular.foreach + * @name angular.forEach * @function * * @description @@ -123,11 +123,13 @@ var _undefined = undefined, * 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`. *
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);
--
cgit v1.2.3