Created
November 12, 2011 09:57
-
-
Save polotek/1360330 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cursor = dbclient.collection('example', _).find(_); | |
var count = cursor.count(_); | |
for(var obj = cursor.nextObject(_), oi = 0; obj; obj = | |
cursor.nextObject(_), oi++) { | |
/** | |
* do something with every obj here | |
* suppose for this example it needs total count and offset (oi). | |
*/ | |
}); | |
A simple file paths manipulation rule: | |
function _extendPath(_, path) { | |
if (!_exists(_, path + ".js") && _exists(_, path) && | |
fs.stat(path, _).isDirectory()) { | |
if (_exists(_, path + "/main.js")) | |
return path + "/main"; | |
else if (_exists(_, path + "/index.js")) | |
return path + "/index"; | |
} | |
return path; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This example is a combination of some node constructs that | |
// have been talked about a lot, but don't exist yet in this form. | |
// It also includes an example of an abstraction around the core | |
// db driver interface above to make it easier to stream. | |
var Filter = require('streams').Filter; // doesn't exist yet, but it should | |
DB.stream('example', someQuery) | |
.pipe(new Filter(function(obj, idx, dbstream) { | |
/** | |
* do something with every obj here | |
* idx is the current offset, count is on dbstream.count | |
*/ | |
}).on('error', function(err, idx, dbstream) { | |
// handle error, you also have the index that failed | |
// and a reference to the stream | |
}); | |
// Also note how the parameters to filter functions follow the standard forEach paradigm. | |
// That's nice I think. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var async = require('async') | |
, path = require('path'); | |
var exts = [ | |
'' | |
, '/main' | |
, '/index' | |
] | |
function extendPath(filePath, cb) { | |
// here I'm assuming that if it's a js file, we're done | |
// if I can't assume that, then I'd probably look at my | |
// files and try to put myself in a position where I can | |
// assume that. | |
if(/\.js$/.test(filePath) { return cb(null, filePath); } | |
// It seems unfortunate that we have to map the extra path | |
// parts onto the filPath first. But it's a small, fast loop. | |
// And it keeps things more elegant than multiple if/else | |
// wrangling. IMO that's what makes the above example harder | |
// than it needs to be. Also, I can check more paths just by | |
// updating the above array. | |
var candidates = exts.map(function(ext) { | |
return filePath + ext; | |
}); | |
// I'm partial to the async library by caolan so I used | |
// one of it's nice methods. This will call exists on each | |
// item in turn and return the one that passes to the callback. | |
async.detect(candidates, function(file, cb) { | |
path.exists(file + '.js', cb); | |
} | |
, function(err, file) { | |
// we could've just let the callback stand alone here | |
// but there's the special case where none of the options | |
// was found and we just return the original path. That seems | |
// odd to me, but I'll do it in the interest of following | |
// the set requirements | |
cb(err, file || path); | |
}); | |
} | |
// unannotated version | |
var async = require('async') | |
, path = require('path'); | |
var exts = [ | |
'' | |
, '/main' | |
, '/index' | |
] | |
function extendPath(filePath, cb) { | |
if(/\.js$/.test(filePath) { return cb(null, filePath); } | |
var candidates = exts.map(function(ext) { | |
return filePath + ext; | |
}); | |
async.detect(candidates, function(file, cb) { | |
path.exists(file + '.js', cb); | |
} | |
, function(err, file) { | |
cb(err, file || path); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment