Skip to content

Commit b6d0684

Browse files
committed
fix(types): use separate type for decoded values
Decoded values are never undefined and decoded arrays should be mutable.
1 parent 0780015 commit b6d0684

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/decode.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CharCode, isDigit } from './charCode.js'
2-
import { CodableObject, CodableValue } from './types.js'
2+
import { DecodedObject, DecodedValue } from './types.js'
33

44
type URLSearchParams = typeof globalThis extends {
55
URLSearchParams: infer T extends abstract new (...args: any) => any
@@ -10,8 +10,8 @@ type URLSearchParams = typeof globalThis extends {
1010
get(key: string): string | null
1111
}
1212

13-
export function decode(input: URLSearchParams): CodableObject {
14-
const result: CodableObject = {}
13+
export function decode(input: URLSearchParams): DecodedObject {
14+
const result: DecodedObject = {}
1515
let key: string | undefined
1616
try {
1717
for (key of input.keys()) {
@@ -37,18 +37,18 @@ const enum ValueMode {
3737
String,
3838
}
3939

40-
const constantsMap: Record<string, CodableValue> = {
40+
const constantsMap: Record<string, DecodedValue> = {
4141
null: null,
4242
false: false,
4343
true: true,
4444
}
4545

46-
function decodeValue(input: string, cursor = { pos: 0 }): CodableValue {
46+
function decodeValue(input: string, cursor = { pos: 0 }): DecodedValue {
4747
const startPos = cursor.pos
4848
const nested = startPos > 0
4949

5050
let mode: number = ValueMode.Unknown
51-
let result: CodableValue
51+
let result: DecodedValue | undefined
5252

5353
let pos = startPos
5454
let charCode = input.charCodeAt(pos)
@@ -139,7 +139,7 @@ function decodeValue(input: string, cursor = { pos: 0 }): CodableValue {
139139
}
140140

141141
case ValueMode.Array: {
142-
const array: CodableValue[] = []
142+
const array: DecodedValue[] = []
143143

144144
while (++pos < input.length) {
145145
charCode = input.charCodeAt(pos)
@@ -166,7 +166,7 @@ function decodeValue(input: string, cursor = { pos: 0 }): CodableValue {
166166
}
167167

168168
case ValueMode.Object: {
169-
const object: CodableObject = {}
169+
const object: DecodedObject = {}
170170
let key = ''
171171
let keyPos = pos + 1
172172
let open = true

src/types.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
export type CodableObject = { toJSON(): CodableValue } | CodableRecord
1+
type JsonPrimitive = string | number | boolean | null
2+
type CodablePrimitive = JsonPrimitive | bigint | undefined
3+
type DecodedPrimitive = JsonPrimitive | bigint
24

5+
export type CodableObject = { toJSON(): CodableValue } | CodableRecord
36
export type CodableRecord = { [key: string]: CodableValue }
4-
57
export type CodableValue =
6-
| string
7-
| number
8-
| boolean
9-
| bigint
10-
| null
11-
| undefined
12-
| CodableObject
138
| readonly CodableValue[]
9+
| CodableObject
10+
| CodablePrimitive
11+
12+
export type DecodedObject = { [key: string]: DecodedValue }
13+
export type DecodedValue = DecodedObject | DecodedValue[] | DecodedPrimitive

0 commit comments

Comments
 (0)