Skip to content

Instantly share code, notes, and snippets.

@dlmanning
dlmanning / watch.js
Last active Dec 23, 2015
Possible regression between 0.10.16 and 0.10.17 related to libuv patch
View watch.js
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 Nov 7, 2013
Sample code for a PCS exercise
View addressbook.js
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 Nov 12, 2013
A function to asynchronously confirm the existence of every file in an array.
View ifFilesExist.js
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 Dec 1, 2013
Solutions to one of the javascript koans
View gist:7741148
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;
View package.json
{
"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": {
View main.js
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 Aug 29, 2015
simple api proxy that queues requests when cached data is out of date.
View node-forecast-proxy.js
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;
View stream-class.js
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 Jan 22, 2015
The mysterious case of the vanishing outer closure
View vanishing-closure.js
zip();
zap();
zoom();
function zip () {
var a = 1;
(function () {
// v8 will remove the outer closure
debugger;
@dlmanning
dlmanning / index.html
Last active Aug 29, 2015
d3 minimap with draggable minimap window
View index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>minimap</title>
<style>
#ref {
display: none;
}
</style>