Skip to content

Commit

Permalink
fix: function asString argument types for trigger V8 optimization (mo…
Browse files Browse the repository at this point in the history
…nomorphi) (#689)
  • Loading branch information
nigrosimone authored Mar 10, 2024
1 parent 64e163d commit dfb627e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,21 @@ function buildSingleTypeSerializer (context, location, input) {
} else if (schema.format === 'unsafe') {
return `json += serializer.asUnsafeString(${input})`
} else {
return `json += serializer.asString(${input})`
return `
if (typeof ${input} !== 'string') {
if (${input} === null) {
json += '""'
} else if (${input} instanceof Date) {
json += '"' + ${input}.toISOString() + '"'
} else if (${input} instanceof RegExp) {
json += serializer.asString(${input}.source)
} else {
json += serializer.asString(${input}.toString())
}
} else {
json += serializer.asString(${input})
}
`
}
}
case 'integer':
Expand Down
14 changes: 0 additions & 14 deletions lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,6 @@ module.exports = class Serializer {
}

asString (str) {
if (typeof str !== 'string') {
if (str === null) {
return '""'
}
if (str instanceof Date) {
return '"' + str.toISOString() + '"'
}
if (str instanceof RegExp) {
str = str.source
} else {
str = str.toString()
}
}

if (str.length < 42) {
return this.asStringSmall(str)
} else if (STR_ESCAPE.test(str) === false) {
Expand Down

0 comments on commit dfb627e

Please sign in to comment.