Options
All
  • Public
  • Public/Protected
  • All
Menu

A sorted string set that implements a superset of the standard JS Set interface. Supports approximate matching and allows serialization to/from a binary format.

The string set can store any valid Unicode string, including the empty string, strings that include characters from the supplementary (or "astral") planes, and so on.

Strings are stored using a ternary search tree, which has well-balanced performance characteristics when properly constructed. Namely, it is important that strings not be added in ascending lexicographic order. (To avoid this when adding strings from a sorted list, use addAll instead of add.)

Hierarchy

  • TernaryStringSet

Implements

  • Set<string>
  • Iterable<string>

Index

Constructors

constructor

  • Creates a new set. The set will be empty unless the optional iterable source object is specified. If a source is provided, all of its elements will be added to the new set. If source contains any element that would cause add() to throw an error, the constructor will also throw an error for that element.

    Note: Since strings are iterable, passing a string to the constructor will create a new set containing one string for each unique code point in the source string, and not a singleton set containing just the source string as you might expect.

    throws

    TypeError if a specified source is not iterable.

    Parameters

    • Optional source: Iterable<string>

      An optional iterable object whose strings will be added to the new set.

    Returns TernaryStringSet

Accessors

[toStringTag]

  • get [toStringTag](): string
  • Returns the string tag used by Object.prototype.toString() for this class.

    Returns string

compacted

  • get compacted(): boolean
  • Returns whether the set is currently compact.

    Returns boolean

    True if the set is compacted, in which case mutating the set could have significant performance implications.

size

  • get size(): number
  • Returns the number of unique strings in this set.

    Returns number

    The non-negative integer number of string elements in the set.

stats

Methods

[iterator]

  • [iterator](): IterableIterator<string>
  • Returns an iterator over the strings in this set, in ascending lexicographic order. As a result, this set can be used in for...of loops and other contexts that expect iterable objects.

    Returns IterableIterator<string>

    An non-null iterator over the strings in this set.

add

  • Adds a string to this set. The string can be empty, but cannot be null. Adding a string that is already present has no effect. If inserting multiple strings in sorted order, prefer addAll over this method.

    throws

    TypeError if the argument is not a string.

    Parameters

    • s: string

      The non-null string to add.

    Returns TernaryStringSet

    This set, allowing chained calls.

addAll

  • Adds zero or more strings to this set.

    If the collection is sorted in ascending order and no other strings have been added to this set, the underlying tree is guaranteed to be balanced, ensuring good search performance. If the collection is in random order, the tree is likely to be nearly balanced.

    throws

    TypeError if any of the arguments is not a string.

    Parameters

    • Rest ...strings: string[]

      Zero or more strings to be added to the set.

    Returns TernaryStringSet

    This set, allowing chained calls.

  • Adds an entire array, or subarray, of strings to this set. By default, the entire collection is added. If the start and/or end are specified, only the elements in the specified range are added.

    If the collection is sorted in ascending order and no other strings have been added to this set, the underlying tree is guaranteed to be balanced, ensuring good search performance. If the collection is in random order, the tree is likely to be nearly balanced.

    throws

    ReferenceError if the collection is null.

    throws

    TypeError if strings is not an array or if any element is not a string or if the start or end are not integer numbers.

    throws

    RangeError if the start or end are out of bounds, that is, less than 0 or greater than strings.length.

    Parameters

    • strings: readonly string[]

      The non-null collection of strings to add.

    • Optional start: number

      The optional index of the first element to add (inclusive, default is 0).

    • Optional end: number

      The optional index of the last element to add (exclusive, default is strings.length)

    Returns TernaryStringSet

    This set, allowing chained calls.

balance

  • balance(): void
  • Balances the tree structure, minimizing the depth of the tree. This may improve search performance, especially after adding or deleting a large number of strings.

    It is not normally necessary to call this method as long as care was taken not to add large numbers of strings in lexicographic order. That said, two scenarios where this methof may be particularly useful are:

    • If the set will be used in two phases, with strings being added in one phase followed by a phase of extensive search operations.
    • If the string is about to be serialized to a buffer for future use.

    As detailed under addAll, if the entire contents of the set were added by a single call to addAll using a sorted array, the tree is already balanced and calling this method will have no benefit.

    Note: This method undoes the effect of compact(). If you want to balance and compact the tree, be sure to balance it first.

    Returns void

clear

compact

  • compact(): void
  • Compacts the set to reduce its memory footprint and improve search performance. Compaction allows certain nodes of the underlying tree to be shared, effectively converting it to a graph. For large sets, the result is typically a significantly smaller footprint. The tradeoff is that compacted sets cannot be mutated. Any attempt to do so, such as adding or deleting a string, will automatically decompact the set to a its standard tree form, if necessary, before performing the requested operation.

    Compaction is an excellent option if the primary purpose of a set is matching or searching against a fixed string collection. Since compaction and decompaction are both expensive operations, it may not be suitable if the set is expected to be modified intermittently.

    Returns void

delete

  • delete(s: any): boolean
  • Removes the specified string from this set, if it is present. If it is not present, this has no effect. Non-strings are accepted, but treated as if they are not present.

    Parameters

    • s: any

      The non-null string to delete.

    Returns boolean

    True if the string was in this set; false otherwise.

deleteAll

  • deleteAll(...elements: string[]): boolean
  • Removes multiple elements from this set.

    Parameters

    • Rest ...elements: string[]

      The elements to remove.

    Returns boolean

    True if every element was present and was removed.

difference

  • Returns a new set that is the difference of this set and the elements of the specified iterable. The new set will contain all elements that are only members of this set and not both.

    throws

    TypeError if the specified target is not iterable.

    Parameters

    • rhs: Iterable<any>

      The iterable whose elements should be subtracted from this set; the iterable's elements do not have to be strings.

    Returns TernaryStringSet

    A new set containing only those elements in this set and that are not in the specified iterable.

entries

  • entries(): IterableIterator<[string, string]>
  • Returns an iterator over the entries in this set, for compatibility with Map objects. The result is the same as that of values() except that each string is wrapped in an array and repeated twice.

    Note: Unlike standard Sets, this set's values are returned in ascending lexicographic order, not the order in which items were added.

    Returns IterableIterator<[string, string]>

    An iterator over the key-value pairs in this set, with each value acting as its own key.

equals

  • equals(rhs: any): boolean
  • Returns whether this set contains exactly the same elements as the specified iterable. Any object is accepted for comparison; if it is not a set or iterable, the result is always false.

    Parameters

    • rhs: any

      The set (or other object) to compare this set to.

    Returns boolean

    True if the specified object is iterable, has the same number of elements as this set, and this set also contains each of those elements.

every

  • every(predicate: (value: string, index: number, set: TernaryStringSet) => boolean, thisArg?: unknown): boolean
  • Returns whether every element in this set passes a test implemented by the specified function.

    throws

    TypeError if the predicate is not a function.

    Parameters

    • predicate: (value: string, index: number, set: TernaryStringSet) => boolean

      A function that accepts strings from this set and returns true if the string passes the desired test.

    • Optional thisArg: unknown

      An optional value to use as this when calling the predicate.

    Returns boolean

    True if at every element in this set passes the test.

filter

  • Returns a new set containing all of the strings in this set that pass a test implemented by the specified function.

    throws

    TypeError if the predicate is not a function.

    Parameters

    • predicate: (value: string, index: number, set: TernaryStringSet) => boolean

      A function that accepts strings from this set and returns true if the string should be included in the new set.

    • Optional thisArg: unknown

      An optional value to use as this when calling the predicate.

    Returns TernaryStringSet

    A new set containing only those elements for which the predicate return value is true.

find

  • find(predicate: (value: string, index: number, set: TernaryStringSet) => boolean, thisArg?: unknown): string
  • Returns the first element in this set that satisfies a test implemented by the specified function. If no element satisfies the test, the result is undefined.

    throws

    TypeError if the predicate is not a function.

    Parameters

    • predicate: (value: string, index: number, set: TernaryStringSet) => boolean

      A function that accepts strings from this set and returns true if the string passes the desired test.

    • Optional thisArg: unknown

      An optional value to use as this when calling the predicate.

    Returns string

    The first string to pass the test when tested in sorted order, or undefined.

