| Total Complexity | 1 |
| Complexity/F | 0 |
| Lines of Code | 26 |
| Function Count | 0 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Coverage | 11.11% |
| Changes | 0 | ||
| 1 | import {tryCatch} from 'fp-ts/lib/Either'; |
||
| 2 | import {pipe, pipeable} from 'fp-ts/lib/pipeable'; |
||
| 3 | import {fromEither, taskEither, taskify} from 'fp-ts/lib/TaskEither'; |
||
| 4 | import {readFile} from 'fs'; |
||
| 5 | |||
| 6 | const TE = pipeable(taskEither); |
||
| 7 | |||
| 8 | const fromString = (s: string) => tryCatch( |
||
| 9 | () => JSON.parse(s) as unknown, |
||
| 10 | 2 | error => error instanceof Error |
|
| 11 | ? error |
||
| 12 | : Error('Cannot parse JSON data') |
||
| 13 | ); |
||
| 14 | |||
| 15 | const fromFile = (path: string) => pipe( |
||
| 16 | taskify(readFile)(path), |
||
| 17 | TE.mapLeft(({message}) => Error(message)), |
||
| 18 | TE.map(b => b.toString()), |
||
| 19 | TE.chain(s => fromEither(fromString(s))) |
||
| 20 | ); |
||
| 21 | |||
| 22 | export const Json = { |
||
| 23 | fromFile: fromFile, |
||
| 24 | fromString: fromString |
||
| 25 | }; |
||
| 26 |