UNPKG

@bitblit/ratchet-common

Version:

Common tools for general use

56 lines 2.5 kB
export class RequireRatchet { static isNullOrUndefined(ob) { return Object.is(ob, null) || Object.is(ob, undefined); } static notNullOrUndefined(ob, name = 'object') { if (RequireRatchet.isNullOrUndefined(ob)) { throw new Error(name + ' may not be null or undefined'); } } static notNullUndefinedOrOnlyWhitespaceString(ob, name = 'string') { if (RequireRatchet.isNullOrUndefined(ob) || ob.trim() === '') { throw new Error(name + ' may not be null or undefined or only whitespace string'); } } static notNullUndefinedOrEmptyArray(ob, name = 'string') { if (RequireRatchet.isNullOrUndefined(ob) || ob.length === 0) { throw new Error(name + ' may not be null or undefined or an empty array'); } } static equal(ob1, ob2, message = 'Values must be equal') { if (ob1 !== ob2) { throw new Error(message); } } static true(ob, message = 'Value must be true') { RequireRatchet.equal(ob, true, message); } static noNullOrUndefinedValuesInArray(arr, expectedLength = null, customMsg = null) { RequireRatchet.notNullOrUndefined(arr, 'Source array may not be null'); if (expectedLength !== null && arr.length !== expectedLength) { throw new Error(`Expected array of length ${expectedLength} but was ${arr.length} ${customMsg}`); } for (let i = 0; i < arr.length; i++) { if (RequireRatchet.isNullOrUndefined(arr[i])) { throw new Error(`Array index ${i} was null or undefined ${customMsg}`); } } } static noNullOrUndefinedValuesInRestArgs(expectedLength, ...restArgs) { RequireRatchet.notNullOrUndefined(restArgs, 'Source array may not be null'); if (expectedLength !== null && restArgs.length !== expectedLength) { throw new Error(`Expected array of length ${expectedLength} but was ${restArgs.length}`); } for (let i = 0; i < restArgs.length; i++) { if (RequireRatchet.isNullOrUndefined(restArgs[i])) { throw new Error(`Array index ${i} was null or undefined`); } } } static constructorArgumentsMatchLengthAndAreNonNull() { const args = Array.from(arguments); const len = this.constructor.length; return RequireRatchet.noNullOrUndefinedValuesInArray(args, len); } } //# sourceMappingURL=require-ratchet.js.map