UNPKG

@imranbarbhuiya/mongoose-fuzzy-searching

Version:
247 lines (246 loc) 8.93 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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 }); exports.createNGrams = exports.removeFuzzyElements = exports.createFields = void 0; var mongoose_1 = require("mongoose"); var ngrams_1 = require("../ngrams"); var utils_1 = require("../utils"); var FieldCreator = /** @class */ (function () { function FieldCreator() { var _this = this; this.createByFieldType = function (item) { if ((0, utils_1.isString)(item)) { _this.fromString(item); return; } if (item.keys) { _this.fromObjectKeys(item); return; } _this.fromObject(item); }; } FieldCreator.prototype.createByFields = function (fields) { fields.forEach(this.createByFieldType); return this; }; return FieldCreator; }()); var Create = /** @class */ (function (_super) { __extends(Create, _super); function Create(schema) { var _this = _super.call(this) || this; _this.schema = schema; _this._indexes = {}; _this._weights = {}; _this.addToSchema = function (name) { var _a; return _a = {}, _a["".concat(name, "_fuzzy")] = _this.createSchemaObject([mongoose_1.Schema.Types.String], { default: "", index: false, }), _a; }; _this.addArrayToSchema = function (name) { var _a; return _a = {}, _a["".concat(name, "_fuzzy")] = _this.createSchemaObject(mongoose_1.Schema.Types.Mixed, { default: [], index: false, }), _a; }; return _this; } Create.prototype.createSchemaObject = function (typeValue, options) { return __assign(__assign({}, options), { type: typeValue }); }; Create.prototype.fromString = function (item) { this.schema.add(this.addToSchema(item)); this.indexes["".concat(item, "_fuzzy")] = "text"; }; Create.prototype.fromObject = function (item) { this.schema.add(this.addToSchema(item.name)); this.indexes["".concat(item.name, "_fuzzy")] = "text"; if (item.weight) { this.weights["".concat(item.name, "_fuzzy")] = item.weight; } }; Create.prototype.fromObjectKeys = function (item) { var _this = this; item.keys.forEach(function (key) { _this.indexes["".concat(item.name, "_fuzzy.").concat(key, "_fuzzy")] = "text"; }); this.schema.add(this.addArrayToSchema(item.name)); }; Object.defineProperty(Create.prototype, "indexes", { get: function () { return this._indexes; }, enumerable: false, configurable: true }); Object.defineProperty(Create.prototype, "weights", { get: function () { return this._weights; }, enumerable: false, configurable: true }); return Create; }(FieldCreator)); var Remove = /** @class */ (function (_super) { __extends(Remove, _super); function Remove(_schema) { var _this = _super.call(this) || this; _this._schema = _schema; return _this; } Remove.prototype.fromString = function (item) { delete this.schema["".concat(item, "_fuzzy")]; }; Remove.prototype.fromObject = function (item) { delete this.schema["".concat(item.name, "_fuzzy")]; }; Remove.prototype.fromObjectKeys = function (item) { delete this.schema["".concat(item.name, "_fuzzy")]; }; Object.defineProperty(Remove.prototype, "schema", { get: function () { return this._schema; }, enumerable: false, configurable: true }); return Remove; }(FieldCreator)); var Generate = /** @class */ (function (_super) { __extends(Generate, _super); function Generate(_attributes) { var _this = _super.call(this) || this; _this._attributes = _attributes; return _this; } Object.defineProperty(Generate.prototype, "attributes", { get: function () { return this._attributes; }, enumerable: false, configurable: true }); Generate.prototype.fromString = function (item) { var value = this.attributes[item]; if (value === undefined) { return; } if (value === null || value === "") { this.attributes["".concat(item, "_fuzzy")] = []; return; } if (Array.isArray(value)) { value = value.join(" "); } var options = (0, utils_1.prepareNgrams)({ text: value }); this.attributes["".concat(item, "_fuzzy")] = (0, ngrams_1.makeNGrams)(options); }; Generate.prototype.fromObject = function (item) { var value = this.attributes["".concat(item.name)]; if (value === undefined) { return; } if (value === null || value === "") { this.attributes["".concat(item.name, "_fuzzy")] = []; return; } var escapeSpecialCharacters = item.escapeSpecialCharacters !== false; var minSize = item.minSize, prefixOnly = item.prefixOnly; if (Array.isArray(value)) { value = value.join(" "); } var options = (0, utils_1.prepareNgrams)({ text: value, escapeSpecialCharacters: escapeSpecialCharacters, minSize: minSize, prefixOnly: prefixOnly, }); this.attributes["".concat(item.name, "_fuzzy")] = (0, ngrams_1.makeNGrams)(options); }; Generate.prototype.fromObjectKeys = function (item) { var value = this.attributes["".concat(item.name)]; if (value === undefined) { return; } if (value === null) { this.attributes["".concat(item.name, "_fuzzy")] = []; return; } var escapeSpecialCharacters = item.escapeSpecialCharacters !== false; var attrs = []; var obj = {}; var data = this.attributes[item.name]; if (!Array.isArray(data)) { data = [data]; } data.forEach(function (attributes) { var minSize = item.minSize, prefixOnly = item.prefixOnly; item.keys.forEach(function (key) { var _a; var text = attributes[key]; if (text === undefined) { return; } var options = (0, utils_1.prepareNgrams)({ text: text, escapeSpecialCharacters: escapeSpecialCharacters, minSize: minSize, prefixOnly: prefixOnly, }); obj = __assign(__assign({}, obj), (_a = {}, _a["".concat(key, "_fuzzy")] = text === null || text === "" ? [] : (0, ngrams_1.makeNGrams)(options), _a)); }); attrs.push(obj); }); this.attributes["".concat(item.name, "_fuzzy")] = attrs; }; return Generate; }(FieldCreator)); var createFields = function (schema, fields) { var create = new Create(schema).createByFields(fields); return { indexes: create.indexes, weights: create.weights }; }; exports.createFields = createFields; var removeFuzzyElements = function (fields) { return function (_doc, ret) { return new Remove(ret).createByFields(fields).schema; }; }; exports.removeFuzzyElements = removeFuzzyElements; var createNGrams = function (attributes, fields) { new Generate(attributes).createByFields(fields); }; exports.createNGrams = createNGrams;