I need to perform the equivalent of the async.eachSeries()
method, but I did not want to add a dependency such as require('async')
.
So I came up with the implementation below, and I was wondering if there is a better way to do this?
it 'String::start_Process_Capture_Console_Out', (done)->
runTest = (testData,next)->
name = testData.process_Name
parameter = testData.process_Parameter
expected_Data = testData.expected_Data
name.start_Process_Capture_Console_Out parameter, (data)->
data.assert_Is(expected_Data)
next()
runTests = (testsData, next)->
if testsData.empty()
next()
else
runTest testsData.pop(), ()-> runTests(testsData, next)
testsData = [
{process_Name: 'echo' , process_Parameter: 'hello' , expected_Data:'hello\n' }
{process_Name: 'echo' , process_Parameter: ['hello','me'], expected_Data:'hello,me\n' }
{process_Name: 'git' , process_Parameter: ['xyz' ], expected_Data:'git: \'xyz\' is not a git command. See \'git --help\'.\n' }
{process_Name: 'ls' , process_Parameter: '.' , expected_Data:'LICENSE\nREADME.md\nindex.js\nnode_modules\npackage.json\nsrc\ntest\n' }
]
runTests(testsData, done)
For reference here are the start_Process
and start_Process_Capture_Console_Out
string prototype methods
String::start_Process = (args...)->
args ?= []
return child_process.spawn(@.str(),args)
String::start_Process_Redirect_Console = (args...)->
args ?= []
childProcess = @.start_Process(args)
childProcess.stdout.on 'data', (data)->console.log(data.str().trim())
childProcess.stderr.on 'data', (data)->console.log(data.str().trim())
return childProcess
String::start_Process_Capture_Console_Out = (args... , callback)->
consoleData = ""
childProcess = @.start_Process(args)
childProcess.stdout.on 'data', (data)->consoleData+=data
childProcess.stderr.on 'data', (data)->consoleData+=data
childProcess.on 'exit', ()->
callback(consoleData)
return childProcess
One solution would be to add a prototype to the Array
class, maybe called async_Each_Series
so that we could just have:
testsData = [
{process_Name: 'echo' , process_Parameter: 'hello' , expected_Data:'hello\n' }
{process_Name: 'echo' , process_Parameter: ['hello','me'], expected_Data:'hello,me\n' }
{process_Name: 'git' , process_Parameter: ['xyz' ], expected_Data:'git: \'xyz\' is not a git command. See \'git --help\'.\n' }
{process_Name: 'ls' , process_Parameter: '.' , expected_Data:'LICENSE\nREADME.md\nindex.js\nnode_modules\npackage.json\nsrc\ntest\n' }
]
testsData.async_Each_Series(runTest, done)
the nodejs Array class already has a forEach method, but it does not take a callback
if you need the callback, write (js this time)
Or you can also avoid dependencies and cut-and-past copy the relevant code into yours.
(Edit: in short, yes, there is a better way -- write a general-purpose iterator and use it for the these tests, do not write special-purpose loop just for this.)
My implementation of applyVisitor (also in https://npmjs.org/package/aflow)