Remove callback-pattern from label parsers

This commit is contained in:
Shane Kilkelly 2017-06-13 11:10:21 +01:00
parent c25b6b792d
commit bd6133aadb
2 changed files with 22 additions and 47 deletions

View file

@ -1,6 +1,5 @@
ProjectEntityHandler = require "../Project/ProjectEntityHandler"
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
Async = require('async')
module.exports = LabelsHandler =
@ -12,9 +11,7 @@ module.exports = LabelsHandler =
ProjectEntityHandler.getAllDocs projectId, (err, docs) ->
if err?
return callback(err)
LabelsHandler.extractLabelsFromProjectDocs docs, (err, projectLabels) ->
if err?
return callback(err)
projectLabels = LabelsHandler.extractLabelsFromProjectDocs docs
callback(null, projectLabels)
getLabelsForDoc: (projectId, docId, callback=(err, docLabels)->) ->
@ -27,30 +24,20 @@ module.exports = LabelsHandler =
ProjectEntityHandler.getDoc projectId, docId, (err, lines) ->
if err?
return callback(err)
LabelsHandler.extractLabelsFromDoc lines, (err, docLabels) ->
if err?
return callback(err)
docLabels = LabelsHandler.extractLabelsFromDoc lines
callback(null, docLabels)
extractLabelsFromDoc: (lines, callback=(err, docLabels)->) ->
extractLabelsFromDoc: (lines) ->
docLabels = []
for line in lines
re = LabelsHandler.labelCaptureRegex()
while (labelMatch = re.exec(line))
if labelMatch[1]
docLabels.push(labelMatch[1])
callback(null, docLabels)
return docLabels
extractLabelsFromProjectDocs: (projectDocs, callback=(err, projectLabels)->) ->
extractLabelsFromProjectDocs: (projectDocs) ->
projectLabels = {} # docId => List[Label]
docs = for _path, doc of projectDocs
doc
Async.eachSeries(
docs
, (doc, cb) ->
LabelsHandler.extractLabelsFromDoc doc.lines, (err, docLabels) ->
projectLabels[doc._id] = docLabels
cb(err)
, (err, x) ->
callback(err, projectLabels)
)
for _path, doc of projectDocs
projectLabels[doc._id] = LabelsHandler.extractLabelsFromDoc(doc.lines)
return projectLabels

View file

@ -32,15 +32,9 @@ describe 'LabelsHandler', ->
'six seven'
]
it 'should not produce an error', (done) ->
@LabelsHandler.extractLabelsFromDoc @lines, (err, docLabels) ->
expect(err).to.equal null
done()
it 'should extract all the labels', (done) ->
@LabelsHandler.extractLabelsFromDoc @lines, (err, docLabels) ->
it 'should extract all the labels', ->
docLabels = @LabelsHandler.extractLabelsFromDoc @lines
expect(docLabels).to.deep.equal ['aaa', 'bbb']
done()
describe 'extractLabelsFromProjectDocs', ->
beforeEach ->
@ -59,19 +53,13 @@ describe 'LabelsHandler', ->
}
}
it 'should not produce an error', (done) ->
@LabelsHandler.extractLabelsFromProjectDocs @docs, (err, projectLabels) ->
expect(err).to.be.oneOf [null, undefined]
done()
it 'should extract all the labels', (done) ->
@LabelsHandler.extractLabelsFromProjectDocs @docs, (err, projectLabels) ->
it 'should extract all the labels', ->
projectLabels = @LabelsHandler.extractLabelsFromProjectDocs @docs
expect(projectLabels).to.deep.equal {
'id_one': ['aaa'],
'id_two': [],
'id_three': ['bbb', 'ccc']
}
done()
describe 'getLabelsForDoc', ->
beforeEach ->
@ -79,7 +67,7 @@ describe 'LabelsHandler', ->
@fakeLabels = ['aaa']
@DocumentUpdaterHandler.flushDocToMongo = sinon.stub().callsArgWith(2, null)
@ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @fakeLines)
@LabelsHandler.extractLabelsFromDoc = sinon.stub().callsArgWith(1, null, @fakeLabels)
@LabelsHandler.extractLabelsFromDoc = sinon.stub().returns(@fakeLabels)
@call = (callback) =>
@LabelsHandler.getLabelsForDoc @projectId, @docId, callback
@ -118,7 +106,7 @@ describe 'LabelsHandler', ->
}
@fakeLabels = ['aaa']
@ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @fakeDocs)
@LabelsHandler.extractLabelsFromProjectDocs = sinon.stub().callsArgWith(1, null, @fakeLabels)
@LabelsHandler.extractLabelsFromProjectDocs = sinon.stub().returns(@fakeLabels)
@call = (callback) =>
@LabelsHandler.getAllLabelsForProject @projectId, callback