1.2.0 • Published 6 years ago

@quenk/kindof v1.2.0

Weekly downloads
12
License
Apache-2.0
Repository
github
Last release
6 years ago

KindOf

Provides a function that tests whether a value matches a given pattern.

Installation

  npm install --save @quenk/kindof

Usage

kindOf takes two parameters, the first is the value to test and the second the pattern.

  import {kindOf} from '@quenk/kindof';

  class T {
  
    name: 'King T'

  }

  const t = new T();

  if(kindOf(1, 1))
    console.log('yes');

  if(kindOf(1, Number))
    console.log('yes');

  if(kindOf('one', 'one'))
    console.log('yes')

  if(kindOf('one', String))
    console.log('yes')

  if(kindOf(t, T))
    console.log('yes')

  if(kindOf(t, {name:String}))
    console.log('yes')

Description

Currently pattern matches are carried out like this:

PatternDescriptionExample
stringMatches the string exactly.kindOf('somestring', 'somestring')
numberMatches the number exactly.kindOf(1, 1)
booleanMatches the boolean literal exactly.kindOf(true, true); kindOf(false, false)
objectTreated as a shape, each key is treated as a pattern.kindOf({one:1, two:'two'}, {one:1, two:String})
functionTreated as a constructor and results in an instanceof check.kindOf(new Point(), Point)
StringMatches any string value.kindOf('foo', String)
BooleanMatches any boolean value.kindOf(false, Boolean)
NumberMatches any number value.kindOf(12, Number)
ArrayMatches an array regardless of content.kindOf(12, Array)

License

Apache-2.0 (c) Quenk Technologies Limited.

1.2.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago