Skip to content

Commit 08ff8e5

Browse files
committed
feat: added typings
fix: fixed test BREAKING CHANGE: Validation now required to get correct result and handle correctly
1 parent fb22253 commit 08ff8e5

15 files changed

+343
-204
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
types.d.ts

.prettierrc.cjs

-2
This file was deleted.

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ Options include:
6363
buffer: false, // set to true if you are parsing from buffers instead of strings
6464
required: false, // set to true if all properties are required
6565
ordered: false, // set to true if your properties have the same order always
66-
validate: true, // set to false to disable extra type validation
6766
validateStrings: true, // set to false to disable extra type validation
6867
fullMatch: true, // set to false to do fastest match based on the schema (unsafe!)
6968
unescapeStrings: true, // set to false if you don't need to unescape \ chars

lib/number.js

+12-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ parseNumberBuffer.pointer = 0
66
module.exports = defaults
77

88
function defaults (opts) {
9-
const { buffer, validate } = opts
9+
const { buffer } = opts
1010
const { name } = ops(opts)
1111

1212
return compileNumber
@@ -17,20 +17,20 @@ function defaults (opts) {
1717
}
1818

1919
if (prop) {
20-
prop.set(`parseNumber(${name}, ptr, ${validate})`)
20+
prop.set(`parseNumber(${name}, ptr)`)
2121
gen('ptr = parseNumber.pointer')
2222
} else {
2323
const num = gen.sym('num')
2424
gen(`
25-
const ${num} = parseNumber(${name}, ptr, ${validate})
25+
const ${num} = parseNumber(${name}, ptr)
2626
parse.pointer = parseNumber.pointer
2727
return ${num}
2828
`)
2929
}
3030
}
3131
}
3232

33-
function parseFloatString (n, buf, ptr, validate) {
33+
function parseFloatString (n, buf, ptr) {
3434
let num = 0
3535
let div = 1
3636

@@ -78,15 +78,13 @@ function parseFloatString (n, buf, ptr, validate) {
7878
return n + num / div
7979
}
8080

81-
if (validate) {
82-
throw new Error('Unexpected token in number')
83-
}
81+
throw new Error('Unexpected token in number')
8482
}
8583

8684
return n + num / div
8785
}
8886

