summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/content/buffer.js10
-rw-r--r--common/content/ui.js8
-rw-r--r--common/content/util.js17
3 files changed, 24 insertions, 11 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
@@ -388,6 +388,23 @@ const util = { //{{{
},
/**
+ * Math utility methods.
+ * @singleton
+ */
+ Math: {
+ /**
+ * Returns the specified <b>value</b> constrained to the range <b>min</b> -
+ * <b>max</b>.
+ *
+ * @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.
*
* @param {string} uri