@@ -20,67 +20,68 @@ export class Sql {
20
20
rawStrings : ReadonlyArray < string > ,
21
21
rawValues : ReadonlyArray < RawValue >
22
22
) {
23
- let valuesLength = rawValues . length ;
24
- let stringsLength = rawStrings . length ;
25
-
26
- if ( stringsLength === 0 ) {
27
- throw new TypeError ( "Expected at least 1 string" ) ;
28
- }
23
+ if ( rawStrings . length - 1 !== rawValues . length ) {
24
+ if ( rawStrings . length === 0 ) {
25
+ throw new TypeError ( "Expected at least 1 string" ) ;
26
+ }
29
27
30
- if ( stringsLength - 1 !== valuesLength ) {
31
28
throw new TypeError (
32
- `Expected ${ stringsLength } strings to have ${ stringsLength - 1 } values`
29
+ `Expected ${ rawStrings . length } strings to have ${
30
+ rawStrings . length - 1
31
+ } values`
33
32
) ;
34
33
}
35
34
36
- for ( const child of rawValues ) {
37
- if ( child instanceof Sql ) {
38
- valuesLength += child . values . length - 1 ;
39
- stringsLength += child . strings . length - 2 ;
40
- }
41
- }
35
+ const valuesLength = rawValues . reduce < number > (
36
+ ( len , value ) => len + ( value instanceof Sql ? value . values . length : 1 ) ,
37
+ 0
38
+ ) ;
42
39
43
40
this . values = new Array ( valuesLength ) ;
44
- this . strings = new Array ( stringsLength ) ;
41
+ this . strings = new Array ( valuesLength + 1 ) ;
45
42
46
43
this . strings [ 0 ] = rawStrings [ 0 ] ;
47
44
48
45
// Iterate over raw values, strings, and children. The value is always
49
46
// positioned between two strings, e.g. `index + 1`.
50
- let index = 1 ;
51
- let position = 0 ;
52
- while ( index < rawStrings . length ) {
53
- const child = rawValues [ index - 1 ] ;
54
- const rawString = rawStrings [ index ++ ] ;
47
+ let i = 0 ,
48
+ pos = 0 ;
49
+ while ( i < rawValues . length ) {
50
+ const child = rawValues [ i ++ ] ;
51
+ const rawString = rawStrings [ i ] ;
55
52
56
53
// Check for nested `sql` queries.
57
54
if ( child instanceof Sql ) {
58
55
// Append child prefix text to current string.
59
- this . strings [ position ] += child . strings [ 0 ] ;
56
+ this . strings [ pos ] += child . strings [ 0 ] ;
60
57
61
58
let childIndex = 0 ;
62
59
while ( childIndex < child . values . length ) {
63
- this . values [ position ++ ] = child . values [ childIndex ++ ] ;
64
- this . strings [ position ] = child . strings [ childIndex ] ;
60
+ this . values [ pos ++ ] = child . values [ childIndex ++ ] ;
61
+ this . strings [ pos ] = child . strings [ childIndex ] ;
65
62
}
66
63
67
64
// Append raw string to current string.
68
- this . strings [ position ] += rawString ;
65
+ this . strings [ pos ] += rawString ;
69
66
} else {
70
- this . values [ position ++ ] = child ;
71
- this . strings [ position ] = rawString ;
67
+ this . values [ pos ++ ] = child ;
68
+ this . strings [ pos ] = rawString ;
72
69
}
73
70
}
74
71
}
75
72
76
73
get text ( ) {
77
- return this . strings . reduce (
78
- ( text , part , index ) => `${ text } $${ index } ${ part } `
79
- ) ;
74
+ let i = 1 ,
75
+ value = this . strings [ 0 ] ;
76
+ while ( i < this . strings . length ) value += `$${ i } ${ this . strings [ i ++ ] } ` ;
77
+ return value ;
80
78
}
81
79
82
80
get sql ( ) {
83
- return this . strings . join ( "?" ) ;
81
+ let i = 1 ,
82
+ value = this . strings [ 0 ] ;
83
+ while ( i < this . strings . length ) value += `?${ this . strings [ i ++ ] } ` ;
84
+ return value ;
84
85
}
85
86
86
87
inspect ( ) {
0 commit comments