Skip to content

Instantly share code, notes, and snippets.

@andrew8088
andrew8088 / localStateRedux.js
Created March 15, 2016 18:49
A simple way to save your Redux state in localStorage.
let propsToSave = [];
export const setProps = (props) => propsToSave = props;
export const getLocalStore = () => JSON.parse(localStorage.getItem('state'));
export const setLocalStore = (store) => {
var toSave = {};
var state = store.getState();
propsToSave.forEach(p => toSave[p] = state[p]);
localStorage.setItem('state', JSON.stringify(toSave));
};
@andrew8088
andrew8088 / terminalCommandTest.js
Created July 29, 2015 18:00
How to test a terminal command.
var test = require('tape');
var execSync = require('child_process').execSync;
test('ls', function (t) {
var output = execSync('ls').toString();
t.equal(output, 'node_modules\ntest.js\n');
t.end();
});
@andrew8088
andrew8088 / users.js
Created July 27, 2015 21:59
Are these good unit tests? I've been lazy about doing good unit testing, but I'm trying to change that.
'use strict';
var db = require('mongoose').connection;
var credential = require('credential');
var User = require('./models/user'); // the mongoose model
exports.createUser = function createUser(userAttrs, callback) {
db.once('open', function () {
User.findOne({ username: userAttrs.username }, 'username', function (err, person) {
if (err) return callback(err);
if (person) {
@andrew8088
andrew8088 / chirps.js
Created June 15, 2015 20:17
The Flux store creator that I'm currently using (with Facebook's own flux repo, which includes just a dispatcher). `chirps.js` is an example of it in action.
var constants = require('../constants');
var ChirpStore = module.exports = require('./store').extend({
init: function () {
this.bind(constants.RECEIVE_CHIRPS, this.loadChirps);
this.bind(constants.CREATED_CHIRP, this.loadChirps);
},
loadChirps: function (data) {
data = ({}).toString.call(data).indexOf('Array') > 0 ? data : [data];
data.forEach(ChirpStore.add.bind(ChirpStore));
@andrew8088
andrew8088 / server.js
Last active August 29, 2015 14:08
A Node Problem
// This will hang, and you'll never see "done" because it doesn't drain the `res` Readable Stream.
var http = require('http');
var server = http.createServer(function (req, res) {
res.writeHead(404);
res.end();
});
server.listen(3000, function () {
http.get('http://localhost:3000/', function (res) {
@andrew8088
andrew8088 / stringify.js
Last active August 23, 2022 07:54
A simple implementation of JSON.stringify; covers every case I could think of
function stringify(obj) {
if (typeof obj !== 'object' || obj === null || obj instanceof Array) {
return value(obj);
}
return '{' + Object.keys(obj).map(function (k) {
return (typeof obj[k] === 'function') ? null : '"' + k + '":' + value(obj[k]);
}).filter(function (i) { return i; }) + '}';
}
@andrew8088
andrew8088 / words.json
Created March 18, 2014 19:49
Word Game Data
[{"id":1,"level":3,"word":"anguine","definition":"snakelike"},
{"id":2, "level":1,"word":"cardinal","definition":"of fundamental importance"},
{"id":3, "level":3,"word":"detersion","definition":"act of cleansing"},
{"id":4, "level":3,"word":"exiguous","definition":"meager"},
{"id":5, "level":2,"word":"fraternise","definition":"associate with"},
{"id":6, "level":2,"word":"masticate","definition":"chew"},
{"id":7, "level":2,"word":"prognosticate","definition":"predict"},
{"id":8, "level":2,"word":"abscond","definition":"leave hurriedly"},
{"id":9, "level":2,"word":"accoutrements","definition":"personal clothing and accessories"},
{"id":10,"level":1,"word":"dollop","definition":"shapeless mass"},
@andrew8088
andrew8088 / fizzbuzz.befunge
Created December 24, 2012 16:31
FizzBuzz in Befunge.
1v >"zzuBzziF" ,,,,,,,,$v
>>::35*%!| >"zzuB" ,,,,$v
>:5%!| >"zziF",,,,$v
|%+1*:*25:+1< >:3%!|
@ .
^ ,*25< <
@andrew8088
andrew8088 / gist:4360639
Created December 22, 2012 19:37
Having some fun with the Befunge programming language. This is a simple looping coin toss program.
> v
v,,,,,,,,,,,,,,,,,,,,"Welcome to Coin Toss"<
>52*, v
v"Type H or T to choose heads or tails:"<
>,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,v
v `*8*52~ <
v ? v
v_v ^ v_v
> v <
v <
@andrew8088
andrew8088 / detectives.sql
Created July 6, 2012 01:41
Intrepid Detectives database for the SQL Essentials course for Tuts+ Premium
CREATE DATABASE intrepid_detectives;
USE intrepid_detectives;
CREATE TABLE detectives(
id INTEGER NOT NULL AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50) NOT NULL,
phone_number VARCHAR(10) NOT NULL,
certification_date DATE NOT NULL,
CONSTRAINT detectives_pk PRIMARY KEY (id)