-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create trie programmatically in options
- Loading branch information
Showing
28 changed files
with
196 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Operators } from '../render/operator' | ||
|
||
export interface Trie { | ||
[key: string]: any; | ||
} | ||
|
||
export function createTrie (operators: Operators): Trie { | ||
const trie: Trie = {} | ||
for (const [name, handler] of Object.entries(operators)) { | ||
let node = trie | ||
|
||
for (let i = 0; i < name.length; i++) { | ||
const c = name[i] | ||
node[c] = node[c] || {} | ||
|
||
if (i === name.length - 1 && c !== '=') { | ||
node[c].needBoundary = true | ||
} | ||
|
||
node = node[c] | ||
} | ||
|
||
node.handler = handler | ||
node.end = true | ||
} | ||
return trie | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
import { expect } from 'chai' | ||
import { matchOperator } from '../../../src/parser/match-operator' | ||
import { defaultOperators } from '../../../src/types' | ||
import { createTrie } from '../../../src/util/operator-trie' | ||
|
||
describe('parser/matchOperator()', function () { | ||
const trie = createTrie(defaultOperators) | ||
it('should match contains', () => { | ||
expect(matchOperator('contains', 0)).to.equal(8) | ||
expect(matchOperator('contains', 0, trie)).to.equal(8) | ||
}) | ||
it('should match comparision', () => { | ||
expect(matchOperator('>', 0)).to.equal(1) | ||
expect(matchOperator('>=', 0)).to.equal(2) | ||
expect(matchOperator('<', 0)).to.equal(1) | ||
expect(matchOperator('<=', 0)).to.equal(2) | ||
expect(matchOperator('>', 0, trie)).to.equal(1) | ||
expect(matchOperator('>=', 0, trie)).to.equal(2) | ||
expect(matchOperator('<', 0, trie)).to.equal(1) | ||
expect(matchOperator('<=', 0, trie)).to.equal(2) | ||
}) | ||
it('should match binary logic', () => { | ||
expect(matchOperator('and', 0)).to.equal(3) | ||
expect(matchOperator('or', 0)).to.equal(2) | ||
expect(matchOperator('and', 0, trie)).to.equal(3) | ||
expect(matchOperator('or', 0, trie)).to.equal(2) | ||
}) | ||
it('should not match if word not terminate', () => { | ||
expect(matchOperator('true1', 0)).to.equal(-1) | ||
expect(matchOperator('containsa', 0)).to.equal(-1) | ||
expect(matchOperator('true1', 0, trie)).to.equal(-1) | ||
expect(matchOperator('containsa', 0, trie)).to.equal(-1) | ||
}) | ||
it('should match if word boundary found', () => { | ||
expect(matchOperator('>=1', 0)).to.equal(2) | ||
expect(matchOperator('contains b', 0)).to.equal(8) | ||
expect(matchOperator('>=1', 0, trie)).to.equal(2) | ||
expect(matchOperator('contains b', 0, trie)).to.equal(8) | ||
}) | ||
}) |
Oops, something went wrong.