Skip to content

Instantly share code, notes, and snippets.

@markmur
markmur / binary-tree-oop.js
Last active June 10, 2019 21:40
Binary Search Tree Implementation
class Node {
constructor(value, left = null, right = null) {
this.value = value;
this.left = left;
this.right = right;
}
}
class BinarySearchTree {
constructor() {
@markmur
markmur / sequential.js
Last active February 16, 2020 14:49
Execute async functions in series
/**
* Execute an array of async functions in series
*/
const sequential = async fns => {
const AsyncFunction = (async () => {}).constructor
// Use "some" rather than "every" here to abort faster
if (!fns.some(fn => fn instanceof AsyncFunction)) {
throw new TypeError('`fns` argument should be an array of Async functions')
}
@markmur
markmur / settings.json
Created April 15, 2019 12:23
Set prettier as default linter in vscode (CMD + Shift + P - "Preferences: Open Settings (JSON)")
{
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
time_namelookup: %{time_namelookup}\n
time_connect: %{time_connect}\n
time_appconnect: %{time_appconnect}\n
time_pretransfer: %{time_pretransfer}\n
time_redirect: %{time_redirect}\n
time_starttransfer: %{time_starttransfer}\n
----------\n
time_total: %{time_total}\n
@markmur
markmur / custom-express-responses.js
Created October 17, 2018 19:36
Custom Express Responses
/**
* Create custom response
* @param {Object} res - express response object
* @param {Number} status - status code
* @param {String} defaultMessage - default response
* @returns {Function} returns function which access response message
*/
const createResponse = (res, status, defaultMessage) => data => {
// Set status code
res.status(status)
@markmur
markmur / cmds.sh
Created September 10, 2018 14:42
Handy Terminal Commands
# 'cat' after first line
cat {filename} | tail -n+2
# (CSV) Get every value of first column (separated by comma)
cat {filename} | awk -F',' '{printf $1","}'
# (CSV) Get every value of first column (one per line)
cat {filename} | awk -F',' '{print $1}'
# Only print first x lines (4 in this case)
@markmur
markmur / index.js
Last active August 17, 2018 16:05
Express server
// Tell "now" to look for a now-secrets.json file
// (where we'll store our local secrets - NOT production secrets)
require('now-env')
const path = require('path')
const express = require('express')
const session = require('express-session')
const bodyParser = require('body-parser')
const morgan = require('morgan') // optional
const passport = require('passport')
@markmur
markmur / now.json
Last active August 17, 2018 11:45
Now: deploy, alias latest and remove old deployments
{
"name": "app-name",
"alias": "app-name.now.sh"
}
@markmur
markmur / Container.js
Last active July 18, 2018 21:45
Provide Firebase store data + subscription method to child routes
import React, { Component } from 'react'
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'
import FirebaseProvider, { Consumer as FirebaseConsumer } from './FirebaseProvider'
class Container extends Component {
render() {
return (
<Router>
<FirebaseProvider>
<FirebaseConsumer>
@markmur
markmur / get.js
Created July 16, 2018 13:27
Lookup a deeply nested object property by dot.notation
/**
* Lookup an object property by dot notation
* @param {Object} obj - object to perform lookup
* @param {String} key - property location
* @param {Any} fallback - fallback if not found
* @return {Any} returns value of lookup if found, otherwise undefined
*/
const get = (obj, key, fallback) =>
key
.split('.')