-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# ts-cast: Basics (example) | ||
|
||
## Install and Start | ||
|
||
```sh | ||
git clone https://github.com/DScheglov/ts-cast.git | ||
cd ts-cast/examples/either | ||
npm install | ||
npm start | ||
``` | ||
|
||
**Output** | ||
|
||
```shell | ||
Book { | ||
title: 'My First Book', | ||
annotation: undefined, | ||
year: 2021, | ||
authors: [ Person { name: 'John Smith', email: 'john@smith.com' } ], | ||
coords: [ 1.2, 23.32 ] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"start": "ts-node ./src" | ||
}, | ||
"dependencies": { | ||
"fp-ts": "^3.0.0-alpha.15", | ||
"ts-cast": "latest", | ||
"validator": "^13.5.2" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^24.9.1", | ||
"@types/node": "^14.0.13", | ||
"@types/validator": "^13.1.2", | ||
"@typescript-eslint/eslint-plugin": "^2.34.0", | ||
"@typescript-eslint/parser": "^2.34.0", | ||
"eslint": "^6.7.2", | ||
"eslint-config-airbnb": "^18.1.0", | ||
"eslint-plugin-import": "^2.21.2", | ||
"eslint-plugin-jest": "^23.13.2", | ||
"ts-node": "^9.1.1", | ||
"typescript": "^3.7.5" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"airbnb/base" | ||
], | ||
"plugins": [ | ||
"@typescript-eslint", | ||
"jest" | ||
], | ||
"parser": "@typescript-eslint/parser", | ||
"rules": { | ||
"max-len": [ | ||
"error", | ||
{ | ||
"code": 100, | ||
"ignoreStrings": true | ||
} | ||
], | ||
"no-param-reassign": "off", | ||
"implicit-arrow-linebreak": "off", | ||
"import/no-unresolved": 0, | ||
"import/prefer-default-export": 0, | ||
"indent": [ | ||
2, | ||
2, | ||
{ | ||
"flatTernaryExpressions": true | ||
} | ||
], | ||
"no-unused-vars": "off", | ||
"no-undef": "error", | ||
"no-tabs": "error", | ||
"no-nested-ternary": 0, | ||
"import/extensions": 0, | ||
"arrow-parens": [ | ||
"error", | ||
"as-needed" | ||
], | ||
"operator-linebreak": 0, | ||
"no-underscore-dangle": 0, | ||
"@typescript-eslint/no-unused-vars": [ | ||
"error" | ||
], | ||
"jest/no-disabled-tests": "warn", | ||
"jest/no-focused-tests": "error", | ||
"jest/no-identical-title": "error", | ||
"jest/prefer-to-have-length": "warn", | ||
"jest/valid-expect": "error" | ||
}, | ||
"env": { | ||
"jest/globals": true | ||
}, | ||
"ignorePatterns": [ | ||
"examples/**/*", | ||
"lib/**/*", | ||
"esm/**/*" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as E from 'fp-ts/Either'; | ||
import { pipe } from 'fp-ts/function'; | ||
import { Book, eitherStr } from './schema'; | ||
|
||
const debugBook = (data: unknown) => | ||
pipe( | ||
data, | ||
eitherStr(Book), | ||
E.orElseW(error => E.right({ error })), | ||
E.map(result => console.dir(result, { depth: null })), | ||
); | ||
|
||
debugBook({ | ||
title: "My First Book", | ||
year: 2021, | ||
authors: [{ name: "John Smith", email: "john@smith.com" }], | ||
coords: [1.2, 23.32] | ||
}); | ||
/* Prints: | ||
Book { | ||
title: 'My First Book', | ||
annotation: undefined, | ||
year: 2021, | ||
authors: [ Person { name: 'John Smith', email: 'john@smith.com' } ], | ||
coords: [ 1.2, 23.32 ] | ||
} | ||
*/ | ||
|
||
debugBook(null); | ||
/* Prints: | ||
{ error: 'Book is expected but "null" received.' } | ||
*/ | ||
|
||
debugBook({}); | ||
/* Prints: | ||
{ error: 'string is expected in title but "undefined" received.' } | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as E from 'fp-ts/Either'; | ||
import { integer, number, string, struct, tuple, array, toBe, Caster } from 'ts-cast'; | ||
import v from 'validator'; | ||
|
||
export const eitherStr = <T>(caster: Caster<T>) => caster.either( | ||
({ message }) => E.left(message), | ||
E.right | ||
); | ||
|
||
export const Person = struct({ | ||
name: string, | ||
email: string.restrict(toBe(v.isEmail, "a valid email")), | ||
}, 'Person'); | ||
|
||
export const Coords = tuple(number, number); | ||
|
||
export const Book = struct({ | ||
title: string, | ||
annotation: string.optional, | ||
year: integer, | ||
authors: array(Person), | ||
coords: Coords.optional, | ||
}, 'Book'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"outDir": "./esm", | ||
"baseUrl": "./src", | ||
"alwaysStrict": true, | ||
"noImplicitAny": true, | ||
"experimentalDecorators": false, | ||
"emitDecoratorMetadata": false, | ||
"module": "CommonJS", | ||
"moduleResolution": "node", | ||
"allowSyntheticDefaultImports": false, | ||
"esModuleInterop": true, | ||
"resolveJsonModule": true, | ||
"removeComments": true, | ||
"declaration": true, | ||
"importHelpers": false, | ||
"strictNullChecks": true, | ||
"sourceMap": true | ||
}, | ||
"types": ["jest", "node"], | ||
"lib": ["es6", "es7"], | ||
"include": ["./src/**/*.ts"] | ||
} |