amos-tool
Version:
amos ui tool
87 lines (76 loc) • 3.53 kB
JavaScript
;
var _templateObject, _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"), _taggedTemplateLiteral2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteral")), _require = require("./is"), isCollection = _require.isCollection, contains = _require.contains, isArray = _require.isArray, hasOwnProp = _require.hasOwnProp, isObject = _require.isObject, isString = _require.isString, METHODS = [ "min", "max", "average", "dense", "ordinal" ], MISSING = [ "last", "first", "remove", "*" ];
function validate(e, r) {
if (!isObject(r)) return new TypeError("invalid argument. Options argument must be an object. Value: '".concat(r, "'."));
if (hasOwnProp(r, "encoding") && (e.encoding = r.encoding, !isArray(e.encoding))) return new TypeError("invalid option. " % s(_templateObject || (_templateObject = (0,
_taggedTemplateLiteral2.default)([ " option must be an array. Option: 'encoding=", "'." ])), e.encoding));
if (hasOwnProp(r, "method") && (e.method = r.method, !isString(e.method) || !contains(METHODS, e.method))) {
var n = "invalid option. `method` option must be one of the following: `min, max, average, dense, ordinal`. Option: " + e.method + " .";
return new TypeError(n);
}
if (hasOwnProp(r, "missing") && (e.missing = r.missing, !isString(e.missing) || !contains(MISSING, e.missing))) {
var i = "invalid option. missing option must be one of the following: `last,first,remove`. Option: " + e.missing + " .";
return new TypeError(i);
}
return null;
}
function isMissing(e, r) {
var n, i, t;
for (n = e.length, i = new Array(n), t = 0; t < n; t++) i[t] = contains(r, e[t]);
return i;
}
function sum(e) {
var r, n, i;
for (r = e.length, n = 0, i = 0; i < r; i++) n += e[i];
return n;
}
function compareFunction(e, r) {
return e < r ? -1 : e > r ? 1 : 0;
}
function order(e) {
var r, n;
for (r = new Array(e.length), n = 0; n < e.length; n++) r[n] = n;
return r.sort(function(r, n) {
return compareFunction(e[r], e[n]);
});
}
function ranks(e, r) {
var n, i, t, o, a, s, l, u, g, m, c, f, h, d, p, w, b, v;
if (!isCollection(e)) throw new TypeError("invalid argument. First argument must be an array-like object. Value: '".concat(e, "'."));
if (h = {}, arguments.length > 1 && (p = validate(h, r))) throw p;
for (c = h.method || "average", s = h.encoding || [ null, NaN ], g = h.missing || "last",
w = e.length, d = [], b = 0; b < w; b++) contains(s, e[b]) || d.push(e[b]);
if (n = isMissing(e, s), w = d.length, o = 0, f = new Array(w), u = order(d), "ordinal" === c) for (b = 0; b < w; b++) f[u[b]] = b + 1; else for (i = 0,
b = 0; b < w; b++) if (l = b + 1, b === w - 1 || d[u[b]] !== d[u[l]]) {
switch (c) {
case "min":
m = l - i;
break;
case "max":
m = l;
break;
case "dense":
m = l - i - o, o += i;
break;
default:
m = l - .5 * i;
}
for (v = b - i; v < l; v++) f[u[v]] = m;
i = 0;
} else i += 1;
if ("first" === g) {
for (t = sum(n), v = 1, a = new Array(n.length), b = 0; b < n.length; b++) n[b] ? (a[b] = v,
v += 1) : a[b] = f.shift() + t;
return a;
}
if ("last" === g) {
for (a = new Array(n.length), b = 0; b < n.length; b++) n[b] ? a[b] = b + f.length + 1 : a[b] = f.shift();
return a;
}
if ("*" === g) {
for (a = new Array(n.length), b = 0; b < n.length; b++) n[b] ? a[b] = null : a[b] = f.shift();
return a;
}
return f;
}
module.exports = ranks;