Skip to content

Commit d2e8141

Browse files
committed
feat: add isSafeUnquotedExpression
1 parent 0062329 commit d2e8141

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Lightweight and performant Lucene-like parser, serializer and search engine.
1919
* [Boolean operators](#boolean-operators)
2020
* [Serializer](#serializer)
2121
* [AST](#ast)
22+
* [Utilities](#utilities)
2223
* [Compatibility with Lucene](#compatibility-with-lucene)
2324
* [Recipes](#recipes)
2425
* [Handling syntax errors](#handling-syntax-errors)
@@ -339,6 +340,21 @@ There are 11 AST tokens that describe a parsed Liqe query.
339340

340341
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.
341342

343+
## Utilities
344+
345+
```ts
346+
import {
347+
isSafeUnquotedExpression,
348+
} from 'liqe';
349+
350+
/**
351+
* Determines if an expression requires quotes.
352+
* Use this if you need to programmatically manipulate the AST
353+
* before using a serializer to convert the query back to text.
354+
*/
355+
isSafeUnquotedExpression(expression: string): boolean;
356+
```
357+
342358
## Compatibility with Lucene
343359

344360
The following Lucene abilities are not supported:

src/Liqe.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ export {
3535
export {
3636
serialize,
3737
} from './serialize';
38+
export {
39+
isSafeUnquotedExpression,
40+
} from './isSafeUnquotedExpression';

src/isSafeUnquotedExpression.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const isSafeUnquotedExpression = (expression: string): boolean => {
2+
return /^[#$*@A-Z_a-z][#$*.@A-Z_a-z-]*$/.test(expression);
3+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import test from 'ava';
2+
import {
3+
isSafeUnquotedExpression,
4+
} from '../../src/isSafeUnquotedExpression';
5+
6+
const testExpression = (t, expected) => {
7+
t.is(isSafeUnquotedExpression(t.title), expected);
8+
};
9+
10+
test('foo', testExpression, true);
11+
12+
test('.foo', testExpression, false);

0 commit comments

Comments
 (0)