UNPKG

symspell-ex

Version:

Spelling correction & Fuzzy search based on symmetric delete spelling correction algorithm

81 lines (80 loc) 2.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MemoryStore = void 0; const core_1 = require("../core"); const megahash_1 = __importDefault(require("megahash")); class MemoryStore { constructor() { this.name = 'memory_store'; this._language = core_1.Languages.ENGLISH; this._maxEntryLength = 0; this._initialized = false; } async initialize() { this._terms = { [this._language]: [] }; this._entries = { [this._language]: new megahash_1.default() }; this._initialized = true; } isInitialized() { return this._initialized; } async setLanguage(language) { if (this._terms[language] == null) { this._terms[language] = []; this._entries[language] = new megahash_1.default(); } this._language = language; } async pushTerm(key) { return this._terms[this._language].push(key); } async getTermAt(index) { return this._terms[this._language][index] || null; } async getTermsAt(indexes) { const terms = []; for (let i = 0; i < indexes.length; i += 1) { terms.push(this._terms[this._language][indexes[i]] || null); } return terms; } async setEntry(key, value) { const result = this._entries[this._language].set(key, value); if (key.length > this._maxEntryLength) { this._maxEntryLength = key.length; } return (result === 1 || result === 2); } async getEntry(key) { if (key == null || key.length === 0) return null; return this._entries[this._language].get(key) || null; } async getEntries(keys) { const result = []; for (let i = 0; i < keys.length; i += 1) { const item = this._entries[this._language].get(keys[i]); if (item != null && item.length > 0) { result.push(item); } else { result.push(null); } } return result; } async hasEntry(key) { return this._entries[this._language].has(key); } async maxEntryLength() { return this._maxEntryLength; } async clear() { this._terms = { [this._language]: [] }; this._entries = { [this._language]: new megahash_1.default() }; } } exports.MemoryStore = MemoryStore;