Skip to content

Commit

Permalink
chore: v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Jan 13, 2020
1 parent 2951d8e commit f9a20d3
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 64 deletions.
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: ['https://paypal.me/hustcc', 'https://atool.vip']
29 changes: 29 additions & 0 deletions .github/workflows/build.yml
@@ -0,0 +1,29 @@
name: build

on: [push]

jobs:
build:

runs-on: macOS-latest

strategy:
matrix:
node-version: [8.x, 10.x, 12.x]

steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: ci
run: |
npm install
npm run build
npm run test
- name: coverall
if: success()
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,3 +7,4 @@ coverage
node_modules
lib/
package-lock.json
.idea
9 changes: 0 additions & 9 deletions .npmignore

This file was deleted.

28 changes: 18 additions & 10 deletions README.md
Expand Up @@ -6,7 +6,12 @@
>
> Inspired by [prop-types](https://github.com/facebook/prop-types).
[![Ver](https://img.shields.io/npm/v/variable-type.svg)](https://www.npmjs.com/package/variable-type) [![Build Status](https://travis-ci.org/hustcc/variable-type.svg?branch=master)](https://travis-ci.org/hustcc/variable-type) [![Coverage Status](https://coveralls.io/repos/github/hustcc/variable-type/badge.svg?branch=master)](https://coveralls.io/github/hustcc/variable-type) [![npm download](https://img.shields.io/npm/dm/variable-type.svg)](https://www.npmjs.com/package/variable-type)
[![npm Version](https://img.shields.io/npm/v/variable-type.svg)](https://www.npmjs.com/package/variable-type)
[![Build Status](https://github.com/hustcc/variable-type/workflows/build/badge.svg)](https://github.com/hustcc/variable-type/actions)
[![Coverage Status](https://coveralls.io/repos/github/hustcc/variable-type/badge.svg?branch=master)](https://coveralls.io/github/hustcc/variable-type?branch=master)
[![npm download](https://img.shields.io/npm/dm/variable-type.svg)](https://www.npmjs.com/package/variable-type)
[![npm License](https://img.shields.io/npm/l/variable-type.svg)](https://www.npmjs.com/package/variable-type)



## 1. Install
Expand All @@ -15,7 +20,7 @@
Then import it.

```typescript
```ts
import VT from 'variable-type';
```

Expand Down Expand Up @@ -93,9 +98,9 @@ VT.or([
- `Array` type.

```js
var arr = ['hello', 'world', 25, new Date(1992, 8, 1)];
const arr = ['hello', 'world', 25, new Date(1992, 8, 1)];

var types = VT.arrayOf(
const types = VT.arrayOf(
VT.or([
VT.number,
VT.string,
Expand All @@ -109,13 +114,13 @@ types.check(arr); // will get true.
- `Object` type.

```js
var obj = {
const obj = {
name: 'hustcc',
boy: true,
birthday: new Date(1992, 8, 1)
};

var types = VT.shape({
const types = VT.shape({
name: VT.string,
boy: VT.bool,
birthday: VT.instanceOf(Date)
Expand Down Expand Up @@ -191,18 +196,21 @@ VT.shape({
## 4. Test & Perf

```
npm i
# install dependence
$ npm i
npm run test
# run unit test
$ npm run test
npm run perf
# run performance test
$ npm run perf
```

> [OPS] variable-type / prop-types = 5.033

## License

ISC@[hustcc](https://github.com/hustcc).
MIT@[hustcc](https://github.com/hustcc).


6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -3,9 +3,11 @@
"version": "1.0.0",
"description": "Runtime type checking for variable and similar objects.",
"main": "lib/index.js",
"files": [
"lib"
],
"scripts": {
"coverage": "jest --coverage",
"test": "jest",
"test": "jest --coverage",
"perf": "ts-node perf/benchmark.ts",
"build": "tsc"
},
Expand Down
2 changes: 1 addition & 1 deletion perf/benchmark.ts
Expand Up @@ -4,7 +4,7 @@
*/
import VT from '../src';
import PT from 'prop-types';
import Benchmark, {Suite, Event} from 'benchmark';
import Benchmark, { Suite, Event } from 'benchmark';

const suite = new Benchmark.Suite();

Expand Down
4 changes: 2 additions & 2 deletions src/Type.ts
Expand Up @@ -2,7 +2,7 @@
* Created by hustcc on 17/08/01.
*/

import _optional from './types/optional';
import optionalChecker from './types/optional';

export type TypeChecker = (t: any) => boolean;

Expand All @@ -21,6 +21,6 @@ export default class Type {
};

readonly optional: () => Type = (): Type => {
return _optional(this);
return optionalChecker(this);
};
}
80 changes: 40 additions & 40 deletions src/index.ts
Expand Up @@ -22,49 +22,49 @@ type TypeGenerator = (toCheck: any) => Type;
type EnumValidator = (toCheck: any[]) => Type;

export type VariableType = {
readonly undefined: Type;
readonly bool: Type;
readonly func: Type;
readonly number: Type;
readonly string: Type;
readonly null: Type;
readonly object: Type;
readonly array: Type;
readonly any: Type,
readonly and: TypeCombiner;
readonly or: TypeCombiner;
readonly not: TypeTransformer;
readonly instanceOf: TypeGenerator;
readonly typeOf: TypeGenerator;
readonly in: EnumValidator;
readonly oneOf: EnumValidator;
readonly oneOfType: TypeCombiner;
readonly arrayOf: TypeTransformer;
readonly shape: (typeShape: Record<string, Type>) => Type;
readonly apply: (func: TypeChecker) => Type;
readonly undefined: Type;
readonly bool: Type;
readonly func: Type;
readonly number: Type;
readonly string: Type;
readonly null: Type;
readonly object: Type;
readonly array: Type;
readonly any: Type,
readonly and: TypeCombiner;
readonly or: TypeCombiner;
readonly not: TypeTransformer;
readonly instanceOf: TypeGenerator;
readonly typeOf: TypeGenerator;
readonly in: EnumValidator;
readonly oneOf: EnumValidator;
readonly oneOfType: TypeCombiner;
readonly arrayOf: TypeTransformer;
readonly shape: (typeShape: Record<string, Type>) => Type;
readonly apply: (func: TypeChecker) => Type;
};

const VT: VariableType = {
undefined: typeOf('undefined'),
bool: typeOf('boolean'), // simple
func: typeOf('function'), // simple
number: typeOf('number'), // simple
string: typeOf('string'), // simple
null: typeOf('null'),
object: typeOf('object'),
array: typeOf('array'),
and,
or,
not,
any: new Type(() => true),
instanceOf,
typeOf,
in: oneOf,
oneOf, // cname of `in`, name from prop-types
oneOfType: or, // cname of `or`, name from prop-types
arrayOf,
shape,
apply
undefined: typeOf('undefined'),
bool: typeOf('boolean'), // simple
func: typeOf('function'), // simple
number: typeOf('number'), // simple
string: typeOf('string'), // simple
null: typeOf('null'),
object: typeOf('object'),
array: typeOf('array'),
and,
or,
not,
any: new Type(() => true),
instanceOf,
typeOf,
in: oneOf,
oneOf, // cname of `in`, name from prop-types
oneOfType: or, // cname of `or`, name from prop-types
arrayOf,
shape,
apply
};

export default VT;

0 comments on commit f9a20d3

Please sign in to comment.