diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..a7c7088 --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,22 @@ +name: Checked Code +on: + pull_request: + types: [opened, synchronize, reopened] + push: + branches: [main] + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Setup Deno + uses: denolib/setup-deno@v2 + with: + deno-version: v1.x + - name: Test + run: deno test --allow-write --allow-read + - name: Lint + run: deno lint --unstable + - name: Format Check + run: deno fmt --check --ignore=README.md,README_JP.md,./.github/ \ No newline at end of file diff --git a/README.md b/README.md index b5cb843..c1a43ab 100644 --- a/README.md +++ b/README.md @@ -68,11 +68,11 @@ Returns an Object that matches the conditions, with the name of the key as the f No arguments return all DB data. ``` typescript // Perfect match -const data = await db.find("name", "Toika Asomaka"); +const data = db.find("name", "Toika Asomaka"); // Partial match -const dataPartial = await db.find("name", /Toika/); +const dataPartial = db.find("name", /Toika/); // All DB data -const dataAll = await db.find(); +const dataAll = db.find(); ``` ### Test diff --git a/README_JP.md b/README_JP.md index 91c2132..8dfcb21 100644 --- a/README_JP.md +++ b/README_JP.md @@ -68,11 +68,11 @@ await db.delete("name", "あそまか といか"); 引数なしは、DBデータすべてを返します。 ``` typescript // 完全一致 -const data = await db.find("name", "あそまか といか"); +const data = db.find("name", "あそまか といか"); // 部分検索 -const dataPartial = await db.find("name", /といか/); +const dataPartial = db.find("name", /といか/); // すべてのDBデータ -const dataAll = await db.find(); +const dataAll = db.find(); ``` ## テスト diff --git a/mod.test.ts b/mod.test.ts index 56c3cac..41f06b1 100644 --- a/mod.test.ts +++ b/mod.test.ts @@ -40,7 +40,7 @@ const addDb = async (db: BracesDB): Promise => { description: item.description, show: item.show, }, - "name" + "name", ); } }; @@ -58,11 +58,11 @@ Deno.test( filename, }); await addDb(db); - assertEquals(await db.find(), dataList); + assertEquals(db.find(), dataList); } finally { await deleteDbFile(); } - } + }, ); Deno.test( @@ -77,11 +77,11 @@ Deno.test( await addDb(db); await db.delete("name", "Kintaro"); const dataDeleteList = dataList.filter((item) => item.name !== "Kintaro"); - assertEquals(await db.find(), dataDeleteList); + assertEquals(db.find(), dataDeleteList); } finally { await deleteDbFile(); } - } + }, ); Deno.test( @@ -94,13 +94,13 @@ Deno.test( filename, }); await addDb(db); - const data = await db.find("name", "Kintaro"); + const data = db.find("name", "Kintaro"); const dataFindList = dataList.filter((item) => item.name === "Kintaro"); assertEquals(data, dataFindList); } finally { await deleteDbFile(); } - } + }, ); Deno.test( @@ -113,15 +113,13 @@ Deno.test( filename, }); await addDb(db); - const data = await db.find("name", /taro/); + const data = db.find("name", /taro/); const dataFindList = dataList.filter((item) => /taro/.test(item.name || "") ); - console.log(data); - console.log(dataFindList); assertEquals(data, dataFindList); } finally { await deleteDbFile(); } - } + }, ); diff --git a/mod.ts b/mod.ts index 0d6da26..7a29b7a 100644 --- a/mod.ts +++ b/mod.ts @@ -1,4 +1,9 @@ -import { isExistFileSync, writeFileSync, readFileSync, writeFile } from 'https://github.com/windchime-yk/deno-util/raw/master/mod.ts'; +import { + isExistFileSync, + readFileSync, + writeFile, + writeFileSync, +} from "https://github.com/windchime-yk/deno-util/raw/master/mod.ts"; export type BracesDBOption = { /** @@ -7,7 +12,7 @@ export type BracesDBOption = { * If `memory` is in-memory. * If `file` is generate file */ - type: 'memory' | 'file', + type: "memory" | "file"; /** * Folder path. * It is generated in the project root by default. @@ -15,36 +20,41 @@ export type BracesDBOption = { * * ex. db/store/ */ - folder?: string, + folder?: string; /** * File Name. * Saved `{filename}.db` . * The default name is `main` . * If the type is `memory`, you don't need it. */ - filename?: string -} + filename?: string; +}; export class BracesDB { - private readonly type: string - private readonly folder?: string - private readonly file: string - private data: T[] + private readonly type: string; + private readonly folder?: string; + private readonly file: string; + private data: T[]; constructor(option: BracesDBOption) { - const { type, folder = './', filename = 'main' } = option + const { type, folder = "./", filename = "main" } = option; - this.type = type - this.folder = folder - this.file = `${folder}${folder?.slice(-1) === '/' ? '' : '/'}${filename}.db` - this.data = [] + this.type = type; + this.folder = folder; + this.file = `${folder}${ + folder?.slice(-1) === "/" ? "" : "/" + }${filename}.db`; + this.data = []; - if (this.folder && !isExistFileSync(this.folder)) Deno.mkdirSync(this.folder, { recursive: true }) - if (this.folder && !isExistFileSync(this.file) && this.type === 'file') - writeFileSync(JSON.stringify(this.data), this.file) + if (this.folder && !isExistFileSync(this.folder)) { + Deno.mkdirSync(this.folder, { recursive: true }); + } + if (this.folder && !isExistFileSync(this.file) && this.type === "file") { + writeFileSync(JSON.stringify(this.data), this.file); + } if (isExistFileSync(this.file)) { - const json: T[] = JSON.parse(readFileSync(this.file)) - this.data = json + const json: T[] = JSON.parse(readFileSync(this.file)); + this.data = json; } } @@ -54,11 +64,14 @@ export class BracesDB { * @param keyword Use the specified key to prevent duplication */ async add(object: T, keyword: keyof T) { - const isDuplicate = this.data.filter(item => item[keyword] === object[keyword]).length - if (isDuplicate) return + const isDuplicate = + this.data.filter((item) => item[keyword] === object[keyword]).length; + if (isDuplicate) return; - this.data.push(object) - if (this.type === 'file') await writeFile(JSON.stringify(this.data), this.file) + this.data.push(object); + if (this.type === "file") { + await writeFile(JSON.stringify(this.data), this.file); + } } /** @@ -67,9 +80,11 @@ export class BracesDB { * @param keyword Wording of search conditions */ async delete(key: keyof T, keyword: T[keyof T]) { - const candidate = this.data.filter(item => item[key] !== keyword) - this.data = candidate - if (this.type === 'file') await writeFile(JSON.stringify(candidate), this.file) + const candidate = this.data.filter((item) => item[key] !== keyword); + this.data = candidate; + if (this.type === "file") { + await writeFile(JSON.stringify(candidate), this.file); + } } /** @@ -77,9 +92,11 @@ export class BracesDB { * @param key The key of the Object you want to search * @param keyword Wording of search conditions */ - async find(key?: keyof T, keyword?: T[keyof T] | RegExp) { - if (key && keyword instanceof RegExp) return this.data.filter(item => keyword.test(`${item[key]}`)) - else if (key && keyword) return this.data.filter(item => item[key] === keyword) - else return this.data + find(key?: keyof T, keyword?: T[keyof T] | RegExp) { + if (key && keyword instanceof RegExp) { + return this.data.filter((item) => keyword.test(`${item[key]}`)); + } else if (key && keyword) { + return this.data.filter((item) => item[key] === keyword); + } else return this.data; } -} \ No newline at end of file +}