Skip to content

Commit 0d1bd0d

Browse files
committed
🔧 fix: handle root array
1 parent 47d4795 commit 0d1bd0d

File tree

5 files changed

+127
-12
lines changed

5 files changed

+127
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.1.2 - 4 Mar 2025
2+
Bug fix:
3+
- handle primitive type when array is root
4+
15
# 0.1.1 - 4 Mar 2025
26
Feature:
37
- support Record

example/index.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
import { t } from 'elysia'
22
import { createAccelerator } from '../src/index'
33

4-
const shape = t.Record(
5-
t.String(),
6-
t.Object({
7-
a: t.String(),
8-
b: t.String()
9-
})
10-
)
4+
const shape = t.Array(t.Number())
115

12-
const value = {
13-
a: { a: 'a', b: 'a' },
14-
c: { a: 'a', b: 'b' }
15-
} satisfies typeof shape.static
6+
const value = [1,2] satisfies typeof shape.static
167

178
const stringify = createAccelerator(shape)
189

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-accelerator",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Speed up JSON stringification by providing OpenAPI/TypeBox model",
55
"license": "MIT",
66
"scripts": {

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,15 @@ const accelerate = (
376376
instruction.array++
377377

378378
if (schema.items.type === 'string') {
379+
if(isRoot) v += 'return `'
380+
379381
if (nullableCondition)
380382
v += `\${${nullableCondition}?"null":${property}.length?\`[${joinStringArray(property)}]\`:"[]"}`
381383
else
382384
v += `\${${property}.length?\`[${joinStringArray(property)}]\`:"[]"}`
383385

386+
if(isRoot) v += '`'
387+
384388
break
385389
}
386390

@@ -390,11 +394,15 @@ const accelerate = (
390394
schema.items.type === 'bigint' ||
391395
isInteger(schema.items)
392396
) {
397+
if(isRoot) v += 'return `'
398+
393399
if (nullableCondition)
394400
v += `\${${nullableCondition}?'"null"':${property}.length?\`[$\{${property}.toString()}]\`:"[]"`
395401
else
396402
v += `\${${property}.length?\`[$\{${property}.toString()}]\`:"[]"}`
397403

404+
if(isRoot)v += '`'
405+
398406
break
399407
}
400408

test/array.test.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { t } from 'elysia'
2+
import { type TAnySchema } from '@sinclair/typebox'
3+
import { createAccelerator } from '../src'
4+
5+
import { describe, expect, it } from 'bun:test'
6+
7+
const isEqual = (shape: TAnySchema, value: unknown, expected = value) =>
8+
expect(JSON.parse(createAccelerator(shape)(value))).toEqual(expected)
9+
10+
describe('Array', () => {
11+
it('handle string array at root', () => {
12+
const shape = t.Array(t.String())
13+
14+
isEqual(shape, ['a', 'b'])
15+
})
16+
17+
it('handle number array at root', () => {
18+
const shape = t.Array(t.Number())
19+
20+
isEqual(shape, [1, 2])
21+
})
22+
23+
it('handle boolean array at root', () => {
24+
const shape = t.Array(t.Number())
25+
26+
isEqual(shape, [true, false])
27+
})
28+
29+
it('handle big int array at root', () => {
30+
const shape = t.Array(t.Number())
31+
32+
isEqual(shape, [1n, 2n], [1, 2])
33+
})
34+
35+
it('handle array union at root', () => {
36+
const shape = t.Array(t.Union([t.String(), t.Number()]))
37+
38+
isEqual(shape, ['a', 'b', 1, 2, 'c'])
39+
})
40+
41+
it('handle array object', () => {
42+
const shape = t.Array(
43+
t.Object({
44+
a: t.String(),
45+
b: t.String()
46+
})
47+
)
48+
49+
isEqual(
50+
shape,
51+
[
52+
{
53+
a: 'a',
54+
b: 'b'
55+
},
56+
{
57+
a: 'a',
58+
b: 'b',
59+
c: 'c'
60+
}
61+
],
62+
[
63+
{
64+
a: 'a',
65+
b: 'b'
66+
},
67+
{
68+
a: 'a',
69+
b: 'b'
70+
}
71+
]
72+
)
73+
})
74+
75+
it('handle array object with optional', () => {
76+
const shape = t.Array(
77+
t.Object({
78+
a: t.String(),
79+
b: t.Optional(t.String())
80+
})
81+
)
82+
83+
isEqual(
84+
shape,
85+
[
86+
{
87+
a: 'a'
88+
},
89+
{
90+
a: 'a',
91+
b: 'b'
92+
},
93+
{
94+
a: 'a',
95+
b: 'b',
96+
c: 'c'
97+
}
98+
],
99+
[
100+
{ a: 'a' },
101+
{
102+
a: 'a',
103+
b: 'b'
104+
},
105+
{
106+
a: 'a',
107+
b: 'b'
108+
}
109+
]
110+
)
111+
})
112+
})

0 commit comments

Comments
 (0)