diff --git a/common/content/bookmarks.js b/common/content/bookmarks.js index 813ba849..6e406605 100644 --- a/common/content/bookmarks.js +++ b/common/content/bookmarks.js @@ -328,7 +328,7 @@ function Bookmarks() //{{{ args.completeFilter = have.pop(); let prefix = filter.substr(0, filter.length - args.completeFilter.length); - let tags = util.Array.uniq(util.Array.flatten([b.tags for ([k, b] in Iterator(cache.bookmarks))])); + let tags = Array_([b.tags for ([k, b] in Iterator(cache.bookmarks))]).flatten().uniq(); return [[prefix + tag, tag] for ([i, tag] in Iterator(tags)) if (have.indexOf(tag) < 0)]; } @@ -1023,7 +1023,7 @@ function History() //{{{ let sh = window.getWebNavigation().sessionHistory; let obj = []; obj.index = sh.index; - obj.__iterator__ = function () util.Array.iteritems(this); + obj.__iterator__ = function () Array_.iteritems(this); for (let i in util.range(0, sh.count)) { obj[i] = { index: i, __proto__: sh.getEntryAtIndex(i, false) }; @@ -1044,7 +1044,7 @@ function History() //{{{ liberator.beep(); else { - let index = util.Math.constrain(current + steps, start, end); + let index = Math_.constrain(current + steps, start, end); window.getWebNavigation().gotoIndex(index); } }, diff --git a/common/content/buffer.js b/common/content/buffer.js index cc7f0b94..5492c439 100644 --- a/common/content/buffer.js +++ b/common/content/buffer.js @@ -71,7 +71,7 @@ function Buffer() //{{{ { let values = ZoomManager.zoomValues; let cur = values.indexOf(ZoomManager.snap(ZoomManager.zoom)); - let i = util.Math.constrain(cur + steps, 0, values.length - 1); + let i = Math_.constrain(cur + steps, 0, values.length - 1); if (i == cur && fullZoom == ZoomManager.useFullZoom) liberator.beep(); @@ -96,7 +96,7 @@ function Buffer() //{{{ if (win.scrollMaxX > 0 || win.scrollMaxY > 0) return win; - for (let frame in util.Array.itervalues(win.frames)) + for (let frame in Array_.itervalues(win.frames)) if (frame.scrollMaxX > 0 || frame.scrollMaxY > 0) return frame; @@ -359,7 +359,7 @@ function Buffer() //{{{ if (elements.length > 0) { - count = util.Math.constrain(count, 1, elements.length); + count = Math_.constrain(count, 1, elements.length); buffer.focusElement(elements[count - 1]); } else @@ -648,7 +648,7 @@ function Buffer() //{{{ level = buffer.textZoom + parseInt(arg, 10); // relative args shouldn't take us out of range - level = util.Math.constrain(level, ZOOM_MIN, ZOOM_MAX); + level = Math_.constrain(level, ZOOM_MIN, ZOOM_MAX); } else return void liberator.echoerr("E488: Trailing characters"); @@ -805,7 +805,7 @@ function Buffer() //{{{ const ACCESS_READ = Ci.nsICache.ACCESS_READ; let cacheKey = doc.location.toString().replace(/#.*$/, ""); - for (let proto in util.Array.itervalues(["HTTP", "FTP"])) + for (let proto in Array_.itervalues(["HTTP", "FTP"])) { try { diff --git a/common/content/commands.js b/common/content/commands.js index d7fcaa10..9b523583 100644 --- a/common/content/commands.js +++ b/common/content/commands.js @@ -460,7 +460,7 @@ function Commands() //{{{ __iterator__: function () { let sorted = exCommands.sort(function (a, b) a.name > b.name); - return util.Array.itervalues(sorted); + return Array_.itervalues(sorted); }, /** @property {string} The last executed Ex command line. */ @@ -652,7 +652,7 @@ function Commands() //{{{ argCount = "*"; var args = []; // parsed options - args.__iterator__ = function () util.Array.iteritems(this); + args.__iterator__ = function () Array_.iteritems(this); args.string = str; // for access to the unparsed string args.literalArg = ""; @@ -1150,7 +1150,7 @@ function Commands() //{{{ { command: this.name, bang: true, - options: util.Array.toObject( + options: Array_.toObject( [[v, typeof cmd[k] == "boolean" ? null : cmd[k]] // FIXME: this map is expressed multiple times for ([k, v] in Iterator({ argCount: "-nargs", bang: "-bang", count: "-count", description: "-description" })) diff --git a/common/content/completion.js b/common/content/completion.js index 30740f2b..f2276dcb 100644 --- a/common/content/completion.js +++ b/common/content/completion.js @@ -256,7 +256,7 @@ CompletionContext.prototype = { let prefix = self.value.substring(minStart, context.offset); return context.items.map(function makeItem(item) ({ text: prefix + item.text, item: item.item })); }); - return { start: minStart, items: util.Array.flatten(items), longestSubstring: this.longestAllSubstring }; + return { start: minStart, items: Array_.flatten(items), longestSubstring: this.longestAllSubstring }; } catch (e) { @@ -279,7 +279,7 @@ CompletionContext.prototype = { lists.pop()); if (!substrings) // FIXME: How is this undefined? return []; - return util.Array.uniq(substrings); + return Array_.uniq(substrings); }, // Temporary get longestAllSubstring() diff --git a/common/content/events.js b/common/content/events.js index 5d76c2dd..343f6038 100644 --- a/common/content/events.js +++ b/common/content/events.js @@ -198,7 +198,7 @@ function AutoCommands() //{{{ return { - __iterator__: function () util.Array.itervalues(store), + __iterator__: function () Array_.itervalues(store), /** * Adds a new autocommand. cmd will be executed when one of the diff --git a/common/content/hints.js b/common/content/hints.js index 9646d3f8..cba49632 100644 --- a/common/content/hints.js +++ b/common/content/hints.js @@ -786,7 +786,7 @@ function Hints() //{{{ /////////////////////////////////////////////////////////////////////////////{{{ const DEFAULT_HINTTAGS = - util.Array(["input[not(@type='hidden')]", "a", "area", "iframe", "textarea", "button", "select"]) + Array_(["input[not(@type='hidden')]", "a", "area", "iframe", "textarea", "button", "select"]) .map(function (spec) [spec, "xhtml:" + spec]).flatten() .concat("*[@onclick or @onmouseover or @onmousedown or @onmouseup or @oncommand or @role='link']") .map(function (node) "//" + node).join(" | "); diff --git a/common/content/io.js b/common/content/io.js index 55029905..4ab4af6a 100644 --- a/common/content/io.js +++ b/common/content/io.js @@ -283,7 +283,7 @@ function IO() //{{{ // TODO: Use a set/specifiable list here: let lines = [cmd.serial().map(commands.commandToString) for (cmd in commands) if (cmd.serial)]; - lines = util.Array.flatten(lines); + lines = Array_.flatten(lines); // source a user .vimperatorrc file lines.unshift('"' + liberator.version + "\n"); @@ -399,7 +399,7 @@ function IO() //{{{ completion.charset = function (context) { context.anchored = false; context.generate = function () { - let names = util.Array( + let names = Array_( "more1 more2 more3 more4 more5 unicode".split(" ").map(function (key) options.getPref("intl.charsetmenu.browser." + key).split(', ')) ).flatten().uniq(); @@ -477,7 +477,7 @@ function IO() //{{{ } } - return util.Array.flatten(commands); + return Array_.flatten(commands); }; }; diff --git a/common/content/liberator.js b/common/content/liberator.js index f2058ae2..c4d00496 100644 --- a/common/content/liberator.js +++ b/common/content/liberator.js @@ -228,7 +228,7 @@ const liberator = (function () //{{{ { let opts = [v.opts for ([k, v] in Iterator(groups))]; opts = opts.map(function (opt) [[k, v[0]] for ([k, v] in Iterator(opt))]); - return util.Array.flatten(opts); + return Array_.flatten(opts); }, validator: function (val) Option.validateCompleter.call(this, val) && [v for ([k, v] in Iterator(groups))].every(function (g) !g.validator || g.validator(val)) @@ -880,7 +880,7 @@ const liberator = (function () //{{{ return Array.map(doc.getElementsByClassName("tag"), function (elem) [elem.textContent, file]); }); - return util.Array.flatten(res); + return Array_.flatten(res); }; }; diff --git a/common/content/mappings.js b/common/content/mappings.js index 32bf284d..d0cc32fd 100644 --- a/common/content/mappings.js +++ b/common/content/mappings.js @@ -337,7 +337,7 @@ function Mappings() //{{{ function (context, obj, args) { let mode = args[0]; - return util.Array.flatten( + return Array_.flatten( [ [[name, map.description] for ([i, name] in Iterator(map.names))] for ([i, map] in Iterator(user[mode].concat(main[mode]))) diff --git a/common/content/modes.js b/common/content/modes.js index f578084a..c130c23a 100644 --- a/common/content/modes.js +++ b/common/content/modes.js @@ -129,7 +129,7 @@ const modes = (function () //{{{ NONE: 0, - __iterator__: function () util.Array.itervalues(this.all), + __iterator__: function () Array_.itervalues(this.all), get all() mainModes.slice(), diff --git a/common/content/options.js b/common/content/options.js index 8d3f4c19..f310c3a8 100644 --- a/common/content/options.js +++ b/common/content/options.js @@ -378,11 +378,11 @@ Option.prototype = { switch (operator) { case "+": - newValue = util.Array.uniq(Array.concat(this.values, values), true); + newValue = Array_(this.values).concat(values).uniq(true); break; case "^": // NOTE: Vim doesn't prepend if there's a match in the current value - newValue = util.Array.uniq(Array.concat(values, this.values), true); + newValue = Array_(values).concat(this.values).uniq(true); break; case "-": newValue = this.values.filter(function (item) values.indexOf(item) == -1); diff --git a/common/content/style.js b/common/content/style.js index b9ec5b88..3b9b0f27 100644 --- a/common/content/style.js +++ b/common/content/style.js @@ -470,7 +470,7 @@ function Styles(name, store) sss.unregisterSheet(uri, agent ? sss.AGENT_SHEET : sss.USER_SHEET); }; } -let (array = util.Array) +let (array = Array_) { util.extend(Styles.prototype, { get sites() array([v.sites for ([k, v] in this.userSheets)]).flatten().uniq().__proto__, diff --git a/common/content/template.js b/common/content/template.js index ae0ff09d..4dab16da 100644 --- a/common/content/template.js +++ b/common/content/template.js @@ -15,7 +15,7 @@ const template = { //{{{ map: function map(iter, func, sep, interruptable) { if (iter.length) // FIXME: Kludge? - iter = util.Array.itervalues(iter); + iter = Array_.itervalues(iter); let ret = <>; let n = 0; for each (let i in Iterator(iter)) diff --git a/common/content/ui.js b/common/content/ui.js index be9927c4..575e94b1 100644 --- a/common/content/ui.js +++ b/common/content/ui.js @@ -216,7 +216,7 @@ function CommandLine() //{{{ this.index += diff; if (this.index < 0 || this.index > this.store.length) { - this.index = util.Math.constrain(this.index, 0, this.store.length); + this.index = Math_.constrain(this.index, 0, this.store.length); liberator.beep(); // I don't know why this kludge is needed. It // prevents the caret from moving to the end of @@ -436,7 +436,7 @@ function CommandLine() //{{{ idx = null; break; default: - idx = util.Math.constrain(idx, 0, this.items.length - 1); + idx = Math_.constrain(idx, 0, this.items.length - 1); break; } @@ -525,7 +525,7 @@ function CommandLine() //{{{ if (this.type.list) completionList.show(); - this.wildIndex = util.Math.constrain(this.wildIndex + 1, 0, this.wildtypes.length - 1); + this.wildIndex = Math_.constrain(this.wildIndex + 1, 0, this.wildtypes.length - 1); this.preview(); statusTimer.tell(); @@ -1895,7 +1895,7 @@ function ItemList(id) //{{{ let end = startIndex + options["maxitems"]; function getRows(context) { - function fix(n) util.Math.constrain(n, 0, len); + function fix(n) Math_.constrain(n, 0, len); let len = context.items.length; let start = off; end -= !!context.message + context.incomplete; @@ -1924,7 +1924,7 @@ function ItemList(id) //{{{ for (let [i, row] in Iterator(context.getRows(start, end, doc))) nodes[i] = row; - for (let [i, row] in util.Array.iteritems(nodes)) + for (let [i, row] in Array_.iteritems(nodes)) { if (!row) continue; @@ -2285,7 +2285,7 @@ function StatusLine() //{{{ // tab numbers set if (options.get("guioptions").has("n", "N")) { - for (let [i, tab] in util.Array.iteritems(getBrowser().mTabs)) + for (let [i, tab] in Array_.iteritems(getBrowser().mTabs)) tab.setAttribute("ordinal", i + 1); } diff --git a/common/content/util.js b/common/content/util.js index a8e410bc..365ba401 100644 --- a/common/content/util.js +++ b/common/content/util.js @@ -183,10 +183,9 @@ const util = { //{{{ * * @param {object} obj The object to alter. * @param {string} key The name of the property to memoize. - * @param {function} getter A function of zero to two arguments which - * will return the property's value. obj is - * passed as the first argument, key as the - * second. + * @param {function} getter A function of zero to two arguments which will + * return the property's value. obj is passed as the first + * argument, key as the second. */ memoize: function memoize(obj, key, getter) { @@ -406,23 +405,6 @@ const util = { //{{{ return ary; }, - /** - * Math utility methods. - * @singleton - */ - Math: { - /** - * Returns the specified value constrained to the range min - - * max. - * - * @param {number} value The value to constrain. - * @param {number} min The minimum constraint. - * @param {number} max The maximum constraint. - * @returns {number} - */ - constrain: function constrain(value, min, max) Math.min(Math.max(min, value), max) - }, - /** * Converts a URI string into a URI object. * @@ -458,7 +440,7 @@ const util = { //{{{ if (typeof object != "object") return false; - const NAMESPACES = util.Array.toObject([ + const NAMESPACES = Array_.toObject([ [NS, 'liberator'], [XHTML, 'html'], [XUL, 'xul'] @@ -477,7 +459,7 @@ const util = { //{{{ } let tag = "<" + [namespaced(elem)].concat( [namespaced(a) + "=" + template.highlight(a.value, true) - for ([i, a] in util.Array.iteritems(elem.attributes))]).join(" "); + for ([i, a] in Array_.iteritems(elem.attributes))]).join(" "); if (!elem.firstChild || /^\s*$/.test(elem.firstChild) && !elem.firstChild.nextSibling) tag += '/>'; @@ -727,19 +709,18 @@ const util = { //{{{ } }; //}}} -// TODO: Why don't we just push all util.BuiltinType up into modules? --djk /** * Array utility methods. */ -util.Array = function Array_(ary) { +var Array_ = function Array_(ary) { var obj = { __proto__: ary, __iterator__: function () this.iteritems(), __noSuchMethod__: function (meth, args) { - let res = (util.Array[meth] || Array[meth]).apply(null, [this.__proto__].concat(args)); - if (util.Array.isinstance(res)) - return util.Array(res); + let res = (Array_[meth] || Array[meth]).apply(null, [this.__proto__].concat(args)); + if (Array_.isinstance(res)) + return Array_(res); return res; }, concat: function () [].concat.apply(this.__proto__, arguments), @@ -747,7 +728,7 @@ util.Array = function Array_(ary) { }; return obj; } -util.Array.isinstance = function isinstance(obj) { +Array_.isinstance = function isinstance(obj) { return Object.prototype.toString.call(obj) == "[object Array]"; }; /** @@ -760,7 +741,7 @@ util.Array.isinstance = function isinstance(obj) { * @... {string} 0 - Key * @... 1 - Value */ -util.Array.toObject = function toObject(assoc) +Array_.toObject = function toObject(assoc) { let obj = {}; assoc.forEach(function ([k, v]) { obj[k] = v; }); @@ -775,7 +756,7 @@ util.Array.toObject = function toObject(assoc) * @param {Array} ary * @returns {Array} */ -util.Array.flatten = function flatten(ary) Array.concat.apply([], ary), +Array_.flatten = function flatten(ary) Array.concat.apply([], ary), /** * Returns an Iterator for an array's values. @@ -783,7 +764,7 @@ util.Array.flatten = function flatten(ary) Array.concat.apply([], ary), * @param {Array} ary * @returns {Iterator(Object)} */ -util.Array.itervalues = function itervalues(ary) +Array_.itervalues = function itervalues(ary) { let length = ary.length; for (let i = 0; i < length; i++) @@ -796,7 +777,7 @@ util.Array.itervalues = function itervalues(ary) * @param {Array} ary * @returns {Iterator([{number}, {Object}])} */ -util.Array.iteritems = function iteritems(ary) +Array_.iteritems = function iteritems(ary) { let length = ary.length; for (let i = 0; i < length; i++) @@ -812,7 +793,7 @@ util.Array.iteritems = function iteritems(ary) * @param {boolean} unsorted * @returns {Array} */ -util.Array.uniq = function uniq(ary, unsorted) +Array_.uniq = function uniq(ary, unsorted) { let ret = []; if (unsorted) @@ -833,6 +814,24 @@ util.Array.uniq = function uniq(ary, unsorted) return ret; }; +/** + * Math utility methods. + * @singleton + */ +var Math_ = { + __proto__ : Math, + /** + * Returns the specified value constrained to the range min - + * max. + * + * @param {number} value The value to constrain. + * @param {number} min The minimum constraint. + * @param {number} max The maximum constraint. + * @returns {number} + */ + constrain: function constrain(value, min, max) Math.min(Math.max(min, value), max) +}; + function Struct() { let self = this instanceof Struct ? this : new Struct(); diff --git a/xulmus/content/library.js b/xulmus/content/library.js index a0b33f1c..587fefd0 100644 --- a/xulmus/content/library.js +++ b/xulmus/content/library.js @@ -42,7 +42,7 @@ function Library() // {{{ { let albums = toJSArray(MAIN_LIBRARY.getItemsByProperty(SBProperties.artistName, artist)) .map(function (track) track.getProperty(SBProperties.albumName)); - return util.Array.uniq(albums); + return Array_.uniq(albums); }, /** diff --git a/xulmus/content/player.js b/xulmus/content/player.js index 2697fe07..6e13b962 100644 --- a/xulmus/content/player.js +++ b/xulmus/content/player.js @@ -402,7 +402,7 @@ function Player() // {{{ if (/^[+-]/.test(arg)) level = player.volume + level; - player.volume = util.Math.constrain(level, 0, 1); + player.volume = Math_.constrain(level, 0, 1); }, { argCount: "1" }); @@ -567,7 +567,7 @@ function Player() // {{{ let min = 0; let max = gMM.playbackControl.duration - 5000; // TODO: 5s buffer like cmus desirable? - gMM.playbackControl.position = util.Math.constrain(position, min, max); + gMM.playbackControl.position = Math_.constrain(position, min, max); }, // FIXME: 10% ?