4.0.1 • Published 4 years ago

type-diff v4.0.1

Weekly downloads
16
License
ISC
Repository
github
Last release
4 years ago

type-diff

Simple, readable, structural type comparisons.

Build Status Coverage Status Greenkeeper badge dependencies Status devDependencies Status

Quick Example

import assert from 'assert'
import { diff, Nullable, Optional, Any } from 'type-diff'

const Type = {
  id: Number,
  tags: [String]
  contacts: [{
    id: Number,
    name: String,
    contacted: Nullable(Date),
    tags: Optional([String]),
    wharrgarbl: Any()
  }]
}

const value = {
  id: 1,
  tags: ['good', 'cool'],
  contacts: [
    { id: 2, name: 'John Doe', contacted: null, wharrgarbl: _ => 'wheee' },
    { id: '3', name: 'Jane Doe', contacted: new Date(), tags: ['cool'] }
  ]
}

assert.deepEqual(diff(Type, value), {
  contacts: {
    '1': {
      id: {
        actual: 'String',
        expected: 'Number',
        value: '3'
      },
      wharrgarbl: {
        actual: 'Undefined',
        expected: 'Any'
      }
    }
  }
})

Usage

diff(Type, value)

Generate an object literal representation of the difference between your Type and a given value as shown above in the Quick Example.

Supported Built-ins

  • Number
  • String
  • Boolean
  • Object
  • Array
  • Date
  • Function

Helpers

Nullable(Type): Allows the corresponding value to be either the correct Type or null.

Optional(Type): Allows the corresponding value to be either the correct Type or undefined.

Any | Any(): Requires that the corresponding value be anything but undefined. Used in conjuction with Optional if undefined is allowed; as in Optional(Any)

4.0.1

4 years ago

4.0.0

4 years ago

3.1.1

8 years ago

3.1.0

8 years ago

3.0.2

8 years ago

3.0.1

8 years ago

3.0.0

8 years ago

2.2.0-beta.0

8 years ago

2.1.0-1

8 years ago

2.0.0-beta.1

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago