-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (59 loc) · 1.55 KB
/
index.js
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
68
69
70
71
72
const log = require( './lib/log' )
const fs = require( 'fs' )
const moment = require( 'moment' )
const randomize = require( 'randomatic' )
const {
// ADT
Async,
Async: { fromNode },
// Helper
constant,
curry,
identity,
// Pointfree
bimap,
} = require( 'crocks' )
const R = require( 'ramda' )
/* Config */
const DOJO_LENGTH = 500
const DOJO_PATTERN = '0!'
const DOJO_WORDS_PER_LINE = 50
// data :: IO => String
const date = moment()
.format()
// createFilename :: String -> String
const createFilename = name =>
'./dojo/' + name + '.txt'
// getRandomString :: IO => String -> Int -> String
const getRandomString = curry(
( pattern, length ) => randomize( pattern, length )
)
// getDojo :: _ -> String
const getDojo = () =>
getRandomString( DOJO_PATTERN, DOJO_LENGTH )
// wrapText :: Int -> String -> String
const wrapText = curry(
( wordsPerLine, str ) =>
R.reduce(
( xs, x ) => xs.length % wordsPerLine ? ( xs + x ) : ( xs + '\r\n\r\n' + x ),
'',
str
)
)
// wrapDojo :: String -> String
const wrapDojo = wrapText( DOJO_WORDS_PER_LINE )
// writeFile :: IO
const writeFile = fromNode( fs.writeFile )
// writeFileAsync :: Options -> String -> String -> Async Error b
const writeFileAsync = curry(
( options, path, data ) =>
writeFile( path, data, options )
)
// writeTextFile :: String -> Async Error String
const writeTextFile = writeFileAsync( 'utf-8' )
// Async fork reflection
const fork = a => a.fork(
log.bind( null, 'rej' ),
log.bind( null, 'res' )
)
fork( writeTextFile( createFilename( date ), wrapDojo( getDojo() ) ) )