Skip to content

Instantly share code, notes, and snippets.

View musubu's full-sized avatar

Musubu Inc. musubu

View GitHub Profile
@musubu
musubu / gist:2156761
Created March 22, 2012 06:59
use raw query with sequelize
var Sequelize = require('Sequelize');
var sequelize = new Sequelize('dbname', 'username', 'password');
sequelize.connectorManager.client.query('select * from tablename', function(err, results) {
if (!err) {
console.log(results);
// you'll get an array of objects
// [{id: 1, ...}, {id: 2, ...}]
} else {
console.log(err);
}
@musubu
musubu / normalizeParamsAttributesNullValue.js
Created March 23, 2012 08:50
normalize null value to 0 space for object's attribute
var async = require('async');
function normalizeParamsAttributesNullValue (params, callback) {
async.forEachSeries(Object.keys(params), function(k, cb) {
if (params[k] === null) {
params[k] = '';
}
cb();
}, function() {
callback(params);
@musubu
musubu / gist:2169566
Created March 23, 2012 10:53
print memory usage for specific process.
ps alx | grep node | awk '{printf ("%d\t%s\n", $8,$13)}'
@musubu
musubu / gist:2178166
Created March 24, 2012 04:12
simplest use of node module.
// entry.js
function Entry (title) {
this.title = title;
}
Entry.prototype = {
title: null;
}
@musubu
musubu / gist:2179331
Created March 24, 2012 07:09
convert date to utc isostring format
var dates = ['2003-12-13T18:30:02Z'
, '2003-12-13T18:30:02.25Z'
, '2003-12-13T18:30:02+01:00'
, '2003-12-13T18:30:02.25+01:00'
, '1980'
, 'invalid'
, null
]
function toISOString (string, callback) {
@musubu
musubu / gist:2202583
Created March 26, 2012 03:01
escape and unescape in node.js
var querystring = require('querystring');
var original = 'http://example.com/product/abcde.html';
var escaped = querystring.escape(original);
console.log(escaped);
// http%3A%2F%2Fexample.com%2Fproduct%2Fabcde.html
var unescaped = querystring.unescape(escaped);
console.log(unescaped);
@musubu
musubu / gist:2203631
Created March 26, 2012 07:13
compare two objects in javascript
compareObjects = function(obj1, obj2, callback) {
var paramName;
var compare = function(objA, objB, param) {
var paramObjA = objA[param]
, paramObjB = (typeof objB[param] === 'undefined') ? false : objB[param];
switch (typeof objA[param]) {
case "object" : return (compareObjects(paramObjA, paramObjB));
case "function" : return (paramObjA.toString() === paramObjB.toString());
@musubu
musubu / gist:2275092
Created April 1, 2012 12:35
simple queue in node
var async = require('async')
, childProcess = require('child_process');
var commands = ['ls -l', 'echo hello'];
async.forEachSeries(commands, function(command, cb) {
childProcess.exec(command, function(err, stdout, stderr) {
if (!err) {
console.log(stdout);
console.log(stderr);
@musubu
musubu / gist:2297179
Created April 4, 2012 02:24
pretty print xml by xmllint
$ node build-opds.js
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/terms/" xmlns:opds="http://opds-spec.org/2010/catalog"><id>urn:uuid:433a5d6a-0b8c-4933-af65-4ca4f02763eb</id><title>feed title</title><updated>2010-01-10T10:01:11Z</updated><author><name>Feed author</name><uri>http://opds-spec.org</uri><email>foo@example.org</email></author><link rel="self" type="application/atom+xml;profile=opds-catalog;kind=acquisition" href="/opds-catalogs/unpopular.xml"/><entry/></feed>
$ node build-opds.js | xmllint --format -
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/terms/" xmlns:opds="http://opds-spec.org/2010/catalog">
<id>urn:uuid:433a5d6a-0b8c-4933-af65-4ca4f02763eb</id>
<title>feed title</title>
@musubu
musubu / opds-sample.yml
Created April 5, 2012 02:49
YAMLからOPDS取得フィードを生成するためのサンプルのYAML
# ---------------------------------------------------------------------
# YAMLからOPDS取得フィードを生成するためのサンプルのYAML
# ---------------------------------------------------------------------
# 行頭に「#」がある行はコメントです。そこに何を書いてもパソコンには処理されません。
# インデント(行の最初の空白)にタブを使うことはできません。2つの半角スペース(空白文字)を使ってください。
# よく分からない場合はこのファイルの内容をコピーして、実際の内容で書き換えることが無難です。
# YAMLの書き方については http://ja.wikipedia.org/wiki/YAML を参照してください。