# leach

> Leach value by validate schema

Latest version **0.0.2** (published 2013-08-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install leach
pnpm add leach
yarn add leach
bun add leach
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2013-08-02 |
| First published | 2013-08-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jackson Tian |
| Maintainers | jacksontian |
| Keywords | Leach, values |

## Links

- npm: https://www.npmjs.com/package/leach
- Repository: https://github.com/JacksonTian/leach
- Issues: https://github.com/JacksonTian/leach/issues
- npm.io page: https://npm.io/package/leach

## Recent versions

- 0.0.2 (latest) — 2013-08-02
- 0.0.1 — 2013-08-02

## README

# leach [![Build Status](https://secure.travis-ci.org/JacksonTian/leach.png?branch=master)](http://travis-ci.org/JacksonTian/leach) [![Coverage Status](https://coveralls.io/repos/JacksonTian/leach/badge.png)](https://coveralls.io/r/JacksonTian/leach)

## 步骤：

1. 选出需要的字段
2. 检查字段的类型是否正确，如果不对，尝试转换为目标类型
3. 检测值是否符合目标类型
4. 检测值是否在范围中
5. 上述条件任意一条不符合，就舍弃该字段
6. 得到最终值

## 设计思路：

1. Schema
2. 验证方法

## 使用
### Installation

```bash
$ npm install leach
```

### Demo

```js
var schema = {
  'hehe': {
    type: 'number',
    check: function (input) {
      return [1, 2, 3, 4].indexOf(input) !== -1;
    }
  }
};

leach(schema, {'hehe': '1000'});
// => {} 数据不在范围中
leach(schema, {'hehe': '3'});
// => {hehe: 3} 转换为正确的类型且数据在区间中
```

#### 自定义类型

```js
var schema = {
  'hehe': {
    type: 'string',
    check: function (input) {
      return ['hello', 'world'];
    }
  }
};

var opts = {
  converter: { // 数据转换
    'string': function (input) {
      return String(input);
    }
  },
  validator: { // 验证转换后的数据
    'string': function (input) {
      return Object.prototype.toString.call(input) == '[object String]';
    }
  }
};

leach(schema, {hehe: 'hehe'});
// => {} 数据不在范围中
leach(schema, {hehe: new String('hello')});
// => {hehe: 'hello'} 转换为正确的类型且数据在区间中
```

## License
The MIT license

---
_Source: https://npm.io/package/leach · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
