Skip to content

Instantly share code, notes, and snippets.

@monsur
monsur / gist:706839
Created November 19, 2010 17:37
Parses the response from XmlHttpRequest.getAllResponseHeaders() into a key/value pair.
/**
* XmlHttpRequest's getAllResponseHeaders() method returns a string of response
* headers according to the format described here:
* http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders-method
* This method parses that string into a user-friendly key/value pair object.
*/
function parseResponseHeaders(headerStr) {
var headers = {};
if (!headerStr) {
return headers;
@lsmith
lsmith / gist:866445
Created March 11, 2011 19:39
Function to set up a lazy populating property on an object when possible. Getting the property value will do the value calculation, then remove the getter/setter, so it behaves like a normal property. Setting the property assigns the value and removes t
// Note o must be a DOM/COM object for IE8 to work. See below for a feature test for that.
var _define = (Object.defineProperty) ?
function (o, prop, fn) {
function val(v) {
var ret = (arguments.length) ? v : fn.call(this);
// delete needed for IE8
delete o[prop];
Object.defineProperty(o, prop, {
value: ret,
@csanz
csanz / encrypt_decrypt.js
Created August 30, 2011 16:06
Simple String Encryption & Decryption with Node.js
function encrypt(text){
var cipher = crypto.createCipher('aes-256-cbc','d6F3Efeq')
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text){
var decipher = crypto.createDecipher('aes-256-cbc','d6F3Efeq')
var dec = decipher.update(text,'hex','utf8')
@chrisroos
chrisroos / gpg-import-and-export-instructions.md
Created September 9, 2011 10:49
Instructions for exporting/importing (backup/restore) GPG keys

Every so often I have to restore my gpg keys and I'm never sure how best to do it. So, I've spent some time playing around with the various ways to export/import (backup/restore) keys.

Method 1

Backup the public and secret keyrings and trust database

cp ~/.gnupg/pubring.gpg /path/to/backups/
cp ~/.gnupg/secring.gpg /path/to/backups/
cp ~/.gnupg/trustdb.gpg /path/to/backups/

or, instead of backing up trustdb...

@jonathantneal
jonathantneal / matchesSelector.polyfill.js
Created July 6, 2012 21:51
matchesSelector Polyfill // returns whether an element matches a selector
this.Element && function(ElementPrototype) {
ElementPrototype.matchesSelector = ElementPrototype.matchesSelector ||
ElementPrototype.mozMatchesSelector ||
ElementPrototype.msMatchesSelector ||
ElementPrototype.oMatchesSelector ||
ElementPrototype.webkitMatchesSelector ||
function (selector) {
var node = this, nodes = (node.parentNode || node.document).querySelectorAll(selector), i = -1;
while (nodes[++i] && nodes[i] != node);
@dherman
dherman / loader-api.md
Last active January 31, 2019 03:02
Complete ES6 Loader API

Notational Conventions

This section describes the conventions used here to describe type signatures.

A [T] is an array-like value (only ever used read-only in this API), i.e., one with an integer length and whose indexed properties from 0 to length - 1 are of type T.

A type T? should be read as T | undefined -- that is, an optional value that may be undefined.

Loaders

@staltz
staltz / introrx.md
Last active July 15, 2024 15:43
The introduction to Reactive Programming you've been missing
@rotty3000
rotty3000 / code.md
Last active August 29, 2015 14:08
If you hate checked exceptions try this

Hate Checked Exceptions???

Try this little trick borrowed from Netty.

So you have an exception:

  • you don't want to check
  • you don't want to swallow
  • you don't want to pollute the method signature

Consider the following class Throw:

@henvic
henvic / plato-history-importer.sh
Created April 2, 2015 19:39
Plato history importer
#!/bin/bash
TITLE="Project Name"
REPORT_DIR="reports/complexity"
FILES="lib test bin tasks gulpfile.js"
BACK=$1
JSHINTRC=$2
OFFSET=$3
STEPS=$4
@brycebaril
brycebaril / output.md
Last active February 23, 2020 22:35
process.nextTick vs setImmediate

@mafintosh asks: "Does anyone have a good code example of when to use setImmediate instead of nextTick?"

https://twitter.com/mafintosh/status/624590818125352960

The answer is "generally anywhere outside of core".

process.nextTick is barely asynchronous. Flow-wise it is asynchronous, but it will trigger before any other asynchronous events can (timers, io, etc.) and thus can starve the event loop.

In this script I show a starved event loop where I just synchronously block, use nextTick and setImmediate