From 95dffaf95a3653e55a34e090b800f9a79e9a8fd5 Mon Sep 17 00:00:00 2001 From: Doug Kearns Date: Sat, 30 May 2009 20:29:23 +1000 Subject: [PATCH] Add util.Math.constrain. --- common/content/buffer.js | 10 +++------- common/content/ui.js | 8 ++++---- common/content/util.js | 17 +++++++++++++++++ vimperator/content/bookmarks.js | 2 +- xulmus/content/player.js | 4 ++-- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/common/content/buffer.js b/common/content/buffer.js index 762b0e10..ac10e134 100644 --- a/common/content/buffer.js +++ b/common/content/buffer.js @@ -72,8 +72,7 @@ function Buffer() //{{{ { let values = ZoomManager.zoomValues; let cur = values.indexOf(ZoomManager.snap(ZoomManager.zoom)); - let i = cur + steps; - i = Math.max(0, Math.min(values.length - 1, i)); + let i = util.Math.constrain(cur + steps, 0, values.length - 1); if (i == cur && fullZoom == ZoomManager.useFullZoom) liberator.beep(); @@ -361,7 +360,7 @@ function Buffer() //{{{ if (elements.length > 0) { - count = Math.min(Math.max(count, 1), elements.length); + count = util.Math.constrain(count, 1, elements.length); buffer.focusElement(elements[count - 1]); } else @@ -649,10 +648,7 @@ function Buffer() //{{{ level = buffer.textZoom + parseInt(arg, 10); // relative args shouldn't take us out of range - if (level < ZOOM_MIN) - level = ZOOM_MIN; - if (level > ZOOM_MAX) - level = ZOOM_MAX; + level = util.Math.constrain(level, ZOOM_MIN, ZOOM_MAX); } else return void liberator.echoerr("E488: Trailing characters"); diff --git a/common/content/ui.js b/common/content/ui.js index 288e367b..0ed9799e 100644 --- a/common/content/ui.js +++ b/common/content/ui.js @@ -154,7 +154,7 @@ function CommandLine() //{{{ this.index += diff; if (this.index < 0 || this.index > this.store.length) { - this.index = Math.max(0, Math.min(this.store.length, this.index)); + this.index = util.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 @@ -373,7 +373,7 @@ function CommandLine() //{{{ idx = null; break; default: - idx = Math.max(0, Math.min(this.items.length - 1, idx)); + idx = util.Math.constrain(idx, 0, this.items.length - 1); break; } @@ -479,7 +479,7 @@ function CommandLine() //{{{ if (this.type.list) completionList.show(); - this.wildIndex = Math.max(0, Math.min(this.wildtypes.length - 1, this.wildIndex + 1)); + this.wildIndex = util.Math.constrain(this.wildIndex + 1, 0, this.wildtypes.length - 1); this.preview(); statusTimer.tell(); @@ -1831,7 +1831,7 @@ function ItemList(id) //{{{ let end = startIndex + options["maxitems"]; function getRows(context) { - function fix(n) Math.max(0, Math.min(len, n)); + function fix(n) util.Math.constrain(n, 0, len); end -= !!context.message + context.incomplete; let len = context.items.length; let start = off; diff --git a/common/content/util.js b/common/content/util.js index fc3e2b22..bc17ee18 100644 --- a/common/content/util.js +++ b/common/content/util.js @@ -387,6 +387,23 @@ 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. * diff --git a/vimperator/content/bookmarks.js b/vimperator/content/bookmarks.js index 6fca9451..a66b3a06 100644 --- a/vimperator/content/bookmarks.js +++ b/vimperator/content/bookmarks.js @@ -831,7 +831,7 @@ function History() //{{{ liberator.beep(); else { - let index = Math.max(start, Math.min(end, current + steps)); + let index = util.Math.constrain(current + steps, start, end); window.getWebNavigation().gotoIndex(index); } }, diff --git a/xulmus/content/player.js b/xulmus/content/player.js index 999b14f7..dbd590c3 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 = Math.min(Math.max(level, 0), 1); + player.volume = util.Math.constrain(level, 0, 1); }, { argCount: "1" }); @@ -528,7 +528,7 @@ function Player() // {{{ let min = 0; let max = gMM.playbackControl.duration - 5000; // TODO: 5s buffer like cmus desirable? - gMM.playbackControl.position = Math.min(Math.max(position, min), max); + gMM.playbackControl.position = util.Math.constrain(position, min, max); }, // FIXME: 10% ?