Skip to content

Instantly share code, notes, and snippets.

View JamesMGreene's full-sized avatar

James M. Greene JamesMGreene

View GitHub Profile
@JamesMGreene
JamesMGreene / globalErrorHandlerHookup.js
Created November 29, 2012 21:52
jQuery event handlers to prevent logging after the window goes crazy! =)
(function($, window, undefined) {
// WARNING! Set this as you see fit
var suppressUnhandledErrors = true;
var okToLogErrors = true;
var isOnline = true;
var offlineErrorQueue = [];
var logError = function(err) {
@unscriptable
unscriptable / AMD-factory-with-require.js
Created November 20, 2012 15:11
Simple AMD/node boilerplate. These aren't quite "normal" AMD and aren't "pure" CJS, but work in AMD loaders and node-like environments (like RingoJS).
// Typical AMD factory that returns a value, but uses an r-value (sync) require(),
// rather than a long, awkward dependency list.
// You cannot use module.exports or exports to declare the module:
(function (define){
define(function (require) {
"use strict";
var mod = require('pkb/modA');
return {
@dunkfordyce
dunkfordyce / gist:4065345
Created November 13, 2012 11:38
Convert single line comments in javascript to console.log statements with esprima/falafel/javascript
var falafel = require('falafel');
var src = require('fs').readFileSync(process.argv[2]).toString();
var output = falafel(src, {comment: true}, function (node) {
if (node.type === 'Line') {
node.update('console.log("COMMENT:' + node.value + '");');
}
});
console.log(output);
@domenic
domenic / promises.md
Last active March 31, 2024 14:07
You're Missing the Point of Promises

This article has been given a more permanent home on my blog. Also, since it was first written, the development of the Promises/A+ specification has made the original emphasis on Promises/A seem somewhat outdated.

You're Missing the Point of Promises

Promises are a software abstraction that makes working with asynchronous operations much more pleasant. In the most basic definition, your code will move from continuation-passing style:

getTweetsFor("domenic", function (err, results) {
 // the rest of your code goes here.
@valueof
valueof / notabs.js
Created October 13, 2012 18:12
A reporter that discards all warnings about mixed tabs and errors
/**
* notabs.js
*
* A reporter that discards all warnings about mixed tabs and errors.
* This file was based on our default reporter so output is the same.
*
* Usage:
* jshint myfile.js --reporter=notabs.js
*/
@jlongster
jlongster / gist:3881008
Created October 12, 2012 19:28
js destructuring macro
// Note: there are bugs in hygienic renaming, so this doesn't work all the time yet.
macro varr {
case [$var (,) ...] = $expr => {
var i = 0;
var arr = $expr;
$(var $var = arr[i++];) ...
}
@JamesMGreene
JamesMGreene / getUrlAsLocation.js
Created September 18, 2012 14:20
Break a URL down by utilizing an HTML anchor ("a") element to turn it into a Location object
var getUrlAsLocation = (function() {
var urlPartKeyNames = ["href", "protocol", "host", "hostname", "port", "pathname", "search", "hash"];
return function(url) {
var link = document.createElement("a"),
urlAsLocation = {};
link.href = url;
for (var i = 0, len = urlPartKeyNames.length; i < len; i++) {
var urlPartKey = urlPartKeyNames[i];
urlAsLocation[urlPartKey] = link[urlPartKey];
}
@millermedeiros
millermedeiros / example.html
Created August 28, 2012 13:52
node.js script to inline static includes
<!DOCTYPE html>
<!-- #include "inc_header.html" title="Example" header="Sample Title" -->
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<h1>Sample Title</h1>
@firedfox
firedfox / retrieve-text-files.js
Created August 10, 2012 09:49
phantomjs retrieving text files
const TEXT_PREFIX = /<html><head><\/head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/;
const TEXT_SUFFIX = /<\/pre><\/body><\/html>/;
var page = require('webpage').create();
page.onLoadFinished = function() {
var content = page.content;
if (TEXT_PREFIX.test(content) && TEXT_SUFFIX.test(content)) {
content = content.replace(TEXT_PREFIX, '').replace(TEXT_SUFFIX, '');
};
@ehynds
ehynds / remove-logging.js
Created August 6, 2012 15:16
Grunt task to remove console logging
grunt.registerMultiTask("removelogging", "Remove console logging", function() {
var done = this.async();
var exec = require("child_process").exec;
var options = this.data.options || {};
grunt.file.expandFiles(this.data.files).forEach(function(file) {
var cmd = "sed -E 's/console\.(log|warn|error|assert|count|clear|group|groupEnd|trace|debug|dir|dirxml|profile|profileEnd|time|timeEnd)\((.*)\);?//g' " + file;
exec(cmd, options, function(error, stdout) {
if(error !== null) {