-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: finally figured out all of the modules nonsense, good grief what…
… a mess.
- Loading branch information
Showing
17 changed files
with
88 additions
and
47 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,26 @@ | ||
import { fromEnum, decodeWithDefault } from '../../../libs/utils/io-ts'; | ||
import * as E from 'fp-ts/lib/Either'; | ||
|
||
enum Foo { | ||
FOO = 'foo', | ||
BAR = 'bar', | ||
} | ||
|
||
const FooSchema = fromEnum('Foo', Foo); | ||
|
||
test('fromEnum works', () => { | ||
expect(FooSchema).toBeTruthy(); | ||
expect(FooSchema.name).toBe('Foo'); | ||
expect(FooSchema.is(Foo.FOO)).toBeTruthy(); | ||
expect(FooSchema.is(Foo.BAR)).toBeTruthy(); | ||
expect(FooSchema.is(null)).toBeFalsy(); | ||
expect(FooSchema.is(true)).toBeFalsy(); | ||
expect(FooSchema.encode(Foo.FOO)).toBe(Foo.FOO); | ||
expect(E.isRight(FooSchema.decode(Foo.FOO))).toBeTruthy(); | ||
expect(E.isLeft(FooSchema.decode(null))).toBeTruthy(); | ||
}); | ||
|
||
test('decodeWithDefault works', () => { | ||
expect(decodeWithDefault(E.right(Foo.FOO), Foo.BAR)).toBe(Foo.FOO); | ||
expect(decodeWithDefault(E.left(Foo.FOO), Foo.BAR)).toBe(Foo.BAR); | ||
}); |
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
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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,33 +1,39 @@ | ||
// stuff for making io-ts nicer | ||
import * as E from 'fp-ts/lib/Either'; | ||
import { identity, pipe } from 'fp-ts/lib/function'; | ||
import t, { Type } from 'io-ts'; | ||
import { pipe } from 'fp-ts/lib/function'; | ||
import * as t from 'io-ts'; | ||
|
||
// From: https://github.com/gcanti/io-ts/issues/216#issuecomment-599020040 | ||
/** | ||
* this utility function can be used to turn a TypeScript enum into a io-ts codec. | ||
* this utility function can be used to turn a TypeScript enum into an io-ts codec. | ||
*/ | ||
export function fromEnum<EnumType>(enumName: string, theEnum: Record<string, string | number>) { | ||
export function fromEnum<EnumType extends string>( | ||
enumName: string, | ||
theEnum: Record<string, EnumType>, | ||
): t.Type<EnumType, EnumType, unknown> { | ||
const isEnumValue = (input: unknown): input is EnumType => Object.values<unknown>(theEnum).includes(input); | ||
|
||
return new Type<EnumType>( | ||
return new t.Type<EnumType>( | ||
enumName, | ||
isEnumValue, | ||
(input, context) => (isEnumValue(input) ? t.success(input) : t.failure(input, context)), | ||
identity, | ||
t.identity, | ||
); | ||
} | ||
|
||
/** | ||
* Takes the result of a decode (an Either) and unpacks the value. If there is any error it will get logged to | ||
* the Console and will then return and empty object cast to the T typw. I know, janky. | ||
* */ | ||
export function unsafeDecode<T, E>(e: E.Either<E, T>): T { | ||
* | ||
* @param e an Either | ||
* @param defaultValue the default value to return if isLeft(e) === true | ||
* @returns if isRight(e) then the value in e, otherwise defaultValue with a console.error logged | ||
*/ | ||
export function decodeWithDefault<E, T>(e: E.Either<E, T>, defaultValue: T): T { | ||
return pipe( | ||
e, | ||
E.match((err) => { | ||
console.error(err); | ||
return {} as T; | ||
}, identity), | ||
console.error(`Failed to decode value from Either "${e}". Got error below`); | ||
console.dir(err); | ||
return defaultValue; | ||
}, t.identity), | ||
); | ||
} |
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
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
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
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
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
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 |
---|---|---|
|
@@ -35,7 +35,7 @@ | |
"next-env.d.ts", | ||
"**/*.ts", | ||
"**/*.tsx" | ||
], | ||
, "jest.config.js" ], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
|