Skip to content

Instantly share code, notes, and snippets.

String.prototype.closest = function(find, pos) {
var found = -1;
var cur1,cur2; cur1 = cur2 = pos;
while (cur1 > 0 || cur2 <= this.length) {
is = this.substr(cur1, find.length);
it = this.substr(cur2, find.length);
if (is == find) return cur1;
this is a 10:45:01 test
*
x
* *
x x
*
y
* test position
x/y positive / negative result
String.prototype.closest2 = function(find, pos) {
var cur = 0;
while (cur <= this.length) {
if (pos + cur <= this.length) var is = this.substr(pos + cur, find.length);
if (pos - cur > 0) var it = this.substr(pos - cur, find.length);
if (is == find) return pos + cur;
if (it == find) return pos - cur;
// I have no idea what anyone would want to use this for.
function ask(questions, passcb, failcb) {
var require = [];
var pass = [];
var fail = [];
for (var question in questions) {
if (question.search('required') > -1) {
require.push(question);
var Script = process.binding('evals').Script;
Script.runInNewContext('setTimeout(function() {sys.puts(\'2 seconds\')}, 2000);', {sys: require('sys'), setTimeout: setTimeout});
var sandbox = {setTimeout: setTimeout, callback: function(output) {
require('sys').puts('Output: ' + output);
}};
Script.runInNewContext('setTimeout(function() {callback.call(this, \'2 more seconds\');}, 2000);', sandbox);
var sys = require('sys');
var EventEmitter = require('events').EventEmitter;
var Script = process.binding('evals').Script;
function EventScript(code, filename) {
this.code = code;
this.filename = filename;
};
sys.inherits(EventScript, EventEmitter);
@robinduckett
robinduckett / jsdom.insert.jquery.js
Created August 23, 2011 16:21
This will insert HTML into a DOM using jsdom
var jsdom = require('jsdom');
var request = require('request');
request.get({url: 'http://google.com/', followRedirect: true}, function(err, res, body) {
if (!err) {
if (res.statusCode == 200) {
jsdom.env(body, ['http://code.jquery.com/jquery-1.5.min.js'], function(errors, window) {
$ = window.jQuery;
$('#lga').append('<div>WHAT</div>');
@robinduckett
robinduckett / backbone.compatmodel.js
Created February 9, 2012 14:37
Backbone.CompatModel
Backbone.CompatModel = function(attributes, options) {
var defaults;
attributes || (attributes = {});
if (options && options.parse) attributes = this.parse(attributes);
if (defaults = getValue(this, 'defaults')) {
attributes = _.extend({}, defaults, attributes);
}
if (options && options.collection) this.collection = options.collection;
this.attributes = {};
this._escapedAttributes = {};
@robinduckett
robinduckett / namespace.coffee
Created April 12, 2012 08:33
Namespacing for Node.js
path = require('path')
loader = (ns) ->
ns = ns.toString()
parts = ns.split /\./
requirePath = ns.replace /\./g, '/'
try
requirePath = path.dirname(require.main.filename) + '/' + requirePath
require requirePath
ns = {}
test = 0
class ns._Users
constructor: ->
test++
class ns.Users
_instance = undefined