Skip to content

Instantly share code, notes, and snippets.

@bfunc
bfunc / interview_test_helpers.ts
Created October 4, 2024 08:12 — forked from IlyaGershman/interview_test_helpers.ts
This is a small set of functions to help you write simple tests without frameworks
type Config = {
logPerformance?: boolean;
timeout?: number; // Timeout in milliseconds
};
function createTestRunner(initialConfig?: Config) {
let globalConfig: Config = {
logPerformance: false,
timeout: 5000,
...initialConfig,
@bfunc
bfunc / machine.js
Created August 24, 2021 15:16
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@bfunc
bfunc / machine.js
Created July 22, 2021 08:08
Generated by XState Viz: https://xstate.js.org/viz
// The Door - state machine
const fetchMachine = Machine({
id: 'TheDoor',
initial: 'locked',
context: {},
states: {
locked:{
id:'locked',
on: { UNLOCK: 'unlocked' }
},
@bfunc
bfunc / machine.js
Created April 25, 2021 16:47
Generated by XState Viz: https://xstate.js.org/viz
const wait = ms => new Promise(r => setTimeout(() => r(), ms))
const fetchResource = async ({ url, fakeDelayMs = 0 }) => {
await wait(fakeDelayMs)
return fetch(url).then(res => res.json())
}
const constants = {
MAX_QUESTION_SCORE: 3,
MAX_RETRIES: 15,
FAILURE_DELAY: 2000,
@bfunc
bfunc / machine.js
Created April 11, 2021 22:56
Generated by XState Viz: https://xstate.js.org/viz
const wait = ms => new Promise(r => setTimeout(() => r(), ms));
const fetchPosts = (context, event) => {
const { q: num } = context;
console.log('> fetchPosts',num)
if (num == 5) {
return new Promise((r, reject) => reject(new Error('Oh too bad...')));
}
@bfunc
bfunc / machine.js
Last active April 14, 2021 22:00
Generated by XState Viz: https://xstate.js.org/viz
const wait = ms => new Promise(r => setTimeout(() => r(), ms))
const fetchResource = async ({ url, fakeDelayMs = 0 }) => {
await wait(fakeDelayMs)
return fetch(url).then(res => res.json())
}
const constants = {
MAX_QUESTION_SCORE: 3,
MAX_RETRIES: 15,
@bfunc
bfunc / machine.js
Last active April 10, 2021 00:12
Generated by XState Viz: https://xstate.js.org/viz
const redditMachine = Machine(
{
id: 'reddit',
initial: 'idle',
context: {
question: 'em',
},
states: {
const lessThan = function(end, value) {
return function(value) {
return value < end;
};
}
// 25 alphabet character letters
const lessThan26 = lessThan(26);
const lazyMap = function* (iterable, callback) {
@bfunc
bfunc / es6-generators.js
Last active August 2, 2019 20:02
observable API for synchronous-style async functions
const isPromise = obj => Boolean(obj) && typeof obj.then === 'function';
const next = (iter, callbacks, prev = undefined) => {
const { onNext, onCompleted } = callbacks;
const item = iter.next(prev);
const value = item.value;
if (item.done) {
return onCompleted();
@bfunc
bfunc / index.js
Created June 4, 2019 12:13
stats.js gist
var stats = new Stats();
document.body.appendChild(stats.dom);
requestAnimationFrame(function loop() {
stats.update();
requestAnimationFrame(loop)
});