Skip to content

Instantly share code, notes, and snippets.

View BretCameron's full-sized avatar
🏠
Working from home

Bret Cameron BretCameron

🏠
Working from home
  • YuLife
  • London
View GitHub Profile
@BretCameron
BretCameron / frontmatter.plugin.mjs
Created September 16, 2024 11:11
A custom Remark plugin to create a super minimal MDX writing
// src/lib/frontmatter.plugin.mjs
import yaml from "js-yaml";
import { valueToEstree } from "estree-util-value-to-estree";
// Helper function to traverse nodes recursively
const traverse = (node, callback) => {
callback(node);
if ("children" in node && Array.isArray(node.children)) {
node.children.forEach((child) => traverse(child, callback));
@BretCameron
BretCameron / promise-all-overflow-1.js
Created April 15, 2020 08:16
A snippet from V8's unit tests, taken for demonstration purposes
// Original source: v8/test/mjsunit/es6/promise-all-overflow-1.js
// https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/test/mjsunit/es6/promise-all-overflow-1.js#L9-L12
// Make sure we properly throw a RangeError when overflowing the maximum
// number of elements for Promise.all, which is capped at 2^21 bits right
// now, since we store the indices as identity hash on the resolve element
// closures.
const a = new Array(2 ** 21 - 1);
const p = Promise.resolve(1);
@BretCameron
BretCameron / polyfill.js
Created April 12, 2020 13:27
MDN's suggested polyfill for Object.assign
if (typeof Object.assign !== 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, "assign", {
value: function assign(target, varArgs) { // .length of function is 2
'use strict';
if (target === null || target === undefined) {
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
@BretCameron
BretCameron / server.ts
Created November 24, 2019 16:40
Boilerplate to start an Express server and connect to MongoDB using Mongoose
import * as express from "express";
import * as mongoose from "mongoose";
const app = express();
const uri = "mongodb://localhost:27017/test"; // replace with your URI
mongoose
.connect(uri, {
useNewUrlParser: true,
@BretCameron
BretCameron / App.css
Created November 15, 2019 18:00
Some minimal CSS to style the typescript counter examples
* {
text-align: center;
}
button {
padding: 10px 20px;
font-size: 1.5rem;
}
.flex {
@BretCameron
BretCameron / App.tsx
Created November 15, 2019 17:59
The parent component for the counter Typescript examples
import React, { FC } from "react";
import "./App.css";
import FunctionalCounter from "./components/FunctionalCounter";
import ClassCounter from "./components/ClassCounter";
const App: FC = (): JSX.Element => {
return (
<>
<div className="flex">
@BretCameron
BretCameron / FunctionalCounter.tsx
Created November 15, 2019 17:58
A simple counter, made using React functional components and typescript
import React, { FC, useState } from "react";
interface Props {
title: string;
initialCount: number;
}
const FunctionalCounter: FC<Props> = ({ title, initialCount }) => {
const [count, setCount] = useState(initialCount);
@BretCameron
BretCameron / ClassCounter.tsx
Created November 15, 2019 17:58
A simple counter, made using React classes and typescript
import React, { Component } from "react";
interface Props {
title: string;
initialCount: number;
}
interface State {
count: number;
}
@BretCameron
BretCameron / App.tsx
Created November 4, 2019 22:01
A React component with a very simple implementation of infinite scroll (written in Typescript)
import React from 'react';
import './App.css';
const debounce = (fn: Function, time: number) => {
let timeout: NodeJS.Timer;
return function(...args: any[]) {
const functionCall = function(this: any) {
fn.apply(this, args);
clearTimeout(timeout);
};
@BretCameron
BretCameron / container.js
Created October 15, 2019 20:46
An example container used for functional programming, to help separate impure functions from pure logic.
const isFunction = fn => fn && Object.prototype.toString.call(fn) === '[object Function]';
const isAsync = fn => fn && Object.prototype.toString.call(fn) === '[object AsyncFunction]';
const isPromise = p => p && Object.prototype.toString.call(p) === '[object Promise]';
const tap = f => x => {
f(x);
return x;
};