# type-inspect

> Type Inspector

Latest version **2.3.0** (published 2023-11-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install type-inspect
pnpm add type-inspect
yarn add type-inspect
bun add type-inspect
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.3.0 |
| Published | 2023-11-16 |
| First published | 2019-03-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 72.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Andi Heinkelein |
| Maintainers | andifeind, kippis, firetux |
| Keywords | inspect, inspector, log, print |

## Links

- npm: https://www.npmjs.com/package/type-inspect
- Repository: https://github.com/Andifeind/type-inspect
- Homepage: https://github.com/Andifeind/type-inspect#readme
- Issues: https://github.com/Andifeind/type-inspect/issues
- npm.io page: https://npm.io/package/type-inspect

## Dependencies (1)

- [colorfy](https://npm.io/package/colorfy.md) ^2.4.0

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 2.3.0 (latest) — 2023-11-16
- 2.2.1 — 2023-10-05
- 2.2.0 — 2022-03-29
- 2.1.0 — 2022-03-05
- 2.0.5 — 2022-02-25
- 2.0.4 — 2021-10-28
- 2.0.3 — 2020-07-06
- 2.0.2 — 2020-07-06
- 2.0.1 — 2020-07-06
- 2.0.0 — 2020-06-04
- 1.4.6 — 2020-05-23
- 1.4.5 — 2020-05-23
- 1.4.4 — 2019-07-24
- 1.4.3 — 2019-07-06
- 1.4.2 — 2019-07-01
- … 6 more at https://npm.io/package/type-inspect/versions

## 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`.

```js
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**

```js
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.

```js
{
  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.

```js
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`          |

---
_Source: https://npm.io/package/type-inspect · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
