Skip to content

Commit

Permalink
Support for [email protected] (#11)
Browse files Browse the repository at this point in the history
* add support for [email protected]
* fix typo in installPeerDependencies npm script
* fix broken link to Either.ts in README.md
  • Loading branch information
pyryk authored and aeirola committed Jul 4, 2019
1 parent a0753f0 commit b99ec2f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ It provides the following:

### Promise chain decoding

Decode data from Promise based APIs, without having to worry about how to retrieve data from the [Either](https://github.com/gcanti/fp-ts/blob/master/docs/Either.md) values returned by the `io-ts` types.
Decode data from Promise based APIs, without having to worry about how to retrieve data from the [Either](https://gcanti.github.io/fp-ts/modules/Either.ts.html) values returned by the `io-ts` types.

```typescript
import * as t from 'io-ts';
Expand Down
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"lint:typescript": "tsc --noEmit",
"test:js": "mocha -r ts-node/register test/*.ts",
"build": "tsc",
"installPeerDepenencies": "npm install --no-save io-ts@1.x",
"prepublish": "npm run installPeerDepenencies && npm run build"
"installPeerDependencies": "npm install --no-save io-ts@2.x fp-ts@2.x",
"prepublish": "npm run installPeerDependencies && npm run build"
},
"repository": {
"type": "git",
Expand All @@ -36,7 +36,8 @@
"deep-equal": "^1.0.1"
},
"peerDependencies": {
"io-ts": "1.x"
"fp-ts": "2.x",
"io-ts": "2.x"
},
"devDependencies": {
"@types/chai": "^4.1.7",
Expand All @@ -45,7 +46,6 @@
"@types/mocha": "^5.2.7",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"fp-ts": "^1.19.0",
"mocha": "^6.1.4",
"prettier": "^1.18.2",
"ts-node": "^8.3.0",
Expand Down
13 changes: 6 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import deepEqual from 'deep-equal';
import { either, fold } from 'fp-ts/lib/Either';
import * as t from 'io-ts';
import { PathReporter } from 'io-ts/lib/PathReporter';

Expand Down Expand Up @@ -36,12 +37,10 @@ export function decode<Output, Input>(
Promise<Output>
>(null, type);
default:
return type
.decode(value || arguments[2])
.fold(
errors => Promise.reject(new DecodeError(errors)),
decodedValue => Promise.resolve(decodedValue),
);
return fold<t.Errors, Output, Promise<Output>>(
errors => Promise.reject(new DecodeError(errors)),
decodedValue => Promise.resolve(decodedValue),
)(type.decode(value || arguments[2]));
}
}

Expand Down Expand Up @@ -156,7 +155,7 @@ export function extendDecoder<Input, Output>(
value: unknown,
context: t.Context,
) => {
return baseDecoder.validate(value, context).chain(chainedValue => {
return either.chain(baseDecoder.validate(value, context), chainedValue => {
try {
return t.success(decode(chainedValue));
} catch (e) {
Expand Down
17 changes: 12 additions & 5 deletions test/helpers/chai-fp-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ interface Utils {
export default function(chai: Chai, utils: Utils) {
const Assertion = chai.Assertion;

function isObject(value: unknown): value is object {
return typeof value === 'object' && value !== null;
}

function isEither<L, A>(value: unknown): value is Either.Either<L, A> {
return value instanceof Either.Left || value instanceof Either.Right;
if (!isObject(value)) {
return false;
}
return Either.isLeft(value as any) || Either.isRight(value as any);
}

Assertion.addProperty('left', function() {
const obj = this._obj;
let isLeft: boolean = false;

if (isEither(obj) && obj.isLeft()) {
utils.flag(this, 'object', obj.value);
if (isEither(obj) && Either.isLeft(obj)) {
utils.flag(this, 'object', obj.left);
isLeft = true;
}

Expand All @@ -46,8 +53,8 @@ export default function(chai: Chai, utils: Utils) {
const obj = this._obj;
let isRight: boolean = false;

if (isEither(obj) && obj.isRight()) {
utils.flag(this, 'object', obj.value);
if (isEither(obj) && Either.isRight(obj)) {
utils.flag(this, 'object', obj.right);
isRight = true;
}

Expand Down

0 comments on commit b99ec2f

Please sign in to comment.