Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Refactor forEach to CollectionUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
jbalsas committed Dec 18, 2012
1 parent f5b5a56 commit fa51bfd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/language/JSUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ define(function (require, exports, module) {
DocumentManager = require("document/DocumentManager"),
ChangedDocumentTracker = require("document/ChangedDocumentTracker"),
NativeFileSystem = require("file/NativeFileSystem").NativeFileSystem,
CollectionUtils = require("utils/CollectionUtils"),
PerfUtils = require("utils/PerfUtils"),
StringUtils = require("utils/StringUtils");

Expand Down Expand Up @@ -400,27 +401,19 @@ define(function (require, exports, module) {
var allFunctions = _findAllFunctionsInText(text);
var result = [];
var lines = text.split("\n");
var functionName;

function makeAddFunction(functionName) {
return function (funcEntry) {
var endOffset = _getFunctionEndOffset(text, funcEntry.offsetStart);
result.push({
name: functionName,
lineStart: StringUtils.offsetToLineNum(lines, funcEntry.offsetStart),
lineEnd: StringUtils.offsetToLineNum(lines, endOffset)
CollectionUtils.forEach(allFunctions, function (functionName, functions) {
if (functionName === searchName || searchName === "*") {
functions.forEach(function (funcEntry) {
var endOffset = _getFunctionEndOffset(text, funcEntry.offsetStart);
result.push({
name: functionName,
lineStart: StringUtils.offsetToLineNum(lines, funcEntry.offsetStart),
lineEnd: StringUtils.offsetToLineNum(lines, endOffset)
});
});
};
}

for (functionName in allFunctions) {
if (allFunctions.hasOwnProperty(functionName)) {
if (functionName === searchName || searchName === "*") {
var addFunctionEntry = makeAddFunction(functionName);
allFunctions[functionName].forEach(addFunctionEntry);
}
}
}
});

return result;
}
Expand Down
16 changes: 16 additions & 0 deletions src/utils/CollectionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,23 @@ define(function (require, exports, module) {
return -1;
}

/**
* Iterates over all the properties in an object or elements in an array. Differs from
* $.each in that it iterates over array-like objects like regular objects.
* @param {*} object The object or array to iterate over.
* @param {function(index, value)} callback The function that will be executed on every object.
*/
function forEach(object, callback) {
var keys = Object.keys(object),
len = keys.length,
i;

for (i = 0; i < len; i++) {
callback(keys[i], object[keys[i]]);
}
}

// Define public API
exports.indexOf = indexOf;
exports.forEach = forEach;
});

0 comments on commit fa51bfd

Please sign in to comment.