Skip to content

Instantly share code, notes, and snippets.

@ganeshrvel
Created November 24, 2020 16:39
Show Gist options
  • Save ganeshrvel/cf2b7dc95345e6a52b546d8e78c1d9b3 to your computer and use it in GitHub Desktop.
Save ganeshrvel/cf2b7dc95345e6a52b546d8e78c1d9b3 to your computer and use it in GitHub Desktop.
checkIf
import { assert } from '@sindresorhus/is';
import { IS_PROD } from '../constants/env';
/**
* description - Validate the types. In production mode the validation will be skipped
*
* @param value - variable
* @param {'string'|'boolean'|'number'|'numericString'|'array'|'function'|'object'|'undefinedOrNull'|'undefined'|'null'} type - type to compare
*
* @return {boolean}
*/
export function checkIf(value, type) {
if (IS_PROD) {
return true;
}
if (type === 'string') {
return assert.string(value);
}
if (type === 'boolean') {
return assert.boolean(value);
}
if (type === 'number') {
return assert.number(value);
}
if (type === 'numericString') {
return assert.numericString(value);
}
if (type === 'array') {
return assert.array(value);
}
if (type === 'function') {
return assert.function(value);
}
if (type === 'object') {
return assert.object(value);
}
if (type === 'undefinedOrNull') {
return assert.null(value) || assert.undefined(value);
}
if (type === 'null') {
return assert.null(value);
}
if (type === 'undefined') {
return assert.undefined(value);
}
// eslint-disable-next-line no-throw-literal
throw `invalid 'type' provided in 'checkIf'`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment