src/Json.ts   A
last analyzed

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 26
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 11.11%

Importance

Changes 0
Metric Value
wmc 1
eloc 22
mnd 1
bc 1
fnc 0
dl 0
loc 26
ccs 1
cts 9
cp 0.1111
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
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