mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-22 02:56:01 +00:00
filter parse - ts
This commit is contained in:
1
packages/filterparser/.gitignore
vendored
Normal file
1
packages/filterparser/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
lib
|
||||
25
packages/filterparser/package.json
Normal file
25
packages/filterparser/package.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"version": "0.1.0",
|
||||
"name": "@dbgate/filterparser",
|
||||
"main": "lib/index.js",
|
||||
"typings": "lib/index.d.ts",
|
||||
"scripts": {
|
||||
"prepare": "yarn build",
|
||||
"build": "tsc",
|
||||
"start": "tsc --watch",
|
||||
"test": "jest"
|
||||
},
|
||||
"files": [
|
||||
"lib"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@dbgate/types": "^0.1.0",
|
||||
"@types/node": "^13.7.0",
|
||||
"typescript": "^3.7.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/parsimmon": "^1.10.1",
|
||||
"lodash": "^4.17.15",
|
||||
"parsimmon": "^1.13.0"
|
||||
}
|
||||
}
|
||||
44
packages/filterparser/src/parseFilter.ts
Normal file
44
packages/filterparser/src/parseFilter.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import P from 'parsimmon';
|
||||
import { FilterType } from './types';
|
||||
|
||||
const whitespace = P.regexp(/\s*/m);
|
||||
|
||||
function token(parser) {
|
||||
return parser.skip(whitespace);
|
||||
}
|
||||
|
||||
function interpretEscapes(str) {
|
||||
let escapes = {
|
||||
b: '\b',
|
||||
f: '\f',
|
||||
n: '\n',
|
||||
r: '\r',
|
||||
t: '\t',
|
||||
};
|
||||
return str.replace(/\\(u[0-9a-fA-F]{4}|[^u])/, (_, escape) => {
|
||||
let type = escape.charAt(0);
|
||||
let hex = escape.slice(1);
|
||||
if (type === 'u') {
|
||||
return String.fromCharCode(parseInt(hex, 16));
|
||||
}
|
||||
if (escapes.hasOwnProperty(type)) {
|
||||
return escapes[type];
|
||||
}
|
||||
return type;
|
||||
});
|
||||
}
|
||||
|
||||
const parser = P.createLanguage({
|
||||
expr: r => P.alt(r.string),
|
||||
|
||||
string: () =>
|
||||
token(P.regexp(/"((?:\\.|.)*?)"/, 1))
|
||||
.map(interpretEscapes)
|
||||
.desc('string'),
|
||||
});
|
||||
|
||||
export function parseFilter(value: string, filterType: FilterType) {
|
||||
const ast = parser.expr.tryParse(value);
|
||||
console.log(ast);
|
||||
return ast;
|
||||
}
|
||||
3
packages/filterparser/src/parserFilter.test.ts
Normal file
3
packages/filterparser/src/parserFilter.test.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import parserFilter, { parseFilter } from './parseFilter';
|
||||
|
||||
test('parse string', parseFilter('"123"', 'string'));
|
||||
3
packages/filterparser/src/types.ts
Normal file
3
packages/filterparser/src/types.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
// import types from '@dbgate/types';
|
||||
|
||||
export type FilterType = 'number' | 'string' | 'datetime' | 'logical';
|
||||
14
packages/filterparser/tsconfig.json
Normal file
14
packages/filterparser/tsconfig.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2015",
|
||||
"module": "commonjs",
|
||||
"declaration": true,
|
||||
"skipLibCheck": true,
|
||||
"outDir": "lib",
|
||||
"preserveWatchOutput": true,
|
||||
"esModuleInterop": true
|
||||
},
|
||||
"include": [
|
||||
"src/**/*"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user