Skip to content

Commit

Permalink
refactor(projection-parser): simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
kantord committed Oct 10, 2018
1 parent 14a281f commit ded70a7
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/parsers/projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ type WrappedProjectionNodeType =
| {name: 'projection', value: ListNodeType}
| {name: 'valueProp', value: string};

const packList = (x: ListNodeType): WrappedProjectionNodeType => ({
value: x,
name: 'projection'
})

const packProperty = (x: string): WrappedProjectionNodeType => ({
value: x,
name: 'valueProp'
})

const unpackOne = (
projectable: ProjectableNodeType | ProjectionNodeType | ValuePropNodeType,
projection: WrappedProjectionNodeType
Expand Down Expand Up @@ -43,20 +53,12 @@ const unpack = ([projectable, projections]: [

const ProjectionParser = P.lazy((): mixed => {
const ProjectableParser = require('./projectable').default
const ListParser = require('./list').default
const PropertyParser = P.regexp(/(\.[$A-Z_][0-9A-Z_$]*)+/i)
const ListParser = require('./list').default.map(packList)
const PropertyParser = P.regexp(/(\.[$A-Z_][0-9A-Z_$]*)+/i).map(packProperty)

return P.seq(
ProjectableParser.skip(P.optWhitespace),
P.alt(
ListParser.map((x: ListNodeType): WrappedProjectionNodeType => ({
value: x,
name: 'projection'
})),
PropertyParser.map((x: string): WrappedProjectionNodeType => ({
value: x,
name: 'valueProp'
}))
)
P.alt(ListParser, PropertyParser)
.skip(P.optWhitespace)
.atLeast(1)
).map(unpack)
Expand Down

0 comments on commit ded70a7

Please sign in to comment.