Skip to content

Instantly share code, notes, and snippets.

View karlguillotte's full-sized avatar

Karl Guillotte karlguillotte

  • Montréal, Canada
  • 18:26 (UTC -04:00)
View GitHub Profile
@karlguillotte
karlguillotte / javascript-proxy-as-rest-client.js
Created February 8, 2022 16:34 — forked from DavidWells/javascript-proxy-as-rest-client.js
Using a javascript proxy as low code REST client
/* Using a JavaScript proxy for a super low code REST client */
// via https://dev.to/dipsaus9/javascript-lets-create-aproxy-19hg
// also see https://towardsdatascience.com/why-to-use-javascript-proxy-5cdc69d943e3
const createApi = (url) => {
return new Proxy({}, {
get(target, key) {
return async function(id = "") {
const response = await fetch(`${url}/${key}/${id}`)
if (response.ok) {
return response.json();
@karlguillotte
karlguillotte / machine.js
Last active August 31, 2021 15:08
Generated by XState Viz: https://xstate.js.org/viz
const connectionMachine = Machine({
id: 'connection',
initial: 'testing',
states: {
testing: {
invoke: {
id: 'testing',
src: 'test',
onDone: [{
target: 'unknown',
const fetchMachine = Machine({
id: 'fetch',
initial: 'idle',
context: {
retries: 0
},
states: {
idle: {
on: {
FETCH: 'loading'
@karlguillotte
karlguillotte / machine.js
Last active August 30, 2021 06:08
Generated by XState Viz: https://xstate.js.org/viz
const appStateMachine = Machine({
id: 'app-state',
initial: 'setup',
states: {
setup: {
invoke: {
id: 'listen',
src: 'setup'
},
},
@karlguillotte
karlguillotte / machine.js
Last active August 30, 2021 07:22
Generated by XState Viz: https://xstate.js.org/viz
const agreementMachine = Machine({
id: 'agreement',
initial: 'unknown',
context: {
terms: null, // Terms of use document
status: null, // 'accepted' or perhaps 'declined'
timestamp: null
},
states: {
unknown: {
@karlguillotte
karlguillotte / cache.js
Created August 27, 2021 20:59 — forked from lesleh/cache.js
Basic memory cache implementation for JavaScript.
function Cache(config) {
config = config || {};
config.trim = config.trim || 600;
config.ttl = config.ttl || 3600;
var data = {};
var self = this;
var now = function() {
return new Date().getTime() / 1000;
const fileMachine = Machine({
id: 'file',
type: 'parallel',
states: {
upload: {
initial: 'idle',
states: {
idle: {
on: {
INIT_UPLOAD: 'pending'
const path = 'https://assets.avalanche.ca/images/'
const iconURLs = new Map([
['fall', PATH + 'early_season_icon.svg'],
['spring', PATH + 'spring_situation_icon.svg'],
['summer', PATH + 'summer_conditions_icon.svg'],
])
const src = IconURLs.get(payload.season.value) // payload is https://api.avalanche.ca/forecasts/fr/products/chic-chocs
<img src={src} alt='' />
async function main(lang) {
const name = await getSponsorName()
return await getSponsorData(name, lang)
}
async function getSponsorName() {
const date = new Date()
const url = API + '/static/sponsors/' + date.toISOString().substr(0, 10)
const sponsors = await get(url)