Skip to content

Instantly share code, notes, and snippets.

@lsm
lsm / base.js
Last active Dec 15, 2015
Feature rich Javascript OO implementation
View base.js
/**
* Javascript OO helpers
*/
/**
* Module exports.
*/
exports.Base = Base;
@lsm
lsm / binary_parser.js
Created Feb 14, 2013
Javascript binary parser by Jonas Raoni Soares Silva
View binary_parser.js
/**
* Binary Parser.
* Jonas Raoni Soares Silva
* http://jsfromhell.com/classes/binary-parser [v1.0]
*/
var chr = String.fromCharCode;
var maxBits = [];
for (var i = 0; i < 64; i++) {
maxBits[i] = Math.pow(2, i);
@lsm
lsm / benchmark_js_oo
Last active Oct 13, 2015
Benchmark between different implementation of javascript 'class'
View benchmark_js_oo
# npm install genji benchmark
var genji = require('genji');
var Benchmark = require('benchmark');
var Base = genji.Base;
var util = require('util'),
times = 500000;
@lsm
lsm / gist:1544579
Created Dec 31, 2011
Compare Buffer and String
View gist:1544579
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
var dataString = ['xxx', 'ssss', 'yyyyy', '1121212', 'aaaaa'];
var dataBuffer = [new Buffer('xxx'), new Buffer('ssss'), new Buffer('yyyyy'), new Buffer('1121212'), new Buffer('aaaaa')];
suite.add('Buffer#copy', function() {
var offset = 0;
dataBuffer.forEach(function(chunk) {
@lsm
lsm / background_demo.js
Created Aug 5, 2011 — forked from kosso/background_demo.js
Background Service notification for Titanium
View background_demo.js
/* Kosso : March 12th 2011
This the only way I managed to do this without the app crashing on resume.
Done slightly differently to the KS example, since they unregister the service and
do not use a setInterval timer.
*/
//############ in app.js :
// test for iOS 4+
@lsm
lsm / gsfixer.js
Created Jul 1, 2011
Exporter/importer to fix crapped gridfs files for node-mongodb-native < 0.9.4.4
View gsfixer.js
var mongodbSrc = require('./mongodb-0.9.4-3');
var mongodbDst = require('./mongodb-0.9.6-1');
/**** Edit start ****/
var SRC_HOST = '10.0.0.31';
var SRC_PORT = 27017;
var SRC_DB_NAME = 'mydb';
var SRC_ROOT_COLLECTION = 'images';
var FILE_QUERY = {}; // default to all
@lsm
lsm / export_gridfs.sh
Created Jun 30, 2011
Export files from MongoDB GridFS with directory paths
View export_gridfs.sh
#!/bin/bash
# http://www.vladimirm.com/blog/2011/06/export-files-from-mongodb-gridfs-with-directory-paths/
_host="${1:?Usage: gridfs host db}"
_db="${2:?Usage: gridfs host db}"
while read -r line; do
file=$(echo "$line" | awk -F'\t' '{ print $1 }')
[[ $file == 'connected to'* ]] && continue
directory=${file%/*}
mkdir -p $directory
@lsm
lsm / Validator.js
Created Apr 11, 2011
email,nickname,mobile,password
View Validator.js
// RegExp copied from sina weibo;)
function makeReFn(regStr) {
var re = typeof regStr == 'string' ? new RegExp(regStr) : regStr;
return function (beRegStr) {
if (re.test(beRegStr)) {
return true;
} else {
return false;
}
@lsm
lsm / TiOverLay.js
Created Feb 15, 2011
ti background gradient
View TiOverLay.js
// https://github.com/sspinc/OverlayTest
// Whip up a basic UI ...
Titanium.UI.setBackgroundColor('#fff');
var nav = Titanium.UI.createTabGroup();
var window = Ti.UI.createWindow({ title: 'Web View Test', tabBarHidden: true, navBarHidden: true, backgroundColor: '#c0c0c0' });
var mainTab = Titanium.UI.createTab({
icon: 'KS_nav_views.png',
@lsm
lsm / pure_javascript_hash-ring.js
Created Dec 14, 2010
copied from node-memcached
View pure_javascript_hash-ring.js
var CreateHash = require('crypto').createHash,
StringDecoder = require('string_decoder').StringDecoder,
Bisection = require('./utils').Bisection;
/*
example usage:
var sys = require( 'sys' ),
hashing = require( '../lib/hashring' ),
hashring = new hashing.hashRing(
[ '192.168.0.102:11212', '192.168.0.103:11212', '192.168.0.104:11212' ],