Instantly share code, notes, and snippets.

View gatsby-source-properties.js
// i am expecting this to generate an allPropertiesFile thing in graphqil
// but it doesn't seem to be reflected in there at all
exports.sourceNodes = async ({ actions, createNodeId }) => {
const { createNode } = actions
// sample data will actually come from files.
const data = {
'myfolder/file2': { text: 'hi', buttonThing: 'hey' },
'myfolder/file1': { header: 'hi', footer: 'bye' },
View weird-flow.md

This seems fine

code:

// @flow
const data = document.body.getAttribute('my-attribute') || '{}'
const result = JSON.parse(data)
document.body.removeAttribute('my-attribute')
View param-enforcer.js
// idea 1 - use JOI (or similar) at the router level
let params = {
query: {
limit: Joi.number().integer().min(1).max(100).default(10)
}
}
server.get("/horse", enforceParams(params), controller.myRoute)
// idea 2 - use JOI (or similar) at the controller level
async function myRoute(req, res) {
View custom-errors.js
function handleRequest(req, res) {
// all the important request logic...
// send back the request id
res.send(requestId)
// if sending to an known sender...
if (senderId) {
await optInToExperiment(senderId)
}
View flow-example.js
// no types
function getFields(fields) {
return fields.reduce((prev, curr) => (prev[curr] = curr, prev), {})
}
// no return type
function getFields(fields: Array<string>) {
return fields.reduce((prev, curr) => (prev[curr] = curr, prev), {})
}
View webpack-unused-files.sh
# ----------------------------------- #
webpack --display-modules | awk '{print $2}' | grep ^\.\/ > files-processed.txt;
cd public/js/; # assumes all your files are here
find . -name "*.js" | grep -v eslint | grep -v __ > ../../files-all.txt; # excludes __tests__ and .eslintrc files
cd ../..;
cat files-all.txt | xargs -I '{}' sh -c "grep -q '{}' files-processed.txt || echo '{}'";
rm files-processed.txt files-all.txt;
# ----------------------------------- #
View promise-anti-patterns.md
View flow-experience.md

Trying Flow on my node.js code…Let’s see how this goes

Finding the getting started guide:

  1. google flow
  2. click “getflow.com”
  3. was something else
  4. google flow javascript
  5. found flowtype.org
  6. Click “get started” and landed here https://flowtype.org/docs/getting-started.html#_
View async-router.js
// catch any errors in the promise and either forward them to next(err) or ignore them
const catchErrors = fn => (req, res, next) => fn(req, res, next).catch(next)
const ignoreErrors = fn => (req, res, next) => fn(req, res, next).catch(() => next())
// wrap my routes in those helpers functions to get error handling
app.get('/sendMoney/:amount', catchErrors(sendMoney))
// use our ignoreErrors helper to ignore any errors in the logging middleware
app.get('/doSomethingElse', ignoreErrors(logSomething), doSomethingElse)
View await-fail.js
// rely on try/catch around the awaited Promise
async function doSomething() {
try {
let data = await getData()
} catch (err) {
console.error(err)
}
}
// add a catch handler