2.3.0 • Published 6 months ago

type-inspect v2.3.0

Weekly downloads
260
License
MIT
Repository
github
Last release
6 months ago

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:
InputTypeKindDescription
undefinedundefinedundefinedJavascripts undefined property
nullobjectnullJavascripts null object
numbernumbernumberNumber object
NaNnumbernanNaN object
stringstringstringString object
booleanbooleanbooleanBoolean object
objectobjectobjectObject object
regexpobjectregexpReguler expression object
arrayobjectarrayArray Object
dateobjectdateDate object
mapobjectmapMap object
setobjectsetSet object
promiseobjectpromisePromise object
functionfunctionfunctionFunction Object
generatorfunctiongeneratorGenerator function object
asyncfunctionasyncAsync function object
classfunctionclassClass 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:
NameDescription
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
2.3.0

6 months ago

2.2.1

7 months ago

2.2.0

2 years ago

2.1.0

2 years ago

2.0.5

2 years ago

2.0.4

3 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.4.6

4 years ago

1.4.5

4 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.0

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago