Skip to content

Commit

Permalink
refactor: Split parser into distinct functions
Browse files Browse the repository at this point in the history
  • Loading branch information
q2s2t committed Sep 27, 2018
1 parent d2da77b commit f18a9b1
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 13 deletions.
69 changes: 56 additions & 13 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,62 @@

/**
* Quick wrappper around RegExp named capture groups
* @param {String} line Line of data
* @param {String} lineType lineType to be match
*/
export function matchLine (line, lineType) {
const lineTypes = {
progress: / *(?<percent>\d+)% (?<fileCount>\d+)$/,
infoHeaders: /^(?<property>.+) = (?<value>.+)$/,
infoFooter: /^(?<property>.+): (?<value>.+)$/,
symbolFile: /^(?<symbol>[TU+]) (?<file>.*\s*)$/
export function matchProgress (line) {
const regexp = / *(?<percent>\d+)%( (?<fileCount>\d+)$/
const match = line.match(regexp)
if (match) {
let progress = {}
Object.entries(match.groups).forEach(function ([key, val]) {
progress[key] = Number.parseInt(val, 10)
})
return progress
} else {
return null
}
}

export function matchPropsEquals (line) {
const regexp = /^(?<property>.+) = (?<value>.+)$/
const match = line.match(regexp)
if (match) {
return [match.groups]
} else {
return null
}
const match = line.match(lineTypes[lineType])
}

export function matchSymbolFile (line) {
const regexp = /^(?<symbol>[TU+]) (?<file>.*\s*)$/
const match = line.match(regexp)
if (match) {
const files = [match.groups]
return files
} else {
return null
}
}

export function matchPropsColonDuo (line) {
const regexp = /^(?<property1>.+): (?<value1>.+), # (?<property2>.+): (?<value2>.+)/
const match = line.match(regexp)
if (match) {
let match1 = {}
match1[match.groups.property1] = match.groups.value1
let match2 = {}
match2[match.groups.property2] = match.groups.value2
const props = [match1, match2]
return props
} else {
return null
}
}

export function matchPropsColon (line) {
const regexp = /^(?<property>.+): (?<value>.+)$/
const match = line.match(regexp)
if (match) {
return match.groups
let match1 = {}
match1[match.groups.property1] = match.groups.value1
const props = [match1]
return props
} else {
return null
}
Expand Down
33 changes: 33 additions & 0 deletions test/lib/parser.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* global describe, it */
import { expect } from 'chai'
import { matchProgress, matchPropsColon, matchPropsColonDuo, matchPropsEquals, matchSymbolFile } from '../../lib/parser.js'

describe('Specification: parser.js', function () {
it('progress should return null on non match', function () {
const r = matchProgress('+ test/file/null')
expect(r).to.be.null
})

it('progress should return progress', function () {
const digit1File = matchProgress(' 1% 3')
expect(digit1File.percent).to.equal(1)
expect(digit1File.fileCount).to.equal(3)

const digit2File = matchProgress(' 42% 234')
expect(digit2File.percent).to.equal(42)
expect(digit2File.fileCount).to.equal(234)

const digit3File = matchProgress('100% 23877')
expect(digit3File.percent).to.equal(100)
expect(digit3File.fileCount).to.equal(23877)

const digit1NoFile = matchProgress(' 1%')
expect(digit1NoFile.percent).to.equal(1)

const digit2NoFile = matchProgress(' 42%')
expect(digit2NoFile.percent).to.equal(42)

const digit3NoFile = matchProgress('100%')
expect(digit3NoFile.percent).to.equal(100)
})
})

0 comments on commit f18a9b1

Please sign in to comment.