11import { isArray } from 'radashi'
2- import { CharCode } from './charCode.js'
2+ import { CharCode , isDigit } from './charCode.js'
33import { CodableObject , CodableValue } from './types.js'
44
55export type EncodeOptions = {
@@ -66,7 +66,7 @@ function encodeValue(value: CodableValue): string {
6666 }
6767 // For string values, escape the first character if it's a digit or
6868 // minus sign, since those are used to detect a number value.
69- return encodeString ( value , isNumberLike ( value . charCodeAt ( 0 ) ) )
69+ return encodeString ( value , isNumberLike ( value ) )
7070 }
7171 if ( typeof value === 'number' ) {
7272 if ( Number . isNaN ( value ) || ! Number . isFinite ( value ) ) {
@@ -86,6 +86,14 @@ function encodeValue(value: CodableValue): string {
8686 throw new Error ( `Unsupported value type: ${ typeof value } ` )
8787}
8888
89+ function isNumberLike ( value : string ) : boolean {
90+ const charCode = value . charCodeAt ( 0 )
91+ return (
92+ isDigit ( charCode ) ||
93+ ( charCode === CharCode . Minus && isDigit ( value . charCodeAt ( 1 ) ) )
94+ )
95+ }
96+
8997function encodeArray ( array : readonly CodableValue [ ] ) : string {
9098 let result = ''
9199 for ( let i = 0 ; i < array . length ; i ++ ) {
@@ -106,13 +114,6 @@ function encodeObject(obj: CodableObject): string {
106114 return `{${ encodeProperties ( obj , true ) } }`
107115}
108116
109- function isNumberLike ( charCode : number ) : boolean {
110- return (
111- ( charCode >= CharCode . DigitMin && charCode <= CharCode . DigitMax ) ||
112- charCode === CharCode . Minus
113- )
114- }
115-
116117function encodeString ( str : string , escape ?: boolean ) : string {
117118 // Regardless of the escape flag, we always escape backslashes.
118119 let result = escape || str . charCodeAt ( 0 ) === CharCode . Escape ? '\\' : ''
0 commit comments