Skip to content

Instantly share code, notes, and snippets.

@andrew8088
andrew8088 / mapPromises.js
Created February 15, 2024 14:56
Managing Promise Concurrency in JavaScript
function mapPromises(args, callback, concurrency = 3) {
const { promise, resolve } = Promise.withResolvers();
const results = [];
let cursor = 0;
function next() {
if (cursor < args.length) {
const index = cursor++;
void callback(...args[index]).then(value => {
@andrew8088
andrew8088 / learn-typescript.ts
Created June 3, 2023 22:50
how i learn typescript
type Tables = {
person: {
id: number;
first_name: string;
},
product: {
id: string;
name: string;
createdAt: Date
}
@andrew8088
andrew8088 / tumblr2opml.js
Created November 25, 2010 21:27
You can run this script on tumblr.com/following and you'll get an OPML file for tumblogs on that page print out to the console, ready for you to copy+paste into a file and import into Google Reader.
clear();
var feeds = Array.prototype.slice.call(document.querySelectorAll("div.name a")),
i = 0, p, f, doc = "";
while (feeds[i]) {
f = feeds[i];
feeds[i++] = {
htmlUrl : f.href,
xmlUrl : f.href + 'rss',
title : f.innerHTML,
@andrew8088
andrew8088 / api.test.ts
Created June 6, 2022 00:56
Building your own custom jest matchers
import * as api from './api';
const TOKEN_REGEX = /^app-0.\d+$/;
expect.extend({
toBeValidSession(session: api.Session, userId: string, expectedTTL: number) {
const messages: string[] = [];
const validToken = TOKEN_REGEX.test(session.token);
@andrew8088
andrew8088 / stringify.js
Last active August 23, 2022 07:54
A simple implementation of JSON.stringify; covers every case I could think of
function stringify(obj) {
if (typeof obj !== 'object' || obj === null || obj instanceof Array) {
return value(obj);
}
return '{' + Object.keys(obj).map(function (k) {
return (typeof obj[k] === 'function') ? null : '"' + k + '":' + value(obj[k]);
}).filter(function (i) { return i; }) + '}';
}
@andrew8088
andrew8088 / fizzbuzz.css
Last active July 5, 2022 13:59
FizzBuzz in CSS! Meant to style a bunch of sibling <div>s.
div {
counter-increment: fizzbuzz;
}
div::before {
content: counter(fizzbuzz);
}
div:nth-of-type(3n)::before {
content: 'fizz';
@andrew8088
andrew8088 / Progress.test.tsx
Created May 21, 2022 14:16
Testing Promise Race Conditions with fast-check
import { cleanup, render, screen, fireEvent, act } from '@testing-library/react';
import Progress from './Progress';
import * as fc from 'fast-check';
test('renders learn react link', async () => {
await fc.assert(
fc.asyncProperty(fc.scheduler(), async (s) => {
let number = 0;
const mockGetProgress = s.scheduleFunction(async () => number += 10);
@andrew8088
andrew8088 / number-map.js
Created October 14, 2021 21:53
Romain Numeral Addition
const map = {
1: "I",
2: "II",
3: "III",
4: "IV",
5: "V",
6: "VI",
7: "VII",
8: "VIII",
9: "IX",
@andrew8088
andrew8088 / systems-design-assignment.md
Last active May 4, 2021 19:54
Systems Design Assignment

Summary

The university has decided to invest in a revamp of their library’s digital service, moving it from a legacy system that’s hosted on premise, to a modern, cloud-native system that’s built atop a Platform as a Service like AWS.

Create a searchable system for storing the library’s catalogue, that library members can use to find and check out books.

Requirements

Must Have

  • Store the library’s catalogue of books
@andrew8088
andrew8088 / full-stack-assignment.md
Last active April 22, 2021 13:22
Full Stack Assignment

Task

Create a simple web application to fetch and display weather information.

Requirements

Below are the requirements for this mini-project. Beyond these requirements, you can do whatever you'd like! Please push your final application to GitHub, and include a README.md that gives instructions on how to run it.

You can spend as much or as little time as you'd like on this project. Feel free to put more time/effort into the parts that will show off your skills the best!