Skip to content

Instantly share code, notes, and snippets.

View jhurliman's full-sized avatar
🐨

John Hurliman jhurliman

🐨
View GitHub Profile
@jhurliman
jhurliman / es7-setup.md
Last active January 26, 2016 00:23
ES6/ES7 setup for sandbox development of modern code

These instructions will allow you to write and run one-off node.js scripts using modern ECMAScript syntax, including async/await.

  1. Install Babel globally

npm install -g babel-cli

  1. Add a .babelrc file to the folder holding your code containing the following:

{ "presets": [ "es2015", "stage-3" ] }

@jhurliman
jhurliman / mapAsync.js
Created January 25, 2016 05:06
ES7 implementation of Array.map() and the equivalent for objects
import PromisePool from 'es6-promise-pool'
/**
* Like Array.map() but supports promises and concurrency limiting.
* @param {array} array - Input array to map.
* @param {function} asyncFn(currentValue, index, array) - Called once for each
* value in the array.
* @param {number} [concurrencyLimit] - Optional cap on the number of
* asynchronous methods to execute at once.
* @returns {array} An array containing the mapped output values.
@jhurliman
jhurliman / hackernews-headlines.sh
Created January 15, 2016 20:13
XScreenSaver Apple II Emulator - Hacker News Headlines
curl -silent https://news.ycombinator.com/rss | grep -no '<item><title>[^<]*' | sed 's/<item><title>//'
@jhurliman
jhurliman / hash-buckets.js
Created January 4, 2016 19:25
Demonstration of uniformly allocating integers into a fixed number of hash buckets
import * as math from 'mathjs'
const MINUTES_PER_DAY = 1440
const REFRESH_MINUTES = 1
const HASH_BUCKETS = MINUTES_PER_DAY / REFRESH_MINUTES
const MAX_IDS = 291554
main()
@jhurliman
jhurliman / optimization-test.js
Last active December 13, 2015 20:17
Use V8 internal methods to test if a JS function can be optimized
/**
* Based on code from <https://github.com/petkaantonov/bluebird/wiki/Optimization-killers>
* Run this script using:
* `node --allow-natives-syntax optimization-test.js`
* For more diagnostic output use:
* `node --trace_opt --trace_deopt --allow-natives-syntax optimization-test.js`
*/
function myFunction() {
return 'hello world';
@jhurliman
jhurliman / uuid.js
Created November 22, 2015 04:31
Generate V4 UUIDs in node.js
function uuid() {
var bytes = require('crypto').randomBytes(16);
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
var str = bytes.toString('hex');
return str.substr(0, 8) + '-' +
str.substr(8, 4) + '-' +
str.substr(12, 4) + '-' +
str.substr(16, 4) + '-' +
str.substr(20, 12);
@jhurliman
jhurliman / ec2-instance-info.js
Created November 6, 2015 22:25
Retrieve metadata about the host EC2 instance
var P = require('bluebird');
var request = P.promisify(require('request'));
P.promisifyAll(request);
exports.currentInstanceInfo = currentInstanceInfo;
var INSTANCE_BASE_URL = 'http://169.254.169.254/2014-11-05/';
currentInstanceInfo().then(res => console.dir(res));
@jhurliman
jhurliman / .prompt
Created October 12, 2015 17:16
Bash prompt with git support
# source /usr/local/etc/bash_completion.d/git-prompt.sh
PROMPT_COMMAND=__generate_prompt
__generate_prompt() {
# Undocumented function that updates OS X Terminal\'s knowledge of
# the current
# working directory. Required for Cmd + T to open a new tab in the same
# directory.
#
# See '/etc/bashrc' on an OS X box for details.
@jhurliman
jhurliman / crosswalk2014-excerpt.tsv
Created September 21, 2015 16:31
crosswalk2014-excerpt.tsv
00100300 Faulkner University Faulkner University Yes [YCY] 101189
00100301 Faulkner University Faulkner University - Birmingham Center Yes [YCY] 101189
00100303 Faulkner University Faulkner University - Huntsville Center Yes [YCY] 101189
00100304 Faulkner University Faulkner University - Mobile Center Yes [YCY] 101189
00100305 Faulkner University Faulkner University - No [NNN] 101189
00100306 Faulkner University Faulkner University - No [NNN] 101189
00100307 Faulkner University Faulkner University - No [NNN] 101189
00100308 Faulkner University Faulkner University - No [NNN] 101189
00100309 Faulkner University Faulkner University - No [NNN] 101189
00100310 Faulkner University Faulkner University - No [NNN] 101189
@jhurliman
jhurliman / returnError.js
Created September 14, 2015 01:26
Difference between "return new Error()" and "throw new Error" in bluebird
var P = require('bluebird');
P.resolve().then(function () {
// Change this to throw new Error to get the expected behavior
if (true) return new Error('Returned error');
return { key: { subKey: 'test' } };
}).then(function (obj) {
// This will crash if return new Error is used since the Error is not caught by bluebird
console.log('obj.key.subKey = %s', obj.key.subKey);
}).catch(function (err) {