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

Allow findAllMatchingFunctionsInText to iterate over array-like objects (#1390) #2317

Merged
merged 3 commits into from
Dec 21, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 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 @@ -396,17 +397,17 @@ define(function (require, exports, module) {
* @return {Array.<{offset:number, functionName:string}>}
* Array of objects containing the start offset for each matched function name.
*/
function findAllMatchingFunctionsInText(text, functionName) {
function findAllMatchingFunctionsInText(text, searchName) {
var allFunctions = _findAllFunctionsInText(text);
var result = [];
var lines = text.split("\n");

$.each(allFunctions, function (index, functions) {
if (index === functionName || functionName === "*") {
CollectionUtils.forEach(allFunctions, function (functionName, functions) {
if (functionName === searchName || searchName === "*") {
functions.forEach(function (funcEntry) {
var endOffset = _getFunctionEndOffset(text, funcEntry.offsetStart);
result.push({
name: index,
name: functionName,
lineStart: StringUtils.offsetToLineNum(lines, funcEntry.offsetStart),
lineEnd: StringUtils.offsetToLineNum(lines, endOffset)
});
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;
});