Skip to content

Instantly share code, notes, and snippets.

View jimthedev's full-sized avatar

Jim Cummins jimthedev

View GitHub Profile
@jimthedev
jimthedev / .babelrc
Last active January 30, 2017 08:59
A modern react native stack
{
"presets": ["react-native"],
"plugins": ["transform-decorators-legacy"]
}
@jimthedev
jimthedev / snippet.js
Created January 14, 2017 15:36
Sequelize database seed function
if(process.env.NODE_ENV !== 'production') {
sequelize.sync({force: true}).then(() => {
// Some sample projects
var projects = {
data: [
{
name: 'Kitchen',
}, {
name: 'Bathroom',
@jimthedev
jimthedev / schema-dirty-check.js
Last active January 7, 2017 17:29
quick and dirty schema dirty checking for undeleteable websql databases
import myDatabaseSchema from './myDatabaseSchema';
import runDBMigrations from './myMigrations';
const schemaHash = hash(myDatabaseSchema);
const lastSchemaHash = localStorage.getItem('lastSchemaHash');
if(lastSchemaHash !== null && lastSchemaHash !== schemaHash) {
localStorage.setItem('lashSchemaHash', schemaHash);
console.log('schema changed! the world was deleted!');
runDBMigrations(lastSchemaHash, schemaHash);
@jimthedev
jimthedev / AudioPlayerDOM.js
Created January 2, 2017 19:56
Audio Player for React DOM with dynamic sources
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
/**
* Use case: audio player with dynamic source in react
*
* Usage:
* <AudioPlayerDOM src={this.state.currentFile}/>
*
* Todo: make a better api, actually pass props through
* Todo: use children instead of passing
@jimthedev
jimthedev / consts.js
Created December 1, 2016 15:34
When are consts actually immutable?
// TURNS OUT NOT ALWAYS
const name = "Jim";
name = "James"; // error cannot mutate constants
const age = 1;
age = 2; // Error
const active = true;
active = false // error
@jimthedev
jimthedev / testcafe.md
Created November 15, 2016 14:56
Getting started with TestCafe Browser Testing

In Terminal, execute these to create an empty project:

  mkdir myproject
  cd myproject
  git init
  npm init --yes
  npm install -g testcafe
  npm install --save-dev chai
@jimthedev
jimthedev / using-push-only.js
Created November 1, 2016 15:59
The dangers of Array.prototype.push without splicing first
// THIS ONE HAS UNEXPECTED RESULTS!
// BILL AND JIMS GARAGES ARE THE SAME! WHAT?!
var billsGarage = [];
// Someone stole a car, store it in the garage
function storeCarInGarage(myGarage, car) {
// At the end of the day, the car that was stolen
// should be stored in my garage
@jimthedev
jimthedev / index.js
Created October 26, 2016 17:15
Get a string from a numerator and denomentator
function getFractionString(numerator, denomenator) {
// if we can divide the numerator and the denomenator
// by the highest possible number without a remainder
if ( (numerator % denomenator) === 0 ) {
console.log('denomenator goes into numerator evenly');
return numerator/denomenator;
} else {
console.log('denomenator does not go in evenly');
return Math.floor(numerator/denomenator) + " " + numerator % denomenator + "/" + denomenator
}
@jimthedev
jimthedev / kingmaker.js
Created September 26, 2016 22:02
Given an array, loop and return a winner. Uses a single elimination strategy.
/**
* Find the largest power of two below the given num
*/
function maxPow2Below(num){
return Math.pow(2, Math.floor(Math.log(num)/Math.log(2)));
}
/**
* Create an empty bracket given a number of teams entering the round
* This will prefer acheiving a square bracket by taking remainders