Skip to content

Commit

Permalink
feat: add isSafeUnquotedExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Oct 2, 2022
1 parent 0062329 commit d2e8141
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Lightweight and performant Lucene-like parser, serializer and search engine.
* [Boolean operators](#boolean-operators)
* [Serializer](#serializer)
* [AST](#ast)
* [Utilities](#utilities)
* [Compatibility with Lucene](#compatibility-with-lucene)
* [Recipes](#recipes)
* [Handling syntax errors](#handling-syntax-errors)
Expand Down Expand Up @@ -339,6 +340,21 @@ There are 11 AST tokens that describe a parsed Liqe query.

If you are building a serializer, then you must implement all of them for the complete coverage of all possible query inputs. Refer to the [built-in serializer](./src/serialize.ts) for an example.

## Utilities

```ts
import {
isSafeUnquotedExpression,
} from 'liqe';

/**
* Determines if an expression requires quotes.
* Use this if you need to programmatically manipulate the AST
* before using a serializer to convert the query back to text.
*/
isSafeUnquotedExpression(expression: string): boolean;
```

## Compatibility with Lucene

The following Lucene abilities are not supported:
Expand Down
3 changes: 3 additions & 0 deletions src/Liqe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ export {
export {
serialize,
} from './serialize';
export {
isSafeUnquotedExpression,
} from './isSafeUnquotedExpression';
3 changes: 3 additions & 0 deletions src/isSafeUnquotedExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isSafeUnquotedExpression = (expression: string): boolean => {
return /^[#$*@A-Z_a-z][#$*.@A-Z_a-z-]*$/.test(expression);
};
12 changes: 12 additions & 0 deletions test/liqe/isSafeUnquotedExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import test from 'ava';
import {
isSafeUnquotedExpression,
} from '../../src/isSafeUnquotedExpression';

const testExpression = (t, expected) => {
t.is(isSafeUnquotedExpression(t.title), expected);
};

test('foo', testExpression, true);

test('.foo', testExpression, false);

0 comments on commit d2e8141

Please sign in to comment.