mirror of
https://github.com/muerwre/markdown-home-tab.git
synced 2025-04-25 00:46:41 +07:00
added data chunking for storage
This commit is contained in:
parent
6ede9f157e
commit
e921803267
8 changed files with 821 additions and 145 deletions
74
src/utils/__tests__/chunks.test.ts
Normal file
74
src/utils/__tests__/chunks.test.ts
Normal file
|
@ -0,0 +1,74 @@
|
|||
import { it, expect, describe } from "vitest";
|
||||
import {
|
||||
splitTextInChunks,
|
||||
joinTextFromChunks,
|
||||
makeChunks,
|
||||
} from "~/utils/chunks";
|
||||
|
||||
describe("chunks", () => {
|
||||
describe("splitTextInChunks", () => {
|
||||
const getKey = () => "a"; // key of length 1
|
||||
|
||||
it("splits text in chunks", () => {
|
||||
expect(splitTextInChunks("0123456789", 4, getKey)).toEqual([
|
||||
"012",
|
||||
"345",
|
||||
"678",
|
||||
"9",
|
||||
]);
|
||||
|
||||
expect(splitTextInChunks("", 4, getKey)).toEqual([]);
|
||||
expect(splitTextInChunks("123", 4, getKey)).toEqual(["123"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("joinTextFromChunks", () => {
|
||||
const getKey = (i: number) => (i === 0 ? `test` : `test_${i}`);
|
||||
|
||||
it("collects chunks", () => {
|
||||
expect(
|
||||
joinTextFromChunks(getKey, {
|
||||
test: "abc",
|
||||
test_1: "def",
|
||||
test_2: "ghi",
|
||||
})
|
||||
).toBe("abcdefghi");
|
||||
});
|
||||
|
||||
it("works with undefined keys", () => {
|
||||
expect(
|
||||
joinTextFromChunks(getKey, {
|
||||
z: "abc",
|
||||
z_1: "def",
|
||||
z_2: "ghi",
|
||||
})
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("works with empty strings keys", () => {
|
||||
expect(
|
||||
joinTextFromChunks(getKey, {
|
||||
test: "",
|
||||
test_1: "def",
|
||||
test_2: "ghi",
|
||||
})
|
||||
).toBe("defghi");
|
||||
});
|
||||
});
|
||||
|
||||
describe("makeChunks", () => {
|
||||
const getKey = (i: number) => `test_${i}`;
|
||||
const chunkSize = 3;
|
||||
|
||||
it("makes chunks", () => {
|
||||
expect(
|
||||
makeChunks("abcdefghij", getKey, getKey(0).length + chunkSize)
|
||||
).toEqual({
|
||||
test_0: "abc",
|
||||
test_1: "def",
|
||||
test_2: "ghi",
|
||||
test_3: "j",
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue