
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@berish/typeof
Advanced tools
Library for information about the type of any object or primitive
$ npm install @berish/typeof --save
or
$ yarn add @berish/typeof
Supports typescript
import type, { ITypeofHandler, TypeofResult } from '../';
console.log(type('')); // === 'string'
console.log(type('hello')); // === 'string'
console.log(type(new String('helloo'))); // === 'string'
console.log(type(0)); // === 'number'
console.log(type(-0)); // === 'number'
console.log(type(0xff)); // === 'number'
console.log(type(-3.142)); // === 'number'
console.log(type(Infinity)); // === 'number'
console.log(type(-Infinity)); // === 'number'
console.log(type(NaN)); // === 'number'
console.log(type(Number(53))); // === 'number'
console.log(type(new Number(53))); // === 'number'
console.log(type(true)); // === 'boolean'
console.log(type(false)); // === 'boolean'
console.log(type(new Boolean(true))); // === 'boolean'
console.log(type(undefined)); // === 'undefined'
console.log(type(null)); // === 'null'
console.log(type(Symbol())); // === 'symbol'
console.log(type(Symbol.species)); // === 'symbol'
console.log(
(function() {
return type(arguments);
})(),
); // === 'arguments'
console.log(type(function() {})); // === 'function'
console.log(type(new Function())); // === 'function'
console.log(type(class {})); // === 'function'
console.log(type(/^(.+)$/)); // === 'regexp'
console.log(type(new RegExp('^(.+)$'))); // === 'regexp'
console.log(type(new Date())); // === 'date'
console.log(type(new Set())); // === 'set'
console.log(type(new Map())); // === 'map'
console.log(type(new WeakSet())); // === 'weakset'
console.log(type(new WeakMap())); // === 'weakmap'
console.log(type([])); // === 'array'
console.log(type(Array(5))); // === 'array'
console.log(type({})); // === 'object'
console.log(type(new Object())); // === 'object'
console.log(type(Object())); // === 'object'
console.log(type(new (class A {})())); // === 'object'
class A {
constructor() {
this.name = 'Hello';
}
}
console.log(type(new A())); // === 'object';
type MyType = TypeofResult | 'classAInstance';
const handlerBefore: ITypeofHandler<MyType> = {
before: true,
handler: (value, preview) => {
console.log(value); // === { name: 'Hello' } (as instance of A class);
console.log(preview); // === 'undefined'
return value instanceof A;
},
typeName: 'classAInstance',
};
console.log(type<MyType>(new A(), [handlerBefore])); // === 'classAInstance'
const handlerAfter: ITypeofHandler<MyType> = {
handler: (value, preview) => {
console.log(value); // === { name: 'Hello' } (as instance of A class);
console.log(preview); // === 'object'
return value instanceof A;
},
typeName: 'classAInstance',
};
console.log(type<MyType>(new A(), [handlerAfter])); // === 'classAInstance'
The handler with before: true
is executed before the function logic passes, so in the field preview
we see the value undefined
.
But the handler with before: false
or before: undefined
is executed after the logic of the function passes, so in the field preview
we see the value that would be received by default (as if without a handler)
FAQs
Library for information about the type of any object or primitive
The npm package @berish/typeof receives a total of 10 weekly downloads. As such, @berish/typeof popularity was classified as not popular.
We found that @berish/typeof demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.