type-inspect

2.3.0 • Public • Published

TypeInspect

The TypeInspect module returns information about the data type of a Javascript object. It inspects any supported datatype and returns an object with the type, which is a typeof call, kind the real data type and the value.

import TypeInspect from 'type-inspect'

const inspected = TypeInspect.inspect('foo')

// inspected === {
//   type: 'string',
//   kind: 'string',
//   value: 'foo'
// }


const inspected = TypeInspect.inspect({ bla: 'blubb' })

// inspected === {
//   type: 'object',
//   kind: 'object',
//   value: {
//      bla: {
//        type: 'string',
//        kind: 'string'
//        value: 'blubb'
//      }
//   }
// }


const inspected = TypeInspect.inspect(['one', 2])

// inspected === {
//   type: 'object',
//   kind: 'array',
//   value: [
//     { type: 'string', kind: 'string', value: 'one' },
//     { type: 'number', kind: 'number', value: 2 }
//   ]
// }
Supported datatypes:
Input Type Kind Description
undefined undefined undefined Javascripts undefined property
null object null Javascripts null object
number number number Number object
NaN number nan NaN object
string string string String object
boolean boolean boolean Boolean object
object object object Object object
regexp object regexp Reguler expression object
array object array Array Object
date object date Date object
map object map Map object
set object set Set object
promise object promise Promise object
function function function Function Object
generator function generator Generator function object
async function async Async function object
class function class Class object

Methods

diff(any left, any right)

The diff method goes recursive through two input values and calculates the difference. It returns an instance of TypeDiff

const left = {
  bla: 'Bla',
  blub: 'Blubb',
  blob: undefined
}

const right = {
  bla: 'Bla',
  blub: 'Blobb',
  blab: null
}

const diff = TypeInspect.diff(left, right)
diff.print()

TypeDiff object

Properties

** obj diffResult**

Holds the diff result.

{
  type: 'object',
  kind: 'object',
  values: [{
    type: 'string',
    kind: 'string',
    value: 'Bla',
    key: 'bla',
    isDifferent: false
  }, {
    type: 'string',
    kind: 'string',
    valueAdded: 'Blubb',
    valueRemoved: 'Blobb',
    key: 'blub',
    isDifferent: true
  }, {
    type: 'undefined',
    typeAdded: 'undefined',
    kind: 'undefined',
    kindAdded: 'undefined',
    value: undefined,
    key: 'blob',
    keyAdded: 'blob',
    isDifferent: true
  }, {
    type: 'object',
    typeRemoved: 'object',
    kind: 'null',
    kindRemoved: 'null',
    valueAdded: undefined,
    valueRemoved: null,
    key: 'blab',
    keyRemoved: 'blab',
    isDifferent: true
  }],
  isDifferent: true
}

Methods

diff(any left, any right)

Compares two datatypes and returns a TypeDiff instance. The flag isDifferent indicates that the node or the node's child has differences. You can check the flag on the root level to make sure the whole trees are identical or not.

print([bool printColors])

Print diff result.

parse([bool printColors])

Parse a diff result and returns value as string.

Matcher

With version v2.2.0 matcher support was introduced. A matcher allows to match a value by it's data type, it can be placed in the right side of a diff call.

const {Matcher, TypeInspect} = require('type-inspect')

const left = {
  name: 'Banana',
  isFruit: true
}

const matcher = new Matcher()
const right = {
  name: matcher.isString(),
  isFruit: matcher.isBoolean()
}

const diff = TypeInspect.diff(left, right)
if (diff.isDifferent === false) {
  console.log('Left and right is identical')
}
Supported matchers:
Name Description
isArray() Value is a Array
isAsync() Value is a Async function
isBoolean() Value is a Boolean
isClass() Value is a Class
isDate() Value is a Date
isFunction() Value is a Function
isGenerator() Value is a Generator function
isMap() Value is a Map
isNaN() Value is NaN
isNull() Value is null object
isNumber() Value is a Number
isObject() Value is an Object
isPromise() Value is a Promise
isRegExp() Value is a reguler expression
isSet() Value is a Set
isString() Value is a String
isUndefined() Value is undefined

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.3.0
    5
    • latest

Version History

Package Sidebar

Install

npm i type-inspect

Weekly Downloads

7

Version

2.3.0

License

MIT

Unpacked Size

74.6 kB

Total Files

26

Last publish

Collaborators

  • andifeind
  • kippis
  • firetux