Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from windchime-yk/add-test
Browse files Browse the repository at this point in the history
Add check action
  • Loading branch information
windchime-yk authored Jan 30, 2021
2 parents a8a565a + e299995 commit 48091d4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 48 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -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/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions README_JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```

## テスト
Expand Down
20 changes: 9 additions & 11 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const addDb = async (db: BracesDB<Test>): Promise<void> => {
description: item.description,
show: item.show,
},
"name"
"name",
);
}
};
Expand All @@ -58,11 +58,11 @@ Deno.test(
filename,
});
await addDb(db);
assertEquals(await db.find(), dataList);
assertEquals(db.find(), dataList);
} finally {
await deleteDbFile();
}
}
},
);

Deno.test(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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();
}
}
},
);
79 changes: 48 additions & 31 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -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 = {
/**
Expand All @@ -7,44 +12,49 @@ 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.
* If the type is `memory`, you don't need it.
*
* 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<T> {
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;
}
}

Expand All @@ -54,11 +64,14 @@ export class BracesDB<T> {
* @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);
}
}

/**
Expand All @@ -67,19 +80,23 @@ export class BracesDB<T> {
* @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);
}
}

/**
* Searching the DB
* @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;
}
}
}

0 comments on commit 48091d4

Please sign in to comment.