Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dustinspecker/checker-factory
https://github.com/dustinspecker/checker-factory
Last synced: 2 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/dustinspecker/checker-factory
- Owner: dustinspecker
- License: mit
- Created: 2016-01-24T23:35:39.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-28T20:11:38.000Z (over 7 years ago)
- Last Synced: 2024-08-09T08:33:24.368Z (3 months ago)
- Language: JavaScript
- Size: 25.4 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 76
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# Checker Factory
[![NPM version](https://badge.fury.io/js/checker-factory.svg)](http://badge.fury.io/js/checker-factory) [![Build Status](https://travis-ci.org/dustinspecker/checker-factory.svg?branch=master)](https://travis-ci.org/dustinspecker/checker-factory) [![Coverage Status](https://img.shields.io/coveralls/dustinspecker/checker-factory.svg)](https://coveralls.io/r/dustinspecker/checker-factory?branch=master)
[![Code Climate](https://codeclimate.com/github/dustinspecker/checker-factory/badges/gpa.svg)](https://codeclimate.com/github/dustinspecker/checker-factory) [![Dependencies](https://david-dm.org/dustinspecker/checker-factory.svg)](https://david-dm.org/dustinspecker/checker-factory/#info=dependencies&view=table) [![DevDependencies](https://david-dm.org/dustinspecker/checker-factory/dev-status.svg)](https://david-dm.org/dustinspecker/checker-factory/#info=devDependencies&view=table)
> Checker Factory used by Deku Prop Type Validators
## Install
```bash
npm install --save checker-factory
```## Usage
```javascript
import checkerFactory from 'checker-factory'const numberChecker = checkerFactory('number')
numberChecker.validate('hello', 'age')
// => TypeError: Expected age to be of type `number`, but got `string`numberChecker.validate(3, 'age')
// => undefinedconst requiredString = checkerFactory('string').isRequired
requiredString.validate(undefined, 'name')
// => Error: name is requiredconst evenNumberChecker = checkerFactory((prop, key) => {
if (prop % 2 === 1) {
return new Error(`Expected ${key} to be an even number`)
}
})evenNumberChecker.validate(3, 'id')
// => Error: Expected id to be an even numberconst checkerWithName = checkerFactory('number', 'dog')
checkerWithName.name
// => 'dog'
```## API
### checkerFactory(validator[, name])Returns a CheckerFactory with an `isRequired` getter and `validate` function.
#### validator
type: `function` | `string`If `validator` is a `string`, then a `typeof` check will be performed. If `validator` is a `function`, then the function will be execution. The function is passed the `prop` value and the `key` name.
#### name
type: `string`This is an optional value to retrieve a name for the checker factory.
## License
MIT © [Dustin Specker](https://github.com/dustinspecker)