oftypes v6.0.0
oftypes
Configurable 'typeof' analysis and responses. Javascript ESModule.
Index of contents
Installation
npm install oftypesDescription
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 trueimport { array_ } from 'oftypes'
console.log( await array_( 10 ) )
// prints falsearraybuffer_
signature
arraybuffer( variable: any, resolvers={true: true, false: false} ):Promise<boolean|unknown>
import { arraybuffer_ } from 'oftypes'
console.log( await arraybuffer_( new ArrayBuffer( 1 ) ) )
// prints trueimport { arraybuffer_ } from 'oftypes'
console.log( await arraybuffer_( 10 ) )
// prints falseasync_
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 trueimport { async_ } from 'oftypes'
const variable = new Promise( resolve => resolve( 'promise' ) )
console.log( await async_( variable ) )
// prints falsebigint_
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 trueimport { bigint_ } from 'oftypes'
const variable = function ( ){}
console.log( await bigint_( variable ) )
// prints falseboolean_
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 trueimport { dataview_ } from 'oftypes'
const variable = 'reference not found'
console.log( await dataview_( variable ) )
// prints falseempty_
signature
empty( variable: any, resolvers={true: true, false: false} ):_Promise<boolean|unknown>
import { empty_ } from 'oftypes'
console.log( await empty_( [] ) )
// prints trueimport { empty_ } from 'oftypes'
console.log( await empty_( false ) )
// prints falseerror_
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 trueimport { error_ } from 'oftypes'
const variable = 'reference not found'
console.log( await error_( variable ) )
// prints falsefunction_
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 falseimport { 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 trueimport { map_ } from 'oftypes'
console.log( await map_( new Set() ) )
// prints falsenan_
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 trueset_
signature
set( variable: any, resolvers={true: true, false: false} ):_Promise<boolean|unknown>
import { set_ } from 'oftypes'
console.log( await set_( new Map() ) )
// prints falseimport { set_ } from 'oftypes'
console.log( await set_( new Set() ) )
// prints truestring_
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 falsesymbol_
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 trueimport { symbol_ } from 'oftypes'
const variable = Array(26)
console.log( await symbol_( variable ) )
// prints falseundefined_
signature
undefined( variable: any, resolvers={true: true, false: false} ):_Promise<boolean|unknown>
import { undefined_ } from 'oftypes'
console.log( await undefined_( undefined ) )
// prints trueimport { undefined_ } from 'oftypes'
console.log( await undefined_( null ) )
// prints falseNot 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
resolverargument 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_callbackpropertytype.
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 givenresolvers
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
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago