@@ -11,16 +11,16 @@ import {
11
11
} from "../data" ;
12
12
13
13
export function toSurrealqlString ( input : unknown ) : string {
14
- if ( typeof input === "object " ) {
15
- if ( input === null ) return "NULL" ;
16
- if ( input === undefined ) return "NONE" ;
14
+ if ( typeof input === "string " ) return `s ${ JSON . stringify ( input ) } ` ;
15
+ if ( input === null ) return "NULL" ;
16
+ if ( input === undefined ) return "NONE" ;
17
17
18
+ if ( typeof input === "object" ) {
18
19
// We explicitely use string prefixes to ensure compability with both SurrealDB 1.x and 2.x
19
20
if ( input instanceof Date ) return `d${ JSON . stringify ( input . toISOString ( ) ) } ` ;
20
21
if ( input instanceof Uuid ) return `u${ JSON . stringify ( input . toString ( ) ) } ` ;
21
22
if ( input instanceof RecordId || input instanceof StringRecordId )
22
23
return `r${ JSON . stringify ( input . toString ( ) ) } ` ;
23
- if ( typeof input === "string" ) return `s${ JSON . stringify ( input ) } ` ;
24
24
25
25
if ( input instanceof Geometry ) return toSurrealqlString ( input . toJSON ( ) ) ;
26
26
@@ -38,27 +38,27 @@ export function toSurrealqlString(input: unknown): string {
38
38
switch ( Object . getPrototypeOf ( input ) ) {
39
39
case Object . prototype : {
40
40
let output = "{ " ;
41
- for ( const [ k , v ] of Object . entries ( input as object ) ) {
42
- output += `${ JSON . stringify ( k ) } : ${ toSurrealqlString ( v ) } ,` ;
41
+ const entries = Object . entries ( input as object ) ;
42
+ for ( const [ i , [ k , v ] ] of entries . entries ( ) ) {
43
+ output += `${ JSON . stringify ( k ) } : ${ toSurrealqlString ( v ) } ` ;
44
+ if ( i < entries . length - 1 ) output += ", " ;
43
45
}
44
46
output += " }" ;
45
47
return output ;
46
48
}
47
49
case Map . prototype : {
48
50
let output = "{ " ;
49
- for ( const [ k , v ] of ( input as Map < unknown , unknown > ) . entries ( ) ) {
50
- output += `${ JSON . stringify ( k ) } : ${ toSurrealqlString ( v ) } ,` ;
51
+ const entries = Array . from ( ( input as Map < unknown , unknown > ) . entries ( ) ) ;
52
+ for ( const [ i , [ k , v ] ] of entries . entries ( ) ) {
53
+ output += `${ JSON . stringify ( k ) } : ${ toSurrealqlString ( v ) } ` ;
54
+ if ( i < entries . length - 1 ) output += ", " ;
51
55
}
52
56
output += " }" ;
53
57
return output ;
54
58
}
55
59
case Array . prototype : {
56
- let output = "[ " ;
57
- for ( const v of input as unknown [ ] ) {
58
- output += `${ toSurrealqlString ( v ) } ,` ;
59
- }
60
- output += " ]" ;
61
- return output ;
60
+ const array = ( input as unknown [ ] ) . map ( toSurrealqlString ) ;
61
+ return `[ ${ array . join ( ", " ) } ]` ;
62
62
}
63
63
case Set . prototype : {
64
64
const set = new Set ( [ ...( input as [ ] ) ] . map ( toSurrealqlString ) ) ;
0 commit comments