Skip to content

Commit

Permalink
Add early return to accumulator fn
Browse files Browse the repository at this point in the history
  • Loading branch information
jliuhtonen committed Aug 31, 2024
1 parent d9f9084 commit 2d89a32
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ const appendCurrentVariableToQueryFn =
currentQueryPart: string,
currentQueryPartIndex: number,
): AccumulatedQueryState => {
const isLastQueryPart = currentQueryPartIndex === numberOfQueryParts - 1
if (isLastQueryPart) {
return {
currentVariableIndex: acc.currentVariableIndex,
text: `${acc.text}${currentQueryPart}`,
values: acc.values,
}
}

const currentValue = queryPartValues[currentQueryPartIndex]
if (isSqlFragment(currentValue)) {
const {
Expand Down Expand Up @@ -48,18 +57,12 @@ const appendCurrentVariableToQueryFn =
text: `${acc.text}${currentQueryPart}${arrayVars}`,
values: [...acc.values, ...currentValue],
}
} else if (currentQueryPartIndex < numberOfQueryParts - 1) {
} else {
return {
currentVariableIndex: acc.currentVariableIndex + 1,
text: `${acc.text}${currentQueryPart}$${acc.currentVariableIndex}`,
values: [...acc.values, currentValue],
}
} else {
return {
currentVariableIndex: acc.currentVariableIndex,
text: `${acc.text}${currentQueryPart}`,
values: acc.values,
}
}
}

Expand Down

0 comments on commit 2d89a32

Please sign in to comment.