UNPKG

@s-hiroshi/bks

Version:

Cli bookmarks application

59 lines 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ItemReader = void 0; const fs = require('fs'); const readLines_1 = require("../service/readLines"); class ItemReader { constructor(storageFilePath) { this.storageFilePath = storageFilePath; this.hasStorage = fs.existsSync(storageFilePath); } async init() { this.data = JSON.parse(await (0, readLines_1.readLines)(this.storageFilePath)); } /** * @param keyword * @param content * @return Items[] */ readItem(keyword, content) { if (!this.hasStorage || !keyword || !content) { throw new Error(); } const matchedItems = []; this.data.items.forEach((item, index) => { if (item.keyword.indexOf(keyword) >= 0 || item.content.indexOf(content) >= 0) { matchedItems.push(item); } }); return matchedItems; } /** * @param keyword * @param content * @return Items[] */ readItemStrict(keyword, content) { if (!this.hasStorage || !keyword || !content) { throw new Error(); } const matchedItems = []; this.data.items.forEach((item, index) => { if (item.keyword.indexOf(keyword) >= 0 && item.content.indexOf(content) >= 0) { matchedItems.push(item); } }); return matchedItems; } /** * * @returns Item[] */ readAll() { if (!this.hasStorage) throw new Error(); return this.data.items; } } exports.ItemReader = ItemReader; //# sourceMappingURL=ItemReader.js.map