Skip to content

Instantly share code, notes, and snippets.

@matthewp
matthewp / converstates.js
Last active December 28, 2015 15:19
This uses the power of Underscore/Lodash to do a simple swap, assuming we have an array of objects each containing two fields, we want the other field.
var states = [
{'name':'Alabama', 'abbrev':'AL'},
{'name':'Alaska', 'abbrev':'AK'},
{'name':'Arizona', 'abbrev':'AZ'},
{'name':'Arkansas', 'abbrev':'AR'},
{'name':'California', 'abbrev':'CA'},
{'name':'Colorado', 'abbrev':'CO'},
{'name':'Connecticut', 'abbrev':'CT'},
{'name':'Delaware', 'abbrev':'DE'},
{'name':'Florida', 'abbrev':'FL'},
@matthewp
matthewp / framework.js
Last active December 30, 2015 07:28
Framework checker - Checks which JS framework a page is using.
console.log('This page is using:',
['can', 'Ember', 'Backbone', 'angular', 'ko', 'Ext', 'Spine', 'Batman', 'React']
.filter(function(framework) { return !!window[framework]; }).join(' and ') || 'nothing'
);
@matthewp
matthewp / gist1.js
Created December 28, 2013 21:30
mygist.js
var foo = 'bar';
@matthewp
matthewp / objectis.js
Last active January 1, 2016 17:09
object.is is a polyfill for the es6 egal function: http://wiki.ecmascript.org/doku.php?id=harmony:egal
module.exports = Object.is || function(x, y){
if (x === y) {
// 0 === -0, but they are not identical
return x !== 0 || 1 / x === 1 / y;
}
// NaN !== NaN, but they are identical.
// NaNs are the only non-reflexive value, i.e., if x !== x,
// then x is a NaN.
// isNaN is broken: it converts its argument to number, so
@matthewp
matthewp / weakmap.js
Created April 10, 2014 18:05
WeakMap and private
var privateThings = new WeakMap();
function Private() {
this.data = 'is private';
}
Private.prototype.somePrivateFunction = function() {
this.foo = 'bar';
};
@matthewp
matthewp / sanity.html
Created April 24, 2014 18:53
Sanity check
<html>
<head>
<title>Sanity check</title>
</head>
<body>
<script>
window.runLater = function() {
console.log("Calling runLater");
window.setTimeout(function() {
@matthewp
matthewp / script.js
Created May 26, 2014 21:11
Phantom + Traceur
var page = require('webpage').create();
page.onError = function(msg, trace){
console.log(msg, trace);
};
page.open('http://localhost/phantom_traceur/test.html', function() {
phantom.exit();
});
exports.translate = function(load) {
load.source = 'module.exports = ' + load.source + ';';
};
@matthewp
matthewp / proxy.js
Created September 30, 2014 01:36
Simple proxy in node
var httpProxy = require('http-proxy');
var http = require('http');
var proxy = httpProxy.createProxyServer({});
var server = http.createServer(function(req, res) {
var url = req.url;
console.log(url);
proxy.web(req, res, { target: url });
});
@matthewp
matthewp / metadeps.js
Created November 25, 2014 18:09
meta deps extension
var instantiate = loader.instantiate;
loader.instantiate = function(load){
var loader = this;
var meta = loader.meta[load.name];
var deps = (meta && meta.deps) || [];
if(deps.length) {
var promises = [];
for(var i = 0, len = deps.length; i < len; i++) {
promises.push(loader.import(deps[i]));