Skip to content

Commit

Permalink
remove switch
Browse files Browse the repository at this point in the history
Signed-off-by: francesco <[email protected]>
  • Loading branch information
cesco69 authored Apr 11, 2024
1 parent 49e059c commit 16c54b9
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]/

module.exports = class Serializer {
constructor (options) {
constructor(options) {
switch (options && options.rounding) {
case 'floor':
this.parseInteger = Math.floor
Expand All @@ -23,30 +23,23 @@ module.exports = class Serializer {
this._options = options
}

asInteger (i) {
switch (typeof i) {
case 'number':
case 'boolean':
case 'string':
case 'object': {
if (Number.isInteger(i)) {
return '' + i
}
/* eslint no-undef: "off" */
const integer = this.parseInteger(i)
// check if number is Infinity or NaN
// eslint-disable-next-line no-self-compare
if (integer === Infinity || integer === -Infinity || integer !== integer) {
throw new Error(`The value "${i}" cannot be converted to an integer.`)
}
return '' + integer
}
case 'bigint': return i.toString()
default: throw new Error(`The value "${i}" cannot be converted to an integer.`)
asInteger(i) {
if (Number.isInteger(i)) {
return '' + i
} else if (typeof i === 'bigint') {
return i.toString()
}
/* eslint no-undef: "off" */
const integer = this.parseInteger(i)
// check if number is Infinity or NaN
// eslint-disable-next-line no-self-compare
if (integer === Infinity || integer === -Infinity || integer !== integer) {
throw new Error(`The value "${i}" cannot be converted to an integer.`)
}
return '' + integer
}

asNumber (i) {
asNumber(i) {
// fast cast to number
const num = +i
// check if number is NaN
Expand All @@ -60,11 +53,11 @@ module.exports = class Serializer {
}
}

asBoolean (bool) {
asBoolean(bool) {
return bool && 'true' || 'false' // eslint-disable-line
}

asDateTime (date) {
asDateTime(date) {
if (date === null) return '""'
if (date instanceof Date) {
return '"' + date.toISOString() + '"'
Expand All @@ -75,7 +68,7 @@ module.exports = class Serializer {
throw new Error(`The value "${date}" cannot be converted to a date-time.`)
}

asDate (date) {
asDate(date) {
if (date === null) return '""'
if (date instanceof Date) {
return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, 10) + '"'
Expand All @@ -86,7 +79,7 @@ module.exports = class Serializer {
throw new Error(`The value "${date}" cannot be converted to a date.`)
}

asTime (date) {
asTime(date) {
if (date === null) return '""'
if (date instanceof Date) {
return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(11, 19) + '"'
Expand All @@ -97,7 +90,7 @@ module.exports = class Serializer {
throw new Error(`The value "${date}" cannot be converted to a time.`)
}

asString (str) {
asString(str) {
const len = str.length
if (len < 42) {
// magically escape strings for json
Expand Down Expand Up @@ -133,15 +126,15 @@ module.exports = class Serializer {
}
}

asUnsafeString (str) {
asUnsafeString(str) {
return '"' + str + '"'
}

getState () {
getState() {
return this._options
}

static restoreFromState (state) {
static restoreFromState(state) {
return new Serializer(state)
}
}

0 comments on commit 16c54b9

Please sign in to comment.