npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

oftypes

v6.0.0

Published

Configurable 'typeof' analysis and responses. Javascript ESModule.

Downloads

40

Readme

oftypes


Configurable 'typeof' analysis and responses. Javascript ESModule.

Index of contents


Installation

npm install oftypes

Description

Simple and useful module to check data types.
Every method, related to retrieve data types, accepts the same parameters variable and resolvers.
Exception done for the methods:

covered data types

  • Array
  • ArrayBuffer
  • AsyncFunction
  • BigInt
  • Boolean
  • Buffer
  • DataView
  • Empty (string, array, object, map, set, arraybuffer, dataview, buffer)
  • Error
  • Function
  • Map
  • NaN
  • Null
  • Number
  • Object
  • Promise
  • Set
  • String
  • Symbol
  • Undefined

Oftypes API


array_

signature
array_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { array_ } from 'oftypes'

console.log( await array_( [ 1 ] ) )

// prints true
import { array_ } from 'oftypes'

console.log( await array_( 10 ) )

// prints false

arraybuffer_

signature
array_buffer_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { arraybuffer_ } from 'oftypes'

console.log( await arraybuffer_( new ArrayBuffer( 1 ) ) )

// prints true
import { arraybuffer_ } from 'oftypes'

console.log( await arraybuffer_( 10 ) )

// prints false

async_

signature
async_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { async_ } from 'oftypes'

const variable = [ async() => {} ]
const resolvers = undefined

console.log( await async_( variable[ 0 ] ) )

// prints true
import { async_ } from 'oftypes'

const variable = new Promise( resolve => resolve( 'promise' ) )

console.log( await async_( variable ) )

// prints false

bigint_

signature
bigint_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { bigint_ } from 'oftypes'

const variable = BigInt( 'oftypes' )

console.log( await bigint_( variable ) )
// prints true
import { bigint_ } from 'oftypes'

const variable = function ( ){}

console.log( await bigint_( variable ) )
// prints false

boolean_

signature
boolean_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { boolean_ } from 'oftypes'

const variable = { object: () => {} }
const resolvers = { true: 'it is oftype boolean!', false: 'it is not oftype boolean' }

console.log( await boolean_( variable, resolvers ) )

// prints 'it is not oftype boolean!
import { boolean_ } from 'oftypes'

const variable = true
const resolvers = { true: 'it is oftype boolean!', false: 'it is not oftype boolean' }

console.log( await boolean_( variable, resolvers ) )

// prints 'it is oftype boolean!'

buffer_

signature
buffer_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { buffer_ } from 'oftypes'

const variable = Buffer.from('hello folks')
const resolvers = { true: 'it is oftype buffer!', false: 'it is NOT oftype buffer' }

console.log( await buffer_( variable, resolvers ) )

// prints 'it is oftype buffer!'
import { buffer_ } from 'oftypes'

const variable = 'hello folks'
const resolvers = { true: 'it is oftype buffer!', false: 'it is NOT oftype buffer' }

console.log( await buffer_( variable, resolvers ) )

// prints 'it is NOT oftype buffer!'

dataview_

signature
dataview_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { dataview_ } from 'oftypes'

const variable = new DataView(new ArrayBuffer(1))

console.log( await dataview_( variable ) )

// prints true
import { dataview_ } from 'oftypes'

const variable = 'reference not found'

console.log( await dataview_( variable ) )

// prints false

empty_

signature
empty_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { empty_ } from 'oftypes'

console.log( await empty_( [] ) )

// prints true
import { empty_ } from 'oftypes'

console.log( await empty_( false ) )

// prints false

error_

signature
error_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { error_ } from 'oftypes'

const variable = new ReferenceError('reference not found')

console.log( await error_( variable ) )

// prints true
import { error_ } from 'oftypes'

const variable = 'reference not found'

console.log( await error_( variable ) )

// prints false

function_

signature
function_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { function_ } from 'oftypes'

const variable = 10

console.log( await function_( variable ) )

// prints false
import { function_ } from 'oftypes'

const variable = () => { console.log( 'I\'m a FUNCTION!' ) }
const resolvers = { true: true, false: false }

const resolved = await function_( variable, resolvers )
resolved()

// prints I'm a FUNCTION!

map_

signature
map_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { map_ } from 'oftypes'

console.log( await map_( new Map() ) )

// prints true
import { map_ } from 'oftypes'

console.log( await map_( new Set() ) )

// prints false

nan_

signature
nan_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { nan_ } from 'oftypes'

const variable = 10
const resolvers = { true: 'it is NaN!', false: 'it is not NaN' }

console.log( await nan_( variable, resolvers ) )

// prints it is not NaN!
import { nan_ } from 'oftypes'

const variable = { object: null }
const resolvers = { true: 'it is NaN!', false: 'it is not NaN' }

console.log( await nan_( variable.object, resolvers ) )

// prints it is NaN!

null_

signature
null_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { null_ } from 'oftypes'

const variable = { object: null }
const resolvers = { true: 'it is null!', false: 'it is not null' }

console.log( await null_( variable, resolvers ) )

// prints it is not null!
import { null_ } from 'oftypes'

const variable = { object: null }
const resolvers = { true: 'it is null!', false: 'it is not null' }

console.log( await null_( variable.object, resolvers, payback ) )

// prints it is null!

number_

