summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/lib/mozilla-toolkit-versioning/index.js
blob: 2f607c8804a32aad85a52db87e01ada9339b9d00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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;