piotr-oles/rsql

Allow customizing the quoting behavior of emit.

TimHambourger opened this issue · 1 comments

Is your feature request related to a problem? Please describe.
This is definitely a nice-to-have, not an urgent need. Something I noticed while working on #32 is that the emitter has good internal support for using either quote character (double or single) to escape comparison values, but that emit itself currently only supports using the double quote character. Among other things, this means the emitted RSQL can end up being longer than it it needs to be if the AST being emitted contains a lot of comparison values with double quotes in them.

Describe the solution you'd like
I'd propose adding an optional second parameter to emit giving it the following signature:

type Quote = '"' | "'";

interface EmitOptions {
  /**
   * The preferred quote character to use when `emit` encounters a comparison value that needs to be escaped by wrapping
   * in quotes. Either `"` (the ASCII double quote character) or `'` (the ASCII single quote character). Defaults to `"`
   * (the ASCII double quote character).
   */
  preferredQuote?: Quote;
  /**
   * If `true`, `emit` will override the `preferredQuote` setting on a comparison value-by-comparison value basis if
   * doing so would shorten the emitted RSQL. If `false`, `emit` will use the `preferredQuote` as the quote character
   * for all comparison values encountered. Defaults to `true`.
   */
  optimizeQuotes?: boolean;
}

function emit(expression: ExpressionNode, opts?: EmitOptions): string;

I'll submit a PR for this in just a min.

Implemented in #35