npm.io
1.2.0 • Published 8 years ago

@quenk/kindof

Licence
Apache-2.0
Version
1.2.0
Deps
0
Size
9 kB
Vulns
0
Weekly
0
DeprecatedThis package is deprecated

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:

Pattern Description Example
string Matches the string exactly. kindOf('somestring', 'somestring')
number Matches the number exactly. kindOf(1, 1)
boolean Matches the boolean literal exactly. kindOf(true, true); kindOf(false, false)
object Treated as a shape, each key is treated as a pattern. kindOf({one:1, two:'two'}, {one:1, two:String})
function Treated as a constructor and results in an instanceof check. kindOf(new Point(), Point)
String Matches any string value. kindOf('foo', String)
Boolean Matches any boolean value. kindOf(false, Boolean)
Number Matches any number value. kindOf(12, Number)
Array Matches an array regardless of content. kindOf([12], Array)

License

Apache-2.0 (c) Quenk Technologies Limited.

Keywords