Skip to content

Simple functionality alternative to check data type references such as typeof and instanceof.

License

Notifications You must be signed in to change notification settings

restuwahyu13/is-any-type

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Is Any Type

Build Status Coverage Status CodeFactor codebeat badge Codacy Badge node-current npm PRs Welcome

is-any-type A simple JavaScript function for checking data types, alternative to typeof and instanceof, this can be used anywhere such as typeof and instanceof, for documentation under v4 check here.

Installation

npm install is-any-type -S or yarn add is-any-type -S

Example Usage

  • Example Usage Using CommonJS Module

    const { assert } = require('is-any-type')
    
    let isString = assert.isString('hello wordl') // => true
    let isNumber = assert.isNumber(new Date().getFullYear) // => true
    let isNull = assert.isNull(null) // => true
    let isUndefined = assert.isUndefined(undefined) // => true
    let isObject = assert.isObject({}) // => true
    let isArray = assert.isArray([]) // => true
    let isFunction = assert.isFunction(() => {}) // => true
    let isPromise = assert.isPromise(new Promise((resolve) => resolve('hello wordl'))) // => true
    let isBuffer = assert.isBuffer(Buffer.from('hello world')) // => true
    let isBoolean = assert.isBoolean(true) // => true
    
    let isString = assert.isString(2021) // => false
    let isNumber = assert.isNumber('hello world') // => false
    let isNull = assert.isNull(undefined) // => false
    let isUndefined = isType(null) // => false
    let isObject = assert.isObject([]) // => false
    let isArray = assert.isArray({}) // => false
    let isFunction = assert.isFunction(Promise.resolve('hello world')) // => false
    let isPromise = assert.isPromise(() => 'hello world') // => false
    let isBuffer = assert.isBuffer('hello world') // => false
    let isBoolean = assert.isBoolean(null) // => false
    
    let isStringCompare = assert.isStringCompare('hello wordl', 'hello wordl') // => true
    let isNumberCompare = assert.isNumberCompare(new Date().getFullYear, new Date().getFullYear) // => true
    let isNullCompare = assert.isNullCompare(null, null) // => true
    let isUndefinedCompare = assert.isUndefinedCompare(undefined, undefined) // => true
    let isObjectCompare = assert.isObjectCompare({}, {}) // => true
    let isArrayCompare = assert.isArrayCompare([], []) // => true
    let isFunctionCompare = assert.isFunctionCompare(() => {}, Function) // => true
    let isPromiseCompare = assert.isPromiseCompare(Promise.resolve('hello world'), Promise.resolve('hello world')) // => true
    let isBufferCompare = assert.isBufferCompare(Buffer.from('hello world'), Buffer.from('hello world')) // => true
    let isBooleanCompare = assert.isBooleanCompare(true, false) // => true
    
    let isStringCompare = assert.isStringCompare('hello wordl', 2021) // => false
    let isNumberCompare = assert.isNumberCompare(new Date().getFullYear, 'hello wordl') // => false
    let isNullCompare = assert.isNullCompare(null, true) // => false
    let isUndefinedCompare = assert.isUndefinedCompare(undefined, null) // => false
    let isObjectCompare = assert.isObjectCompare({}, []) // => false
    let isArrayCompare = assert.isArrayCompare([], {}) // => false
    let isFunctionCompare = assert.isFunctionCompare(() => {}, Promise) // => false
    let isPromiseCompare = assert.isPromiseCompare(Promise.resolve('hello world'), Promise.resolve('hello world')) // => false
    let isBufferCompare = assert.isBufferCompare(Buffer.from('hello world'), Promise.resolve('hello wordl')) // => false
    let isBooleanCompare = assert.isBooleanCompare(true, 2021) // => false
  • Example Usage Using ESM Module

    import { assert } from 'is-any-type'
    
    let isString = assert.isString('hello wordl') // => true
    let isNumber = assert.isNumber(new Date().getFullYear) // => true
    let isNull = assert.isNull(null) // => true
    let isUndefined = assert.isUndefined(undefined) // => true
    let isObject = assert.isObject({}) // => true
    let isArray = assert.isArray([]) // => true
    let isFunction = assert.isFunction(() => {}) // => true
    let isPromise = assert.isPromise(new Promise((resolve) => resolve('hello wordl'))) // => true
    let isBuffer = assert.isBuffer(Buffer.from('hello world')) // => true
    let isBoolean = assert.isBoolean(true) // => true
    
    let isString = assert.isString(2021) // => false
    let isNumber = assert.isNumber('hello world') // => false
    let isNull = assert.isNull(undefined) // => false
    let isUndefined = isType(null) // => false
    let isObject = assert.isObject([]) // => false
    let isArray = assert.isArray({}) // => false
    let isFunction = assert.isFunction(Promise.resolve('hello world')) // => false
    let isPromise = assert.isPromise(() => 'hello world') // => false
    let isBuffer = assert.isBuffer('hello world') // => false
    let isBoolean = assert.isBoolean(null) // => false
    
    let isStringCompare = assert.isStringCompare('hello wordl', 'hello wordl') // => true
    let isNumberCompare = assert.isNumberCompare(new Date().getFullYear, new Date().getFullYear) // => true
    let isNullCompare = assert.isNullCompare(null, null) // => true
    let isUndefinedCompare = assert.isUndefinedCompare(undefined, undefined) // => true
    let isObjectCompare = assert.isObjectCompare({}, {}) // => true
    let isArrayCompare = assert.isArrayCompare([], []) // => true
    let isFunctionCompare = assert.isFunctionCompare(() => {}, Function) // => true
    let isPromiseCompare = assert.isPromiseCompare(Promise.resolve('hello world'), Promise.resolve('hello world')) // => true
    let isBufferCompare = assert.isBufferCompare(Buffer.from('hello world'), Buffer.from('hello world')) // => true
    let isBooleanCompare = assert.isBooleanCompare(true, false) // => true
    
    let isStringCompare = assert.isStringCompare('hello wordl', 2021) // => false
    let isNumberCompare = assert.isNumberCompare(new Date().getFullYear, 'hello wordl') // => false
    let isNullCompare = assert.isNullCompare(null, true) // => false
    let isUndefinedCompare = assert.isUndefinedCompare(undefined, null) // => false
    let isObjectCompare = assert.isObjectCompare({}, []) // => false
    let isArrayCompare = assert.isArrayCompare([], {}) // => false
    let isFunctionCompare = assert.isFunctionCompare(() => {}, Promise) // => false
    let isPromiseCompare = assert.isPromiseCompare(Promise.resolve('hello world'), Promise.resolve('hello world')) // => false
    let isBufferCompare = assert.isBufferCompare(Buffer.from('hello world'), Promise.resolve('hello wordl')) // => false
    let isBooleanCompare = assert.isBooleanCompare(true, 2021) // => false
  • Example Usage Using CommonJS Module With Jest

