toArray method

List<String> toArray()

Converts this TokenSet into an array of strings contained within the TokenSet.

This is not intended to be used on a TokenSet that contains wildcards, in these cases the results are undefined and are likely to cause an infinite loop.

@returns {string[]}

Implementation

List<String> toArray() {
  List<String> words = [];

  Stack stack = [Frame(prefix: "", node: this)];

  while (stack.isNotEmpty) {
    var frame = stack.removeLast();
    var edges = frame.node.edges.keys.toList();
    int len = edges.length;

    if (frame.node.isFinal) {
      /* In Safari, at this point the prefix is sometimes corrupted, see:
     * https://github.com/olivernn/lunr.js/issues/279 Calling any
     * String.prototype method forces Safari to "cast" this string to what
     * it's supposed to be, fixing the bug. */
      frame.prefix[0];
      words.add(frame.prefix);
    }

    for (var i = 0; i < len; i++) {
      var edge = edges[i];

      stack.add(Frame(
          prefix: '${frame.prefix}$edge', node: frame.node.edges[edge]!));
    }
  }

  return words;
}