6.0.0 • Published 4 months ago

oftypes v6.0.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
4 months ago

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
arraybuffer( 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


6.0.0

4 months ago

5.0.6

7 months ago

5.0.7

7 months ago

5.0.5

12 months ago

5.0.4

12 months ago

5.0.3

12 months ago

5.0.1

12 months ago

5.0.0

12 months ago

3.0.4

1 year ago

3.0.3

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

4.0.0

1 year ago

2.0.0

1 year ago

3.0.0

1 year ago

1.8.4

1 year ago

1.8.3

2 years ago

1.8.2

2 years ago

1.8.1

2 years ago

1.8.0

2 years ago

0.6.7

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.7.10

2 years ago

1.7.11

2 years ago

1.7.12

2 years ago

1.7.13

2 years ago

0.8.0

2 years ago

0.8.2

2 years ago

0.7.0

2 years ago

0.6.6

2 years ago

0.5.4

2 years ago

0.6.5

2 years ago

0.4.4

2 years ago

0.4.3

2 years ago

0.3.2

2 years ago

0.3.1-beta

2 years ago

0.3.0-beta

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.1.1

2 years ago