alwz
Version:
Extendable library for typecasting
63 lines • 1.62 kB
JavaScript
import EV from './ErrorValue.js';
import { TypesEnum } from '../constants/types.js';
export default {
/**
* @example
* const isString = type('string');
* isString(1); // false
* isString('1'); // true
*/
type: (type) => {
if (type in TypesEnum) {
return ((input) => typeof input === type);
}
else {
throw new EV('unknown type', type);
}
},
/**
* @example
* class Test {}
* const isTest = instance(Test);
* isTest({}); // false
* isTest(new Test()); // true
*/
instance: (prototype) => {
if ((typeof prototype === 'object' && prototype !== null) || typeof prototype === 'function') {
/* @ts-ignore */
return ((input) => input instanceof prototype);
}
else {
throw new EV('invalid prototype', prototype);
}
},
/**
* @example
* const isVariant = variant([1, 2, 3]);
* isVariant(4); // false
* isVariant(3); // true
*/
variant: (list) => {
if (Array.isArray(list)) {
return ((input) => list.includes(input));
}
else {
throw new EV('invalid variants list', list);
}
},
/**
* @example
* const isOdd = check((v) => v % 2 ? true : false);
* isOdd(0); // false
* isOdd(1); // true
*/
check: (guard) => {
if (typeof guard === 'function') {
return guard;
}
else {
throw new EV('invalid guard', guard);
}
},
};
//# sourceMappingURL=Is.js.map