Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
All the things Example
const util = require('util')
const fs = require('fs')
const exec = util.promisify(require('child_process').exec)
const { split, compose, trim, composeK } = require('ramda')
const { createElement } = require('react')
const ReactDOMServer = require('react-dom/server')
const { List } = require('react-virtualized')
const Task = require('data.task')
const c = composeK
const cat = file =>
new Task((reject, resolve) =>
exec(`cat ${file}`)
.then(({ stdout }) => resolve(stdout))
.catch(reject)
)
const VList = rowRenderer => list =>
Task.of(
createElement(
List,
{
rowCount: list.length,
height: 200,
rowHeight: 30,
width: 200,
rowRenderer: rowRenderer(list)
},
null
)
)
const addClass = className => element => ({
...element,
props: {
...element.props,
className: (element.props.className || '') + ' ' + className
}
})
const div = ({ key, style, content }) =>
createElement('div', { key, style }, content)
const rowRenderer = element => list => ({
key,
style,
index
}) => element({ key, style, content: list[index] })
const toArray = text =>
Task.of(
compose(
split('\n'),
trim
)(text)
)
const indexHtml = content =>
`<html lang="en">
<head>
<meta charset="utf-8">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<link rel="stylesheet" href="https://unpkg.com/tachyons/css/tachyons.min.css"/>
<title></title>
</head>
<style>
.bg-blue {
background-color: #2e8ccf;
}
.bg-orange {
background-color: #b4881d;
}
</style>
<body class="sans-serif ma2" style="background-color:#194455">
${content}
</body>
</html>`
const genHtml = data =>
Task.of(
compose(
data => fs.writeFileSync('./index.html', data) || data,
indexHtml,
ReactDOMServer.renderToString
)(data)
)
const blueRow = rowRenderer(
compose(
addClass('white'),
addClass('bg-blue'),
addClass('pa2'),
addClass('bb'),
div
)
)
const allTheThings = c(genHtml, VList(blueRow), toArray, cat)
allTheThings('sample.txt').fork(console.error, console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment