npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

type-inspect

v2.3.0

Published

Type Inspector

Downloads

87

Readme

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 |