Validate that a variable is of a certain data type.
npm install dotherightthing/dtrt-type-validate
// script.cjs
const dtrtValidate = require('dtrt-type-validate');
// script.mjs
import dtrtValidate from 'dtrt-type-validate';
<!-- page.html -->
<!-- use import-maps to aid module resolution of bare import specifiers -->
<script type="importmap">
{
"imports": {
"dtrt-string-utils": "./node_modules/dtrt-string-utils/dist/dtrt-string-utils.mjs",
"dtrt-type-validate": "./node_modules/dtrt-type-validate/dist/dtrt-type-validate.mjs"
}
}
</script>
<script src="/script.mjs" type="module"></script>
// script.cjs/.mjs
const foo = (param) => {
const validObject = dtrtValidate.validate(param, 'object', 'myFunc');
// do something with validObject
};
const bar = (param) => {
if (dtrtValidate.isArray(param)) {
// do something with param which is an array
}
};
npm run lint
npm run test