@imranbarbhuiya/mongoose-fuzzy-searching
Version:
Mongoose fuzzy searching plugin
99 lines (98 loc) • 4.7 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var ngrams_1 = require("./ngrams");
var utils_1 = require("./utils");
jest.mock("./utils");
describe("nGrams", function () {
var options = (0, utils_1.prepareNgrams)({});
describe("with default values", function () {
it("should return empty array without attributes", function () {
expect((0, ngrams_1.nGrams)("", options.minSize, options.prefixOnly)).toStrictEqual([]);
});
it('should be equal to ["a"] when the given text is `a`', function () {
expect((0, ngrams_1.nGrams)("a", options.minSize, options.prefixOnly)).toStrictEqual([
"a",
]);
});
it('should be equal to `["aa", "aaa"]` when the given text is `aaa`', function () {
expect((0, ngrams_1.nGrams)("aaa", options.minSize, options.prefixOnly)).toStrictEqual([
"aa",
"aaa",
]);
});
it('should be equal to `["23", "12", "123"]` when the given text is 123 (as a number)', function () {
expect((0, ngrams_1.nGrams)("123", options.minSize, options.prefixOnly)).toStrictEqual([
"23",
"12",
"123",
]);
});
it("should apply default `prefixOnly` if it is not present", function () {
expect((0, ngrams_1.nGrams)("123", options.minSize)).toStrictEqual(["23", "12", "123"]);
});
});
describe("with custom minSize", function () {
it("should throw an Error when the minSize is negative", function () {
expect(ngrams_1.nGrams.bind(ngrams_1.nGrams, "aaa", -1, options.prefixOnly)).toThrow("minSize must be greater than 0.");
});
it("should throw an Error when the minSize is 0", function () {
expect(ngrams_1.nGrams.bind(ngrams_1.nGrams, "aaa", 0, options.prefixOnly)).toThrow("minSize must be greater than 0.");
});
it('should return `["aaa"]` when the minSize is 3', function () {
expect((0, ngrams_1.nGrams)("aaa", 3, options.prefixOnly)).toStrictEqual(["aaa"]);
});
});
describe("with custom `prefixOnly`", function () {
it('should return `["aa", "aaa", "aaaa", "aaaaa"]` when the minSize is 3 and prefixOnly to true', function () {
expect((0, ngrams_1.nGrams)("aaaaa", options.minSize, true)).toStrictEqual([
"aa",
"aaa",
"aaaa",
"aaaaa",
]);
});
});
describe("with both values as custom", function () {
it('should return `["abc","abcd", "abcde", "abcdef", "abcdefg"]` when the minSize is 3 and prefixOnly to true', function () {
expect((0, ngrams_1.nGrams)("abcdefg", 3, true)).toStrictEqual([
"abc",
"abcd",
"abcde",
"abcdef",
"abcdefg",
]);
});
});
});
describe("makeNGrams", function () {
var options = (0, utils_1.prepareNgrams)({});
describe("with default minSize", function () {
it("should return empty array without attribute", function () {
expect((0, ngrams_1.makeNGrams)(__assign(__assign({}, options), { text: "" }))).toStrictEqual([]);
});
it('should return `["oe", "jo", "joe", "do", "doe"]` with attribute `Joe Doe`', function () {
expect((0, ngrams_1.makeNGrams)(__assign(__assign({}, options), { text: "Joe Doe" }))).toStrictEqual(["oe", "jo", "joe", "do", "doe", "joe doe"]);
});
});
describe("with specific `minSize`", function () {
it('should return `["joe", "doe"]` when the minSize is 3', function () {
expect((0, ngrams_1.makeNGrams)(__assign(__assign({}, options), { text: "Joe Doe", escapeSpecialCharacters: false, minSize: 3 }))).toStrictEqual(["joe", "doe", "joe doe"]);
});
});
describe("with `prefixOnly`", function () {
it('should return `["joe", "doe", "joe doe"]` when the minSize is 3', function () {
expect((0, ngrams_1.makeNGrams)(__assign(__assign({}, options), { text: "Joe Doe", escapeSpecialCharacters: false, minSize: 3, prefixOnly: false }))).toStrictEqual(["joe", "doe", "joe doe"]);
});
});
});