Skip to content

Instantly share code, notes, and snippets.

@danielmascena
Created August 19, 2020 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielmascena/17da8f10fa0a77ba141501b0b99f3c5c to your computer and use it in GitHub Desktop.
Save danielmascena/17da8f10fa0a77ba141501b0b99f3c5c to your computer and use it in GitHub Desktop.
A helper script to set checker types easily
/**
* @file This is a helper script to set checker types easily.
* @copyright Daniel Mascena 2020
* @function setTypeOf
*/
const setTypeOf = function() {
"use strict";
/** @constant
* @private
*/
const regExp = /\s{1}[Date|Array|RegExp|Promise|Error]+/g,
errTypo = new TypeError('Use only Built-in Objects'),
oProto = Object.prototype,
isProtoOf = oProto.isPrototypeOf.bind(oProto),
defProp = Object.defineProperty;
/**
* @params {(Array|Set|Map)} ArrayOfTypes - Iterable object with built-in objects
* @throws Will throw an error if the argument is a non iterable object.
*/
return (arrayOfTypes) => {
const arr = Object(arrayOfTypes);
// check for parameter type and the content
if (Symbol.iterator in arr?'forEach' in arr?false:true:true) {
throw errTypo;
}
if (arr.toString().match(regExp).length < arr.length) {
console.log('%cPass only standards built-in objects as a values','color: orange; font-size: 12px');
console.log('https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects');
}
arr.forEach((fn) => {
const fnProto = Object(fn).prototype;
if (isProtoOf(fnProto) && !fnProto.hasOwnProperty('typeOf')) {
defProp(fnProto, 'typeOf', {
get() {
return super.toString().slice(8, -1);
}
});
}
});
};
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment