-
-
Save indexzero/1596474 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 fs = require('fs') | |
var DataCollector = function(arr) { | |
this.setFiles(arr) | |
} | |
DataCollector.prototype = { | |
setFiles: function(arr) { | |
this.files = arr | |
} | |
, getFilenameLengths: function() { | |
var sizes = {} | |
this.files.forEach(function(filename, index) { | |
sizes[filename] = filename.length | |
}) | |
return sizes | |
} | |
, getFileData: function() { | |
var fileData = {} | |
this.files.forEach(function(filename, index) { | |
fs.readFile(filename, function(err, data) { | |
fileData[filename] = data | |
}) | |
}) | |
return fileData | |
} | |
} | |
var array = ["foo.json", "bar.json", "baz.json"] | |
var dc1 = new DataCollector(array) | |
console.log(dc1.getFilenameLengths()) // yay! | |
var dc2 = new DataCollector(array) | |
console.log(dc2.getFileData()) // womp womp |
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 fs = require('fs') | |
var DataCollector = function(arr) { | |
this.setFiles(arr) | |
} | |
DataCollector.prototype = { | |
setFiles: function(arr) { | |
this.files = arr | |
} | |
, getFilenameLengths: function() { | |
var sizes = {} | |
this.files.forEach({|filename, index| | |
sizes[filename] = filename.length | |
}) | |
return sizes | |
} | |
, getFileData: function() { | |
var fileData = {} | |
this.files.forEach({|filename, index| | |
fs.readFile(filename, {|err, data| | |
fileData[filename] = data | |
}) | |
}) | |
return fileData | |
} | |
} | |
var array = ["foo.json", "bar.json", "baz.json"] | |
var dc1 = new DataCollector(array) | |
console.log(dc1.getFilenameLengths()) // yay! | |
var dc2 = new DataCollector(array) | |
console.log(dc2.getFileData()) // still doesn't work |
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 fs = require('fs') | |
var DataCollector = function(arr) { | |
this.setFiles(arr) | |
} | |
DataCollector.prototype = { | |
setFiles: function(arr) { | |
this.files = arr | |
} | |
, getFilenameLength: function(filename) { | |
return filename.length; | |
} | |
, getAllFilenameLengths: function() { | |
var sizes = {} | |
this.files.forEach(function(filename, index) { | |
sizes[filename] = this.getFilenameLength(filename); | |
}) | |
return sizes | |
} | |
, getFileData: function(filename) { | |
fs.readFile(filename, function(err, data) { | |
if(err) throw err // useless | |
return data; // yeah right | |
}) | |
} | |
, getAllFileData: function() { | |
var fileData = {} | |
this.files.forEach(function(filename, index) { | |
fileData[filename] = this.getFileData(filename) // where's your callback? | |
}) | |
return fileData | |
} | |
} | |
var array = ["foo.json", "bar.json", "baz.json"] | |
var dc1 = new DataCollector(array) | |
console.log(dc1.getAllFilenameLengths()) // uh oh, we broke this! | |
var dc2 = new DataCollector(array) | |
console.log(dc2.getFileData('bar.json')) // nope | |
console.log(dc2.getAllFileData()) // nada |
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 fs = require('fs') | |
var DataCollector = function(arr) { | |
this.setFiles(arr) | |
} | |
DataCollector.prototype = { | |
setFiles: function(arr) { | |
this.files = arr | |
} | |
, getFilenameLength: function(index) { | |
var filename = this.files[index]; | |
return filename.length; | |
} | |
, getAllFilenameLengths: function() { | |
var sizes = {} | |
for(var i = 0; i < this.files.length; i++) { | |
var filename = this.files[i] // hoisted var, no real harm here | |
sizes[filename] = this.getFilenameLength(i) // double lookup, also not terrible | |
} | |
return sizes | |
} | |
, getFileData: function(filename) { | |
fs.readFile(filename, {|err, data| | |
if(err) throw err // maybe? | |
return data // hopefully? | |
}) | |
} | |
, getAllFileData: function() { | |
var fileData = {} | |
for(var i = 0; i < this.files.length; i++) { | |
var filename = this.files[i] | |
fileData[filename] = this.getFileData(filename) // this surely works right? | |
} | |
return fileData | |
} | |
} | |
var array = ["foo.json", "bar.json", "baz.json"] | |
var dc1 = new DataCollector(array) | |
console.log(dc1.getAllFilenameLengths()) // yay (again)! | |
var dc2 = new DataCollector(array) | |
console.log(dc2.getFileData('bar.json')) // probably not | |
console.log(dc2.getAllFileData()) // not likely |
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 fs = require('fs') | |
var DataCollector = function(arr) { | |
this.setFiles(arr) | |
} | |
DataCollector.prototype = { | |
setFiles: function(arr) { | |
this.files = arr | |
} | |
, getFilenameLength: function(index) { | |
return this.files[i].length | |
} | |
, getAllFilenameLengths: function() { | |
var sizes = {} | |
, filename = null | |
for(var i = 0; i < this.files.length; i++) { | |
filename = this.files[i] | |
sizes[filename] = filename.length | |
} | |
return sizes | |
} | |
//, getFileData: function(filename, callback) { | |
// fs.readFile(filename, callback) | |
//} | |
// better yet | |
, getFileData: fs.readFile | |
, getAllFileData: function(callback) { | |
var self = this | |
, fileData = {} | |
, num = this.files.length | |
this.files.forEach(function(filename, index) { | |
self.getFileData(filename, function(err, data) { | |
num-- | |
if(!err) { | |
fileData[filename] = data | |
} | |
if(err || num === 0) callback(err, fileData) | |
}) | |
}) | |
} | |
} | |
var array = ["foo.json", "bar.json", "baz.json"] | |
var dc1 = new DataCollector(array) | |
console.log(dc1.getAllFilenameLengths()) // yay! | |
var dc2 = new DataCollector(array) | |
// works but you have to handle the error | |
dc2.getFileData('bar.json', function(err, data) { | |
console.log(data); | |
}) | |
// works but same problem | |
dc2.getAllFileData(function(err, data) { | |
console.log(data); | |
}) |
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 fs = require('fs') | |
var DataCollector = function(arr) { | |
this.setFiles(arr) | |
} | |
DataCollector.prototype = { | |
setFiles: function(arr) { | |
this.files = arr | |
} | |
, getFilenameLength: function(index) { | |
return this.files[i].length | |
} | |
, getAllFilenameLengths: function() { | |
var sizes = {} | |
, filename = null | |
for(var i = 0; i < this.files.length; i++) { | |
filename = this.files[i] | |
sizes[filename] = filename.length | |
} | |
return sizes | |
} | |
//, getFileData: function(filename, callback) { | |
// fs.readFile(filename, callback) | |
//} | |
// better yet | |
, getFileData: fs.readFile | |
, getAllFileData: function(callback) { | |
var fileData = {} | |
, num = this.files.length | |
this.files.forEach({|filename, index| | |
this.getFileData(filename, {|err, data| | |
num-- | |
if(!err) { | |
fileData[filename] = data | |
} | |
if(err || num === 0) callback(err, fileData) | |
}) | |
}) | |
} | |
} | |
var array = ["foo.json", "bar.json", "baz.json"] | |
var dc1 = new DataCollector(array) | |
console.log(dc1.getAllFilenameLengths()) // yay! | |
var dc2 = new DataCollector(array) | |
// works but you have to handle the error | |
dc2.getFileData('bar.json', {|err, data| | |
console.log(data); | |
}) | |
// works but same problem | |
dc2.getAllFileData({|err, data| | |
console.log(data); | |
}) |
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 fs = require('fs') | |
, memoize = require('functools').memoize | |
, async = requie('async') | |
var DataCollector = function(arr) { | |
this.files = arr | |
} | |
DataCollector.prototype = { | |
, getAllFilenameLengths: memoize(function() { | |
var sizes = {} | |
this.files.forEach(function(filename) { | |
sizes[filename] = filename.length | |
}) | |
return sizes | |
}) | |
, getFileData: memoize.async(fs.readFile) | |
, getAllFileData: memoize.async(function(callback) { | |
var fileData = {} | |
async.collect(this.files, this.getFileData, {|filename, data| | |
fileData[filename] = data | |
}).then(function(err) { | |
callback(err, fileData) | |
}) | |
}) | |
} | |
var array = ["foo.json", "bar.json", "baz.json"] | |
var dc1 = new DataCollector(array) | |
console.log(dc1.getAllFilenameLengths()) // yay! | |
var dc2 = new DataCollector(array) | |
dc2.getAllFileData(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment