Skip to content

Instantly share code, notes, and snippets.

View jdalton's full-sized avatar

John-David Dalton jdalton

View GitHub Profile

RegExp.escape(string)

Computes a new version of a String value in which certain characters have been escaped, so that the regular expression engine will interpret any metacharacters that it may contain as character literals.

When the escape function is called with one argument string, the following steps are taken:

  1. Let string be ToString(string).
  2. ReturnIfAbrupt(string).
  3. Let length be the number of characters in string.
  4. Let R be the empty string.
var assert = require('assert')
, subclass = function(type, proto){
// make sure we are extending a global constructor
// accepted: [Boolean, Number, String, Array, Object, Function, RegExp, Date]
if(!global[type.name] || global[type.name].name != type.name) throw new Error();
var constructor = proto.constructor,
keys = Object.keys(proto),
Obj = process.binding('evals').Script.runInNewContext('x = '+type.name), // eval ftw
@csnover
csnover / README.md
Created February 21, 2011 01:11
Gyazo script
  1. Change $path and $uri as necessary in gyazo.php and upload it to your server
  2. Change HOST and CGI as necessary in script and use it to replace Gyazo.app/Contents/Resources/script (right-click -> Show Package Contents to get there)
  3. Continue along as usual
js> isNaN(null);
false
js> null > -1;
true
js> null < 1;
true
js> null > 0;
false
js> null < 0;
false
@devongovett
devongovett / gist:1037265
Created June 21, 2011 04:47
jsondb + msgpack
var arr = [],
obj = {'abcdef' : 1, 'qqq' : 13, '19' : [1, 2, 3, 4]};
for(var i = 0; i < 5000; i++)
arr.push(obj);
// jsondb(arr) looks something like this:
// ["abcdef", "qqq", "19", [1, 13, [1, 2, 3, 4]], [1, 13, [1, 2, 3, 4]]...]
> JSON.stringify(arr).length //original
#!/usr/bin/env bash
cat $1 | R -rR --slurp \
'unlines' \
'to-lower' \
'match /\w+/g' \
'lodash.uniq' \
'sort-by length' \
'reverse' \
'take 10'
@davidaurelio
davidaurelio / hastouch.html
Created September 6, 2011 17:10
Checking for touch support
<!doctype html>
<title>has touch?</title>
<h1>Has this device touch? </h1>
<script>
var isTouch = (function(){
try{
var event = document.createEvent("TouchEvent"); // Should throw an error if not supported
return !!event.initTouchEvent; // Check for existance of initialization method
}catch(error){
return false;
@ralphholzmann
ralphholzmann / _with.js
Created April 25, 2012 19:35
JavaScript with-like function
var obj = {
"ralph" : 1,
"jon" : 2,
"alex" : 3
};
function _with( fn, data ) {
var keys = Object.keys( data ),
values = keys.map(function( key ) {
@bobrik
bobrik / benchmark.js
Created July 11, 2012 10:12
lodash objectDifference implementation and benchmark code
(function(module) {
var Benchmark = require('../vendor/benchmark.js/benchmark.js'),
_ = require('../vendor/underscore/underscore.js'),
lodash = require('../lodash.js');
var numbers = [],
fourNumbers = [5, 25, 10, 30],
twoNumbers = [12, 21],
i;
(function () {
var textarea;
function createTextArea() {
if (textarea) {
return textarea;
}
textarea = document.createElement('textarea');
textarea.style.position = 'absolute';
textarea.style.left = '-9999px';
textarea.style.top = (window.pageYOffset || document.documentElement.scrollTop) + 'px';