@hcikit/workflow
Version:
A workflow manager for running experiments.
156 lines (155 loc) • 5.03 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
/**
* Shuffles array in place. ES6 version
* @param {Array} a An array containing the items.
*/
export function shuffle(a) {
var _a;
for (var i = a.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
_a = __read([a[j], a[i]], 2), a[i] = _a[0], a[j] = _a[1];
}
return a;
}
/**
* Returns a random choice from the array.
* @param {Array} a An array containing the options.
*/
export function randomChoice(a) {
return a[Math.floor(Math.random() * a.length)];
}
/**
* Returns a random choice from the array.
* @param {Array} a An array containing the options.
*/
export function randomChoiceNoReplacement(a) {
var index = Math.floor(Math.random() * a.length);
var choice = a[index];
a.splice(index, 1);
return choice;
}
/**
* Returns a random number between min (inclusive) and max (exclusive)
*/
export function randInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; // The maximum is exclusive and the minimum is inclusive
}
/**
* Generates a random GUID.
*/
export function uuidv4() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
var r = (Math.random() * 16) | 0;
var v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
/**
* Generates a random string.
*/
export function randomString() {
return Math.random().toString(36).substring(2);
}
/**
* Gets the OS of the user.
*/
export function getOS() {
var e_1, _a;
var oss = {
Win: "Windows",
Mac: "MacOS",
X11: "UNIX",
Linux: "Linux",
};
try {
for (var _b = __values(Object.keys(oss)), _c = _b.next(); !_c.done; _c = _b.next()) {
var os = _c.value;
if (navigator.appVersion.indexOf(os) >= 0) {
return oss[os];
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return "Unknown";
}
/**
* Grabs a bunch of info from the browser for logging. Tries not to include too much information but there is more such as the location API etc.
*/
export function getBrowserInfo() {
// https://stackoverflow.com/a/37093316/1036813
return {
browserName: navigator.appName,
browserEngine: navigator.product,
browserVersion1a: navigator.appVersion,
browserVersion1b: navigator.userAgent,
browserLanguage: navigator.language,
browserOnline: navigator.onLine,
browserPlatform: navigator.platform,
sizeScreenW: window.screen.width,
sizeScreenH: window.screen.height,
sizeInW: window.innerWidth,
sizeInH: window.innerHeight,
sizeAvailW: window.screen.availWidth,
sizeAvailH: window.screen.availHeight,
scrColorDepth: window.screen.colorDepth,
scrPixelDepth: window.screen.pixelDepth,
};
}
export function getUrlParams(defaults) {
if (defaults === void 0) { defaults = {
participant: "".concat(randomString(), "-default"),
WORKER_ID: "".concat(randomString(), "-default"),
ASSIGNMENT_ID: "".concat(randomString(), "-default"),
HIT_ID: "".concat(randomString(), "-default"),
}; }
var params = Object.fromEntries(new URLSearchParams(window.location.search));
return __assign(__assign({}, defaults), params);
}
export function getAllMetadata() {
return __assign(__assign(__assign(__assign({}, getUrlParams()), process.env), getBrowserInfo()), { os: getOS() });
}