summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/mozilla-toolkit-versioning
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
committerMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
commitac46df8daea09899ce30dc8fd70986e258c746bf (patch)
tree2750d3125fc253fd5b0671e4bd268eff1fd97296 /toolkit/jetpack/mozilla-toolkit-versioning
parent8cecf8d5208f3945b35f879bba3015bb1a11bec6 (diff)
downloadUXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.gz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.lz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.xz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.zip
Move Add-on SDK source to toolkit/jetpack
Diffstat (limited to 'toolkit/jetpack/mozilla-toolkit-versioning')
-rw-r--r--toolkit/jetpack/mozilla-toolkit-versioning/index.js112
-rw-r--r--toolkit/jetpack/mozilla-toolkit-versioning/lib/utils.js15
-rw-r--r--toolkit/jetpack/mozilla-toolkit-versioning/package.json21
3 files changed, 148 insertions, 0 deletions
diff --git a/toolkit/jetpack/mozilla-toolkit-versioning/index.js b/toolkit/jetpack/mozilla-toolkit-versioning/index.js
new file mode 100644
index 000000000..2f607c880
--- /dev/null
+++ b/toolkit/jetpack/mozilla-toolkit-versioning/index.js
@@ -0,0 +1,112 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+var versionParse = require('./lib/utils').versionParse;
+
+var COMPARATORS = ['>=', '<=', '>', '<', '=', '~', '^'];
+
+exports.parse = function (input) {
+ input = input || '';
+ input = input.trim();
+ if (!input)
+ throw new Error('`parse` argument must be a populated string.');
+
+ // Handle the "*" case
+ if (input === "*") {
+ return { min: undefined, max: undefined };
+ }
+
+ var inputs = input.split(' ');
+ var min;
+ var max;
+
+ // 1.2.3 - 2.3.4
+ if (inputs.length === 3 && inputs[1] === '-') {
+ return { min: inputs[0], max: inputs[2] };
+ }
+
+ inputs.forEach(function (input) {
+ var parsed = parseExpression(input);
+ var version = parsed.version;
+ var comparator = parsed.comparator;
+
+ // 1.2.3
+ if (inputs.length === 1 && !comparator)
+ min = max = version;
+
+ // Parse min
+ if (~comparator.indexOf('>')) {
+ if (~comparator.indexOf('='))
+ min = version; // >=1.2.3
+ else
+ min = increment(version); // >1.2.3
+ }
+ else if (~comparator.indexOf('<')) {
+ if (~comparator.indexOf('='))
+ max = version; // <=1.2.3
+ else
+ max = decrement(version); // <1.2.3
+ }
+ });
+
+ return {
+ min: min,
+ max : max
+ };
+};
+
+function parseExpression (input) {
+ for (var i = 0; i < COMPARATORS.length; i++)
+ if (~input.indexOf(COMPARATORS[i]))
+ return {
+ comparator: COMPARATORS[i],
+ version: input.substr(COMPARATORS[i].length)
+ };
+ return { version: input, comparator: '' };
+}
+
+/**
+ * Takes a version string ('1.2.3') and returns a version string
+ * that'll parse as one less than the input string ('1.2.3.-1').
+ *
+ * @param {String} vString
+ * @return {String}
+ */
+function decrement (vString) {
+ return vString + (vString.charAt(vString.length - 1) === '.' ? '' : '.') + '-1';
+}
+exports.decrement = decrement;
+
+/**
+ * Takes a version string ('1.2.3') and returns a version string
+ * that'll parse as greater than the input string by the smallest margin
+ * possible ('1.2.3.1').
+ * listed as number-A, string-B, number-C, string-D in
+ * Mozilla's Toolkit Format.
+ * https://developer.mozilla.org/en-US/docs/Toolkit_version_format
+ *
+ * @param {String} vString
+ * @return {String}
+ */
+function increment (vString) {
+ var match = versionParse(vString);
+ var a = match[1];
+ var b = match[2];
+ var c = match[3];
+ var d = match[4];
+ var lastPos = vString.length - 1;
+ var lastChar = vString.charAt(lastPos);
+
+ if (!b) {
+ return vString + (lastChar === '.' ? '' : '.') + '1';
+ }
+ if (!c) {
+ return vString + '1';
+ }
+ if (!d) {
+ return vString.substr(0, lastPos) + (++lastChar);
+ }
+ return vString.substr(0, lastPos) + String.fromCharCode(lastChar.charCodeAt(0) + 1);
+}
+exports.increment = increment;
diff --git a/toolkit/jetpack/mozilla-toolkit-versioning/lib/utils.js b/toolkit/jetpack/mozilla-toolkit-versioning/lib/utils.js
new file mode 100644
index 000000000..e068085c0
--- /dev/null
+++ b/toolkit/jetpack/mozilla-toolkit-versioning/lib/utils.js
@@ -0,0 +1,15 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * Breaks up a version string into the 4 components
+ * defined in:
+ * https://developer.mozilla.org/en-US/docs/Toolkit_version_format
+ * @params {String} val
+ * @return {String}
+ */
+function versionParse (val) {
+ return val.match(/^([0-9\.]*)([a-zA-Z]*)([0-9\.]*)([a-zA-Z]*)$/);
+}
+exports.versionParse = versionParse;
diff --git a/toolkit/jetpack/mozilla-toolkit-versioning/package.json b/toolkit/jetpack/mozilla-toolkit-versioning/package.json
new file mode 100644
index 000000000..d9b0424e5
--- /dev/null
+++ b/toolkit/jetpack/mozilla-toolkit-versioning/package.json
@@ -0,0 +1,21 @@
+{
+ "name": "mozilla-toolkit-versioning",
+ "version": "0.0.2",
+ "description": "Parser for Mozilla's toolkit version format",
+ "main": "index.js",
+ "scripts": {
+ "test": "./node_modules/.bin/mocha --reporter spec --ui bdd"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/jsantell/mozilla-toolkit-versioning.git"
+ },
+ "author": "Jordan Santell",
+ "license": "MIT",
+ "dependencies": {},
+ "devDependencies": {
+ "mocha": "^1.21.4",
+ "chai": "^1.9.1",
+ "mozilla-version-comparator": "^1.0.2"
+ }
+}