oftypes

Configurable typeof responses. All the primitives are covered [bigint, boolean, buffer, string, number, promise, undefined, symbol, null]. Function, Array, Promise ( STD built-in objects ) and Buffer ( Node.js ) are differentiated from Object. Javascript

Usage no npm install needed!

<script type="module">
  import oftypes from 'https://cdn.skypack.dev/oftypes';
</script>

README

oftypes


Configurable typeof responses. All the primitives are covered [bigint, boolean, buffer, string, number, promise, undefined, symbol, null]. Function, Array, Promise ( STD built-in objects ) and Buffer ( Node.js ) are differentiated from Object. Javascript ESM module.

Standard built-in object on MDN

Javascript ESM module.

Functions & Examples


Installation

npm i oftypes

Functions & Examples


  • The array_ checking function.

array_(variable, [resolvers], [payback]) ⇒ Promise | PromiseFulfilledResult<any> | any

Kind: global function

Param Type Default Description
variable any Variable to check for.
[resolvers] Object {true:true,false:false} Default is set to true and false, but can be set to anything.
[payback] boolean false If true it will send back the variable value.

Example

import { array_ } from 'oftypes'

const variable = [ () => {console.log( 'I\'m a FUNCTION!' )} ]
const resolvers = undefined
const payback = undefined

console.log( await array_( variable, resolvers, payback ) )

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

const variable = () => {console.log( 'I\'m a FUNCTION!' )}
const resolvers = undefined
const payback = undefined

console.log( await array_( variable, resolvers, payback ) )

// yield false

  • The bigint_ checking function.

bigint_(variable, [resolvers], [payback]) ⇒ Promise | PromiseFulfilledResult<any> | any

Kind: global function

Param Type Default Description
variable any Variable to check for.
[resolvers] Object {true:true,false:false} Default is set to true and false, but can be set to anything.
[payback] boolean false If true it will send back the variable value.

Example

import { bigint_ } from 'oftypes'

const variable = BigInt( 'oftypes' )

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

const variable = function ( ){}

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

  • The boolean_ checking function.

boolean_(variable, [resolvers], [payback]) ⇒ Promise | PromiseFulfilledResult<any> | any

Kind: global function

Param Type Default Description
variable any Variable to check for.
[resolvers] Object {true:true,false:false} Default is set to true and false, but can be set to anything.
[payback] boolean false If true it will send back the variable value.

Example

import { boolean_ } from 'oftypes'

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

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

// yield ['it is not oftype boolean!', { object: ()=>{} }]
import { boolean_ } from 'oftypes'

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

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

// yield ['it is oftype boolean!', true]

  • The buffer_ checking function.

buffer_(variable, [resolvers], [payback]) ⇒ Promise | PromiseFulfilledResult<any> | any

Kind: global function

Param Type Default Description
variable any Variable to check for.
[resolvers] Object {true:true,false:false} Default is set to true and false, but can be set to anything.
[payback] boolean false If true it will send back the variable value.

Example

import { buffer_ } from 'oftypes'

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

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

// yield ['it is oftype buffer!', <Buffer 68 65 6c 6c 6f 20 66 6f 6c 6b 73>]
import { buffer_ } from 'oftypes'

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

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

// yield ['it is NOT oftype buffer!', 'hello folks']

  • The function_ checking function.

function_(variable, [resolvers], [payback]) ⇒ Promise | PromiseFulfilledResult<any> | any

Kind: global function

Param Type Default Description
variable any Variable to check for.
[resolvers] Object {true:true,false:false} Default is set to true and false, but can be set to anything.
[payback] boolean false If true it will send back the variable value.

Example

import { function_ } from 'oftypes'

const variable = 10
const resolvers = undefined
const payback = false

console.log( await function_( variable, resolvers, payback ) )

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

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

const resolved = await function_( variable, resolvers, payback )
resolved[ 1 ]()

// yield I'm a FUNCTION!

  • The null_ checking function.

null_(variable, [resolvers], [payback]) ⇒ Promise | PromiseFulfilledResult<any> | any

Kind: global function

