duckie
A Javascript type annotation and assertion library with no dependecies. You can make it work anywhere as long as you have es5 shimed your environment.
Install
For node/browserify/webpack user, use npm install.
npm install duckie
For client user, please download the dist file in the build
directory.
Example
var duckie = ;duckiearray // trueduckiearray // false duckie // true duckie //true
Example
With duckie, it will ease all of your pain. Defining types is just like defining an object.
{ duckie; // if data doesn't fit the type, it will throw error}
Quick Start
Intro
All the type-checker below will contain 2 functions:
test
will return a boolean to indicate if the val belongs to the typeassert
will throw an error if the val doesn't belong to the type
For example:
duckie.array.test('abcd')
will return false, andduckie.array.assert('abcd')
will throw an error.
Basic Type
There are 8 basic types for you to directly use, they are:
bool
string
number
undefined
null
array
object
anything
, means any value.
example:
duckiebool trueduckienumber trueduckiearray trueduckiearray false
Conditional Type
oneOf(Array)
, any value that is one of the enums.
duckie // trueduckie // false
oneOfType([Type1, Type2..TypeN])
, any value that belongs to one of the listed types.
duckie // trueduckie // trueduckie //false
maybe(Type)
, any value that belongs to the type or exactly equals toundefined
orNaN
.
duckie //trueduckie // trueduckie //trueduckie // false
Composite Type
arrayOf(Type)
, any value that is an array and all the items in the array is typeType
duckie //trueduckie // false, because there is a number 1 which isn't of type Stringduckie //true, it means array can contain string(s) or null/undefined(s)
objectOf(/*definition*/)
, an object that contains the structure described withdefinition
.
duckie // trueduckie // true, because it only check duck typeduckie
Complex Type
How to desribe a data structure that:
- is an array.
- each item is a
Person
- the definition of
Person
is: it has a name as String, age as Number and hobbies as Array of String.
Obviously, we can combine arrayOf
and ObjectOf
together to achieve the goal.
// first, build an array with person typeduckie // second, investigate Person typeduckie // finally, combine them and we getduckie
Do you find it a little verbose? It looks like we are using something DSL rather than use JS. Why can't we define data structure just as defining data itself? The answer is: Yes, we can.
For arrayOf
and objectOf
, you don't need to wrap any type checker
for inner definitions. It means:
duckie is just the same as: duckie
so the complex definition of T.arrayOf(Person)
can be written as:
duckie
Shortcut
For the reason we may want to defined the data structure just like defining the data itself. We can use duckie(Type)
to create a type checker. For Example:
// the same as duckie.array.test([1,2,3]) // the same as duckie.arrayOf(Number).test([1,2,3]) // the same as duckie.objectOf({name: String}).test({name: 'jack'})
CAUTION:
duckie([Number, String])
still meansduckie.arrayOf(Number)
because we tend to think all the items in the array should be the same type and expressionduckie([Number, String])
doesn't make any sense.
License
MIT