Skip to content

Instantly share code, notes, and snippets.

View kaizhu256's full-sized avatar

kai zhu kaizhu256

View GitHub Profile
@kaizhu256
kaizhu256 / gist:2877576
Created June 5, 2012 20:25
javascript cosine fit by fft / gauss-newton
my.Array2.prototype.cosFit = function(yy) {};:
return yy._cosFitFft(this);
## OPTIMIZATION - cache callback
my.Array2.prototype._cosFitFft = function(cff) {};:
{{my.Array2.rgxEach1.1}}, amp, awp, cc, dy, ii3, jj, ll3, mm, nn, scale, ss, stride3, yy2;
{{my.Array2.rgxEach1.2}}
awp = [null, null, null];
for(mm = 0; (1 << mm) < ll2; mm += 1) {;} nn = 1 << mm;
scale = (nn + 0.5) / this.ll2; yy2 = new window.Float64Array(nn);
{{my.Array2.rgxEach1.3}}
@kaizhu256
kaizhu256 / gist:2896850
Created June 8, 2012 16:54
nodejs http client for retrieving binary data
my.httpGet = function(kwargs) {};:
var data, ll, ll2, rsp, tmp; ll = ll2 = 0;
if(typeof kwargs === 'string') {kwargs = {'hpath': kwargs};}
my.xhrInit(kwargs);
if(kwargs.proxy) {kwargs.hpath = kwargs.proxy + '/' + kwargs.hpath;}
my.ooUpdateUndefined();:
my.ooUpdateUndefined(kwargs, my.urlParse(kwargs.hpath)),
{}:
'client': rqd[(kwargs.protocol).slice(0, -1)],
'fncData': function(chunk) {},:
@kaizhu256
kaizhu256 / gist:2925083
Created June 13, 2012 16:26
javascript regular expression utilities rgxCount rgxEach rgxEscape rgxFormat
my.rgxCount = function(ss, rgx) {
var ll = 0; my.rgxEach(ss, rgx, function() {ll += 1;}); return ll;
};
my.rgxEach = function(ss, rgx, fnc) {
var fnd, ii; if(!rgx.global) {return;} ii = 0;
while(true) {fnd = rgx.exec(ss); if(!fnd || fnc(fnd,ii) === false) {return;} ii += 1;}
};
my.rgxEscape = function(ss, flag) {
return new RegExp(ss.replace(/([$\\(*+.?\[\^{|])/g, '\\$1'), flag);
};
@kaizhu256
kaizhu256 / gist:2963882
Created June 21, 2012 04:46
debootstrap chroot
## chroot
$ wget http://ftp.us.debian.org/debian/pool/main/d/debootstrap/debootstrap_1.0.10lenny1_all.deb
$ ar x debootstrap_1.0.10lenny1_all.deb
$ tar xzf data.tar.gz -C /
$ mkdir /debian
$ debootstrap --arch i386 unstable /debian/ http://ftp.us.debian.org/debian/
$ cp /etc/hosts /debian/etc/hosts
$ mount /debian/dev/pts
$ mount /debian/dev/shm
$ mount /debian/proc
@kaizhu256
kaizhu256 / gist:2984806
Created June 24, 2012 20:34
javascript my.xhrUpload
my.xhrUpload = function(kwargs) {
kwargs.method = kwargs.method || 'POST';
my.xhrInit(kwargs, null, {'uploadFileInfo': kwargs.fpp});
kwargs.postData =
kwargs.postData || (kwargs.fpp.mozSlice || kwargs.fpp.webkitSlice).call(kwargs.fpp);
return my.xhrGet(kwargs);
};
@kaizhu256
kaizhu256 / gist:4482049
Last active December 10, 2015 19:29
javascript - json stringify circular objects in 50 lines of code this code recursively traverses a json array / object. it will stringify all circular objects it encounters for first time and cache their references. that way it can avoid them again in the future.
/*jslint indent: 2, nomen: true, regexp: true, stupid: true*/
(function () {
'use strict';
var circularObject, exports = {};
exports.jsStringifyCircular = function (oo, arr) {
//// stringify circular objects
var ii, kk, out, vv;
arr = arr || [];
@kaizhu256
kaizhu256 / gist:4482069
Last active October 20, 2021 16:25
javascript - very fast and simple uuid4 generator benchmarked on http://jsperf.com/uuid4/8
/*jslint bitwise: true, indent: 2, nomen: true, regexp: true, stupid: true*/
(function () {
'use strict';
var exports = {};
exports.uuid4 = function () {
//// return uuid of form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
var uuid = '', ii;
for (ii = 0; ii < 32; ii += 1) {
@kaizhu256
kaizhu256 / gist:4511667
Last active December 10, 2015 23:48
nodejs - asynchronously, recursively delete directories and files in under 100 lines of code
/*jslint bitwise: true, indent: 2, nomen: true, regexp: true, stupid: true*/
(function () {
'use strict';
exports.requireFs = require('fs');
function FsRemoveR() {
}
FsRemoveR.prototype.callbackDefault = function (err) {
@kaizhu256
kaizhu256 / gist:4511752
Created January 11, 2013 15:57
nodejs - asynchronously and recursively create nested directories (like mkdir -p) in under 50 lines of code
/*jslint bitwise: true, indent: 2, nomen: true, regexp: true, stupid: true*/
(function () {
'use strict';
exports.requireFs = require('fs');
exports.requirePath = require('path');
function _fsMkdirP(dirname, callback) {
exports.requireFs.mkdir(dirname, function (err) {
switch (err && err.code) {
@kaizhu256
kaizhu256 / gist:4512114
Created January 11, 2013 16:38
nodejs - efficient http proxy that directly pipes data from website to browser in under 50 lines of code
/*jslint bitwise: true, indent: 2, nomen: true, regexp: true, stupid: true*/
(function () {
'use strict';
exports.requireHttp = require('http');
exports.requireUrl = require('url');
exports.handlerHttpProxy = function (request, response, next) {
var kk,
proxy = exports.requireUrl.parse(/http.*/.exec(request.url)[0]);