Skip to content

Instantly share code, notes, and snippets.

View cmmartin's full-sized avatar

Charlie Martin cmmartin

  • Founderpath
  • Brooklyn, NY
View GitHub Profile
@cmmartin
cmmartin / BufferStream.js
Last active November 17, 2015 05:54
Convert a stream to a Buffer in Node.js
import { Writable } from 'stream'
/**
* Create a Buffer from a stream (warning: loads all chunks into memory)
* Usage: myStream.pipe(new BufferStream(buffer => {}, err => {}))
*/
export default class BufferStream extends Writable {
data = []
@cmmartin
cmmartin / MusicalNotes.swift
Created December 11, 2015 04:23
Musical notes mapped to their frequencies in Hz
let musicalNotes = [
// name + octave: frequency (Hz)
"C0": 16.35,
"C#0": 17.32,
"D0": 18.35,
"D#0": 19.45,
"E0": 20.60,
"F0": 21.83,
"F#0": 23.12,
"G0": 24.50,
@cmmartin
cmmartin / VibrantNavigationBar.swift
Created January 22, 2016 03:34
Apple a UIVibrancyEffect to an iOS navigation bar
override func viewWillAppear(animated: Bool) {
// Make the bar actually transparent by setting an empty image as the background
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true
var bounds = (self.navigationController?.navigationBar.bounds)!
// make it cover the status bar by offseting y by -20
bounds.offsetInPlace(dx: 0.0, dy: -20.0)
bounds.size.height = bounds.height + 20.0
@cmmartin
cmmartin / BlurredBackgroundView.swift
Created January 22, 2016 03:37
UIVisualEffectView with a background image
import UIKit
class BlurredBackgroundView: UIView {
let imageView: UIImageView
let blurView: UIVisualEffectView
override init(frame: CGRect) {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
blurView = UIVisualEffectView(effect: blurEffect)
imageView = UIImageView(image: UIImage(name: "someBackgroundImage.jpg"))
@cmmartin
cmmartin / unfuck-cors.sh
Last active April 18, 2016 21:19
Open Chrome on OSX with web security disabled to avoid CORS shenanigans in development
open -a Google\ Chrome --args --disable-web-security --user-data-dir
@cmmartin
cmmartin / auth.conf
Last active February 10, 2016 16:04
Nginx http auth with htpasswd
auth_basic "Restricted";
auth_basic_user_file /home/dokku/$APP/nginx.conf.d/.htpasswd;
@cmmartin
cmmartin / Example-BouncingText.js
Last active October 17, 2021 11:13
React Native HOC for easily adding spring animations to your components
/*
* EXAMPLE USAGE
* This component is text that will bounce on mount and
* every time `this.props.someProperty` changes.
* Too bad react native doesn't support decorators yet :/
*/
import React, {
Component,
StyleSheet,
@cmmartin
cmmartin / write-file-promise.js
Created May 25, 2017 19:44
Write a file in Node.js, creating its path if necessary. Returns a promise.
const fs = require('fs')
const path = require('path')
const mkpath = require('mkpath')
module.exports = function writeFilePromise(fileName, contents) {
return new Promise((resolve, reject) => {
mkpath(path.dirname(fileName), err => {
if (err) reject(err)
else {
fs.writeFile(fileName, contents, err => {
@cmmartin
cmmartin / read-file-promise.js
Last active May 25, 2017 20:02
Read a file in Node.js. Returns a promise.
const fs = require('fs')
module.exports = function readFilePromise(path, encoding) {
return new Promise((resolve, reject) => {
try {
var filename = require.resolve(path)
fs.readFile(filename, encoding || 'utf8', (err, file) => {
if (err) reject(err)
else resolve(file)
})
@cmmartin
cmmartin / react16-portal.js
Last active December 12, 2017 22:33
An example of how simple it is to create a portal in React v16+
// @flow
/* eslint-env browser */
import * as React from 'react'
import { createPortal } from 'react-dom'
type Props = {
children: React.Node,
}
export default class Portal extends React.Component<Props> {