🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

type-inspect

Package Overview
Dependencies
Maintainers
3
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

type-inspect

Type Inspector

2.3.0
latest
Source
npm
Version published
Weekly downloads
1.2K
-73.22%
Maintainers
3
Weekly downloads
 
Created
Source

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

Keywords

inspect

FAQs

Package last updated on 16 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts