cspell-trie-lib
Advanced tools
Comparing version 4.1.7 to 4.1.8
@@ -5,5 +5,22 @@ import { Sequence } from 'gensequence'; | ||
import { WalkerIterator } from './walker'; | ||
export interface TrieOptions { | ||
compoundCharacter: string; | ||
stripCaseAndAccentsPrefix: string; | ||
forbiddenWordPrefix: string; | ||
} | ||
export declare const defaultTrieOptions: TrieOptions; | ||
declare type Optional<T> = { | ||
[key in keyof T]?: T[key]; | ||
}; | ||
declare type PartialTrieOptions = Optional<TrieOptions> | undefined; | ||
export declare class Trie { | ||
readonly root: TrieNode; | ||
constructor(root: TrieNode); | ||
private count?; | ||
private _options; | ||
constructor(root: TrieNode, count?: number | undefined, options?: PartialTrieOptions); | ||
/** | ||
* Number of words in the Trie | ||
*/ | ||
size(): number; | ||
get options(): TrieOptions; | ||
find(text: string, minCompoundLength?: boolean | number): TrieNode | undefined; | ||
@@ -47,3 +64,4 @@ findCompound(text: string, minCompoundLength?: number, minLength?: number): TrieNode | undefined; | ||
insert(word: string): this; | ||
static create(words: Iterable<string> | IterableIterator<string>): Trie; | ||
static create(words: Iterable<string> | IterableIterator<string>, options?: PartialTrieOptions): Trie; | ||
} | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const gensequence_1 = require("gensequence"); | ||
const TrieNode_1 = require("./TrieNode"); | ||
const suggest_1 = require("./suggest"); | ||
const util_1 = require("./util"); | ||
const walker_1 = require("./walker"); | ||
const _defaultTrieOptions = { | ||
compoundCharacter: '+', | ||
stripCaseAndAccentsPrefix: '~', | ||
forbiddenWordPrefix: '!' | ||
}; | ||
exports.defaultTrieOptions = Object.freeze(_defaultTrieOptions); | ||
function mergeOptionalWithDefaults(options) { | ||
const { compoundCharacter = exports.defaultTrieOptions.compoundCharacter, stripCaseAndAccentsPrefix = exports.defaultTrieOptions.stripCaseAndAccentsPrefix, forbiddenWordPrefix = exports.defaultTrieOptions.forbiddenWordPrefix, } = options || {}; | ||
return { compoundCharacter, stripCaseAndAccentsPrefix, forbiddenWordPrefix }; | ||
} | ||
class Trie { | ||
constructor(root) { | ||
constructor(root, count, options) { | ||
this.root = root; | ||
// The root can be a word | ||
if (root.f) { | ||
root.f = root.f ? (root.f & ~TrieNode_1.FLAG_WORD) : root.f; | ||
} | ||
this.count = count; | ||
this._options = mergeOptionalWithDefaults(options); | ||
} | ||
/** | ||
* Number of words in the Trie | ||
*/ | ||
size() { | ||
var _a; | ||
this.count = (_a = this.count, (_a !== null && _a !== void 0 ? _a : util_1.countWords(this.root))); | ||
return this.count; | ||
} | ||
get options() { | ||
return this._options; | ||
} | ||
find(text, minCompoundLength = false) { | ||
@@ -100,6 +118,6 @@ const minLength = !minCompoundLength || minCompoundLength === true ? undefined : minCompoundLength; | ||
} | ||
static create(words) { | ||
static create(words, options) { | ||
const root = util_1.createTriFromList(words); | ||
util_1.orderTrie(root); | ||
return new Trie(root); | ||
return new Trie(root, undefined, options); | ||
} | ||
@@ -106,0 +124,0 @@ } |
@@ -25,2 +25,3 @@ import { Sequence } from 'gensequence'; | ||
export declare function countNodes(root: TrieNode): number; | ||
export declare function countWords(root: TrieNode): number; | ||
export declare function isCircular(root: TrieNode): boolean; |
@@ -104,2 +104,23 @@ "use strict"; | ||
exports.countNodes = countNodes; | ||
function countWords(root) { | ||
const visited = new Map(); | ||
function walk(n) { | ||
if (visited.has(n)) { | ||
return visited.get(n); | ||
} | ||
let cnt = n.f ? 1 : 0; | ||
// add the node to the set to avoid getting stuck on circular references. | ||
visited.set(n, cnt); | ||
if (!n.c) { | ||
return cnt; | ||
} | ||
for (const c of n.c.values()) { | ||
cnt += walk(c); | ||
} | ||
visited.set(n, cnt); | ||
return cnt; | ||
} | ||
return walk(root); | ||
} | ||
exports.countWords = countWords; | ||
function isCircular(root) { | ||
@@ -106,0 +127,0 @@ const seen = new Set(); |
{ | ||
"name": "cspell-trie-lib", | ||
"version": "4.1.7", | ||
"version": "4.1.8", | ||
"description": "Trie Data Structure to support cspell.", | ||
@@ -17,3 +17,3 @@ "main": "dist/index.js", | ||
"coverage": "jest --coverage", | ||
"test-watch": "../../node_modules/.bin/jest --watch", | ||
"test-watch": "jest --watch", | ||
"test": "jest", | ||
@@ -37,3 +37,3 @@ "watch": "tsc -w" | ||
"dependencies": { | ||
"gensequence": "^3.0.1" | ||
"gensequence": "^3.0.3" | ||
}, | ||
@@ -61,3 +61,3 @@ "nyc": { | ||
}, | ||
"gitHead": "73f2223b8b222203c2e996d865564ef85fb67075" | ||
"gitHead": "cbe103ba540d0ec7af38876372722802f80f4098" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
78519
2118
Updatedgensequence@^3.0.3