-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheither.js
More file actions
67 lines (56 loc) · 1.37 KB
/
either.js
File metadata and controls
67 lines (56 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//import { readFileSync } from 'fs';
const fs = require('fs');
const config = require("../JSON/config.json");
const Right= x => ({
chain: f => f(x),
map: f => Right(f(x)),
fold: (f, g) => g(x),
toString: `Right(${x})`
})
const Left = x => ({
chain: f => Left(x),
map: f => Left(x),
fold: (f, g) => f(x),
toString: `Left(${x})`
})
const fromNullable = x => x != null ? Right(x) : Left()
const findColor = name => fromNullable({
red: '#ff4444',
blue: '#3b5998',
yellow: '#fff68f'}[name]
)
const res = () =>
findColor('red')
.map(x => x.toUpperCase())
.map(x => x.slice(1))
.fold(
() => 'no color!',
color => color
)
console.log("findColor Example:" + res())
// tryCatch, when called, if it blows up, I'll get a Left of the error and if it doesn't I'll get a right
const tryCatch = f => {
try {
return Right(f())
} catch (e) {
return Left(e)
}
}
const readFileSync = path =>
tryCatch(() => fs.readFileSync(path))
const getPort = () =>
readFileSync('../JSON/config.json')
.map(contents => JSON.parse(contents))
.map(config => config.port)
.fold(() => 8080, x => x)
const getPort_ = () => {
try {
const str = readFileSync('../config.json')
const config = JSON.parse(str)
return config.port
} catch (e) {
return 5000
}
}
const portex = getPort();
console.log("getPort Example: " + portex) // 3000