map / forEach use native impl, if available. ace2_common extends the prototype of Array, if not

This commit is contained in:
Matthias Bartelmeß 2012-03-13 21:10:10 +01:00
parent cccd8a923c
commit b28bfe8e31
4 changed files with 44 additions and 60 deletions

View File

@ -29,8 +29,7 @@ function isNodeText(node)
function object(o) function object(o)
{ {
var f = function() var f = function(){};
{};
f.prototype = o; f.prototype = o;
return new f(); return new f();
} }
@ -44,37 +43,32 @@ function extend(obj, props)
return obj; return obj;
} }
function forEach(array, func)
{ var forEachImpl = function(fn){
for (var i = 0; i < array.length; i++) for (var i = 0; i < this.length; i++)
{ {
var result = func(array[i], i); var result = func(this[i], i, this);
if (result) break;
} }
} }
function map(array, func) function mapImpl(fn)
{ {
var result = []; var result = [];
// must remain compatible with "arguments" pseudo-array
for (var i = 0; i < array.length; i++) for (var i = 0; i < this.length; i++)
{ {
if (func) result.push(func(array[i], i)); if (fn) result.push(fn(this[i], i, this));
else result.push(array[i]); else result.push(this[i]);
} }
return result; return result;
} }
function filter(array, func)
{ Array.prototype.forEach = Array.prototype.forEach || forEachImpl;
var result = []; Array.prototype.each = Array.prototype.each || forEachImpl;
// must remain compatible with "arguments" pseudo-array Array.prototype.map = Array.prototype.map || mapImpl;
for (var i = 0; i < array.length; i++)
{
if (func(array[i], i)) result.push(array[i]);
}
return result;
}
function isArray(testObject) function isArray(testObject)
{ {
@ -147,9 +141,6 @@ var identity = function(x){return x};
exports.isNodeText = isNodeText; exports.isNodeText = isNodeText;
exports.object = object; exports.object = object;
exports.extend = extend; exports.extend = extend;
exports.forEach = forEach;
exports.map = map;
exports.filter = filter;
exports.isArray = isArray; exports.isArray = isArray;
exports.browser = browser; exports.browser = browser;
exports.getAssoc = getAssoc; exports.getAssoc = getAssoc;
@ -157,6 +148,5 @@ exports.setAssoc = setAssoc;
exports.binarySearch = binarySearch; exports.binarySearch = binarySearch;
exports.binarySearchInfinite = binarySearchInfinite; exports.binarySearchInfinite = binarySearchInfinite;
exports.htmlPrettyEscape = htmlPrettyEscape; exports.htmlPrettyEscape = htmlPrettyEscape;
exports.map = map;
exports.noop = noop; exports.noop = noop;
exports.identity = identity; exports.identity = identity;

View File

@ -26,16 +26,14 @@ var Ace2Common = require('./ace2_common');
var isNodeText = Ace2Common.isNodeText; var isNodeText = Ace2Common.isNodeText;
var object = Ace2Common.object; var object = Ace2Common.object;
var extend = Ace2Common.extend; var extend = Ace2Common.extend;
var forEach = Ace2Common.forEach;
var map = Ace2Common.map;
var filter = Ace2Common.filter;
var isArray = Ace2Common.isArray; var isArray = Ace2Common.isArray;
var browser = Ace2Common.browser; var browser = Ace2Common.browser;
var getAssoc = Ace2Common.getAssoc; var getAssoc = Ace2Common.getAssoc;
var setAssoc = Ace2Common.setAssoc; var setAssoc = Ace2Common.setAssoc;
var binarySearchInfinite = Ace2Common.binarySearchInfinite; var binarySearchInfinite = Ace2Common.binarySearchInfinite;
var htmlPrettyEscape = Ace2Common.htmlPrettyEscape; var htmlPrettyEscape = Ace2Common.htmlPrettyEscape;
var map = Ace2Common.map;
var noop = Ace2Common.noop; var noop = Ace2Common.noop;
var makeChangesetTracker = require('./changesettracker').makeChangesetTracker; var makeChangesetTracker = require('./changesettracker').makeChangesetTracker;
@ -684,7 +682,7 @@ function Ace2Inner(){
} }
else else
{ {
lines = map(text.split('\n'), textify); lines = text.split('\n').map(textify);
} }
var newText = "\n"; var newText = "\n";
if (lines.length > 0) if (lines.length > 0)
@ -1622,8 +1620,7 @@ function Ace2Inner(){
} }
//var fragment = magicdom.wrapDom(document.createDocumentFragment()); //var fragment = magicdom.wrapDom(document.createDocumentFragment());
domInsertsNeeded.push([nodeToAddAfter, lineNodeInfos]); domInsertsNeeded.push([nodeToAddAfter, lineNodeInfos]);
forEach(dirtyNodes, function(n) dirtyNodes.forEach(function(n){
{
toDeleteAtEnd.push(n); toDeleteAtEnd.push(n);
}); });
var spliceHints = {}; var spliceHints = {};
@ -1655,14 +1652,14 @@ function Ace2Inner(){
//var isTimeUp = newTimeLimit(100); //var isTimeUp = newTimeLimit(100);
// do DOM inserts // do DOM inserts
p.mark("insert"); p.mark("insert");
forEach(domInsertsNeeded, function(ins) domInsertsNeeded.forEach(function(ins)
{ {
insertDomLines(ins[0], ins[1], isTimeUp); insertDomLines(ins[0], ins[1], isTimeUp);
}); });
p.mark("del"); p.mark("del");
// delete old dom nodes // delete old dom nodes
forEach(toDeleteAtEnd, function(n) toDeleteAtEnd.forEach(function(n)
{ {
//var id = n.uniqueId(); //var id = n.uniqueId();
// parent of n may not be "root" in IE due to non-tree-shaped DOM (wtf) // parent of n may not be "root" in IE due to non-tree-shaped DOM (wtf)
@ -1772,7 +1769,7 @@ function Ace2Inner(){
var charEnd = rep.lines.offsetOfEntry(endEntry) + endEntry.width; var charEnd = rep.lines.offsetOfEntry(endEntry) + endEntry.width;
//rep.lexer.lexCharRange([charStart, charEnd], isTimeUp); //rep.lexer.lexCharRange([charStart, charEnd], isTimeUp);
forEach(infoStructs, function(info) infoStructs.forEach(function(info)
{ {
var p2 = PROFILER("insertLine", false); var p2 = PROFILER("insertLine", false);
var node = info.node; var node = info.node;
@ -2083,10 +2080,8 @@ function Ace2Inner(){
var linesMutatee = { var linesMutatee = {
splice: function(start, numRemoved, newLinesVA) splice: function(start, numRemoved, newLinesVA)
{ {
domAndRepSplice(start, numRemoved, map(Array.prototype.slice.call(arguments, 2), function(s) var args = Array.prototype.slice.call(arguments, 2);
{ domAndRepSplice(start, numRemoved, args.map(function(s){ return s.slice(0, -1); }), null);
return s.slice(0, -1);
}), null);
}, },
get: function(i) get: function(i)
{ {
@ -2098,7 +2093,7 @@ function Ace2Inner(){
}, },
slice_notused: function(start, end) slice_notused: function(start, end)
{ {
return map(rep.lines.slice(start, end), function(e) return rep.lines.slice(start, end).map(function(e)
{ {
return e.text + '\n'; return e.text + '\n';
}); });
@ -2131,7 +2126,7 @@ function Ace2Inner(){
} }
} }
var lineEntries = map(newLineStrings, createDomLineEntry); var lineEntries = newLineStrings.map(createDomLineEntry);
doRepLineSplice(startLine, deleteCount, lineEntries); doRepLineSplice(startLine, deleteCount, lineEntries);
@ -2142,12 +2137,12 @@ function Ace2Inner(){
} }
else nodeToAddAfter = null; else nodeToAddAfter = null;
insertDomLines(nodeToAddAfter, map(lineEntries, function(entry) insertDomLines(nodeToAddAfter, lineEntries.map(function(entry)
{ {
return entry.domInfo; return entry.domInfo;
}), isTimeUp); }), isTimeUp);
forEach(keysToDelete, function(k) infoStructs.forEach(function(k)
{ {
var n = doc.getElementById(k); var n = doc.getElementById(k);
n.parentNode.removeChild(n); n.parentNode.removeChild(n);
@ -2467,7 +2462,7 @@ function Ace2Inner(){
function doRepLineSplice(startLine, deleteCount, newLineEntries) function doRepLineSplice(startLine, deleteCount, newLineEntries)
{ {
forEach(newLineEntries, function(entry) newLineEntries.forEach(function(entry)
{ {
entry.width = entry.text.length + 1; entry.width = entry.text.length + 1;
}); });
@ -2482,7 +2477,7 @@ function Ace2Inner(){
currentCallStack.repChanged = true; currentCallStack.repChanged = true;
var newRegionEnd = rep.lines.offsetOfIndex(startLine + newLineEntries.length); var newRegionEnd = rep.lines.offsetOfIndex(startLine + newLineEntries.length);
var newText = map(newLineEntries, function(e) var newText = newLineEntries.map(function(e)
{ {
return e.text + '\n'; return e.text + '\n';
}).join(''); }).join('');
@ -2512,7 +2507,7 @@ function Ace2Inner(){
selEndHintChar = rep.lines.offsetOfIndex(hints.selEnd[0]) + hints.selEnd[1] - oldRegionStart; selEndHintChar = rep.lines.offsetOfIndex(hints.selEnd[0]) + hints.selEnd[1] - oldRegionStart;
} }
var newText = map(newLineEntries, function(e) var newText = newLineEntries.map(function(e)
{ {
return e.text + '\n'; return e.text + '\n';
}).join(''); }).join('');
@ -3053,7 +3048,7 @@ function Ace2Inner(){
{ {
// returns index of cleanRange containing i, or -1 if none // returns index of cleanRange containing i, or -1 if none
var answer = -1; var answer = -1;
forEach(cleanRanges, function(r, idx) cleanRanges.forEach(function(r, idx)
{ {
if (i >= r[1]) return false; // keep looking if (i >= r[1]) return false; // keep looking
if (i < r[0]) return true; // not found, stop looking if (i < r[0]) return true; // not found, stop looking
@ -3846,7 +3841,7 @@ function Ace2Inner(){
function getRepHTML() function getRepHTML()
{ {
return map(rep.lines.slice(), function(entry) return rep.lines.slice().map(function(entry)
{ {
var text = entry.text; var text = entry.text;
var content; var content;
@ -4585,7 +4580,7 @@ function Ace2Inner(){
function teardown() function teardown()
{ {
forEach(_teardownActions, function(a) _teardownActions.forEach(function(a)
{ {
a(); a();
}); });

View File

@ -28,9 +28,6 @@ var linestylefilter = require('./linestylefilter').linestylefilter;
var colorutils = require('./colorutils').colorutils; var colorutils = require('./colorutils').colorutils;
var Ace2Common = require('./ace2_common'); var Ace2Common = require('./ace2_common');
var map = Ace2Common.map;
var forEach = Ace2Common.forEach;
// These parameters were global, now they are injected. A reference to the // These parameters were global, now they are injected. A reference to the
// Timeslider controller would probably be more appropriate. // Timeslider controller would probably be more appropriate.
function loadBroadcastJS(socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, BroadcastSlider) function loadBroadcastJS(socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, BroadcastSlider)
@ -155,7 +152,7 @@ function loadBroadcastJS(socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, Bro
// splice the lines // splice the lines
splice: function(start, numRemoved, newLinesVA) splice: function(start, numRemoved, newLinesVA)
{ {
var newLines = map(Array.prototype.slice.call(arguments, 2), function(s) { var newLines = Array.prototype.slice.call(arguments, 2).map(function(s) {
return s; return s;
}); });
@ -278,7 +275,7 @@ function loadBroadcastJS(socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, Bro
debugLog('Time Delta: ', timeDelta) debugLog('Time Delta: ', timeDelta)
updateTimer(); updateTimer();
var authors = map(padContents.getActiveAuthors(), function(name) var authors = padContents.getActiveAuthors().map(function(name)
{ {
return authorData[name]; return authorData[name];
}); });
@ -384,7 +381,7 @@ function loadBroadcastJS(socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, Bro
changesetLoader.queueUp(start, 1, update); changesetLoader.queueUp(start, 1, update);
} }
var authors = map(padContents.getActiveAuthors(), function(name){ var authors = padContents.getActiveAuthors().map(function(name){
return authorData[name]; return authorData[name];
}); });
BroadcastSlider.setAuthors(authors); BroadcastSlider.setAuthors(authors);
@ -527,7 +524,7 @@ function loadBroadcastJS(socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, Bro
authorMap[obj.author] = obj.data; authorMap[obj.author] = obj.data;
receiveAuthorData(authorMap); receiveAuthorData(authorMap);
var authors = map(padContents.getActiveAuthors(),function(name) { var authors = padContents.getActiveAuthors().map(function(name) {
return authorData[name]; return authorData[name];
}); });
@ -607,10 +604,13 @@ function loadBroadcastJS(socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, Bro
setChannelState("DISCONNECTED", reason); setChannelState("DISCONNECTED", reason);
} }
/// Since its not used, import 'forEach' has been dropped
/*window['onloadFuncts'] = []; /*window['onloadFuncts'] = [];
window.onload = function () window.onload = function ()
{ {
window['isloaded'] = true; window['isloaded'] = true;
forEach(window['onloadFuncts'],function (funct) forEach(window['onloadFuncts'],function (funct)
{ {
funct(); funct();

View File

@ -29,7 +29,6 @@
var Security = require('./security'); var Security = require('./security');
var hooks = require('./pluginfw/hooks'); var hooks = require('./pluginfw/hooks');
var Ace2Common = require('./ace2_common'); var Ace2Common = require('./ace2_common');
var map = Ace2Common.map;
var noop = Ace2Common.noop; var noop = Ace2Common.noop;
var identity = Ace2Common.identity; var identity = Ace2Common.identity;
@ -142,10 +141,10 @@ domline.createDomLine = function(nonEmpty, doesWrap, optBrowser, optDocument)
var extraOpenTags = ""; var extraOpenTags = "";
var extraCloseTags = ""; var extraCloseTags = "";
map(hooks.callAll("aceCreateDomLine", { hooks.callAll("aceCreateDomLine", {
domline: domline, domline: domline,
cls: cls cls: cls
}), function(modifier) }).map(function(modifier)
{ {
cls = modifier.cls; cls = modifier.cls;
extraOpenTags = extraOpenTags + modifier.extraOpenTags; extraOpenTags = extraOpenTags + modifier.extraOpenTags;