<commit_before>const StringUtil = require("../StringUtil");

describe("StringUtil", function() {
  describe("#arrayToJoinedString", function() {
    it("joins array with default separator", function() {
      var result = StringUtil.arrayToJoinedString([1, 2]);

      expect(result).toEqual("1, 2");
    });

    it("joins array with the given separator", function() {
      var result = StringUtil.arrayToJoinedString([1, 2], "-");

      expect(result).toEqual("1-2");
    });

    it("does not append separator if array has only one  element", function() {
      var result = StringUtil.arrayToJoinedString([1]);

      expect(result).toEqual("1");
    });

    it("returns empty string if array is null", function() {
      var result = StringUtil.arrayToJoinedString(null);

      expect(result).toEqual("");
    });

    it("returns empty string if array is undefined", function() {
      var result = StringUtil.arrayToJoinedString();

      expect(result).toEqual("");
    });

    it("returns empty string if array is and object", function() {
      var result = StringUtil.arrayToJoinedString({});

      expect(result).toEqual("");
    });

    it("returns empty string if array is empty", function() {
      var result = StringUtil.arrayToJoinedString([]);

      expect(result).toEqual("");
    });
  });

  describe("#getSearchTokens", function() {
    it("splits on non-word characters, but not slashes", function() {
      expect(
        StringUtil.getSearchTokens(
          "foo/bar\\.baz.quis,qux quux[quuz]corge grault,garply\twaldo\bfred"
        )
      ).toEqual([
        "foo/bar",
        "baz",
        "quis",
        "qux",
        "quux",
        "quuz",
        "corge",
        "grault",
        "garply",
        "waldo",
        "fred"
      ]);
    });

    it("returns converts input to string if not a string", function() {
      expect(StringUtil.getSearchTokens(10)).toEqual(["10"]);
    });

    it("removes empty strings", function() {
      expect(StringUtil.getSearchTokens("bar\\.baz")).toEqual(["bar", "baz"]);
    });

    it("lowercases strings", function() {
      expect(StringUtil.getSearchTokens("QuUx[qUuz]coRge")).toEqual([
        "quux",
        "quuz",
        "corge"
      ]);
    });
  });

  describe("#filterByString", function() {
    it("filters using a key as getter", function() {
      var _return = StringUtil.filterByString(
        [{ id: 0, foo: "bar" }, { id: 1, foo: "baz" }, { id: 2, foo: "bar" }],
        "foo",
        "bar"
      );

      expect(_return).toEqual([{ id: 0, foo: "bar" }, { id: 2, foo: "bar" }]);
    });

    it("filters using a function as getter", function() {
      var _return = StringUtil.filterByString(
        [{ id: 0, foo: "bar" }, { id: 1, foo: "baz" }, { id: 2, foo: "bar" }],
        function(el) {
          return el.foo;
        },
        "baz"
      );
      expect(_return).toEqual([{ id: 1, foo: "baz" }]);
    });
  });

  describe("#escapeForRegExp", function() {
    it("escapes string", function() {
      var _return = StringUtil.escapeForRegExp("-[]/{}()*+?.\\^$|");
      var escaped = "\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\\\^\\$\\|";
      expect(_return).toEqual(escaped);<commit_msg>test(StringUtil): Relevance sorting of filterByString<commit_after>const StringUtil = require("../StringUtil");

describe("StringUtil", function() {
  describe("#arrayToJoinedString", function() {
    it("joins array with default separator", function() {
      var result = StringUtil.arrayToJoinedString([1, 2]);

      expect(result).toEqual("1, 2");
    });

    it("joins array with the given separator", function() {
      var result = StringUtil.arrayToJoinedString([1, 2], "-");

      expect(result).toEqual("1-2");
    });

    it("does not append separator if array has only one  element", function() {
      var result = StringUtil.arrayToJoinedString([1]);

      expect(result).toEqual("1");
    });

    it("returns empty string if array is null", function() {
      var result = StringUtil.arrayToJoinedString(null);

      expect(result).toEqual("");
    });

    it("returns empty string if array is undefined", function() {
      var result = StringUtil.arrayToJoinedString();

      expect(result).toEqual("");
    });

    it("returns empty string if array is and object", function() {
      var result = StringUtil.arrayToJoinedString({});

      expect(result).toEqual("");
    });

    it("returns empty string if array is empty", function() {
      var result = StringUtil.arrayToJoinedString([]);

      expect(result).toEqual("");
    });
  });

  describe("#getSearchTokens", function() {
    it("splits on non-word characters, but not slashes", function() {
      expect(
        StringUtil.getSearchTokens(
          "foo/bar\\.baz.quis,qux quux[quuz]corge grault,garply\twaldo\bfred"
        )
      ).toEqual([
        "foo/bar",
        "baz",
        "quis",
        "qux",
        "quux",
        "quuz",
        "corge",
        "grault",
        "garply",
        "waldo",
        "fred"
      ]);
    });

    it("returns converts input to string if not a string", function() {
      expect(StringUtil.getSearchTokens(10)).toEqual(["10"]);
    });

    it("removes empty strings", function() {
      expect(StringUtil.getSearchTokens("bar\\.baz")).toEqual(["bar", "baz"]);
    });

    it("lowercases strings", function() {
      expect(StringUtil.getSearchTokens("QuUx[qUuz]coRge")).toEqual([
        "quux",
        "quuz",
        "corge"
      ]);
    });
  });

  describe("#filterByString", function() {
    it("filters using a key as getter", function() {
      var _return = StringUtil.filterByString(
        [{ id: 0, foo: "bar" }, { id: 1, foo: "baz" }, { id: 2, foo: "bar" }],
        "foo",
        "bar"
      );

      expect(_return).toEqual([{ id: 0, foo: "bar" }, { id: 2, foo: "bar" }]);
    });

    it("filters using a function as getter", function() {
      var _return = StringUtil.filterByString(
        [{ id: 0, foo: "bar" }, { id: 1, foo: "baz" }, { id: 2, foo: "bar" }],
        function(el) {
          return el.foo;
        },
        "baz"
      );
      expect(_return).toEqual([{ id: 1, foo: "baz" }]);
    });

    it("sorts by relevance, word distance from beginning of string", function() {
      const _return = StringUtil.filterByString(
        [
          { id: 0, foo: "beta-baz" },
          { id: 1, foo: "community-baz" },
          { id: 2, foo: "baz" }
        ],
        "foo",
        "baz"
      );

      expect(_return.length).toEqual(3);
      expect(_return[0].foo).toEqual("baz");
      expect(_return[1].foo).toEqual("beta-baz");
      expect(_return[2].foo).toEqual("community-baz");
    });

    it("sorts by relevance, combining relevance of multiple search tokens", function() {
      const _return = StringUtil.filterByString(
        [
          { id: 0, foo: "footballs" },
          { id: 1, foo: "strange bazookas" },
          { id: 2, foo: "bar" },
          { id: 3, foo: "foo bar baz" }
        ],
        "foo",
        "foo baz"
      );

      expect(_return.length).toEqual(3);
      expect(_return[0].foo).toEqual("foo bar baz");
      expect(_return[1].foo).toEqual("footballs");
      expect(_return[2].foo).toEqual("strange bazookas");
    });
  });

  describe("#escapeForRegExp", function() {
    it("escapes string", function() {
      var _return = StringUtil.escapeForRegExp("-[]/{}()*+?.\\^$|");
      var escaped = "\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\\\^\\$\\|";
      expect(_return).toEqual(escaped);
{'fraction_non_alphanumeric': 0.18920710686820472, 'fraction_numerical': 0.008088040307610714, 'mean_word_length': 2.469641214351426, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '239925', 'n_tokens_mistral': 2452, 'n_tokens_neox': 2372, 'n_words': 522}