mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 06:03:45 +01:00
Remove callback-pattern from label parsers
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
ProjectEntityHandler = require "../Project/ProjectEntityHandler"
|
||||
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
|
||||
Async = require('async')
|
||||
|
||||
|
||||
module.exports = LabelsHandler =
|
||||
@@ -12,10 +11,8 @@ module.exports = LabelsHandler =
|
||||
ProjectEntityHandler.getAllDocs projectId, (err, docs) ->
|
||||
if err?
|
||||
return callback(err)
|
||||
LabelsHandler.extractLabelsFromProjectDocs docs, (err, projectLabels) ->
|
||||
if err?
|
||||
return callback(err)
|
||||
callback(null, projectLabels)
|
||||
projectLabels = LabelsHandler.extractLabelsFromProjectDocs docs
|
||||
callback(null, projectLabels)
|
||||
|
||||
getLabelsForDoc: (projectId, docId, callback=(err, docLabels)->) ->
|
||||
# Flush doc first, because this action is often performed while
|
||||
@@ -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)
|
||||
callback(null, docLabels)
|
||||
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
|
||||
|
||||
@@ -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) ->
|
||||
expect(docLabels).to.deep.equal ['aaa', 'bbb']
|
||||
done()
|
||||
it 'should extract all the labels', ->
|
||||
docLabels = @LabelsHandler.extractLabelsFromDoc @lines
|
||||
expect(docLabels).to.deep.equal ['aaa', 'bbb']
|
||||
|
||||
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) ->
|
||||
expect(projectLabels).to.deep.equal {
|
||||
'id_one': ['aaa'],
|
||||
'id_two': [],
|
||||
'id_three': ['bbb', 'ccc']
|
||||
}
|
||||
done()
|
||||
it 'should extract all the labels', ->
|
||||
projectLabels = @LabelsHandler.extractLabelsFromProjectDocs @docs
|
||||
expect(projectLabels).to.deep.equal {
|
||||
'id_one': ['aaa'],
|
||||
'id_two': [],
|
||||
'id_three': ['bbb', 'ccc']
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user