Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serialize function #51

Open
punkpeye opened this issue Sep 28, 2022 · 1 comment
Open

Add serialize function #51

punkpeye opened this issue Sep 28, 2022 · 1 comment

Comments

@punkpeye
Copy link

It would be nice to be able to re-build query using offsets.

@punkpeye
Copy link
Author

I tried implementing this but ran into a few limitations:

  • No way to tell if text is quoted
  • No way to tell what quotes text is using

This is what I've got so far:

type SearchParserOffset = (
  | SearchParserKeyWordOffset
  | SearchParserTextOffset
) & {
  offsetStart: number;
  offsetEnd: number;
};

type SearchParserKeyWordOffset = {
  keyword: string;
  value?: string;
};

type SearchParserTextOffset = {
  text: string;
};

export const serializeSearchQuery = (offsets: SearchParserOffset[]) => {
  let searchQuery = "";

  for (const offset of offsets) {
    const gapSize = offset.offsetStart - searchQuery.length;

    if (gapSize > 0) {
      searchQuery += Array.from({ length: gapSize }).fill(" ").join("");
    }

    if ("text" in offset) {
      if (offset.text.includes(" ")) {
        if (offset.text.includes(`'`) && !offset.text.includes(`"`)) {
          searchQuery += `"${offset.text}"`;
        } else {
          searchQuery += `'${offset.text.replaceAll(`'`, `\'`)}'`;
        }
      } else {
        searchQuery += offset.text;
      }
    } else {
      searchQuery += offset.keyword + ":" + offset.value;
    }
  }

  return searchQuery;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant