Skip to content

Latest commit

 

History

History
409 lines (232 loc) · 9.62 KB

README.md

File metadata and controls

409 lines (232 loc) · 9.62 KB

@kamilmielnik/trie

@kamilmielnik/trie

Table of contents

Classes

Interfaces

Type Aliases

Variables

Functions

Type Aliases

Descendant

Ƭ Descendant: Object

Type declaration

Name Type
node Node
prefix string

Defined in

types.ts:11


TraverseCallback

Ƭ TraverseCallback: (descendant: Descendant) => boolean | void

Type declaration

▸ (descendant): boolean | void

Parameters
Name Type
descendant Descendant
Returns

boolean | void

Defined in

types.ts:9


TraverseOptions

Ƭ TraverseOptions: Object

Type declaration

Name Type Description
prefix? string Set the prefix to be applied to all descendants. It should be the prefix represented by the Node at which traversing starts. Defaults to empty string.
sort? boolean Set to true to visit Nodes in alphabetical order. Defaults to false.
wordsOnly? boolean Set to true to only visit Nodes representing complete words. Defaults to false.

Defined in

types.ts:16

Variables

CLOSE_PARENS

Const CLOSE_PARENS: ")"

Represents end of a node in serialized format.

Defined in

constants.ts:4


OPEN_PARENS

Const OPEN_PARENS: "("

Represents start of a node in serialized format.

Defined in

constants.ts:9

Functions

add

add(node, word): Node

Inserts given word into given node.

Parameters

Name Type Description
node Node Node to insert the word to.
word string Word to be inserted into node.

Returns

Node

Node representing the end of the added word.

Defined in

lib/add.ts:10


deserialize

deserialize(serialized): Node

Creates a new Node by deserializing given string.

The inverse of serialize.

Parameters

Name Type Description
serialized string String with value returned by serialize.

Returns

Node

Instance of a root Node of deserialized string.

Defined in

lib/deserialize.ts:12


find

find(node, prefix): undefined | Node

Finds Node representing given prefix in given Node.

Parameters

Name Type Description
node Node Node to look for prefix in.
prefix string Prefix to be found.

Returns

undefined | Node

Node representing a given prefix, undefined if there is no such node.

Defined in

lib/find.ts:10


fromArray

fromArray(words): Node

Creates a new Node based on array of words.

Parameters

Name Type
words string[]

Returns

Node

New Node containing all given words.

Params

words - array of words to put in the Node.

Defined in

lib/fromArray.ts:11


has

has(node, word): boolean

Tells you whether given word is in the Node.

Parameters

Name Type Description
node Node Node to look for word in.
word string Word to be found.

Returns

boolean

true if given word is in the Node, false otherwise.

Defined in

lib/has.ts:12


hasPrefix

hasPrefix(node, prefix): boolean

Tells you whether there are any words with given prefix in the Node.

See: https://en.wikipedia.org/wiki/String_operations#Prefixes

Parameters

Name Type Description
node Node Node to look for prefix in.
prefix string Prefix to be found.

Returns

boolean

true if there are any words with given prefix in the Node, false otherwise.

Defined in

lib/hasPrefix.ts:14


nodeKeyComparator

nodeKeyComparator(key1, key2): number

Comparator that allows sorting Node keys alphabetically with the exception of "wordEnd" which should always come first.

Parameters

Name Type Description
key1 string First key to compare.
key2 string Second key to compare.

Returns

number

-1 if key1 should precede key2, 0 if they're the same, 1 if key2 should precede key1.

Defined in

lib/nodeKeyComparator.ts:9


remove

remove(node, word): boolean

Removes given word from given Node if it exists.

Parameters

Name Type Description
node Node Node to remove word from.
word string Word to be removed.

Returns

boolean

true if the word was removed, false otherwise.

Defined in

lib/remove.ts:10


serialize

serialize(node): string

Converts given Node into a string.

The inverse of deserialize.

It serializes 42.8 MB Polish dictionary down to 18.7 MB (-56%).

It serializes 1.9 MB English (US) dictionary down to 1.4 MB (-30%).

It serializes 3 MB English (GB) dictionary down to 2 MB (-32%).

Parameters

Name Type Description
node Node Node to serialize.

Returns

string

String with serialized data.

Defined in

lib/serialize.ts:37


toArray

toArray(node, options?): Descendant[]

Finds all descendants of given Node and returns them as an array.

Parameters

Name Type Description
node Node Node to look for descendants in.
options? TraverseOptions See TraverseOptions.

Returns

Descendant[]

An array of descendants.

Defined in

lib/toArray.ts:12


traverse

traverse(node, callback, options?): void

Visits every descendant Node of given Node and calls a callback.

Parameters

Name Type Description
node Node Node to look for descendant Nodes in.
callback TraverseCallback Callback that will be called for each visited Node. Return true from callback to stop traversing.
options TraverseOptions See TraverseOptions.

Returns

void

Defined in

lib/traverse.ts:17