const { assert } = require('is-any-type')

describe('Is Any Type Group Testing', () => {

  test('Should be value is string', () => {
    const type = assert.isString('hello wordl')
    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be string match value from string', () => {
    const type = assert.isStringCompare('hello wordl', 'hello wordl')
    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be value is number', () => {
    const type = assert.isNumber(2021)
    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be number match value from number', () => {
    const type = assert.isNumberCompare(2021, 2021)

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be value is object', () => {
    const type1 = assert.isObject({ name: 'john doe' })
    const type2 = assert.isObject({})
    
    expect(type1).toBeDefined()
    expect(type2).toBeDefined()

    expect(type1).toBeTruthy()
    expect(type2).toBeTruthy()
  })

  test('Should be object match value from object', () => {
    const type1 = assert.isObjectCompare({ name: 'john doe' }, { name: 'john doe' })
    const type2 = assert.isObjectCompare({}, {})

    expect(type1).toBeDefined()
    expect(type2).toBeDefined()

    expect(type1).toBeTruthy()
    expect(type2).toBeTruthy()
  })

  test('Should be value is array', () => {
    const type1 = assert.isArray([1, 2, 3, 4, 5])
    const type2 = assert.isArray([])
    
    expect(type1).toBeDefined()
    expect(type2).toBeDefined()

    expect(type1).toBeTruthy()
    expect(type2).toBeTruthy()
  })

  test('Should be array match value from array', () => {
    const type1 = assert.isArrayCompare([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])
    const type2 = assert.isArrayCompare([], [])

    expect(type1).toBeDefined()
    expect(type2).toBeDefined()

    expect(type1).toBeTruthy()
    expect(type2).toBeTruthy()
  })

  test('Should be value is function', () => {
    const type = assert.isFunction(() => 'hello wordl')

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be function match value from function', () => {
    const type = assert.isFunctionCompare(() => 'hello wordl', () => 'hello wordl 2')

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })


  test('Should be value is promise', () => {
    const promise = async () => 'hello wordl'

    const type1 = assert.isPromise(Promise.resolve('hello wordl'))
    const type2 = assert.isPromise(new Promise((resolve, reject) => resolve('hello wordl')))
    const type3 = assert.isPromise(promise())

    expect(type1).toBeDefined()
    expect(type2).toBeDefined()
    expect(type3).toBeDefined()

    expect(type1).toBeTruthy()
    expect(type2).toBeTruthy()
    expect(type3).toBeTruthy()
  })

  test('Should be promise match value from promise', () => {
    const promise = async () => 'hello wordl'

    const type1 = assert.isPromiseCompare(Promise.resolve('hello wordl'), Promise.resolve('hello wordl'))
    const type2 = assert.isPromiseCompare(new Promise((resolve, reject) => resolve('hello wordl')), promise())
    const type3 = assert.isPromiseCompare(promise(), Promise.resolve('hello wordl'))

    expect(type1).toBeDefined()
    expect(type2).toBeDefined()
    expect(type3).toBeDefined()

    expect(type1).toBeTruthy()
    expect(type2).toBeTruthy()
    expect(type3).toBeTruthy()
  })


  test('Should be value is buffer', () => {
    const type = assert.isBuffer(Buffer.from('hello wordl'))

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be buffer match value from buffer', () => {
    const type = assert.isBufferCompare(Buffer.from('hello wordl'), Buffer.from('hello john'))

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be value is null', () => {
    const type = assert.isNull(null)

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be null match value from null', () => {
    const type = assert.isNullCompare(null, null)

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be value is from undefined', () => {
    let result
    const type = assert.isUndefined(result)
    
    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('undefined match value from undefined', () => {
    const type = assert.isUndefinedCompare(undefined, undefined)

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be value is from boolean', () => {
    const type = assert.isBoolean(true)

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be boolean match value from boolean', () => {
    const type = assert.isBooleanCompare(true, false)

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })
})
  • Example Usage Using ESM Module With Jest

import { assert } from 'is-any-type'

describe('Is Any Type Group Testing', () => {

  test('Should be value is string', () => {
    const type = assert.isString('hello wordl')
    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be string match value from string', () => {
    const type = assert.isStringCompare('hello wordl', 'hello wordl')
    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be value is number', () => {
    const type = assert.isNumber(2021)
    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be number match value from number', () => {
    const type = assert.isNumberCompare(2021, 2021)

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be value is object', () => {
    const type1 = assert.isObject({ name: 'john doe' })
    const type2 = assert.isObject({})
    
    expect(type1).toBeDefined()
    expect(type2).toBeDefined()

    expect(type1).toBeTruthy()
    expect(type2).toBeTruthy()
  })

  test('Should be object match value from object', () => {
    const type1 = assert.isObjectCompare({ name: 'john doe' }, { name: 'john doe' })
    const type2 = assert.isObjectCompare({}, {})

    expect(type1).toBeDefined()
    expect(type2).toBeDefined()

    expect(type1).toBeTruthy()
    expect(type2).toBeTruthy()
  })

  test('Should be value is array', () => {
    const type1 = assert.isArray([1, 2, 3, 4, 5])
    const type2 = assert.isArray([])
    
    expect(type1).toBeDefined()
    expect(type2).toBeDefined()

    expect(type1).toBeTruthy()
    expect(type2).toBeTruthy()
  })

  test('Should be array match value from array', () => {
    const type1 = assert.isArrayCompare([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])
    const type2 = assert.isArrayCompare([], [])

    expect(type1).toBeDefined()
    expect(type2).toBeDefined()

    expect(type1).toBeTruthy()
    expect(type2).toBeTruthy()
  })

  test('Should be value is function', () => {
    const type = assert.isFunction(() => 'hello wordl')

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be function match value from function', () => {
    const type = assert.isFunctionCompare(() => 'hello wordl', () => 'hello wordl 2')

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })


  test('Should be value is promise', () => {
    const promise = async () => 'hello wordl'

    const type1 = assert.isPromise(Promise.resolve('hello wordl'))
    const type2 = assert.isPromise(new Promise((resolve, reject) => resolve('hello wordl')))
    const type3 = assert.isPromise(promise())

    expect(type1).toBeDefined()
    expect(type2).toBeDefined()
    expect(type3).toBeDefined()

    expect(type1).toBeTruthy()
    expect(type2).toBeTruthy()
    expect(type3).toBeTruthy()
  })

  test('Should be promise match value from promise', () => {
    const promise = async () => 'hello wordl'

    const type1 = assert.isPromiseCompare(Promise.resolve('hello wordl'), Promise.resolve('hello wordl'))
    const type2 = assert.isPromiseCompare(new Promise((resolve, reject) => resolve('hello wordl')), promise())
    const type3 = assert.isPromiseCompare(promise(), Promise.resolve('hello wordl'))

    expect(type1).toBeDefined()
    expect(type2).toBeDefined()
    expect(type3).toBeDefined()

    expect(type1).toBeTruthy()
    expect(type2).toBeTruthy()
    expect(type3).toBeTruthy()
  })


  test('Should be value is buffer', () => {
    const type = assert.isBuffer(Buffer.from('hello wordl'))

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be buffer match value from buffer', () => {
    const type = assert.isBufferCompare(Buffer.from('hello wordl'), Buffer.from('hello john'))

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be value is null', () => {
    const type = assert.isNull(null)

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be null match value from null', () => {
    const type = assert.isNullCompare(null, null)

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be value is from undefined', () => {
    let result
    const type = assert.isUndefined(result)
    
    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('undefined match value from undefined', () => {
    const type = assert.isUndefinedCompare(undefined, undefined)

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be value is from boolean', () => {
    const type = assert.isBoolean(true)

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })

  test('Should be boolean match value from boolean', () => {
    const type = assert.isBooleanCompare(true, false)

    expect(type).toBeDefined()
    expect(type).toBeTruthy()
  })
})

Testing

  • Testing Via Local

    npm run test or make test
  • Testing Via Local And Build

    make build
  • Testing Via Docker

    docker build -t is-any-type or make dkb tag=is-any-type

Bugs

For information on bugs related to package libraries, please visit here

Contributing

Want to make Is-Any-Type more perfect ? Let's contribute and follow the contribution guide.

License

BACK TO TOP