89-
function parseNumberString (buf, ptr, validate) {
87+
function parseNumberString (buf, ptr) {
9088
var num = 0
9189
var sign = 1
9290

@@ -98,7 +96,7 @@ function parseNumberString (buf, ptr, validate) {
9896
while (ptr < buf.length) {
9997
switch (buf.charCodeAt(ptr++)) {
10098
case 46:
101-
return sign * parseFloatString(num, buf, ptr, validate)
99+
return sign * parseFloatString(num, buf, ptr)
102100
case 48:
103101
num = num * 10
104102
continue
@@ -138,15 +136,13 @@ function parseNumberString (buf, ptr, validate) {
138136
return sign * num
139137
}
140138

141-
if (validate) {
142-
throw new Error('Unexpected token in number')
143-
}
139+
throw new Error('Unexpected token in number')
144140
}
145141

146142
return sign * num
147143
}
148144

149-
function parseFloatBuffer (n, buf, ptr, validate) {
145+
function parseFloatBuffer (n, buf, ptr) {
150146
var num = 0
151147
var div = 1
152148
while (ptr < buf.length) {
@@ -192,15 +188,13 @@ function parseFloatBuffer (n, buf, ptr, validate) {
192188
return (n * div + num) / div
193189
}
194190

195-
if (validate) {
196-
throw new Error('Unexpected token in number')
197-
}
191+
throw new Error('Unexpected token in number')
198192
}
199193

200194
return (n * div + num) / div
201195
}
202196

203-
function parseNumberBuffer (buf, ptr, validate) {
197+
function parseNumberBuffer (buf, ptr) {
204198
var num = 0
205199
var sign = 1
206200

@@ -252,9 +246,7 @@ function parseNumberBuffer (buf, ptr, validate) {
252246
return sign * num
253247
}
254248

255-
if (validate) {
256-
throw new Error('Unexpected token in number')
257-
}
249+
throw new Error('Unexpected token in number')
258250
}
259251

260252
return sign * num

lib/object.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ function defaults (opts) {
1111

1212
const allowEmptyObjects = opts.allowEmptyObjects !== false
1313
const required = !!opts.allRequired
14-
const validate = opts.validate !== false
1514
const ordered = !!opts.ordered
1615
const fullMatch = opts.fullMatch !== false
1716
const defaults = opts.defaults !== false
@@ -23,7 +22,7 @@ function defaults (opts) {
2322
class Required {
2423
constructor (gen, o, fields) {
2524
this.name = o + 'Required'
26-
this.fields = validate ? fields.filter(isRequired) : []
25+
this.fields = fields.filter(isRequired)
2726
this.count = this.fields.length
2827
this.gen = gen
2928
this.vars = []
@@ -120,9 +119,9 @@ function defaults (opts) {
120119
gen(`while (${ch('ptr++')} !== ${code('}')}) {`)
121120
if (prettyPrinted) {
122121
gen(
123-
`if (${ch('ptr')} === ${code(' ')} || ${ch('ptr')} === ${code(
122+
`while (${ch('ptr')} === ${code(' ')} || ${ch('ptr')} === ${code(
124123
'\n'
125-
)}) continue`
124+
)}) ptr++`
126125
)
127126
}
128127

@@ -145,13 +144,10 @@ function defaults (opts) {
145144
}
146145

147146
function genblk (field, offset, gen) {
148-
gen(`ptr += ${offset + 1 + 1}`)
149147
if (prettyPrinted) {
150-
gen(
151-
`if (${ch('ptr')} === ${code(' ')} || ${ch('ptr')} === ${code(
152-
'\n'
153-
)}) ptr++`
154-
)
148+
gen(`ptr += ${offset + 1 + 1 + 1}`)
149+
} else {
150+
gen(`ptr += ${offset + 1 + 1}`)
155151
}
156152
reqs.set(field)
157153
genany(gen, new Property(gen, o, field), field)
@@ -165,8 +161,8 @@ function defaults (opts) {
165161
}
166162
}
167163

168-
function gendef (gen) {
169-
gen(`throw new Error('Unexpected key in object')`)
164+
function gendef (gen, name) {
165+
gen(`throw new Error('Unexpected key "${name}" in object')`)
170166
}
171167

172168
function genif (gen, o, fields, i, genany) {

lib/ops.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
exports = module.exports = (opts) =>
1+
exports = module.exports = opts =>
22
opts.buffer ? exports.buffer : exports.string
33
exports.buffer = create(true)
44
exports.string = create(false)

lib/schema.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function convertToRawSchema (jsons, opts) {
4040

4141
if (jsons.anyOf) {
4242
s.type = ANY_OF
43-
s.types = jsons.anyOf.map((t) => convertToRawSchema(t, opts))
43+
s.types = jsons.anyOf.map(t => convertToRawSchema(t, opts))
4444
} else {
4545
switch (jsons.type) {
4646
case 'integer':

lib/string.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@ const ops = require('./ops')
44
module.exports = defaults
55

66
function defaults (opts) {
7-
const { name, validate, validateStrings } = ops(opts)
7+
const { name, validateStrings } = ops(opts)
88
const buffer = !!opts.buffer
99
const unesc = opts.unescapeStrings !== false
1010

1111
return compileString
1212

1313
function compileString (gen, prop) {
1414
if (!gen.scope.parseString) {
15-
gen.scope.parseString = genparser(
16-
buffer,
17-
validate || validateStrings,
18-
unesc
19-
)
15+
gen.scope.parseString = genparser(buffer, validateStrings, unesc)
2016
}
2117

2218
if (prop) {

lib/switch.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = defaults
66
function defaults (opts) {
77
if (!opts) opts = {}
88

9-
const { eq, ch, ptr, code, validate } = opsDefaults(opts)
9+
const { eq, ch, ptr, code } = opsDefaults(opts)
1010
const fullMatch = opts.fullMatch !== false
1111

1212
return compileSwitch
@@ -45,7 +45,7 @@ function defaults (opts) {
4545
if (more) {
4646
if (gendef) {
4747
gen('} else {')
48-
gendef(gen, ` as not found any more fields`, validate)
48+
gendef(gen, match.substring(0, match.length - 1))
4949
gen('}')
5050
} else {
5151
gen('}')
@@ -56,7 +56,7 @@ function defaults (opts) {
5656
if (fullMatch) {
5757
if (gendef) {
5858
gen('} else {')
59-
gendef(gen, ` as no full-match`, validate)
59+
gendef(gen, match.substring(0, match.length - 1))
6060
gen('}')
6161
} else {
6262
gen('}')
@@ -68,7 +68,7 @@ function defaults (opts) {
6868
if (sw) {
6969
if (gendef) {
7070
gen('default:')
71-
gendef(gen, `, something got wrong`, validate)
71+
gendef(gen, 'default' + JSON.stringify(tree))
7272
}
7373
gen('}')
7474
}
@@ -104,7 +104,7 @@ function group (strings) {
104104
}
105105
tree.push([
106106
s.slice(0, min),
107-
group(strings.slice(0, i).map((s) => s.slice(min)))
107+
group(strings.slice(0, i).map(s => s.slice(min)))
108108
])
109109
strings = strings.slice(i)
110110
}

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33
"version": "2.3.0",
44
"description": "Turbocharged JSON.parse for type stable JSON data",
55
"main": "index.js",
6+
"types": "types.d.ts",
67
"dependencies": {
78
"generate-function": "^2.3.1"
89
},
910
"devDependencies": {
10-
"@commitlint/cli": "^12.1.1",
11-
"commitlint-config-airlight": "^1.0.1",
11+
"@commitlint/cli": "^12.1.4",
12+
"@types/tape": "4",
13+
"commitlint-config-airlight": "^1.0.3",
1214
"husky": "^6.0.0",
1315
"lint-staged": "^11.0.0",
14-
"prettier-config-airlight": "^1.0.0",
1516
"prettier-standard": "^16.4.1",
17+
"tap-diff": "^0.1.1",
1618
"tape": "^4.13.3"
1719
},
1820
"scripts": {
1921
"lint": "prettier-standard --lint",
20-
"test": "tape test/*.js",
22+
"test": "tape test/*.js | tap-diff",
2123
"prepare": "husky install"
2224
},
2325
"repository": {

test/anyOf.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const t = require('tape')
44

55
const tjp = require('../index')
66

7-
t.test('should parse anyOf string and null', (t) => {
7+
t.test('should parse anyOf string and null', t => {
88
t.plan(2)
99

1010
const parser = tjp({
@@ -30,7 +30,7 @@ t.test('should parse anyOf string and null', (t) => {
3030
})
3131
})
3232

33-
t.test('should parse nested anyOf string and null', (t) => {
33+
t.test('should parse nested anyOf string and null', t => {
3434
t.plan(3)
3535

3636
const parser = tjp({
@@ -75,7 +75,7 @@ t.test('should parse nested anyOf string and null', (t) => {
7575
})
7676
})
7777

78-
t.test('should parse anyOf string, boolean or null', (t) => {
78+
t.test('should parse anyOf string, boolean or null', t => {
7979
t.plan(3)
8080

8181
const parser = tjp({
@@ -107,7 +107,7 @@ t.test('should parse anyOf string, boolean or null', (t) => {
107107
})
108108
})
109109

110-
t.test('should parse anyOf array and null', (t) => {
110+
t.test('should parse anyOf array and null', t => {
111111
t.plan(4)
112112

113113
const parser = tjp({
@@ -142,7 +142,7 @@ t.test('should parse anyOf array and null', (t) => {
142142
})
143143
})
144144

145-
t.test('should parse anyOf boolean and null', (t) => {
145+
t.test('should parse anyOf boolean and null', t => {
146146
t.plan(3)
147147

148148
const parser = tjp({
@@ -171,7 +171,7 @@ t.test('should parse anyOf boolean and null', (t) => {
171171
})
172172
})
173173

174-
t.test('should parse anyOf number and null', (t) => {
174+
t.test('should parse anyOf number and null', t => {
175175
t.plan(12)
176176

177177
const parser = tjp({
@@ -227,7 +227,7 @@ t.test('should parse anyOf number and null', (t) => {
227227
})
228228
})
229229

230-
t.test('should parse anyOf string and object', (t) => {
230+
t.test('should parse anyOf string and object', t => {
231231
t.plan(2)
232232

233233
const parser = tjp({
@@ -260,7 +260,7 @@ t.test('should parse anyOf string and object', (t) => {
260260
})
261261
})
262262

263-
t.test('should parse anyOf string and null', (t) => {
263+
t.test('should parse anyOf string and null', t => {
264264
t.plan(2)
265265

266266
const parser = tjp({

test/random.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ function random (r, optional, depth) {
123123

124124
function number () {
125125
return Math.random() < 0.5
126-
? Math.floor(Math.random() * 1e10) / 1e5
127-
: Math.floor(Math.random() * -1e10) / 1e5
126+
? Math.floor(Math.random() * 1e5) / 1e5
127+
: Math.floor(Math.random() * -1e5) / 1e5
128128
}
129129

130130
function string (alpha, first) {

0 commit comments

Comments
 (0)