diff --git a/app/lib/bundler.js b/app/lib/bundler.js index dbfee7b72d2..1c07fbe4333 100644 --- a/app/lib/bundler.js +++ b/app/lib/bundler.js @@ -197,10 +197,6 @@ _.extend(Bundle.prototype, { // underscore package this.api.require('underscore'); - // XXX this is some kind of legacy crap that someone might need - // but I don't know the details. it should go away. - this.api.require('basics'); - // standard client packages (for now), for the classic skybreak stack this.api.require('deps'); this.api.require('session'); @@ -295,7 +291,7 @@ exports.bundle = function (app_dir, output_path, options) { // stuff' like minimongo bundle.add_standard_packages(); - require('./project.js').get_packages(app_dir).forEach(function (p) { + _.each(require('./project.js').get_packages(app_dir), function (p) { bundle.api.require(p); }); @@ -303,7 +299,7 @@ exports.bundle = function (app_dir, output_path, options) { var user_files = bundle.compute_user_files(app_dir); - user_files.forEach(function (rel_path) { + _.each(user_files, function (rel_path) { var full_path = path.join(app_dir, rel_path); // XXX at some point we should re-work our directory structure and @@ -416,7 +412,7 @@ exports.bundle = function (app_dir, output_path, options) { app_json.load = []; files.mkdir_p(path.join(build_path, 'app'), 0755); - bundle.server_load.forEach(function (rel_path) { + _.each(bundle.server_load, function (rel_path) { var path_in_bundle = path.join('app', rel_path); var full_path = path.join(build_path, path_in_bundle); app_json.load.push(path_in_bundle); diff --git a/app/lib/files.js b/app/lib/files.js index 589b51fefac..cbccf52e666 100644 --- a/app/lib/files.js +++ b/app/lib/files.js @@ -66,12 +66,12 @@ var files = module.exports = { return; } - fileNames.forEach(function (fileName) { + _.each(fileNames, function (fileName) { files.file_list_async(path.join(filepath, fileName), extensions, func); }); }); - } else if (extensions.indexOf(path.extname(filepath)) !== -1) { + } else if (_.indexOf(extensions, path.extname(filepath)) !== -1) { func(filepath); } }); @@ -83,11 +83,11 @@ var files = module.exports = { var stats = fs.statSync(filepath); if (stats.isDirectory()) { var fileNames = fs.readdirSync(filepath); - fileNames.forEach(function (fileName) { + _.each(fileNames, function (fileName) { ret = ret.concat(files.file_list_sync( path.join(filepath, fileName), extensions)); }); - } else if (extensions.indexOf(path.extname(filepath)) !== -1) { + } else if (_.indexOf(extensions, path.extname(filepath)) !== -1) { ret.push(filepath); } @@ -189,7 +189,7 @@ var files = module.exports = { } if (stat.isDirectory()) { - fs.readdirSync(p).forEach(function (file) { + _.each(fs.readdirSync(p), function (file) { file = path.join(p, file); files.rm_recursive(file); }); @@ -238,7 +238,7 @@ var files = module.exports = { cp_r: function (from, to, options) { options = options || {}; files.mkdir_p(to, 0755); - fs.readdirSync(from).forEach(function (f) { + _.each(fs.readdirSync(from), function (f) { if (_.any(options.ignore || [], function (pattern) { return f.match(pattern); })) return; diff --git a/app/lib/project.js b/app/lib/project.js index cd0994d5318..af646aef436 100644 --- a/app/lib/project.js +++ b/app/lib/project.js @@ -36,7 +36,7 @@ var project = module.exports = { get_packages: function (app_dir) { var ret = []; - project._get_lines(app_dir).forEach(function (line) { + _.each(project._get_lines(app_dir), function (line) { line = project._trim_line(line); if (line !== '') ret.push(line); diff --git a/app/server/server.js b/app/server/server.js index a2cd10afcac..d525dd04630 100644 --- a/app/server/server.js +++ b/app/server/server.js @@ -94,7 +94,7 @@ var run = function (bundle_dir) { __skybreak_bootstrap__.mongo_url = mongo_url; // load app code - info.load.forEach(function (filename) { + _.each(info.load, function (filename) { var code = fs.readFileSync(path.join(bundle_dir, filename)); // it's tempting to run the code in a new context so we can // precisely control the enviroment the user code sees. but, diff --git a/app/skybreak/skybreak.js b/app/skybreak/skybreak.js index fb1888d1e3d..4a270484b69 100644 --- a/app/skybreak/skybreak.js +++ b/app/skybreak/skybreak.js @@ -13,7 +13,7 @@ var usage = function() { "Use 'skybreak create ' to create a new Skybreak project.\n" + "\n" + "Commands:\n"); - Commands.forEach(function (cmd) { + _.each(Commands, function (cmd) { if (cmd.help) { var name = cmd.name + " ".substr(cmd.name.length); process.stdout.write(" " + name + cmd.help + "\n"); diff --git a/packages/basics/basics.js b/packages/basics/basics.js deleted file mode 100644 index ad1c784f09d..00000000000 --- a/packages/basics/basics.js +++ /dev/null @@ -1,209 +0,0 @@ -UNIMPLEMENTED = function () { - throw new Error("Unimplemented"); -}; - -if (!Array.prototype.map) { - Array.prototype.map = function (f) { - var len = this.length; - var ret = new Array(len); - for (var i = 0; i < len; i++) - ret[i] = f(this[i]); - return ret; - }; -} - -// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind -if (!Function.prototype.bind) { - Function.prototype.bind = function( obj ) { - var slice = [].slice, - args = slice.call(arguments, 1), - self = this, - nop = function () {}, - bound = function () { - return self.apply(this instanceof nop ? this : ( obj || {} ), - args.concat( slice.call(arguments) ) ); - }; - nop.prototype = self.prototype; - bound.prototype = new nop(); - return bound; - }; -} - -// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf -if (!Array.prototype.indexOf) -{ - Array.prototype.indexOf = function(searchElement /*, fromIndex */) - { - "use strict"; - - if (this === void 0 || this === null) - throw new TypeError(); - - var t = Object(this); - var len = t.length >>> 0; - if (len === 0) - return -1; - - var n = 0; - if (arguments.length > 0) - { - n = Number(arguments[1]); - if (n !== n) // shortcut for verifying if it's NaN - n = 0; - else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) - n = (n > 0 || -1) * Math.floor(Math.abs(n)); - } - - if (n >= len) - return -1; - - var k = n >= 0 - ? n - : Math.max(len - Math.abs(n), 0); - - for (; k < len; k++) - { - if (k in t && t[k] === searchElement) - return k; - } - return -1; - }; -} - -// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filterif (!Array.prototype.filter) -{ - Array.prototype.filter = function(fun /*, thisp */) - { - "use strict"; - - if (this === void 0 || this === null) - throw new TypeError(); - - var t = Object(this); - var len = t.length >>> 0; - if (typeof fun !== "function") - throw new TypeError(); - - var res = []; - var thisp = arguments[1]; - for (var i = 0; i < len; i++) - { - if (i in t) - { - var val = t[i]; // in case fun mutates this - if (fun.call(thisp, val, i, t)) - res.push(val); - } - } - - return res; - }; -} - - -//https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/ForEach -// Production steps of ECMA-262, Edition 5, 15.4.4.18 -if ( !Array.prototype.forEach ) { - - Array.prototype.forEach = function( callbackfn, thisArg ) { - - var T, - O = Object(this), - len = O.length >>> 0, - k = 0; - - // If no callback function or if callback is not a callable function - if ( !callbackfn || !callbackfn.call ) { - throw new TypeError(); - } - - // If the optional thisArg context param was provided, - // Set as this context - if ( thisArg ) { - T = thisArg; - } - - while( k < len ) { - // Store property key string object reference - var Pk = String( k ), - // Determine if property key is present in this object context - kPresent = O.hasOwnProperty( Pk ), - kValue; - - if ( kPresent ) { - // Dereference and store the value of this Property key - kValue = O[ Pk ]; - - // Invoke the callback function with call, passing arguments: - // context, property value, property key, thisArg object context - callbackfn.call( T, kValue, k, O ); - } - - k++; - } - }; -} - -/// http://snipplr.com/view/26662/get-url-parameters-with-jquery--improved/ -/// -/// XXX should this be here? It is client-only (references window). -/// XXX no. move elsewhere, and probably put in a namespace -var getUrlParam = function(name){ - var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec( - window.location.href); - if (!results) { return null; } - return results[1] || null; -} - -/** - * Wait a little while, then call a function. But, if another call to - * coalesce is made with the same key while we're waiting, then forget - * the first call (and begin waiting anew for the second call.) In - * other words, coalesce all of the calls to the function that occur - * within a certain duration of each other into once call. - * - * @param key {String} arbitrary value to identify calls to coalesce - * @param duration {Number} how long to wait, in milliseconds - * @param f {Function} the function to call - */ -var coalesce = (function() { - var timers = {}; - return function (key, duration, f) { - if (key in timers) { - clearTimeout(timers[key]); - delete timers[key]; - } - timers[key] = setTimeout(function () { - delete timers[key]; - f(); - }, duration); - }; -})(); - -/** - * Wait for N asynchronous functions to complete, then call another - * function. Best explained by example: - * - * waitForN(3, function (finish) { - * var results = []; - * var collect = function (answer) { - * results.push(g); - * finish(); - * }; - * - * askYourMom(collect); - * askYourDad(collect); - * askYourSister(collect); - * }, function () { - * // .. do something with 'results' .. - * }); - */ -var waitForN = function (n, start, after) { - return start(function () { - n--; - if (n === 0) - after(); - }); -}; - - diff --git a/packages/basics/package.js b/packages/basics/package.js deleted file mode 100644 index 6acf4e2ef52..00000000000 --- a/packages/basics/package.js +++ /dev/null @@ -1,7 +0,0 @@ -Package.describe({ - summary: "XXX crap that nobody should use and should be made to go away", - internal: true -}); - -// This is legacy crap and should be made to go away. -Package.client_file('basics.js'); diff --git a/packages/htmljs/html.js b/packages/htmljs/html.js index aaac8e322ad..f1eadef6f94 100644 --- a/packages/htmljs/html.js +++ b/packages/htmljs/html.js @@ -27,84 +27,91 @@ // XXX allow style to be set as an object -// XXX todo: don't pollute the namespace.. -var __private__event_names = { - blur: true, - change: true, - click: true, - dblclick: true, - error: true, - focus: true, - focusin: true, - focusout: true, - keydown: true, - keypress: true, - keyup: true, - load: true, - mousedown: true, - mouseenter: true, - mouseleave: true, - mousemove: true, - mouseout: true, - mouseover: true, - mouseup: true, - resize: true, - scroll: true, - select: true, - submit: true -}; +(function () { + var event_names = { + blur: true, + change: true, + click: true, + dblclick: true, + error: true, + focus: true, + focusin: true, + focusout: true, + keydown: true, + keypress: true, + keyup: true, + load: true, + mousedown: true, + mouseenter: true, + mouseleave: true, + mousemove: true, + mouseout: true, + mouseover: true, + mouseup: true, + resize: true, + scroll: true, + select: true, + submit: true + }; -// All HTML4 elements, excluding deprecated element -// http://www.w3.org/TR/html4/index/elements.html -// also excluding the following elements that seem unlikely to be used in the body: -// HEAD, HTML, LINK, MAP, META, NOFRAMES, NOSCRIPT, STYLE, TITLE -('A ABBR ACRONYM B BDO BIG BLOCKQUOTE BR BUTTON CAPTION CITE CODE COL ' + - 'COLGROUP DD DEL DFN DIV DL DT EM FIELDSET FORM H1 H2 H3 H4 H5 H6 HR ' + - 'I IFRAME IMG INPUT INS KBD LABEL LEGEND LI OBJECT OL OPTGROUP OPTION ' + - 'P PARAM PRE Q S SAMP SCRIPT SELECT SMALL SPAN STRIKE STRONG SUB SUP TABLE ' + - 'TBODY TD TEXTAREA TFOOT TH THEAD TR TT U UL VAR').split(' ').forEach( - function (tag) { - window[tag] = function (arg1, arg2) { - var attrs, contents; - if (arg2) { - attrs = arg1; - contents = arg2; - } else { - if (arg1 instanceof Array) { - attrs = {}; - contents = arg1; - } else { - attrs = arg1; - contents = []; - } - } - var elt = document.createElement(tag); - for (var a in attrs) { - if (a === 'cls') - elt.setAttribute('class', attrs[a]); - else if (a === '_for') - elt.setAttribute('for', attrs[a]); - else if (__private__event_names[a]) - // XXX creates a dependency on jQuery.. ick.. - ($(elt)[a])(attrs[a]); - else - elt.setAttribute(a, attrs[a]); - } - var addChildren = function (children) { - children.forEach(function (c) { - if (!c && c !== '') - throw new Error("Bad value for element body: " + c); - else if (c instanceof Array) - addChildren(c); - else if (typeof(c) === "string") - elt.appendChild(document.createTextNode(c)); - else if ('element' in c) - addChildren([c.element]); - else - elt.appendChild(c); - }); - }; - addChildren(contents); - return elt; - }; - }); + // All HTML4 elements, excluding deprecated elements + // http://www.w3.org/TR/html4/index/elements.html + // also excluding the following elements that seem unlikely to be + // used in the body: + // HEAD, HTML, LINK, MAP, META, NOFRAMES, NOSCRIPT, STYLE, TITLE + var tag_names = + ('A ABBR ACRONYM B BDO BIG BLOCKQUOTE BR BUTTON CAPTION CITE CODE COL ' + + 'COLGROUP DD DEL DFN DIV DL DT EM FIELDSET FORM H1 H2 H3 H4 H5 H6 HR ' + + 'I IFRAME IMG INPUT INS KBD LABEL LEGEND LI OBJECT OL OPTGROUP OPTION ' + + 'P PARAM PRE Q S SAMP SCRIPT SELECT SMALL SPAN STRIKE STRONG SUB SUP ' + + 'TABLE TBODY TD TEXTAREA TFOOT TH THEAD TR TT U UL VAR').split(' '); + + for (var i = 0; i < tag_names.length; i++) { + var tag = tag_names[i]; + + window[tag] = function (arg1, arg2) { + var attrs, contents; + if (arg2) { + attrs = arg1; + contents = arg2; + } else { + if (arg1 instanceof Array) { + attrs = {}; + contents = arg1; + } else { + attrs = arg1; + contents = []; + } + } + var elt = document.createElement(tag); + for (var a in attrs) { + if (a === 'cls') + elt.setAttribute('class', attrs[a]); + else if (a === '_for') + elt.setAttribute('for', attrs[a]); + else if (event_names[a]) + // XXX creates a dependency on jQuery.. ick.. + ($(elt)[a])(attrs[a]); + else + elt.setAttribute(a, attrs[a]); + } + var addChildren = function (children) { + for (var i = 0; i < children.length; i++) { + var c = children[i]; + if (!c && c !== '') + throw new Error("Bad value for element body: " + c); + else if (c instanceof Array) + addChildren(c); + else if (typeof(c) === "string") + elt.appendChild(document.createTextNode(c)); + else if ('element' in c) + addChildren([c.element]); + else + elt.appendChild(c); + }; + }; + addChildren(contents); + return elt; + }; + }; +})(); diff --git a/packages/livedata/livedata_client.js b/packages/livedata/livedata_client.js index e331e070687..43ab0c2edcd 100644 --- a/packages/livedata/livedata_client.js +++ b/packages/livedata/livedata_client.js @@ -35,7 +35,7 @@ Sky = window.Sky || {}; // XXX this is all a little whack. Need to think about how we handle // removes, etc. - (changes.inserted || []).forEach(function (elt) { + _.each(changes.inserted || [], function (elt) { if (!coll.find(elt._id)) { coll._collection.insert(elt); } else { @@ -44,10 +44,10 @@ Sky = window.Sky || {}; coll._collection.update({_id: elt._id}, elt); } }); - (changes.updated || []).forEach(function (elt) { + _.each(changes.updated || [], function (elt) { coll._collection.update({_id: elt._id}, elt); }); - (changes.removed || []).forEach(function (id) { + _.each(changes.removed || [], function (id) { coll._collection.remove({_id: id}); }); }); diff --git a/packages/liveui/liveui.js b/packages/liveui/liveui.js index 6276a36b3e5..373be0eda16 100644 --- a/packages/liveui/liveui.js +++ b/packages/liveui/liveui.js @@ -109,7 +109,7 @@ Sky.ui.render = function (render_func, events, event_data) { if (result === null) { result = new_result; if (result instanceof Array) - result.forEach(function (elt) { + _.each(result, function (elt) { Sky.ui._setupEvents(elt, events || {}, event_data); }); else diff --git a/packages/minimongo/modify.js b/packages/minimongo/modify.js index b60c89cd749..769dca65d96 100644 --- a/packages/minimongo/modify.js +++ b/packages/minimongo/modify.js @@ -183,7 +183,7 @@ Collection._modifiers = { } } var values = isEach ? arg["$each"] : [arg]; - values.forEach(function (value) { + _.each(values, function (value) { for (var i = 0; i < x.length; i++) if (Collection._f._equal(value, x[i])) return; diff --git a/packages/minimongo/selector.js b/packages/minimongo/selector.js index 7b157187a1b..d03221088d7 100644 --- a/packages/minimongo/selector.js +++ b/packages/minimongo/selector.js @@ -7,7 +7,7 @@ Collection._f = { // don't get screwed by key order var parts = {}; var remaining = 0; - qval.forEach(function (q) { + _.each(qval, function (q) { var hash = JSON.stringify(q); if (!(hash in parts)) { parts[hash] = true; @@ -300,7 +300,7 @@ Collection._exprForSelector = function (selector, literals) { Collection._exprForDocumentPredicate = function (op, value, literals) { if (op === '$or') { var clauses = []; - value.forEach(function (c) { + _.each(value, function (c) { clauses.push(Collection._exprForSelector(c, literals)); }); if (clauses.length === 0) return 'true'; @@ -309,7 +309,7 @@ Collection._exprForDocumentPredicate = function (op, value, literals) { if (op === '$and') { var clauses = []; - value.forEach(function (c) { + _.each(value, function (c) { clauses.push(Collection._exprForSelector(c, literals)); }); if (clauses.length === 0) return 'true'; @@ -318,7 +318,7 @@ Collection._exprForDocumentPredicate = function (op, value, literals) { if (op === '$nor') { var clauses = []; - value.forEach(function (c) { + _.each(value, function (c) { clauses.push("!(" + Collection._exprForSelector(c, literals) + ")"); }); if (clauses.length === 0) return 'true'; diff --git a/packages/templating/deftemplate.js b/packages/templating/deftemplate.js index b03baffa111..a7d43a25dc4 100644 --- a/packages/templating/deftemplate.js +++ b/packages/templating/deftemplate.js @@ -163,7 +163,7 @@ Sky._def_template = function (name, raw_func, multi) { replaced.push(id); } } - replaced.forEach(function (id) { + _.each(replaced, function (id) { delete Sky._pending_partials[id]; }); } while (replaced.length); diff --git a/tests/minimongo/client/test.js b/tests/minimongo/client/test.js index 594b9c76f33..c88e48c9032 100644 --- a/tests/minimongo/client/test.js +++ b/tests/minimongo/client/test.js @@ -474,7 +474,7 @@ test_ordering = function () { // document ordering under a sort specification var test = function (sorts, docs) { - sorts.forEach(function (sort) { + _.each(sorts, function (sort) { assert_ordering(Collection._compileSort(sort), docs); }); };