1 | zan
|
2 | ===
|
3 |
|
4 | [![NPM version][npm-image]][npm-url]
|
5 | [![Build status][travis-image]][travis-url]
|
6 | [![Test coverage][coveralls-image]][coveralls-url]
|
7 | [![Downloads][downloads-image]][downloads-url]
|
8 |
|
9 | Drop in replacement for `React.PropTypes`:
|
10 |
|
11 | ```js
|
12 | import { types } from 'zan';
|
13 | const { string, number } = types;
|
14 | React.createClass({
|
15 | propTypes: {
|
16 | name: string,
|
17 | age: number.isOptional
|
18 | },
|
19 | render() {
|
20 | // ...
|
21 | },
|
22 | });
|
23 | ```
|
24 |
|
25 | The primary differences this has with `React.PropTypes` is that
|
26 |
|
27 | 1. `zan` exposes an `exactShape` type.
|
28 | 2. checks by default are `isRequired` already and they each expose an `isOptional` method (except for `exactShape`)
|
29 | 3. `zan` exposes a `createCustomChecker` method which can be used as follows:
|
30 |
|
31 | ```js
|
32 | const urlString = createCustomChecker(value => /^https?:/.test(value) );
|
33 | ```
|
34 |
|
35 |
|
36 | This module also exposes an a checker so you can check types manually without React
|
37 |
|
38 | ### Usage
|
39 |
|
40 | ```js
|
41 | import { check, types, createCustomChecker } from 'zan';
|
42 |
|
43 | check(types.bool, true); // null
|
44 | check(types.bool, 123); // returns Error object
|
45 |
|
46 | check(types.shape({name: types.string}), {name: 'Me'}); // null
|
47 | check(types.shape({name: types.string}), {}); // returns Error object
|
48 | check(types.shape({name: types.string}), {name: 'Me', age: 22}); // null
|
49 | check(types.exactShape({name: types.string}), {name: 'Me', age: 22}); // returns Error object
|
50 | ```
|
51 |
|
52 | `check` is curry-able so `const numberChecker = check(types.number); numberChecker(22);` works
|
53 |
|
54 |
|
55 | see [test.js](test.js) from more usage
|
56 |
|
57 |
|
58 | [npm-image]: https://img.shields.io/npm/v/zan.svg?style=flat-square
|
59 | [npm-url]: https://npmjs.org/package/zan
|
60 | [travis-image]: https://img.shields.io/travis/kolodny/zan.svg?style=flat-square
|
61 | [travis-url]: https://travis-ci.org/kolodny/zan
|
62 | [coveralls-image]: https://img.shields.io/coveralls/kolodny/zan.svg?style=flat-square
|
63 | [coveralls-url]: https://coveralls.io/r/kolodny/zan
|
64 | [downloads-image]: http://img.shields.io/npm/dm/zan.svg?style=flat-square
|
65 | [downloads-url]: https://npmjs.org/package/zan
|