typeval
v2.0.0
Published
typeVal es una función útil para determinar el tipo de un valor en JavaScript y realizar comparaciones de tipo de manera flexible y precisa.
Downloads
19
Readme
typeVal Module
Este es un módulo de JavaScript que exporta una función llamada typeVal
. La función toma dos parámetros: value
(valor) y type
(tipo). Así es como funciona:
El objeto option
define las correspondencias entre los tipos de JavaScript y sus representaciones en forma de cadena.
Si no se proporciona el parámetro type
, la función devuelve la representación en forma de cadena del tipo del parámetro value
. Busca la función adecuada en el objeto option
en función del tipo del parámetro value
y la invoca, devolviendo el resultado.
Ejemplo de uso
const { typeVal, typeValmin } = require('typeVal');
// input: value
// output: name
console.log(typeVal(42)); // "number"
console.log(typeVal(BigInt(42))); // "bigint"
console.log(typeVal("")); // "string"
console.log(typeVal(Symbol())); // "symbol"
console.log(typeVal(true)); // "boolean"
console.log(typeVal(undefined)); // "undefined"
console.log(typeVal(()=>{})); // "arrow"
console.log(typeVal(function(){})); // "function"
console.log(typeVal(class {})); // "class"
console.log(typeVal(null)); // "null"
console.log(typeVal([])); // "array"
console.log(typeVal({})); // "object"
console.log(typeVal(new (class {}))); // "instance"
Si se proporciona el parámetro type
, la función verifica si la representación en forma de cadena del tipo del parámetro value
coincide con el type
proporcionado. Lo hace comparando el resultado de invocar la función adecuada del objeto option
con el type
proporcionado. Si coinciden, devuelve true
; de lo contrario, devuelve false
.
Ejemplo de uso
// inputs comparation
// output: boolean
console.log(typeVal(42, "number")); // true
console.log(typeVal(BigInt(42), "bigint")); // true
console.log(typeVal("", "string")); // true
console.log(typeVal(Symbol(), "symbol")); // true
console.log(typeVal(true, "boolean")); // true
console.log(typeVal(undefined, "undefined")); // true
console.log(typeVal(()=>{}, "arrow")); // true
console.log(typeVal(function(){}, "function")); // true
console.log(typeVal(class {}, "class")); // true
console.log(typeVal(null, "null")); // true
console.log(typeVal([], "array")); // true
console.log(typeVal({}, "object")); // true
console.log(typeVal(new (class {}), "instance")); // true