@@ -62,11 +62,9 @@ export const moleculeUnparser =
6262 value ,
6363 unparseAtomOrMolecule ,
6464 )
65- if ( either . isLeft ( result ) ) {
66- return unparseSugarFreeMolecule ( value , unparseAtomOrMolecule )
67- } else {
68- return result
69- }
65+ return either . flatMapLeft ( result , _ =>
66+ unparseSugarFreeMolecule ( value , unparseAtomOrMolecule ) ,
67+ )
7068 } else {
7169 return unparseSugarFreeMolecule ( value , unparseAtomOrMolecule )
7270 }
@@ -156,34 +154,30 @@ const unparseSugaredApply = (
156154 unparseAtomOrMolecule : UnparseAtomOrMolecule ,
157155) => {
158156 const { closeParenthesis, openParenthesis } = punctuation ( kleur )
159- const functionUnparseResult = either . map (
160- either . flatMap (
161- serializeIfNeeded ( expression [ 1 ] . function ) ,
162- unparseAtomOrMolecule ,
157+ return either . flatMap (
158+ either . map (
159+ either . flatMap (
160+ serializeIfNeeded ( expression [ 1 ] . function ) ,
161+ unparseAtomOrMolecule ,
162+ ) ,
163+ unparsedFunction =>
164+ either . isRight ( readFunctionExpression ( expression [ 1 ] . function ) )
165+ ? // Immediately-applied function expressions need parentheses.
166+ openParenthesis . concat ( unparsedFunction ) . concat ( closeParenthesis )
167+ : unparsedFunction ,
163168 ) ,
164169 unparsedFunction =>
165- either . isRight ( readFunctionExpression ( expression [ 1 ] . function ) )
166- ? // Immediately-applied function expressions need parentheses.
167- openParenthesis . concat ( unparsedFunction ) . concat ( closeParenthesis )
168- : unparsedFunction ,
169- )
170- if ( either . isLeft ( functionUnparseResult ) ) {
171- return functionUnparseResult
172- }
173-
174- const argumentUnparseResult = either . flatMap (
175- serializeIfNeeded ( expression [ 1 ] . argument ) ,
176- unparseAtomOrMolecule ,
177- )
178- if ( either . isLeft ( argumentUnparseResult ) ) {
179- return argumentUnparseResult
180- }
181-
182- return either . makeRight (
183- functionUnparseResult . value
184- . concat ( openParenthesis )
185- . concat ( argumentUnparseResult . value )
186- . concat ( closeParenthesis ) ,
170+ either . map (
171+ either . flatMap (
172+ serializeIfNeeded ( expression [ 1 ] . argument ) ,
173+ unparseAtomOrMolecule ,
174+ ) ,
175+ unparsedArgument =>
176+ unparsedFunction
177+ . concat ( openParenthesis )
178+ . concat ( unparsedArgument )
179+ . concat ( closeParenthesis ) ,
180+ ) ,
187181 )
188182}
189183
@@ -209,12 +203,10 @@ const unparseSugaredIndex = (
209203 serializeIfNeeded ( expression [ 1 ] . object ) ,
210204 unparseAtomOrMolecule ,
211205 )
212- if ( either . isLeft ( objectUnparseResult ) ) {
213- return objectUnparseResult
214- } else {
206+ return either . flatMap ( objectUnparseResult , unparsedObject => {
215207 if ( typeof expression [ 1 ] . query !== 'object' ) {
216208 // TODO: It would be nice if this were provably impossible.
217- return either . makeLeft ( {
209+ return either . makeLeft < UnserializableValueError > ( {
218210 kind : 'unserializableValue' ,
219211 message : 'Invalid index expression' ,
220212 } )
@@ -248,13 +240,13 @@ const unparseSugaredIndex = (
248240 } else {
249241 const { dot } = punctuation ( kleur )
250242 return either . makeRight (
251- objectUnparseResult . value
243+ unparsedObject
252244 . concat ( dot )
253245 . concat ( keyPath . map ( quoteKeyPathComponentIfNecessary ) . join ( dot ) ) ,
254246 )
255247 }
256248 }
257- }
249+ } )
258250}
259251
260252const unparseSugaredLookup = (
0 commit comments