Skip to content

Commit fd56be9

Browse files
committed
🎉 feat: support integer, bigint, date, datetime
1 parent a0adfb7 commit fd56be9

File tree

5 files changed

+104
-11
lines changed

5 files changed

+104
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
# 0.0.2
2+
Feature:
3+
- support integer, bigint, date, datetime
4+
15
# 0.0.1 - 3 Feb 2025
26
Bug fix:
3-
- Separate optional comma flag between closure
7+
- separate optional comma flag between closure
48

59
# 0.0.0 - 3 Feb 2025
610
Feature:
7-
- Initial release
11+
- initial release

benchmarks/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export const benchmark = <T extends TAnySchema>(
1616
throw new Error('Invalid result')
1717
}
1818

19+
if (process.env.DEBUG) console.log(encode.toString())
20+
1921
barplot(() => {
2022
summary(() => {
2123
bench('JSON Stingify', () => {

example/index.ts

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

4-
const shape = t.Object({
5-
name: t.String(),
6-
id: t.Number()
7-
})
4+
const shape = t.Object({ a: t.Date() })
85

9-
const value = {
10-
id: 0,
11-
name: 'saltyaom'
12-
} satisfies typeof shape.static
6+
const value = { a: new Date() } satisfies typeof shape.static
137

148
const stringify = createAccelerator(shape)
159

src/index.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ export const mergeObjectIntersection = (schema: TAnySchema): TAnySchema => {
8989
return newSchema
9090
}
9191

92+
const isDateType = (schema: TAnySchema): boolean => {
93+
if (!schema.anyOf || (Kind in schema && schema[Kind] !== 'Union'))
94+
return false
95+
96+
if (!schema.anyOf) return false
97+
98+
let hasDateType = false
99+
let hasStringFormatDate = false
100+
let hasNumberType = false
101+
102+
if (schema.anyOf)
103+
for (const type of schema.anyOf) {
104+
if (!hasDateType && type.type === 'Date') hasDateType = true
105+
106+
if (
107+
!hasStringFormatDate &&
108+
type.type === 'string' &&
109+
(type.format === 'date' || type.format === 'date-time')
110+
)
111+
hasStringFormatDate = true
112+
113+
if (!hasNumberType && type.type === 'number') hasNumberType = true
114+
}
115+
116+
return hasDateType
117+
}
118+
92119
interface Instruction {
93120
array: number
94121
optional: number
@@ -128,6 +155,8 @@ const accelerate = (
128155

129156
case 'number':
130157
case 'boolean':
158+
case 'integer':
159+
case 'bigint':
131160
if (nullableCondition) v = `\${${property}??null}`
132161
else v = `\${${property}}`
133162
break
@@ -212,7 +241,9 @@ const accelerate = (
212241

213242
if (
214243
schema.items.type === 'number' ||
215-
schema.items.type === 'boolean'
244+
schema.items.type === 'boolean' ||
245+
schema.items.type === 'integer' ||
246+
schema.items.type === 'bigint'
216247
) {
217248
if (nullableCondition)
218249
v += `\${${nullableCondition}?null:${property}.length?\`[$\{${property}.join(',')}]\`:"[]"`
@@ -243,6 +274,16 @@ const accelerate = (
243274
break
244275

245276
default:
277+
if (isDateType(schema)) {
278+
if (isNullable || isUndefinable)
279+
v = `\${${nullableCondition}?null:typeof ${property}==="object"?\`\"\${${property}.toISOString()}\"\`:${property}}`
280+
else {
281+
v = `\${typeof ${property}==="object"?\`\"\${${property}.toISOString()}\"\`:${property}}`
282+
}
283+
284+
break
285+
}
286+
246287
v = `$\{JSON.stringify(${property})}`
247288

248289
break

test/index.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,58 @@ describe('Core', () => {
5656
expect(createAccelerator(shape)(value)).toBe('')
5757
})
5858

59+
it('handle date', () => {
60+
const shape = t.Date()
61+
62+
const value = new Date() satisfies typeof shape.static
63+
64+
expect(createAccelerator(shape)(value)).toBe(`"${value.toISOString()}"`)
65+
})
66+
67+
it('handle date timestamp', () => {
68+
const shape = t.Date()
69+
70+
const value = Date.now()
71+
72+
isEqual(shape, value)
73+
})
74+
75+
it('handle nullable date', () => {
76+
const shape = t.Nullable(t.Date())
77+
78+
const value = new Date() satisfies typeof shape.static
79+
expect(createAccelerator(shape)(value)).toBe(`"${value.toISOString()}"`)
80+
81+
const value2 = null satisfies typeof shape.static
82+
isEqual(shape, value2)
83+
})
84+
85+
it('handle nullable date timestamp', () => {
86+
const shape = t.Nullable(t.Date())
87+
88+
const value = 2
89+
isEqual(shape, value)
90+
91+
const value2 = null satisfies typeof shape.static
92+
isEqual(shape, value2)
93+
})
94+
95+
it('handle integer', () => {
96+
const shape = t.Integer()
97+
98+
const value = 2.2 satisfies typeof shape.static
99+
100+
isEqual(shape, value)
101+
})
102+
103+
it('handle bigint', () => {
104+
const shape = t.BigInt()
105+
106+
const value = BigInt(2) satisfies typeof shape.static
107+
108+
isEqual(shape, +(value + ''))
109+
})
110+
59111
it('handle object', () => {
60112
const shape = t.Object({
61113
name: t.String(),

0 commit comments

Comments
 (0)