Skip to content

Commit

Permalink
feat: #1, find node by name
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Jul 6, 2020
1 parent a63566c commit ae40496
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@
"typedoc-default-themes": "^0.5.0",
"typedoc-plugin-external-module-name": "^1.1.1",
"typescript": "^3.9.6"
},
"dependencies": {
"@newdash/newdash": "^5.10.0"
}
}
48 changes: 48 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { Token, TokenType } from './lexer';
import { forEach, isArray } from '@newdash/newdash';
import { isPlainObject } from '@newdash/newdash/isPlainObject';

export type SourceArray = number[] | Uint16Array;

export function stringify(value: SourceArray, index: number, next: number): string {
Expand Down Expand Up @@ -31,4 +35,48 @@ export function required(value: SourceArray, index: number, comparer: Function,
return i >= (min || 0) && i <= max ? index + i : 0;
}

export type Traverser = { [key in TokenType]?: (token: Token) => void }

export function createTraverser(traverser: Traverser) {
return function t(node: Token | Array<any> | Object): void {

if (node instanceof Token) {
if (node.type in traverser) {
traverser[node.type](node);
}
}

if (isPlainObject(node) || isArray(node) || node instanceof Token) {
// @ts-ignore
forEach(node, (item) => {
t(item);
});
}


};
}

/**
* find one node by type
* @param node
* @param type
*/
export function findOne(node: Token, type: TokenType): Token {
let rt: Token;
createTraverser({ [type]: (v: Token) => { rt = v; } })(node);
return rt;
}

/**
* find all nodes by type
* @param node
* @param type
*/
export function findAll(node: Token, type: TokenType): Array<Token> {
const rt: Array<Token> = [];
createTraverser({ [type]: (v: Token) => { rt.push(v); } })(node);
return rt;
}

export default { stringify, is, equals, required };
11 changes: 5 additions & 6 deletions test/query.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Parser } from '../src/parser';
import { findOne } from '../src/utils';
import { TokenType } from '../src/lexer';

describe('Query Test Suite', () => {

Expand Down Expand Up @@ -58,12 +60,9 @@ describe('Query Test Suite', () => {


it('should parse $count', () => {
parser.query('$count=true');
const ast = parser.query('$count=true');
const node = findOne(ast, TokenType.InlineCount);
expect(node).not.toBeUndefined();
});

// it('should parser $query', () => {
// parser.query('/Category/$query');
// });


});

0 comments on commit ae40496

Please sign in to comment.