forEach

  • forEach(callbackFn: (value: string, key: string, set: TernaryStringSet) => void, thisArg?: unknown): void
  • Calls the specified callback function once for each string in this set, passing the string and this set. The string is passed as both value and key to align with Map.forEach. If thisArg is specified, it is used as this when invoking the callback function.

    throws

    TypeError if the callback function is not a function.

    Parameters

    • callbackFn: (value: string, key: string, set: TernaryStringSet) => void

      The function to call for each string.

    • Optional thisArg: unknown

      An optional value to use as this when calling the function.

    Returns void

getArrangementsOf

  • getArrangementsOf(charPattern: string): string[]
  • Returns all strings in this set that can be composed from combinations of the code points in the specified string. Unlike an anagram, all of the code points need not to appear for a match to count. For example, the pattern "coat" can match "cat" even though the o is not used. However, characters cannot appear more often than they appear in the pattern string. The same pattern "coat" cannot match "tot" since it includes only a single t.

    If this set contains the empty string, it is always included in results from this method.

    throws

    ReferenceError if the pattern is null.

    Parameters

    • charPattern: string

      The non-null pattern string.

    Returns string[]

    A (possibly empty) array of strings from the set that can be composed from the pattern characters.

getCompletedBy

  • getCompletedBy(suffix: string): string[]
  • Returns an array of the strings that are completed by the specified suffix string. That is, an array of all strings in the set that end with the suffix, including the suffix itself if appropriate.

    throws

    ReferenceError if the pattern is null.

    Parameters

    • suffix: string

      The non-null pattern to find completions for.

    Returns string[]

    A (possibly empty) array of all strings in the set for which the pattern is a suffix.

getCompletionsOf

  • getCompletionsOf(prefix: string): string[]
  • Returns an array of possible completions for the specified prefix string. That is, an array of all strings in the set that start with the prefix. If the prefix itself is in the set, it is included as the first entry.

    throws

    ReferenceError if the pattern is null.

    Parameters

    • prefix: string

      The non-null pattern to find completions for.

    Returns string[]

    A (possibly empty) array of all strings in the set for which the pattern is a prefix.

getMatchesOf

  • getMatchesOf(predicate: (value: string) => boolean): string[]
  • Returns an array of all strings in this set that pass a test implemented by the specified function. The result is equivalent to Array.from(set).filter(predicate), without creating an intermediate array.

    throws

    TypeError if the predicate is not a function.

    Parameters

    • predicate: (value: string) => boolean

      A function that accepts strings from this set and returns true if the string should be included in the results.

        • (value: string): boolean
        • Parameters

          • value: string

          Returns boolean

    Returns string[]

    A (possibly empty) array of elements that pass the test.

getPartialMatchesOf

  • getPartialMatchesOf(pattern: string, dontCareChar?: string): string[]
  • Returns all strings that match the pattern. The pattern may include zero or more "don't care" characters that can match any code point. By default this character is ".", but any valid code point can be used. For example, the pattern "c.t" would match any of "cat", "cot", or "cut", but not "cup".

    throws

    ReferenceError if the pattern or don't care string is null.

    throws

    TypeError if the don't care string is empty.

    Parameters

    • pattern: string

      A pattern string matched against the strings in the set.

    • dontCareChar: string = "."

      The character that can stand in for any character in the pattern. Only the first code point is used. (Default is ".".)

    Returns string[]

    A (possibly empty) array of strings that match the pattern string.

getRegexMatchesOf

  • getRegexMatchesOf(pattern: string | RegExp): string[]
  • Returns all elements that exactly match a regular expression. That is, the pattern must match the entire string and not just a substring, as if the pattern were explicitly anchored (/^pattern$/).

    throws

    ReferenceError if the pattern is null.

    throws

    TypeError if the pattern is not a string or RegExp.

    throws

    SyntaxError if the pattern is passed in as a string, but isn't valid.

    Parameters

    • pattern: string | RegExp

      The regular expression that elements, in their entirety, must match.

    Returns string[]

    A (possibly empty) array of strings that exactly match the pattern expression.

getWithinEditDistanceOf

  • getWithinEditDistanceOf(pattern: string, distance: number): string[]
  • Returns an array of all strings in the set that are within the specified edit distance of the given pattern string. A string is within edit distance n of the pattern if it can be transformed into the pattern with no more than n insertions, deletions, or substitutions. For example:

    • cat is edit distance 0 from itself;
    • at is edit distance 1 from cat (1 deletion);
    • cot is edit distance 1 from cat (1 substitution); and
    • coats is edit distance 2 from cat (2 insertions).
    throws

    ReferenceError if the pattern is null.

    throws

    TypeError if the distance is not a number.

    throws

    RangeError if the distance is negative.

    Parameters

    • pattern: string

      A pattern string matched against the strings in the set.

    • distance: number

      The maximum number of edits to apply to the pattern string. May be Infinity to allow any number of edits.

    Returns string[]

    A (possibly empty) array of strings from the set that match the pattern.

getWithinHammingDistanceOf

  • getWithinHammingDistanceOf(pattern: string, distance: number): string[]
  • Returns an array of all strings in the set that are within the specified Hamming distance of the given pattern string. A string is within Hamming distance n of the pattern if at most n of its code points are different from those of the pattern. For example:

    • cat is Hamming distance 0 from itself;
    • cot is Hamming distance 1 from cat;
    • cop is Hamming distance 2 from cat; and
    • top is Hamming distance 3 from cat.
    throws

    ReferenceError if the pattern is null.

    throws

    TypeError if the distance is not a number.

    throws

    RangeError if the distance is negative.

    Parameters

    • pattern: string

      A pattern string matched against the strings in the set.

    • distance: number

      The maximum number of code point deviations to allow from the pattern string. May be Infinity to allow any number.

    Returns string[]

    A (possibly empty) array of strings from the set that match the pattern.

has

  • has(s: any): boolean
  • Returns whether this set contains the specified string. If passed a non-string value, returns false.

    Parameters

    • s: any

      The non-null string to test for.

    Returns boolean

    True if the string is present.

intersection

  • Returns a new set that is the intersection of this set and the elements of the specified iterable. The new set will include only those elements that are members of both.

    throws

    TypeError if the specified target is not iterable.

    Parameters

    • rhs: Iterable<any>

      The iterable to intersect with this set.

    Returns TernaryStringSet

    A new set containing only elements in both this set and the iterable.

isDisjointFrom

  • isDisjointFrom(rhs: Iterable<any>): boolean
  • Returns whether this set is disjoint from the elements of the specified iterable, that is, whether this set has no elements in common with the iterable.

    throws

    TypeError if the argument is not an iterable.

    Parameters

    • rhs: Iterable<any>

      The iterable whose elements should be tested against this set.

    Returns boolean

    True if this.intersection(rhs) is empty.

isSubsetOf

  • isSubsetOf(rhs: Iterable<any>): boolean
  • Returns whether this set is a subset of the elements of the specified iterable, that is, whether every element in this set is also an element of the iterable.

    throws

    TypeError if the argument is not an iterable.

    Parameters

    • rhs: Iterable<any>

      The set to compare this set to.

    Returns boolean

    True if this set is a proper subset of, or equal to, the specified iterable.

isSupersetOf

  • isSupersetOf(rhs: Iterable<any>): boolean
  • Returns whether this set is a superset of the elements of the specified iterable, that is, whether every element of the iterable is also an element in this set.

    throws

    TypeError if the argument is not an iterable.

    Parameters

    • rhs: Iterable<any>

      The set to compare this set to.

    Returns boolean

    True if this set is a proper superset of, or equal to, the specified iterable.

join

  • join(separator?: string): string
  • Returns a string that is the concatenation of all strings in this set, separated by a comma or the specified separator.

    Parameters

    • separator: string = ","

      Optional string to use as separator. Default is ",".

    Returns string

    A string containing all of the set's elements, in sorted order, separated by the specified string.

keys

  • keys(): IterableIterator<string>
  • Returns an iterator over the strings in this set. This is included for compatibilty with Sets and Maps; it is equivalent to values().

    Note: Unlike standard Sets, this set's values are returned in ascending lexicographic order, not the order in which items were added.

    Returns IterableIterator<string>

    An non-null iterator over the strings in this set.

