Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite to TypeScript #21

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions .babelrc

This file was deleted.

7 changes: 0 additions & 7 deletions .eslintrc.json

This file was deleted.

22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 18
- 16
- 14
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/dist
/node_modules
/yarn-*.log
/coverage
/package-lock.json
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 18.15.0
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# Change Log

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.2.0] - 2023-08-04

- Rewritten to TypeScript.

## [0.1.1] - 2019-12-19

- Do not use CoreJS builtins.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typecheck [![Build Status](https://travis-ci.org/umbrellio/typecheck.svg?branch=master)](https://travis-ci.org/umbrellio/typecheck) [![Coverage Status](https://coveralls.io/repos/github/umbrellio/typecheck/badge.svg?branch=master)](https://coveralls.io/github/umbrellio/typecheck?branch=master)
# typecheck

Simple, strict, extensible runtime type checker for JavaScript.

Expand Down
14 changes: 0 additions & 14 deletions jest.config.js

This file was deleted.

24 changes: 0 additions & 24 deletions lib/index.js

This file was deleted.

28 changes: 28 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Type, { IType } from './type.js'

import ArrayType from './types/array.js'
import Sum from './types/sum.js'
import Option from './types/option.js'
import Struct from './types/struct.js'

export default {
check(type: IType, value: unknown): boolean {
return type.__check(value)
},
Type,

String: new Type(
v => Object.prototype.toString.call(v) === '[object String]'
),
Number: new Type(
v => Object.prototype.toString.call(v) === '[object Number]'
),
Boolean: new Type(
v => Object.prototype.toString.call(v) === '[object Boolean]'
),

Array: (type: IType) => new ArrayType(type),
Sum: (...types: unknown[]) => new Sum(types),
Option: (type: IType) => new Option(type),
Struct: (def: Record<string, IType>) => new Struct(def),
}
9 changes: 0 additions & 9 deletions lib/type.js

This file was deleted.

17 changes: 17 additions & 0 deletions lib/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type TypeCheckFn = (value: unknown) => boolean

export interface IType {
__check(value: unknown): boolean
}

export default class Type implements IType {
checker: TypeCheckFn

constructor(checker: TypeCheckFn) {
this.checker = checker
}

__check(value: unknown) {
return this.checker(value)
}
}
13 changes: 0 additions & 13 deletions lib/types/array.js

This file was deleted.

14 changes: 14 additions & 0 deletions lib/types/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IType } from '../type.js'
export default class TArray implements IType {
type: IType

constructor(type: IType) {
this.type = type
}

__check(value: unknown) {
if (!Array.isArray(value)) return false

return value.every(v => this.type.__check(v))
}
}
12 changes: 0 additions & 12 deletions lib/types/option.js

This file was deleted.

15 changes: 15 additions & 0 deletions lib/types/option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IType } from '../type.js'
export default class Option implements IType {
type: IType

constructor(type: IType) {
this.type = type
}

__check(value: unknown) {
if (value === null) return true
if (value === undefined) return true

return this.type.__check(value)
}
}
15 changes: 10 additions & 5 deletions lib/types/struct.js → lib/types/struct.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { checkObjectType, hasOwnProperty } from '../util'
import { hasOwnProperty, isObject } from '../util.js'
import { IType } from '../type.js'

export default class Struct {
constructor (definition) {
export default class Struct implements IType {
definition: Record<string, IType>
definitionKeys: string[]
definitionKeysLen: number

constructor(definition: Record<string, IType>) {
this.definition = definition
this.definitionKeys = Object.keys(definition)
this.definitionKeysLen = this.definitionKeys.length
}

__check (value) {
if (!checkObjectType(value, 'Object')) return false
__check(value: unknown) {
if (!isObject(value)) return false
if (this.definitionKeysLen !== Object.keys(value).length) return false

for (let i = 0; i < this.definitionKeysLen; i += 1) {
Expand Down
15 changes: 0 additions & 15 deletions lib/types/sum.js

This file was deleted.

20 changes: 20 additions & 0 deletions lib/types/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isType } from '../util.js'
import { IType } from '../type.js'

export default class Sum implements IType {
values: unknown[]

constructor(values: unknown[]) {
this.values = values
}

__check(value: unknown) {
return this.values.some(v => {
if (isType(v)) {
return v.__check(value)
} else {
return v === value
}
})
}
}
3 changes: 0 additions & 3 deletions lib/util.js

This file was deleted.

13 changes: 13 additions & 0 deletions lib/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IType } from './type.js'

export function isObject(v: unknown): v is Record<string, unknown> {
return Object.prototype.toString.call(v) === '[object Object]'
}

export function isType(v: unknown): v is IType {
return isObject(v) && '__check' in v
}

export function hasOwnProperty(obj: Object, prop: string): boolean {
return Object.prototype.hasOwnProperty.call(obj, prop)
}
45 changes: 21 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"name": "@umbrellio/typecheck",
"description": "Simple, strict, extensible runtime type checker for JavaScript",
"version": "0.1.1",
"version": "0.2.0",
"author": "Alexander Komarov <[email protected]>",
"repository": "umbrellio/typecheck",
"license": "MIT",
"main": "dist/typecheck.cjs.js",
"modules": "dist/typecheck.es.js",
"unpkg": "dist/typecheck.iife.js",
"type": "module",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
"engines": {
"node": ">=14.16 <=18"
},
"files": [
"dist"
],
Expand All @@ -18,27 +21,21 @@
"type-check"
],
"devDependencies": {
"@babel/core": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"babel-eslint": "^10.0.3",
"babel-jest": "^24.9.0",
"core-js": "3",
"coveralls": "^3.0.6",
"eslint": "^6.3.0",
"eslint-config-umbrellio": "^5.0.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-prefer-object-spread": "^1.2.1",
"eslint-plugin-promise": "^4.2.1",
"jest": "^24.9.0",
"rollup": "^1.21.2",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-node-resolve": "^5.2.0"
"ava": "^5.3.1",
"c8": "^8.0.1",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
},
"scripts": {
"build": "rollup -c",
"lint": "eslint lib/**/*.js",
"test": "jest",
"test:coverage": "jest --coverage"
"build": "tsc",
"test": "tsc --noEmit && c8 --reporter=lcov ava"
},
"ava": {
"extensions": {
"ts": "module"
},
"nodeArguments": [
"--loader=ts-node/esm"
]
}
}
Loading