Skip to content

Commit

Permalink
Release: v0.3.1 (#4)
Browse files Browse the repository at this point in the history
- fix: bug on append
  • Loading branch information
maxswjeon authored Apr 15, 2023
2 parents e48a29c + 7e1ba3f commit aa7af75
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flattype",
"version": "0.3.0",
"version": "0.3.1",
"description": "A simple library for creating flat types",
"main": "dist/Flat.js",
"module": "dist/Flat.js",
Expand Down
11 changes: 11 additions & 0 deletions src/Flat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@ export default class Flat {
throw new Error("Flat is not an array");
}

const subKeys = Object.keys(this.flat).filter(
(flatKey) => flatKey.startsWith(key) && flatKey !== key
);

if (subKeys.length === 0) {
const newKey = key === "" ? "0" : `${key}__0`;
this.set(newKey, value);

return this;
}

const index = Object.keys(this.flat).filter(
(flatKey) =>
flatKey.startsWith(key) && !flatKey.slice(key.length).includes("__")
Expand Down
16 changes: 16 additions & 0 deletions test/append.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@ describe("append(key: string, value: any) function", () => {
});
});

it("should append value by key on empty array", () => {
const flat = Flat.from({
foo: [],
});

expect(flat.append("foo", "baz").getData()).toEqual({
foo: ["baz"],
});
});

it("should append value by key on root", () => {
const flat = Flat.from(["foo"]);

expect(flat.append("", "baz").getData()).toEqual(["foo", "baz"]);
});

it("should append value by key on root when root is empty array", () => {
const flat = Flat.from([]);

expect(flat.append("", "baz").getData()).toEqual(["baz"]);
});

it("should throw an error when tried to append on non existing key", () => {
const flat = Flat.from({
foo: "bar",
Expand Down

0 comments on commit aa7af75

Please sign in to comment.