Skip to content

Instantly share code, notes, and snippets.

@simontegg
simontegg / prettify.js
Created July 9, 2022 09:46
javascript - prettify
/*
Make the function more readable and maintainable
*/
function doStuff(text) {
const lowerCased = text.toLocaleLowerCase();
const words = lowerCased.split(' ');
words.reverse();
const trimmedWords = [];
@simontegg
simontegg / util.js
Last active January 20, 2022 02:47
A map function with an error
// inspired by https://rescript-lang.org/docs/manual/v8.0.0/api/belt/map#getexn
function makeErroringMap (array, byIdFunc, defaultValue) {
const wm = new WeakMap()
array.forEach(item => {
wm.set(byIdFunc(item), item)
})
return {
get(id) {
@simontegg
simontegg / selectors.js
Last active January 19, 2022 22:23
Handling nullability in selectors
import { createSelector } from 'reselect'
export const getWorkflows = context => context.workflows
export const getId = context => context.activeId
/** OPTION 1 */
/**
* Will probably throw an error
@simontegg
simontegg / machine.js
Last active March 8, 2021 20:56
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@simontegg
simontegg / machine.js
Last active November 27, 2020 02:04
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@simontegg
simontegg / machine.js
Last active July 27, 2020 03:01
Generated by XState Viz: https://xstate.js.org/viz
const machine = Machine({
"id": "wizard",
"initial": "idle",
"context": {
"steps": [
{
"title": "Step 1",
"next": true
},
{
@simontegg
simontegg / machine.js
Last active November 17, 2020 00:08
Generated by XState Viz: https://xstate.js.org/viz
const machine = Machine({
"id": "playlist:1",
"initial": "loading",
"context": {
"playlistId": 1,
"playlist": {
"name": ""
},
"contribution": {
"cursor": {
@simontegg
simontegg / machine.js
Created July 15, 2020 03:25
Generated by XState Viz: https://xstate.js.org/viz
function submitWizard(context) {
return new Promise((resolve, reject) => {
setTimeOut(() => {
const isSuccess = Math.random() > 0
return isSuccess ? resolve({ data: { your: "result" } }) : reject(Error('did not work'))
}, 3000)
})
}
const wizard = Machine({
Verifying my Blockstack ID is secured with the address 18uZo7LBYySEjYXiHPiXL1qwchSZWD2PCk https://explorer.blockstack.org/address/18uZo7LBYySEjYXiHPiXL1qwchSZWD2PCk
@simontegg
simontegg / sum.js
Last active November 24, 2016 07:31
// classic
function sum (a, b) {
return a + b
}
// old-school
var sum = function (a, b) {
return a + b
}