Skip to content

Instantly share code, notes, and snippets.

fs.stat('public/person/'+rez.doc._id, function(err, stats){
console.log('stats: ' + JSON.stringify(stats)+'\n error: '+err);
if (err || !stats.isDirectory())
console.log(stats.isDirectory());
});
>> stats: {"dev":51712,"ino":434027,"mode":16804,"nlink":2,"uid":0,"gid":0,"rdev":0,"size":4096,"blksize":4096,"blocks":8,"atime":"2011-07-21T21:26:18.000Z","mtime":"2011-07-21T21:26:18.000Z","ctime":"2011-07-21T21:26:18.000Z"}
error: null
TypeError: Cannot call method 'isDirectory' of undefined
console.log(require("assert").ok( err || (stats && stats instanceof fs.Stats) ))
>> undefined for both cases
#### Case1 : if the dir does not exists:
fs.stat('public/person100000/'+rez.doc._id, function(err, stats){
console.log('stats: ' + JSON.stringify(stats)+'\n error: '+err);
//console.log(stats.isDirectory()); this would throw an error
require("assert").ok( err || (stats && stats instanceof fs.Stats) )
fs.mkdir('public/bldgs/'+req.body._id, 777, function(e){console.log(e || 'written');
fs.writeFile('public/bldgs/'+req.body._id+'/'+req.body._id+'.json', JSON.stringify(req.body), function(err){
console.log(err || 'saved!');
res.redirect('/'+req.params.code)
})
});
// shortest distance to checking all relative distances
// because with an array of game objects, we don't need to check
// each object's distance to all other objects
// That would duplicate work (don't want to check one against another
// and the other against the one). So each one needs only be checked
// against objects to right of it
objects = [a,b,c,d,e,f,g] // each letter represents a game object (or comprises the object whole) ie {_id:"1234567", coord:[x,y], radius: 50}
function checkDistance(obj, target){
@NHQ
NHQ / gist:1340661
Created November 4, 2011 22:31
iterator inserter
function connect(dataset){
(function dingdong (set, x){
if(set == undefined){return}
else{
db.connect({async: true},function(error) {
if (error) {
return console.log('CONNECTION error: ' + error);
}
this.query().
@NHQ
NHQ / gist:1522109
Created December 26, 2011 20:57
MAc OSX installer 0.6.6 NPM error
nhq:~ elenagragasin$ which npm
/Users/elenagragasin/local/node/bin/npm
nhq:~ elenagragasin$ which node
/usr/local/bin/node
NHQ:webdev elenagragasin$ npm --version
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
@NHQ
NHQ / gist:1562012
Created January 4, 2012 20:38
Object prototype
> Object.prototype.toJSON = function(){return JSON.stringify(this)};
> obj = {a:1};
> obj.toJSON();
// RangeError: Maximum call stack size exceeded
var DONE= 0;
var Sound;
var paths= ['./build/default/sound', './build/release/sound', 'sound'];
while (paths.length) {
var p= paths.pop();
try { Sound= require(p) } catch (e) { continue }
console.log("Módulo de sonido encontrado en: '"+ p+ "'");
break;
}
var audioLib = require('audiolib'),
fs = require('fs'),
dev, osc, rec, path;
var DONE= 0;
var Sound;
var paths= ['./build/default/sound', './build/release/sound', 'sound'];
while (paths.length) {
var p= paths.pop();
@NHQ
NHQ / gist:1580165
Created January 8, 2012 23:45
CONVERTING FROM AUDIOLIB TO 16 BIT SIGNED INT
function floatToSignedInteger() {'...'}
var buffer = audiolib.sink().rec().join()
var i = 0, len = buffer.length
do {
floatToSignedInteger(/* ??? */)
}
while { i < len}