Created
November 16, 2011 04:18
-
-
Save seanhess/1369222 to your computer and use it in GitHub Desktop.
AS Examples
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
// Use a pure function to do your fancy stuff! (note that we CAN'T do all that manipulation in an as block) | |
// uses the parameter style of asyncifying modules | |
Entity.findByProgram = as(Entity, AugmentedProgram, function(Entity, AugmentedProgram, programId) { | |
var programs = AugmentedProgram.findByProgram(programId) | |
var entitiesWithOffsets = programs.map(function(program) { | |
var entity = Entity.findByName(program.name) | |
return {offset: program.offset, entity: entity} // we'll need both to sort, below | |
}) | |
return Entity.groupByOffset(entitiesWithOffsets) | |
}) | |
Entity.groupByOffset = function(entitiesAndOffsets) { | |
var offsets = {} | |
entitiesAndOffsets.forEach(function(e) { | |
if (!offsets[e.offset]) offsets[e.offset] = [] | |
offsets[e.offset].push(e.entity) | |
}) | |
return offsets | |
} |
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
Entity.findByProgram = function(programId, cb) { | |
AugmentedProgram.findByProgram(programId, function(err, augmentedPrograms) { | |
if (!augmentedPrograms.length) return cb(null, {}) // otherwise the loop below won't finish | |
var entities = {} | |
var processed = 0 | |
augmentedPrograms.forEach(function(augmentedProgram) { | |
Entity.findByName(augmentedProgram.name, function(err, entity) { | |
if (err) return cb(err) | |
if (!entities[augmentedProgram.offset]) entities[augmentedProgram.offset] = [] | |
entities[augmentedProgram.offset].push(entity) | |
if (++processed == augmentedPrograms.length) | |
cb(null, entities) | |
}) | |
}) | |
}) | |
} |
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
exports.concatJsFiles = as(fs, path, function(fs, path, dir, destFile) { | |
var files = fs.readdir(dir) | |
files = files.filter(function (name) { | |
return (name.slice(-3) === '.js'); | |
}); | |
var contents = files.map(function(file) { | |
var fullPath = path.join(dir, file) | |
return fs.readFile(fullPath, 'utf-8') | |
}) | |
var combined = contents.join("\n") | |
fs.writeFile(destFile, combined, 0775) | |
}) |
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
exports.concatJsFiles = as(fs, path, function(fs, path, dir, destFile) { | |
var combined = fs | |
.readdir(dir) | |
.filter(function (name) { | |
return (name.slice(-3) === '.js'); | |
}) | |
.map(function(file) { | |
var fullPath = path.join(dir, file) | |
return fs.readFile(fullPath, 'utf-8') | |
}) | |
.join("\n") | |
fs.writeFile(destFile, combined, 0775) | |
}) |
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
// FS EXAMPLE - concat all js files in a directory and save to filename | |
exports.concatJsFiles = function(dir, destFile, cb) { | |
fs.readdir(dir, function(err, files) { | |
if (err) return cb(err) | |
// Filter *.js files | |
files = files.filter(function (name) { | |
return (name.slice(-3) === '.js'); | |
}); | |
// Read all the files | |
var fileContents = [] | |
function getContentsOfAllFiles(cb) { | |
files.forEach(function(file) { | |
var fullPath = path.join(dir, file) | |
fs.readFile(fullPath, 'utf-8', function(err, contents) { | |
if (err) return cb(err) | |
fileContents.push(contents) | |
if (fileContents.length == files.length) | |
cb(null, fileContents) | |
}) | |
}) | |
} | |
getContentsOfAllFiles(function(err, contents) { | |
if (err) return cb(err) | |
// Join them all together | |
var combined = contents.join("\n") | |
// Save them | |
fs.writeFile(destFile, combined, 0775, cb) | |
}) | |
}) | |
} |
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
// Create a cb-style function, then turn it into an express route | |
// note that this example uses the global-style async version of the module | |
exports.scoresAfter = routeHandler(as(function(req, res) { | |
var scores = AScores.getScores() | |
res.render("weights", {scores:scores}) | |
})) | |
AScores = as.convert(Scores) // you can convert the module at the module level if you want | |
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
exports.scores = function(req, res) { | |
Scores.getScores(function(err, scores) { | |
if (err) return res.send(err) | |
res.render("weights", {scores: scores}) | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment