Skip to content

Instantly share code, notes, and snippets.

View mattiaerre's full-sized avatar
`yarn prettier:fix`

Mattia Richetto mattiaerre

`yarn prettier:fix`
View GitHub Profile
@mattiaerre
mattiaerre / useLocalStorage.js
Created April 29, 2020 02:12
React hook useReducerWithLocalStorage
import { useState } from 'react';
// credit: https://usehooks.com/useLocalStorage/
function useLocalStorage(key, initialValue) {
const [storedValue, setStoredValue] = useState(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
console.log(error);
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index += 1) {
// eslint-disable-next-line no-await-in-loop
await callback(array[index], index, array);
}
}
// credit to https://codeburst.io/javascript-async-await-with-foreach-b6ba62bbf404
@mattiaerre
mattiaerre / README.md
Last active June 13, 2021 10:33
req, res and chaining

How to test it in an Express app

app.get('/playground', handler);

200

http://localhost:9000/playground

@mattiaerre
mattiaerre / makeMenusClient.js
Created December 9, 2018 00:27
How to mock a module that exports a factory function with Jest
import fetch from 'node-fetch';
function makeMenusClient(context) {
return {
getMenus: async rid =>
fetch(`${context.menusBaseUrl}/${rid}`)
.then(response => response.json())
.then(json => json)
};
}
@mattiaerre
mattiaerre / ForStatement.json
Created September 29, 2018 19:40
ForStatement AST part
{
"type": "ForStatement",
"initExpression": {
"type": "VariableDeclarationStatement",
"variables": [
{
"type": "VariableDeclaration",
"typeName": {
"type": "ElementaryTypeName",
"name": "uint",
@mattiaerre
mattiaerre / .prettierrc
Created August 21, 2018 11:27
prettier configuration file
{
"singleQuote": true,
"trailingComma": "none"
}
@mattiaerre
mattiaerre / index.js
Last active August 20, 2017 22:46
Load the OpenComponents client then render a component.
(() => {
const script = document.createElement('script');
script.type = 'application/javascript';
script.src = '//s3.amazonaws.com/oc-registry-dc/components/oc-client/0.40.7/src/oc-client.min.js';
script.onload = () => {
const html = window.oc.build({
baseUrl: '//oc.registry.dc',
name: 'oc-superman',
version: '1.0.0',
parameters: {
@mattiaerre
mattiaerre / open-component-renderer.js
Last active August 20, 2017 22:45
how to client side render an OpenComponents component using the Console (Developer Tools)
((oc, $) => {
// see: https://github.com/opentable/oc/wiki/Browser-client#ocbuild-options
const html = oc.build({
baseUrl: 'http://localhost:3030',
name: 'pi-baltimore-special-offers-promo-banners',
version: '1.X.X',
parameters: {
banner: 'purple-pasta'
}
});
@mattiaerre
mattiaerre / package.json
Last active April 3, 2017 08:52
How to run a CRA in Heroku w/ Express and Node.js build pack
/* info: the original "start" from CRA has been renamed to dev and a new "start" that points to server.js has been added */
{
"scripts": {
"start": "node server.js",
"dev": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}

pre-release

-alpha.1

-alpha.2

-beta.1

-beta.2