Skip to content

Instantly share code, notes, and snippets.

View xjamundx's full-sized avatar

Jamund Ferguson xjamundx

View GitHub Profile
@xjamundx
xjamundx / gatsby-source-properties.js
Last active September 26, 2018 20:24
gatsby properties source plugin
// 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' },
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)
}

This seems fine

code:

// @flow
const data = document.body.getAttribute('my-attribute') || '{}'
const result = JSON.parse(data)
document.body.removeAttribute('my-attribute')
// 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) {
@xjamundx
xjamundx / svgupdate.js
Last active November 16, 2017 22:17
Update Base64 SVG background image.
function updateSVG() {
var el = document.querySelector('.icon-user');
var style = window.getComputedStyle(el);
var uri = style.backgroundImage;
var svg64 = uri.replace(/^.+base64,/, "").replace(/\"?\)$/, "")
var xml = window.atob(svg64);
var hex = '#' + Math.floor(Math.random()*16777215).toString(16);
var color = xml.replace(/fill="#[A-Za-z0-9]+"/, 'fill="' + hex + '"');
var color64 = window.btoa(color);
var colorUri = "url('data:image/svg+xml;base64," + color64 + "')";

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#_
// 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), {})
}
webpack --display-modules | awk '{print $3$4" "$2}' | grep -v bytes | sort -n | tail -100
// 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)
// 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