-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from gadget-inc/watcher
Switch to watcher from chokidar for better performance
- Loading branch information
Showing
6 changed files
with
152 additions
and
65 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,24 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { PathTrie } from "../src/PathTrie.js"; | ||
|
||
describe("PathTrie", () => { | ||
it("should insert and search for paths", () => { | ||
const trie = new PathTrie(); | ||
trie.insert("foo/bar"); | ||
expect(trie.contains("foo/bar")).toBe(true); | ||
expect(trie.contains("foo/baz")).toBe(false); | ||
}); | ||
|
||
it("should check if any paths in the trie start with a given prefix", () => { | ||
const trie = new PathTrie(); | ||
trie.insert("foo/bar"); | ||
trie.insert("foo/baz"); | ||
trie.insert("foo/qux/corge"); | ||
expect(trie.anyStartsWith("foo")).toBe(true); | ||
expect(trie.anyStartsWith("foo/bar")).toBe(true); | ||
expect(trie.anyStartsWith("foo/corge")).toBe(false); | ||
expect(trie.anyStartsWith("qux")).toBe(false); | ||
expect(trie.anyStartsWith("foo/qux")).toBe(true); | ||
expect(trie.anyStartsWith("foo/qux/corge")).toBe(true); | ||
}); | ||
}); |
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,58 @@ | ||
class TrieNode { | ||
children: Record<string, TrieNode> = {}; | ||
isEndOfWord = false; | ||
} | ||
|
||
/** | ||
* Prefix matching datastructure for holding paths and seeing if any match a given incoming path | ||
**/ | ||
export class PathTrie { | ||
root = new TrieNode(); | ||
seen = new Set<string>(); | ||
|
||
insert(path: string) { | ||
if (this.seen.has(path)) { | ||
return; | ||
} | ||
let node = this.root; | ||
const segments = path.split("/"); | ||
for (const segment of segments) { | ||
if (!node.children[segment]) { | ||
node.children[segment] = new TrieNode(); | ||
} | ||
node = node.children[segment]; | ||
} | ||
this.seen.add(path); | ||
node.isEndOfWord = true; | ||
} | ||
|
||
/** | ||
* Has the incoming path been inserted into the trie? | ||
*/ | ||
contains(path: string) { | ||
let node = this.root; | ||
const segments = path.split("/"); | ||
for (const segment of segments) { | ||
if (!node.children[segment]) { | ||
return false; | ||
} | ||
node = node.children[segment]; | ||
} | ||
return node.isEndOfWord; | ||
} | ||
|
||
/** | ||
* Do any paths in the trie start with the given prefix? | ||
*/ | ||
anyStartsWith(prefix: string) { | ||
let node = this.root; | ||
const segments = prefix.split("/"); | ||
for (const segment of segments) { | ||
if (!node.children[segment]) { | ||
return false; | ||
} | ||
node = node.children[segment]; | ||
} | ||
return true; | ||
} | ||
} |
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
Oops, something went wrong.