bal-util
Version:
Common utility functions for Node.js used and maintained by Benjamin Lupton
161 lines (155 loc) • 4.45 kB
JavaScript
// Generated by CoffeeScript 2.3.1
// Import
var balUtilCompare, balUtilPaths;
balUtilCompare = null;
balUtilPaths = require('./paths');
// =====================================
// Compare
balUtilCompare = {
// Version Compare
// http://phpjs.org/functions/version_compare
// MIT Licensed http://phpjs.org/pages/license
versionCompare: function versionCompare(v1, operator, v2) {
var compare, i, j, numVersion, prepVersion, ref, result, vm, x;
i = x = compare = 0;
vm = {
'dev': -6,
'alpha': -5,
'a': -5,
'beta': -4,
'b': -4,
'RC': -3,
'rc': -3,
'#': -2,
'p': -1,
'pl': -1
};
prepVersion = function prepVersion(v) {
v = ('' + v).replace(/[_\-+]/g, '.');
v = v.replace(/([^.\d]+)/g, '.$1.').replace(/\.{2,}/g, '.');
if (!v.length) {
return [-8];
} else {
return v.split('.');
}
};
numVersion = function numVersion(v) {
if (!v) {
return 0;
} else {
if (isNaN(v)) {
return vm[v] || -7;
} else {
return parseInt(v, 10);
}
}
};
v1 = prepVersion(v1);
v2 = prepVersion(v2);
x = Math.max(v1.length, v2.length);
for (i = j = 0, ref = x; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) {
if (v1[i] === v2[i]) {
continue;
}
v1[i] = numVersion(v1[i]);
v2[i] = numVersion(v2[i]);
if (v1[i] < v2[i]) {
compare = -1;
break;
} else if (v1[i] > v2[i]) {
compare = 1;
break;
}
}
if (!operator) {
return compare;
}
result = function () {
switch (operator) {
case '>':
case 'gt':
return compare > 0;
case '>=':
case 'ge':
return compare >= 0;
case '<=':
case 'le':
return compare <= 0;
case '==':
case '=':
case 'eq':
case 'is':
return compare === 0;
case '<>':
case '!=':
case 'ne':
case 'isnt':
return compare !== 0;
case '':
case '<':
case 'lt':
return compare < 0;
default:
return null;
}
}();
// Return result
return result;
},
// Compare Package
packageCompare: function packageCompare(_ref) {
var local = _ref.local,
remote = _ref.remote,
newVersionCallback = _ref.newVersionCallback,
sameVersionCallback = _ref.sameVersionCallback,
oldVersionCallback = _ref.oldVersionCallback,
errorCallback = _ref.errorCallback;
var details, runCompare;
// Prepare
details = {};
// Handler
runCompare = function runCompare() {
if (balUtilCompare.versionCompare(details.local.version, '<', details.remote.version)) {
return typeof newVersionCallback === "function" ? newVersionCallback(details) : void 0;
} else if (balUtilCompare.versionCompare(details.local.version, '==', details.remote.version)) {
return typeof sameVersionCallback === "function" ? sameVersionCallback(details) : void 0;
} else if (balUtilCompare.versionCompare(details.local.version, '>', details.remote.version)) {
return typeof oldVersionCallback === "function" ? oldVersionCallback(details) : void 0;
}
};
// Read local
balUtilPaths.readPath(local, function (err, data) {
var dataStr;
if (err) {
return typeof errorCallback === "function" ? errorCallback(err, data) : void 0;
}
try {
dataStr = data.toString();
details.local = JSON.parse(dataStr);
} catch (error) {
err = error;
return typeof errorCallback === "function" ? errorCallback(err, data) : void 0;
}
// Read remote
return balUtilPaths.readPath(remote, function (err, data) {
if (err) {
return typeof errorCallback === "function" ? errorCallback(err, data) : void 0;
}
try {
dataStr = data.toString();
details.remote = JSON.parse(dataStr);
} catch (error) {
err = error;
return typeof errorCallback === "function" ? errorCallback(err, data) : void 0;
}
// Compare
return runCompare();
});
});
return this;
}
};
// =====================================
// Export
module.exports = balUtilCompare;
;