Skip to content

Instantly share code, notes, and snippets.

View shinout's full-sized avatar
🏠
Working from home

Shin Suzuki shinout

🏠
Working from home
View GitHub Profile
@shinout
shinout / google_site_to_sjis.php
Created March 11, 2011 18:25
GoogleサイトのページをSJISに変換するコード。災害まとめページを家族のケータイからも閲覧してもらえるように作った。
<?php
$url = ""; // ここにgoogle siteのURLをかきます。例: $url = "http://sites.google.com/site/suzukisaigai/";
$a="SHIFT_JIS";
$b="text/html; charset=$a";
header("Content-Type:$b");
echo preg_replace("/ead>/","ead><meta http-equiv=Content-Type content='$b'>", mb_convert_encoding(file_get_contents($url), $a, "UTF-8"));
@shinout
shinout / node.js iconv sample.js
Created March 25, 2011 15:17
iconv -f euc-jp -t utf-8 filename
var spawn = require('child_process').spawn,
iconv = spawn('iconv', ['-f', 'euc-jp', '-t', 'utf-8']),
fs = require('fs');
var filename = __dirname + '/eucjp.html';
var stream = fs.createReadStream(filename);
stream.pipe(iconv.stdin);
iconv.stdout.on('data', function(data) {
@shinout
shinout / shinout.test.js
Created March 25, 2011 15:18
simple test module
module.exports = (function() {
var l = console.log;
var assert = require("assert");
var c = 0;
var t = 0;
function result(name, reset) {
l("\n/*-----------------------");
l(" * test: [ "+ name + " ]");
l(" * result: "+ c + "/" + t + " ->" + parseInt(100*c/t) +'%' );
if ( c > 0 && c == t) {
@shinout
shinout / wstream.js
Created April 15, 2011 04:04
fs.createWriteStream with large data
var fs = require('fs');
var stream = fs.createWriteStream(__dirname + '/out.txt');
var str = 'hogehogefugafugafoobarpiyo[';
for ( var i=0; i < 1000000000; i++) { // 1Gtimes
stream.write(str + i + ']\n');
}
stream.on('drain', function() {
console.log('drain'); // never happen
@shinout
shinout / setter test
Created April 19, 2011 13:38
this.hoge; // <- getter ? setter? set: function(val) {this._piyo = val} // is this piyo affected to original object?
/* see if just "obj.hoge" is setter or getter */
var obj = {};
Object.defineProperty(obj, 'hoge', {
get: function(){console.log("get")},
set: function(){console.log("set")}
});
obj.hoge;
@shinout
shinout / multiprocess.js
Created April 21, 2011 04:04
multiprocess node test (requires linestream)
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var LineStream = require('linestream');
var parallel = Number(process.argv[2]) || 40;
var N = 1000;
var script = 'number.js';
var total = 0;
var endcount = 0;
@shinout
shinout / compare_version.js
Created July 27, 2011 03:28
compare version
function compare_version(v1, v2) {
var v1a = v1.split('.');
var v2a = v2.split('.');
var c;
for (var i=0, l=Math.max(v1a.length, v2a.length); i<l; i++) {
c = (v1a[i] || 0) - (v2a[i] || 0);
if (!c) continue;
return (c > 0) ? 1 : -1;
}
return 0;
@shinout
shinout / WeightedSelection.js
Created July 27, 2011 07:32
Weighted Selection
function WeightedSelection(table, random) {
// TODO validate first.
this.names = Object.keys(table);
this.hists = this.names.map((function() {
var total = 0;
return function(v) {
total += Number(table[v]);
return total;
};
})());
@shinout
shinout / for_loop_with_error_limit.js
Created July 27, 2011 08:39
for loop with error limit
var total = 1000;
var err_limit = 10;
for (var i=0, e=0; i<total && e<err_limit; i++) {
if (Math.random() > 0.750) {
console.log("error");
e++;
i--;
}
else {
console.log("ok");
@shinout
shinout / SortedList.js
Created July 28, 2011 01:40
SortedList sample: list of ranges without overlap
function SortedList(arr, options) {
if (arr instanceof Array) {
return SortedList.build(arr, options);
}
this.arr = [];
options = options || {};
Object.keys(options).forEach(function(k) {
this[k] = options[k];
}, this);
}