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.
An optional iterable object whose strings will be added to the new set.
Returns the string tag used by Object.prototype.toString()
for this class.
Returns whether the set is currently compact.
True if the set is compacted, in which case mutating the set could have significant performance implications.
Returns the number of unique strings in this set.
The non-negative integer number of string elements in the set.
Returns information about this set's underlying tree structure. This method is intended only for testing and performance analysis.
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.
An non-null iterator over the strings in this set.
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.
The non-null string to add.
This set, allowing chained calls.
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.
Zero or more strings to be added to the set.
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.
The non-null collection of strings to add.
The optional index of the first element to add (inclusive, default is 0).
The optional index of the last element to add (exclusive, default is strings.length
)
This set, allowing chained calls.
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:
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.
Removes all strings from this set.
This set, allowing chained calls.
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.
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.
The non-null string to delete.
True if the string was in this set; false otherwise.
Removes multiple elements from this set.
The elements to remove.
True if every element was present and was removed.
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.
The iterable whose elements should be subtracted from this set; the iterable's elements do not have to be strings.
A new set containing only those elements in this set and that are not in the specified iterable.
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 Set
s, this set's values are returned in ascending
lexicographic order, not the order in which items were added.
An iterator over the key-value pairs in this set, with each value acting as its own key.
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
.
The set (or other object) to compare this set to.
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.
Returns whether every element in this set passes a test implemented by the specified function.
A function that accepts strings from this set and returns true if the string passes the desired test.
An optional value to use as this
when calling the predicate.
True if at every element in this set passes the test.
Returns a new set containing all of the strings in this set that pass a test implemented by the specified function.
A function that accepts strings from this set and returns true if the string should be included in the new set.
An optional value to use as this
when calling the predicate.
A new set containing only those elements for which the predicate return value is true.
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
.
A function that accepts strings from this set and returns true if the string passes the desired test.
An optional value to use as this
when calling the predicate.
The first string to pass the test when tested in sorted order, or undefined
.
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.
The function to call for each string.
An optional value to use as this
when calling the function.
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.
The non-null pattern string.
A (possibly empty) array of strings from the set that can be composed from the pattern characters.
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.
The non-null pattern to find completions for.
A (possibly empty) array of all strings in the set for which the pattern is a suffix.
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.
The non-null pattern to find completions for.
A (possibly empty) array of all strings in the set for which the pattern is a prefix.
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.
A function that accepts strings from this set and returns true if the string should be included in the results.
A (possibly empty) array of elements that pass the test.
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"
.
A pattern string matched against the strings in the set.
The character that can stand in for any character in the pattern.
Only the first code point is used. (Default is "."
.)
A (possibly empty) array of strings that match the pattern 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$/
).
The regular expression that elements, in their entirety, must match.
A (possibly empty) array of strings that exactly match the pattern expression.
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); andcoats
is edit distance 2 from cat
(2 insertions).A pattern string matched against the strings in the set.
The maximum number of edits to apply to the pattern string. May be Infinity to allow any number of edits.
A (possibly empty) array of strings from the set that match the pattern.
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
; andtop
is Hamming distance 3 from cat
.A pattern string matched against the strings in the set.
The maximum number of code point deviations to allow from the pattern string. May be Infinity to allow any number.
A (possibly empty) array of strings from the set that match the pattern.
Returns whether this set contains the specified string. If passed a non-string value, returns false.
The non-null string to test for.
True if the string is present.
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.
The iterable to intersect with this set.
A new set containing only elements in both this set and the iterable.
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.
The iterable whose elements should be tested against this set.
True if this.intersection(rhs)
is empty.
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.
The set to compare this set to.
True if this set is a proper subset of, or equal to, the specified iterable.
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.
The set to compare this set to.
True if this set is a proper superset of, or equal to, the specified iterable.
Returns a string that is the concatenation of all strings in this set, separated by a comma or the specified separator.
Optional string to use as separator. Default is ","
.
A string containing all of the set's elements, in sorted order, separated by the specified 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 Set
s, this set's values are returned in ascending
lexicographic order, not the order in which items were added.
An non-null iterator over the strings in this set.
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.
A function that accepts strings from this set and returns the string to be added to the new set.
An optional value to use as this
when calling the mapping function.
A new set containing the results of applying the mapping function to each element in this set.
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.
A function called with the previous accumulator value, the next element to reduce, the element index, and this set.
An optional initial value for the accumulator. If no none is provided, the first element is used.
The final value of the accumulator.
Returns whether at least one element in this set passes a test implemented by the specified function.
A function that accepts strings from this set and returns true if the string passes the desired test.
An optional value to use as this
when calling the predicate.
True if at least one element in this set passes the test.
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.
The iterable whose elements should be exclusive-or'd with this set.
A new set containing those elements either in this set or the iterable, but not both or neither.
Returns a new array of every element in the set. This is equivalent
to Array.from(this)
, but this method is more efficient.
A non-null array of the elements of the set in lexicographic order.
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.
A non-null buffer.
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.
The iterable whose elements should be united with this set.
A new set containing the elements of both this set and the iterable.
Returns an iterator over the strings in this set.
Note: Unlike standard Set
s, this set's values are returned in ascending
lexicographic order, not the order in which items were added.
An non-null iterator over the strings in this set.
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.
The buffer to recreate the set from.
A new set that recreates the original set that was stored in the buffer.
Generated using TypeDoc
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 ofadd
.)