Skip to content

Instantly share code, notes, and snippets.

@dlmanning
dlmanning / watch.js
Last active December 23, 2015 09:59
Possible regression between 0.10.16 and 0.10.17 related to libuv patch
Running watch.js on node 0.10.17 and OS X 10.8.5 gives the following when editing (in Sublime Text) and saving file in the same directory:
change : a.txt
rename : .subl319.tmp
change : a.txt
rename : .subl69d.tmp
etc...
Doing the same on 0.10.18 gives:
@dlmanning
dlmanning / addressbook.js
Created November 7, 2013 03:22
Sample code for a PCS exercise
var fs = require('fs');
var read = require('read');
var myBook = [];
var data = fs.readFileSync(__dirname + '/data.json', { encoding: 'utf8', flag: 'a+' });
if (data) {
myBook = JSON.parse(data);
}
@dlmanning
dlmanning / ifFilesExist.js
Created November 12, 2013 03:29
A function to asynchronously confirm the existence of every file in an array.
function ifFilesExist (files, cb) {
var numberOfFiles = files.length
, returnsChecked = 0
, existancesConfirmed = 0;
for (var i = 0; i < numberOfFiles; i++) {
fs.exists(files[i], next);
}
function next (e) {
@dlmanning
dlmanning / gist:7741148
Created December 1, 2013 21:35
Solutions to one of the javascript koans
it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (imperative)", function () {
var i,j,hasMushrooms, productsICanEat = [];
for (i = 0; i < products.length; i+=1) {
if (products[i].containsNuts === false) {
hasMushrooms = false;
for (j = 0; j < products[i].ingredients.length; j+=1) {
if (products[i].ingredients[j] === "mushrooms") {
hasMushrooms = true;
{
"name": "knode.github.io",
"version": "0.0.1",
"description": "knode.github.io ===============",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"postupdate": "echo \"Updated...\" && exit 0"
},
"repository": {
requirejs.config({
baseUrl: '/js',
paths: {
'backbone': 'lib/backbone',
'underscore': 'lib/underscore',
'jquery': 'lib/jquery',
'hbs': 'lib/require-handlebars-plugin/hbs',
'templates': '../templates'
}
});
@dlmanning
dlmanning / node-forecast-proxy.js
Last active August 29, 2015 13:57
simple api proxy that queues requests when cached data is out of date.
var request = require('request');
var moment = require('moment');
var http = require('http');
var colors = require('colors');
var latLong = "45.5330,-122.6894";
var apiKey = "a75c248d7b83806b66b281dd33e96e36";
var url = 'https://api.forecast.io/forecast';
url += '/' + apiKey + '/' + latLong;
var { Transform } = require('stream'); // use require for non-ES6 Node modules
class MyStream extends Transform {
constructor (options) {
super({
lowWaterMark: 0,
encoding: 'utf8'
});
}
@dlmanning
dlmanning / vanishing-closure.js
Created January 22, 2015 04:06
The mysterious case of the vanishing outer closure
zip();
zap();
zoom();
function zip () {
var a = 1;
(function () {
// v8 will remove the outer closure
debugger;
@dlmanning
dlmanning / index.html
Last active August 29, 2015 14:15
d3 minimap with draggable minimap window
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>minimap</title>
<style>
#ref {
display: none;
}
</style>