map

  • Returns a new set populated with the results of calling the specified mapping function on each element of this set. Like Array.map(), the mapping function can return any value, but non-string values will be coerced for compatibility with the new set.

    throws

    TypeError if the mapping function is not a function.

    Parameters

    • mapper: (value: string, index: number, set: TernaryStringSet) => any

      A function that accepts strings from this set and returns the string to be added to the new set.

    • Optional thisArg: unknown

      An optional value to use as this when calling the mapping function.

    Returns TernaryStringSet

    A new set containing the results of applying the mapping function to each element in this set.

reduce

  • reduce<T>(reducer: (previous: T, current: string, index: number, set: TernaryStringSet) => T, initialValue: T): T
  • reduce(reducer: (previous: string, current: string, index: number, set: TernaryStringSet) => string, initialValue?: string): string
  • Reduces this set to a single accumulated value by calling the specified reducer function with each element in turn. The reducer is passed the accumulator and the next element and returns the new value of the accumulator. The accumulated value may be of any type; it is not restricted to strings.

    throws

    TypeError if the reducer is not a function or if the set is empty and no initial value is provided.

    Type parameters

    • T

    Parameters

    • reducer: (previous: T, current: string, index: number, set: TernaryStringSet) => T

      A function called with the previous accumulator value, the next element to reduce, the element index, and this set.

    • initialValue: T

      An optional initial value for the accumulator. If no none is provided, the first element is used.

    Returns T

    The final value of the accumulator.

  • Parameters

    • reducer: (previous: string, current: string, index: number, set: TernaryStringSet) => string
        • (previous: string, current: string, index: number, set: TernaryStringSet): string
        • Parameters

          Returns string

    • Optional initialValue: string

    Returns string

some

  • some(predicate: (value: string, index: number, set: TernaryStringSet) => boolean, thisArg?: unknown): boolean
  • Returns whether at least one element in this set passes a test implemented by the specified function.

    throws

    TypeError if the predicate is not a function.

    Parameters

    • predicate: (value: string, index: number, set: TernaryStringSet) => boolean

      A function that accepts strings from this set and returns true if the string passes the desired test.

    • Optional thisArg: unknown

      An optional value to use as this when calling the predicate.

    Returns boolean

    True if at least one element in this set passes the test.

subtract

symmetricDifference

  • Returns a new set that is the symmetric difference of this set and the elements of the specified iterable. The new set will include all of the elements that are in either, but not in both.

    throws

    TypeError if the specified target is not iterable.

    Parameters

    • rhs: Iterable<string>

      The iterable whose elements should be exclusive-or'd with this set.

    Returns TernaryStringSet

    A new set containing those elements either in this set or the iterable, but not both or neither.

toArray

  • toArray(): string[]
  • Returns a new array of every element in the set. This is equivalent to Array.from(this), but this method is more efficient.

    Returns string[]

    A non-null array of the elements of the set in lexicographic order.

toBuffer

  • toBuffer(): ArrayBuffer
  • Returns a buffer whose contents can be used to recreate this set. The returned data is independent of the platform on which it is created. The buffer content and length will depend on the state of the set's underlying structure. For this reason you may wish to balance() and/or compact() the set first.

    Returns ArrayBuffer

    A non-null buffer.

union

  • Returns a new set that is the union of this set and the elements of the specified iterable. The new set will include any element that is a member of either.

    throws

    TypeError if the specified target is not iterable or any element is not a string.

    Parameters

    • rhs: Iterable<string>

      The iterable whose elements should be united with this set.

    Returns TernaryStringSet

    A new set containing the elements of both this set and the iterable.

values

  • values(): IterableIterator<string>
  • Returns an iterator over the strings in this set.

    Note: Unlike standard Sets, this set's values are returned in ascending lexicographic order, not the order in which items were added.

    Returns IterableIterator<string>

    An non-null iterator over the strings in this set.

Static fromBuffer

  • Creates a new string set from data in a buffer previously created with toBuffer. Buffers created by an older version of this library can be deserialized by newer versions of this library. The reverse may or may not be true, depending on the specific versions involved.

    throws

    ReferenceError if the specified buffer is null.

    throws

    TypeError if the buffer data is invalid or from an unsupported version.

    Parameters

    • buffer: ArrayBuffer

      The buffer to recreate the set from.

    Returns TernaryStringSet

    A new set that recreates the original set that was stored in the buffer.

Generated using TypeDoc