signature
number_( variable: any, [ resolvers={true: true, false: false}], [ strict=true ] ):
Promise<boolean|unknown>

import { number_ } from 'oftypes'

const variable = 10
const resolvers = { true: 'it is a number!', false: 'it is not a number' }

console.log( await number_( variable, resolvers ) )

// prints it is a number!
import { number_ } from 'oftypes'

const variable = '10'
const resolvers = { true: 'it is a number!', false: 'it is not a number' }

console.log( await string_( variable, resolvers ) )

// prints it is a number!

disabling strict mode.

import { number_ } from 'oftypes'

const variable = '10'
const resolvers = { true: 'it is a number!', false: 'it is not a number' }

console.log( await number_( variable, resolvers, false ) )

// prints it is a number!
import { number_ } from 'oftypes'

const variable = 'folks'
const resolvers = { true: 'it is a number!', false: 'it is not a number' }

console.log( await number_( variable, resolvers ) )

// prints it is not a number!

object_

signature
object_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { object_ } from 'oftypes'

const variable = [ 'hello folks!' ]
const resolvers = { true: 'it is an object!', false: 'it is not an object!' }

console.log( await object_( variable, resolvers ) )

// prints it is not an object!
import { object_ } from 'oftypes'

const variable = { array1: [ 'hello folks!' ] }
const resolvers = { true: 'it is an object!', false: 'it is not an object!' }

console.log( await object_( variable, resolvers ) )

// prints it is an object!

promise_

signature
promise_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { promise_ } from 'oftypes'

const variable = new Promise( resolve => resolve( 'promise' ) )
console.log( await promise_( variable ) )

// prints true

set_

signature
set_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { set_ } from 'oftypes'

console.log( await set_( new Map() ) )

// prints false
import { set_ } from 'oftypes'

console.log( await set_( new Set() ) )

// prints true

string_

signature
string_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { string_ } from 'oftypes'

const variable = 'hello folks!'
const resolvers = { true: 'it is a string!', false: 'it is not a string' }

console.log( await string_( variable, resolvers ) )

// prints it is a string!
import { string_ } from 'oftypes'

const variable = ['hello folks!']

console.log( await string_( variable ) )


// prints false

symbol_

signature
symbol_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { symbol_ } from 'oftypes'

const variable = Symbol( 'symbol_' )

console.log( await symbol_( variable ) )
// prints true
import { symbol_ } from 'oftypes'

const variable = Array(26)

console.log( await symbol_( variable ) )
// prints false

undefined_

signature
undefined_( variable: any, [ resolvers={true: true, false: false}] ):
Promise<boolean|unknown>

import { undefined_ } from 'oftypes'

console.log( await undefined_( undefined ) )

// prints true
import { undefined_ } from 'oftypes'

console.log( await undefined_( null ) )

// prints false

Not Oftypes Functions


oftype_ enhanced typeof

signature
oftype_( variable: any, [ resolver<T>=Map<T, any>], [execute_callback={type:'async|sync|promise'}] ):
Promise<string|unknown>

it rejects if the resolver argument is undefined and the argument execute_callback is set.

  • simple usage.
import { oftype_ } from 'oftypes'

console.log( await oftype_( 10 ) )

// prints -> Number
  • using resolver argument.
import { oftype_ } from 'oftypes'

const resolver = new Map();
resolver.set( 'Array', 'it is an Array' );

console.log( await oftype_( [ 1, 3, 5 ], resolver ) )

// prints -> it is an Array
  • it returns false if the resolver mismatches with the actual type of the variable.
import { oftype_ } from 'oftypes'

const variable = async ()=>{} /*AsyncFunction*/
const resolver = new Map();
resolver.set( 'Symbol', 'it is Symbol' );

console.log( await oftype_( variable, resolver ) )

// prints -> false
  • it executes the callback function depending on the type given to the execute_callback property type.
import { oftype_ } from 'oftypes'

const variable = Symbol( 'symbol' )
const fn_callback = () => { console.log( type ) }
const resolver = new Map();
resolver.set( 'Symbol', fn_callback );

await oftype_( variable, resolver, {type:'sync'} )

// prints -> Symbol given

resolvers

signature

resolvers(truthy: any, falsy: any, [ execute_callback={truthy: 'async|sync|promise', falsy: 'async|sync|promise'}] ):
Promise<uknown>

execute_callback argument will execute the given function (given to truthy&falsy arguments) depending on the type of the truthy and falsy properties. leave it undefined to return the resolver data.

// import the resolvers function.
import { boolean_, resolvers } from 'oftypes'

// usually the resolvers are set like this, and in case we are resolving many oftypes.[functions]
// we will need to declare many unique const/let variables with unique name.

const resolvers_as_object = { true: 'true', false: 'false' }
const boolean = 'string'

// instead of passing the argument as an object -> resolvers_as_object
// we directly pass the resolvers function with the two arguments set to 'true' and 'false' respectively
// the resolvers function will return the same things as the resolvers_as_object would do.

console.log( await boolean_( boolean, await resolvers( 'true', 'false' ) ) )

// prints 'false'

JetBrains OSS License


I want to thank JetBrains to grant me the Open Source Software licence for all their products. This opportunity gives me strength to keep on going with my studies and personal project.
To learn more about this opportunity, have a look at Licences for Open Source Development - Community Support.

Thank you


Contacts