Skip to content

Commit 216a91e

Browse files
committed
Merge branch 'master' of github.com:fastify/fast-json-stringify
2 parents 2525c20 + ab95d2c commit 216a91e

File tree

5 files changed

+65
-26
lines changed

5 files changed

+65
-26
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ jobs:
3838
needs: test
3939
runs-on: ubuntu-latest
4040
steps:
41-
- uses: fastify/[email protected].0
41+
- uses: fastify/[email protected].1
4242
with:
4343
github-token: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ const stringify = fastJson({
342342
savedId: { type: 'string' }
343343
},
344344
// without "required" validation any object will match
345-
required: ['saveId']
345+
required: ['savedId']
346346
},
347347
{
348348
type: 'object',

index.js

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,7 @@ function build (schema, options) {
7272
`
7373

7474
code += `
75-
${$pad2Zeros.toString()}
76-
${$asAny.toString()}
77-
${$asString.toString()}
78-
${$asStringNullable.toString()}
79-
${$asStringSmall.toString()}
80-
${$asDatetime.toString()}
81-
${$asDate.toString()}
82-
${$asTime.toString()}
83-
${$asNumber.toString()}
84-
${$asNumberNullable.toString()}
85-
${$asInteger.toString()}
86-
${$asIntegerNullable.toString()}
87-
${$asNull.toString()}
88-
${$asBoolean.toString()}
89-
${$asBooleanNullable.toString()}
75+
${asFunctions}
9076
9177
var isLong = ${isLong ? isLong.toString() : false}
9278
@@ -116,19 +102,19 @@ function build (schema, options) {
116102
code = buildObject(location, code, main)
117103
break
118104
case 'string':
119-
main = schema.nullable ? $asStringNullable.name : getStringSerializer(schema.format)
105+
main = schema.nullable ? '$asStringNullable' : getStringSerializer(schema.format)
120106
break
121107
case 'integer':
122-
main = schema.nullable ? $asIntegerNullable.name : $asInteger.name
108+
main = schema.nullable ? '$asIntegerNullable' : '$asInteger'
123109
break
124110
case 'number':
125-
main = schema.nullable ? $asNumberNullable.name : $asNumber.name
111+
main = schema.nullable ? '$asNumberNullable' : '$asNumber'
126112
break
127113
case 'boolean':
128-
main = schema.nullable ? $asBooleanNullable.name : $asBoolean.name
114+
main = schema.nullable ? '$asBooleanNullable' : '$asBoolean'
129115
break
130116
case 'null':
131-
main = $asNull.name
117+
main = '$asNull'
132118
break
133119
case 'array':
134120
main = '$main'
@@ -233,6 +219,7 @@ function getTestSerializer (format) {
233219
return stringSerializerMap[format]
234220
}
235221

222+
const asFunctions = `
236223
function $pad2Zeros (num) {
237224
const s = '00' + num
238225
return s[s.length - 2] + s[s.length - 1]
@@ -372,7 +359,7 @@ function $asStringSmall (str) {
372359
surrogateFound = true
373360
}
374361
if (point === 34 || point === 92) {
375-
result += str.slice(last, i) + '\\'
362+
result += str.slice(last, i) + '\\\\'
376363
last = i
377364
found = true
378365
}
@@ -385,6 +372,7 @@ function $asStringSmall (str) {
385372
}
386373
return ((point < 32) || (surrogateFound === true)) ? JSON.stringify(str) : '"' + result + '"'
387374
}
375+
`
388376

389377
function addPatternProperties (location) {
390378
const schema = location.schema
@@ -928,7 +916,7 @@ function buildObject (location, code, name) {
928916
if (schema.nullable) {
929917
code += `
930918
if(input === null) {
931-
return '${$asNull()}';
919+
return 'null';
932920
}
933921
`
934922
}
@@ -967,7 +955,7 @@ function buildArray (location, code, name, key = null) {
967955
if (schema.nullable) {
968956
code += `
969957
if(obj === null) {
970-
return '${$asNull()}';
958+
return 'null';
971959
}
972960
`
973961
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"semver": "^7.1.0",
4242
"standard": "^16.0.1",
4343
"tap": "^15.0.0",
44-
"typescript": "^4.0.2"
44+
"typescript": "^4.0.2",
45+
"webpack": "^5.40.0"
4546
},
4647
"dependencies": {
4748
"ajv": "^6.11.0",

test/webpack.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const webpack = require('webpack')
5+
const path = require('path')
6+
7+
test('the library should work with webpack', async (t) => {
8+
t.plan(1)
9+
const targetdir = path.resolve(__dirname, '..', '.cache')
10+
const targetname = path.join(targetdir, 'webpacktest.js')
11+
const wopts = {
12+
entry: path.resolve(__dirname, '..', 'index.js'),
13+
mode: 'production',
14+
target: 'node',
15+
output: {
16+
path: targetdir,
17+
filename: 'webpacktest.js',
18+
library: {
19+
name: 'fastJsonStringify',
20+
type: 'umd'
21+
}
22+
}
23+
}
24+
await new Promise((resolve, reject) => {
25+
webpack(wopts, (err, stats) => {
26+
if (err) { reject(err) } else { resolve(stats) };
27+
})
28+
})
29+
const build = require(targetname)
30+
const stringify = build({
31+
title: 'webpack should not rename code to be executed',
32+
type: 'object',
33+
properties: {
34+
foo: {
35+
type: 'string'
36+
},
37+
bar: {
38+
type: 'boolean'
39+
}
40+
},
41+
patternProperties: {
42+
foo: {
43+
type: 'number'
44+
}
45+
}
46+
})
47+
48+
const obj = { foo: '42', bar: true }
49+
t.equal(stringify(obj), '{"foo":"42","bar":true}')
50+
})

0 commit comments

Comments
 (0)