Param Type Default Description
variable any Variable to check for.
[resolvers] Object {true:true,false:false} Default is set to true and false, but can be set to anything.
[payback] boolean false If true it will send back the variable value.

Example

import { null_ } from 'oftypes'

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

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

// yield ['it is not null!', { object: null }]
import { null_ } from 'oftypes'

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

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

// yield ['it is null!', null]

  • The number_ checking function.

number_(variable, [resolvers], [payback]) ⇒ Promise | PromiseFulfilledResult<any> | any

Kind: global function

Param Type Default Description
variable any Variable to check for.
[resolvers] Object {true:true,false:false} Default is set to true and false, but can be set to anything.
[payback] boolean false If true it will send back the variable value.

Example

import { number_ } from 'oftypes'

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

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

// yield ['it is a number!', 10]
import { number_ } from 'oftypes'

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

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

// yield ['it is a number!', 10]
import { number_ } from 'oftypes'

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

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

// yield ['it is not a number!', 'folks']

  • The object_ checking function.

object_(variable, [resolvers], [payback]) ⇒ Promise | PromiseFulfilledResult<any> | any

Kind: global function

Param Type Default Description
variable any Variable to check for.
[resolvers] Object {true:true,false:false} Default is set to true and false, but can be set to anything.
[payback] boolean false If true it will send back the variable value.

Example

import { object_ } from 'oftypes'

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

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

// yield ['it is not an object!', ['hello folks!']]
import { object_ } from 'oftypes'

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

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

// yield ['it is an object!', { array1: [ 'hello folks!' ] }]

  • The promise_ checking function.

promise_(variable, [resolvers], [payback]) ⇒ Promise | PromiseFulfilledResult<any> | any

Kind: global function

Param Type Default Description
variable any Variable to check for.
[resolvers] Object {true:true,false:false} Default is set to true and false, but can be set to anything.
[payback] boolean false If true it will send back the variable value.

Example

import { promise_ } from 'oftypes'

const asyncFunction = async()=>{}
console.log( await promise_( asyncFunction ) )

// yield true


  • The string_ checking function.

string_(variable, [resolvers], [payback]) ⇒ Promise | PromiseFulfilledResult<any> | any

Kind: global function

Param Type Default Description
variable any Variable to check for.
[resolvers] Object {true:true,false:false} Default is set to true and false, but can be set to anything.
[payback] boolean false If true it will send back the variable value.

Example

import { string_ } from 'oftypes'

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

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

// yield ['it is a string!', 'hello folks!']

this example show how the resolvers' parameter can return anything you like

import { string_ } from 'oftypes'
import { stat } from 'fs/promises'

const variable = 'hello folks!'
const resolvers = {
    true: async () => {
        return stat( process.cwd() + '/index.js' ).then( stat => stat ).catch( error => error )
    }, false: 'it is not a string',
}
const payback = true

let fileStat = await string_( variable, resolvers, payback )
if ( typeof fileStat[ 0 ] === 'function' )
    console.log( await fileStat[ 0 ]() )
else
    console.log( fileStat )

// yield the stats object of the file
// change the variable to number = 10, it yields ['it is not a string!', 10]
// if the file passed to the stat function is not found, it yields the ENOENT exception

  • The symbol_ checking function.

symbol_(variable, [resolvers], [payback]) ⇒ Promise | PromiseFulfilledResult<any> | any

Kind: global function

Param Type Default Description
variable any Variable to check for.
[resolvers] Object {true:true,false:false} Default is set to true and false, but can be set to anything.
[payback] boolean false If true it will send back the variable value.

Example

import { symbol_ } from 'oftypes'

const variable = Symbol( 'symbol_' )

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

const variable = Array(26)

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

  • The undefined_ checking function.

undefined_(variable, [resolvers], [payback]) ⇒ Promise | PromiseFulfilledResult<any> | any

Kind: global function

Param Type Default Description
variable any Variable to check for.
[resolvers] Object {true:true,false:false} Default is set to true and false, but can be set to anything.
[payback] boolean false If true it will send back the variable value.

Example

import { undefined_ } from 'oftypes'

console.log( await undefined_( undefined ) )

// yield true