Skip to content

Instantly share code, notes, and snippets.

@oakfang
oakfang / pr.sh
Created August 9, 2016 08:12
Command line PRs with github templates
# Make sure to export EDITOR in your .bashrc as either vim or nano.
# Make sure to install vipe (npm i -g juliangruber/vipe)
# Make sure to install hub (brew install hub)
# Don't forget to add your PR title as the first block
alias pr="cat .github/PULL_REQUEST_TEMPLATE.md | vipe | hub pull-request -b develop -F -"
const List = require('./list');
const l = new List(50);
// setup
for (let i = 0; i < 50; i++) {
l.set(i, i);
}
console.log(l.get(5) === 5);
const getPrivateContainer = () => {
const privateProperties = new WeakMap();
const getPrivate = (instance, prop) => {
if (!privateProperties.has(instance)) {
privateProperties.set(instance, {});
}
return privateProperties.get(instance)[prop];
};
const setPrivate = (instance, prop, value) => {
if (!privateProperties.has(instance)) {
@oakfang
oakfang / ensure-oid.js
Created April 9, 2017 08:57
A function to ensure object id props of an object
import _ from 'lodash';
import { ObjectId } from 'mongoose';
import ensureObjectId from './utils';
exports default function ensureObjectIdProps(obj, paths) {
paths.forEach(prop => {
const currentValue = _.get(obj, prop);
_.set(obj, prop, currentValue ? ensureObjectId(currentValue) : new ObjectId());
});
return obj;
@oakfang
oakfang / RelativeRoute.jsx
Created April 30, 2018 12:05
Relative Routes with react-router v4
import React from 'react';
import { withRouter, Route } from 'react-router-dom';
const RelativeRoute = ({ match, path, ...props }) => <Route path={`${match.path}${path}`} {...props} />;
export default withRouter(RelativeRoute);
@oakfang
oakfang / compose-mw.js
Created May 28, 2018 11:38
Compose middleware for express
function composeMiddleware(...middleware) {
return (req, res, next) => middleware
.reverse()
.reduce(
(next, mw) =>
() => mw(req, res, next),
next
)();
}
@oakfang
oakfang / App.tsx
Created January 10, 2019 12:45
Typesafe inject
import React, { Component } from "react";
import { observable, action } from "mobx";
import { inject, Provider, observer } from "mobx-react";
import "./App.css";
declare module "mobx-react" {
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type Diff<MasterObject extends SubObject, SubObject extends Object> = Omit<
MasterObject,
alert();
@oakfang
oakfang / blackbox.js
Last active June 6, 2019 07:01
Black box function
function blackBox() {
var input = [0, 1, 2, 3, 4];
var results = [];
for (var i = 0; i < input.length; i++) {
results.push(
new Promise(function(resolve) {
setTimeout(function() {
resolve(i);
}, 200);
})
@oakfang
oakfang / query.jsx
Created December 31, 2019 12:25
Query params sync
import React, {
useState,
useCallback,
useLayoutEffect,
createContext,
useContext,
useMemo,
} from 'react';
import qs from 'qs';
import { useLocation, useHistory } from 'react-router-dom';