Skip to content

Commit

Permalink
refactor to be more dry
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoniangreen committed Aug 17, 2024
1 parent c2ef245 commit 412498c
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions lib/compile/jtd/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,20 +229,15 @@ function serializeString({gen, data}: SerializeCxt): void {
}

function serializeNumber({gen, data, self}: SerializeCxt): void {
const condition = _`${data} === Infinity || ${data} === -Infinity || Number.isNaN(${data})`
const addNumber = (): CodeGen => gen.add(N.json, _`"" + ${data}`)

if (self.opts.specialNumbers === "null") {
gen.if(
_`${data} === Infinity || ${data} === -Infinity || Number.isNaN(${data})`,
() => gen.add(N.json, _`null`),
() => gen.add(N.json, _`"" + ${data}`)
)
gen.if(condition, () => gen.add(N.json, _`null`), addNumber)
} else if (self.opts.specialNumbers === "string") {
gen.if(
_`${data} === Infinity || ${data} === -Infinity || Number.isNaN(${data})`,
() => gen.add(N.json, str`"${data}"`),
() => gen.add(N.json, _`"" + ${data}`)
)
gen.if(condition, () => gen.add(N.json, str`"${data}"`), addNumber)
} else {
gen.add(N.json, _`"" + ${data}`)
addNumber()
}
}

Expand Down

0 comments on commit 412498c

Please # to comment.