XciD's picture
XciD HF staff
initial commit
8969f81
raw history blame
No virus
2.28 kB
import * as util from 'util';
import * as child_process from 'child_process';
import { ObjectID } from 'mongodb';
const __exec = util.promisify(child_process.exec);
export namespace Utils {
/**
* Return a random integer between min and max (upper bound is exclusive).
*/
export function randomInt(maxOrMin: number, max?: number): number {
return (max)
? maxOrMin + Math.floor(Math.random() * (max - maxOrMin))
: Math.floor(Math.random() * maxOrMin);
}
/**
* Random element from array (usually not needed b/c we extend Array in Extensions.ts).
*/
export function randomItem<T>(arr: T[]): T {
return arr[Math.floor(Math.random()*arr.length)];
}
/**
* Return copy of object, only keeping whitelisted properties.
*/
export function pick<T, K extends keyof T>(o: T, props: K[]): Pick<T, K> {
// inspired by stackoverflow.com/questions/25553910/one-liner-to-take-some-properties-from-object-in-es-6
// Warning: this adds {p: undefined} for props not in the o object.
return Object.assign({}, ...props.map(prop => ({[prop]: o[prop]})));
}
/**
* One param: create list of integers from 0 (inclusive) to n (exclusive)
* Two params: create list of integers from a (inclusive) to b (exclusive)
*/
export function range(n: number, b?: number): number[] {
return (b)
? Array(b - n).fill(0).map((_, i) => n + i)
: Array(n).fill(0).map((_, i) => i);
}
/**
* Gets the value at path of object, or undefined.
*/
export function get(o: any, path: string | string[]) {
const properties = Array.isArray(path) ? path : path.split('.');
let x = o;
for (const p of properties) {
x = x[p];
if (x === undefined) {
return undefined;
}
}
return x;
}
/**
* Asynchronously filter on the given array
*/
export async function filter<T>(array: T[], __filter: (element: T, index: number, array: T[]) => Promise<boolean>, thisArg?: any): Promise<T[]> {
const keeps = await Promise.all(array.map(__filter));
return array.filter((e, i) => keeps[i], thisArg);
}
export function randomStr(length: number): string {
const chars = range(97, 123).map(x => String.fromCharCode(x));
return Array(length).fill(0)
.map(x => randomInt(2) ? randomItem(chars) : randomItem(chars).toUpperCase())
.join("")
;
}
}