Skip to content

Instantly share code, notes, and snippets.

Avatar
`yarn prettier:fix`

Mattia Richetto mattiaerre

`yarn prettier:fix`
View GitHub Profile
@mattiaerre
mattiaerre / useLocalStorage.js
Created Apr 29, 2020
React hook useReducerWithLocalStorage
View useLocalStorage.js
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);
View asyncForEach.js
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 Jan 20, 2021
req, res and chaining
View README.md

How to test it in an Express app

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

200

http://localhost:9000/playground

@mattiaerre
mattiaerre / makeMenusClient.js
Created Dec 9, 2018
How to mock a module that exports a factory function with Jest
View makeMenusClient.js
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 Sep 29, 2018
ForStatement AST part
View ForStatement.json
{
"type": "ForStatement",
"initExpression": {
"type": "VariableDeclarationStatement",
"variables": [
{
"type": "VariableDeclaration",
"typeName": {
"type": "ElementaryTypeName",
"name": "uint",
@mattiaerre
mattiaerre / .prettierrc
Created Aug 21, 2018
prettier configuration file
View .prettierrc
{
"singleQuote": true,
"trailingComma": "none"
}
@mattiaerre
mattiaerre / index.js
Last active Aug 20, 2017
Load the OpenComponents client then render a component.
View index.js
(() => {
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 Aug 20, 2017
how to client side render an OpenComponents component using the Console (Developer Tools)
View open-component-renderer.js
((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 Apr 3, 2017
How to run a CRA in Heroku w/ Express and Node.js build pack
View package.json
/* 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"
}
}
View pre-release.MD

pre-release

-alpha.1

-alpha.2

-beta.1

-beta.2