Skip to content

Instantly share code, notes, and snippets.

@chjj
chjj / regex_fail.js
Created January 24, 2012 20:08
regex failure
var regex = /^ *<\w+(?!:\/|@)\b(?:"[^"]*"|'[^']*'|[^>])*?> *(?:\n{2,}|\s*$)/;
var str = ''
+ '<div class="hero-unit">\n'
+ ' <img src="img/hello_world.png" style="misc: properties;">\n'
+ ' <div>\n'
+ ' <h1>hello world</h1>\n'
+ ' <p>hello world.</p>\n'
+ ' <p><a class="hello world" href="#!hello">hello world</a></p>\n'
+ ' </div>\n'
@chjj
chjj / pocketfm.js
Created December 17, 2011 14:38
pocket-sized file manager/sharing so i dont have to start up samba
/**
* pocketfm
* Pocket-sized File Manager
* Uses plain html to be as simple as possible
* Code is messy, needs revision and needs
* native mv, rm, cp implementations
* Copyright (c) 2011, Christopher Jeffrey (MIT License)
*/
process.title = 'pocketfm';
@chjj
chjj / node_functions.sh
Created November 2, 2011 00:52
node functions for .bashrc
# $ echo "console.log('hello world')" | runjs
runjs() {
cat | xargs -0 node -e
}
# usage:
# $ echo "hello world" | jsrx "/h\w+/g" "hi"
jsrx() {
local str=$(cat | sed "s/\\\/\\\\\\\/g" \
| sed "s/'/\\\'/g" | sed ':a;N;$!ba;s/\r\n\|\n/\\n/g')
@chjj
chjj / frame_extents.c
Created November 1, 2011 08:05
x frame extents
static void
get_frame_extents(Display *dpy, Window w, int *top) {
long *extents;
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned char *data = NULL;
int result;
*top = 0;
@chjj
chjj / archive
Created October 8, 2011 17:41
script for people who dont like file managers
#!/bin/sh
# easy manipulation of archives for people
# who don't like archivers or file managers
# copyright (c) 2011, christopher jeffrey
LIST=
EXTRACT=
CREATE=
ENCRYPT=
@chjj
chjj / good_randomness.js
Created August 27, 2011 16:57
good randomness
// small script i wrote to get strong randomness in node
// putting it here so i dont lose it
// copyright (c) 2011, christopher jeffrey, mit license, etc.
// example output:
// urandom: 0.17843490773309834
// Math.random: 0.7067500613629818
// random: 0.3154916005012327
@chjj
chjj / two_paragraphs.js
Created August 18, 2011 22:46
two paragraphs
var fs = require('fs')
, marked = require('marked');
var text = fs.readFileSync(__dirname + '/test.md', 'utf8');
var two = function(str) {
var tokens = marked.lexer(str)
, paragraphs = 0
, i = 0
, token;
@chjj
chjj / coffeescript.poem
Created August 15, 2011 23:09
a poem about coffeescript
a poem about coffeescript in the style of the lord of the rings:
one does not simply walk into coffeescript
its syntax is guarded by more than just ruby devs
and the significant whitespace is ever present
it is a wasteland barren of curly braces
- riddled with class syntax, postfix conditionals, and operator aliases
the very code you write, is a poisonous fume
not with 10,000 men who knew javascript could you do this
@chjj
chjj / slice_args.js
Created August 9, 2011 01:46
slice arguments
(function() {
var bench = function(func, t) {
var start = new Date()
, i = t || 100000;
while (i--) func(1, 2, 3);
console.log('%s: %sms', func.name, new Date() - start);
};
var slice = Array.prototype.slice;
bench(function _slice() {
@chjj
chjj / fastuint32.js
Created June 27, 2011 10:58
uint32write
// ignore the shims
// the original (slow) version
var Buffer1 = (function() {
var assert = {};
var fail = function() { throw new Error('assertion failed'); };
var Buffer = function() { this.length = 100; };
Buffer.prototype.writeUInt32 = function(value, offset, endian) {
var buffer = this;