@forzalabs/remora
Version:
A powerful CLI tool for seamless data translation.
43 lines (42 loc) • 1.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Checks if 'value' is truthy. If not, throws an exception with custom 'msg' (if present).
*/
const affirm = (value, msg) => {
if (!value)
throw new Error(`Affirm failed${(msg && msg.length > 0) ? `: ${msg}` : ''}`);
};
affirm.hasValue = (value, msg) => {
if (value === null || value === undefined)
throw new Error(`Affirm failed${(msg && msg.length > 0) ? `: ${msg}` : ''}`);
};
affirm.hasItems = (value, msg) => {
if (!value || !Array.isArray(value) || !(value.length > 0))
throw new Error(`Affirm failed${(msg && msg.length > 0) ? `: ${msg}` : ''}`);
};
affirm.doesntInclude = (value, array, msg) => {
affirm(value, msg);
if (!array || !Array.isArray(array) || array.includes(value))
throw new Error(`Affirm failed${(msg && msg.length > 0) ? `: ${msg}` : ''}`);
};
affirm.doesntExist = (item, array, key, msg) => {
affirm(item, msg);
if (!array || !Array.isArray(array) || !array.some(arrayItem => arrayItem[key] === item[key]))
throw new Error(`Affirm failed${(msg && msg.length > 0) ? `: ${msg}` : ''}`);
};
affirm.noDupes = (array, msg) => {
if (!array || !Array.isArray(array) || new Set(array).size !== array.length)
throw new Error(`Affirm failed${(msg && msg.length > 0) ? `: ${msg}` : ''}`);
};
affirm.noDupedItems = (array, key, msg) => {
if (!array || !Array.isArray(array))
throw new Error(`Affirm failed${(msg && msg.length > 0) ? `: ${msg}` : ''}`);
const seen = new Set();
for (const item of array) {
if (seen.has(item[key]))
throw new Error(`Affirm failed${(msg && msg.length > 0) ? `: ${msg}` : ''}`);
seen.add(item[key]);
}
};
exports.default = affirm;