meows
Version:
A kittybin of tools.
54 lines (53 loc) • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("./types");
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Error and Logging
exports.log = (...a) => console.log(...a);
exports.logStr = (msg, ...a) => console.log(`${msg}: `, ...a);
/**
* Log something to console with optional message, then returns identical input.
*
* @example
* Promise.all(pending)
* .then(x => logId(x, 'task: done'))
* .then(sendTask)
*/
exports.logId = (x, msg = 'logId()') => void console.log(`${msg}: `, x) || x;
/**
* Creates a string for an error message for your function.
* @param fnName name of your function
* @param reqs list of type requirements
* @param args arguments received by function
*
* @example
* throw new Error(ErrorMsg(isEven, ['integer'], arguments))
*/
function ErrorMsg(fnName, reqs, args) {
const msg = [`${fnName}() requires a`];
const end = args.map((n, i) => `\n arg[${i}]: ${types_1.typeName(n)}`);
switch (reqs.length) {
case 0: throw new Error(`ErrorMsg() arity requirements not met.`);
case 1: return msg.concat(`${reqs[0]}, but got:`).join(' ').concat(...end);
case 2: return msg.concat(`${reqs[0]} and ${reqs[1]}, but got:`).join(' ').concat(...end);
default: return reqs.reduce((state, n, i, arr) => {
if (i === arr.length - 1)
return state.concat(`and ${n}, but got:`);
else
return state.concat(`${n},`);
}, msg).join(' ').concat(...end);
}
}
exports.ErrorMsg = ErrorMsg;
/**
* Takes an array of boolean functions, an argument to run with the tests, and
* and an optional error message.
*
* @example
* ifOrThrow(100, [isType.int, isEven], `Value wasn't an even integer.`)
*/
function ifOrThrow(ifs, arg, msg) {
if (!ifs.every(λ => λ(arg)))
throw new Error(msg);
}
exports.ifOrThrow = ifOrThrow;