Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / generate-request-id.js
Last active October 28, 2019 13:58
Generate Request Id
const now = () => {
const [sec, nano] = process.hrtime();
return sec * 1e9 + nano;
};
const rand = () => Math.floor(Math.random() * 1e16);
const getRequestId = () =>
(now() + rand())
.toString(16)
@andrew8088
andrew8088 / isProperlyNested.js
Last active April 20, 2021 15:47
isProperlyNested interview question
function isProperlyNested(input) {
}
test("{}", true);
test("(()", false);
test("()[]", true);
test("(([{]))", false);
test("[(())]{}()", true);
test("[(()){}()", false);
@andrew8088
andrew8088 / full-stack-assignment.md
Last active April 22, 2021 13:22
Full Stack Assignment

Task

Create a simple web application to fetch and display weather information.

Requirements

Below are the requirements for this mini-project. Beyond these requirements, you can do whatever you'd like! Please push your final application to GitHub, and include a README.md that gives instructions on how to run it.

You can spend as much or as little time as you'd like on this project. Feel free to put more time/effort into the parts that will show off your skills the best!

@andrew8088
andrew8088 / systems-design-assignment.md
Last active May 4, 2021 19:54
Systems Design Assignment

Summary

The university has decided to invest in a revamp of their library’s digital service, moving it from a legacy system that’s hosted on premise, to a modern, cloud-native system that’s built atop a Platform as a Service like AWS.

Create a searchable system for storing the library’s catalogue, that library members can use to find and check out books.

Requirements

Must Have

  • Store the library’s catalogue of books
@andrew8088
andrew8088 / number-map.js
Created October 14, 2021 21:53
Romain Numeral Addition
const map = {
1: "I",
2: "II",
3: "III",
4: "IV",
5: "V",
6: "VI",
7: "VII",
8: "VIII",
9: "IX",