Skip to content

Instantly share code, notes, and snippets.

var source = require('fs').readFileSync(require('path').join(process.cwd(), process.argv[2]), "utf8");
require("./reflect.js").parse(source);
@zaach
zaach / required.js
Created July 24, 2011 02:05
Finds the names of all modules required in the file.
var JSONSelect = require("JSONSelect");
var Reflect = require("reflect");
var ast = Reflect.parse(read(__filename));
var required = JSONSelect.match('.callee:has(:root > .name:val("require")) ~ .arguments :first-child .value', ast);
// ["JSONSelect", "reflect", "fs", "path"]
function read (path) {
return require("fs").readFileSync(require("path").resolve(path), "utf8");
@zaach
zaach / person.js
Created August 14, 2011 03:46
An example from http://yehudakatz.com/2011/08/ adapted to use the <| operator
var fromPrototype = function(prototype, object) {
var newObject = Object.create(prototype);
for (var prop in object) {
if (object.hasOwnProperty(prop)) {
newObject[prop] = object[prop];
}
}
};
@zaach
zaach / 0_new.md
Created January 22, 2012 23:15
New Jison 0.3 features

Some improvements have been made for parser and lexer grammars in Jison 0.3 (demonstrated in the FlooP/BlooP example below.)

For lexers:

  • Patterns may use unquoted characters instead of strings
  • Two new options, %options flex case-insensitive
  • flex: the rule with the longest match is used, and no word boundary patterns are added
  • case-insensitive: all patterns are case insensitive
  • User code section is included in the generated module
@zaach
zaach / po2json.js
Created February 4, 2012 19:59
PO parser from http://jsgettext.berlios.de/lib/Gettext.js adapted for Node.js and modified to be more like po2json.pl
#!/usr/bin/env node
/*
PO parser from http://jsgettext.berlios.de/lib/Gettext.js
adapted for Node.js and modified to be more like po2json.pl
- Zach Carter <[email protected]>
*/
/*
Pure Javascript implementation of Uniforum message translation.
error: (signup) Error: connect EMFILE
error: (signup) can't verify: Error: connect EMFILE
error: (signup) can't authenticate: Error: connect EMFILE
error: (signup) Error: connect EMFILE
error: (signup) can't authenticate: Error: connect EMFILE
error: (signup) can't authenticate: Error: connect EMFILE
error: (signup) failed to complete user creation: Error: connect EMFILE
error: (signup) failed to complete user creation: Error: connect EMFILE
error: (signup) can't verify: Error: connect EMFILE
error: (signup) can't verify: Error: connect EMFILE
@zaach
zaach / globals
Created June 6, 2012 21:50
jshint lib/ | grep "not defined"
lib/browserid/fake_verification.js: line 15, col 1, 'logger' is not defined.
lib/browserid/fake_verification.js: line 16, col 1, 'wsapi' is not defined.
lib/browserid/fake_verification.js: line 18, col 1, 'logger' is not defined.
lib/browserid/fake_verification.js: line 19, col 1, 'logger' is not defined.
lib/browserid/fake_verification.js: line 25, col 23, 'wsapi' is not defined.
lib/browserid/fake_verification.js: line 25, col 42, 'resp' is not defined.
lib/browserid/views.js: line 181, col 3, 'REDIRECTS' is not defined.
lib/browserid/views.js: line 192, col 19, 'REDIRECTS' is not defined.
lib/browserid/views.js: line 197, col 13, 'REDIRECTS' is not defined.
lib/db/mysql.js: line 92, col 9, 'dne' is not defined.
@zaach
zaach / qsa
Created June 26, 2012 23:46
gcli command
gcli.addCommand({
name: 'qsa',
description: 'perform querySelectorAll on the current document and return number of matches',
params: [
{
name: 'query',
type: 'string',
description: 'CSS selectors seperated by comma',
}
#!/usr/bin/env node
/*
* This module can verify that packages installed during development are
* identical to those installed during deployment. The standard npm shrinkwrap
* only ensures that package versions are the same, but does not verify contents.
* This module checks the shasum of the package tarballs downloaded by npm during
* development and deployment to ensure they are the same.
*
* Usage:
@zaach
zaach / regex.js
Created July 12, 2012 16:49
origin regex
//var org = /^https?:\/\/(?=.{1,254}(?::|$))(?:(?!\d|-)(?![a-zA-Z0-9\-]{1,62}-\.)[a-zA-Z0-9\-]{1,63}\.)*(?:(?!\d|-)(?![a-zA-Z0-9\-]{1,62}-$)[a-zA-Z0-9\-]{1,63})(:\d+)?$/;
var org = /^https?:\/\/(?=.{1,254}(?::|$))(?:(?!\d|-)(?![a-z0-9\-]{1,62}-(?:\.|:|$))[a-z0-9\-]{1,63}\b(?!\.$)\.?)+(:\d+)?$/i;
/* Origin regex explained
/^ // beginning
https?:\/\/ // starts with http:// or https://
(?=.{1,254}(?::|$)) // the hostname chars must be within 1-254 bytes