Skip to content

Commit

Permalink
Release v0.3.3 (#6)
Browse files Browse the repository at this point in the history
- fix: append error
  - added test cases
  - fixed wrong test case on `delete.test.ts`
  - fixed `append` malfunction
  • Loading branch information
maxswjeon authored Apr 16, 2023
2 parents 8cb9a46 + b2f988d commit e5a9576
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 17 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "flattype",
"version": "0.3.2",
"description": "A simple library for creating flat types",
"main": "dist/Flat.js",
"module": "dist/Flat.js",
Expand Down Expand Up @@ -28,4 +29,4 @@
"vite": "^4.2.1",
"vitest": "^0.29.8"
}
}
}
31 changes: 15 additions & 16 deletions src/Flat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,23 +279,22 @@ 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("__")
).length;
const lastIndex = Object.keys(this.flat).reduce((lastIndex, flatKey) => {
if (flatKey.startsWith(key) && flatKey !== key) {
const subKey = flatKey.slice(key.length + (key === "" ? 0 : 2));
const indexSeparator = subKey.indexOf("__");
const index = parseInt(
indexSeparator === -1 ? subKey : subKey.slice(0, indexSeparator),
10
);
return Math.max(index, lastIndex);
}
return lastIndex;
}, -1);

const newKey = key === "" ? index.toString() : `${key}__${index}`;
// Create new key for the value to append
const newKey =
key === "" ? (lastIndex + 1).toString() : `${key}__${lastIndex + 1}`;
this.set(newKey, value);

return this;
Expand Down
43 changes: 43 additions & 0 deletions test/append.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ describe("append(key: string, value: any) function", () => {
});
});

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

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

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

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

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

Expand All @@ -34,6 +60,23 @@ describe("append(key: string, value: any) function", () => {
expect(flat.append("", "baz").getData()).toEqual(["baz"]);
});

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

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

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

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

it("should throw an error when tried to append on non existing key", () => {
const flat = Flat.from({
foo: "bar",
Expand Down
7 changes: 7 additions & 0 deletions test/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,25 @@ describe("delete(key: string) function", () => {
});

flat.delete("foo__1");

flat.append("foo", {
bar: {
baz: "tar",
},
});

expect(flat.getData()).toEqual({
foo: [
{
bar: {
baz: "qux",
},
},
{
bar: {
baz: "foo",
},
},
{
bar: {
baz: "tar",
Expand Down
57 changes: 57 additions & 0 deletions test/getKeys.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, it } from "vitest";
import Flat from "../src/Flat";

describe("get(key: string) function", () => {
it("should get keys on simple objet", () => {
const flat = Flat.from({
foo: "bar",
});

expect(flat.getKeys()).toEqual(["foo"]);
});

it("should get keys on array", () => {
const flat = Flat.from({
foo: ["bar", "baz"],
});

expect(flat.getKeys()).toEqual(["foo", "foo__0", "foo__1"]);
});

it("should get keys on nested object", () => {
const flat = Flat.from({
foo: {
bar: "baz",
},
});

expect(flat.getKeys()).toEqual(["foo", "foo__bar"]);
});

it("should get keys on nested array", () => {
const flat = Flat.from({
foo: {
bar: ["baz", "qux"],
},
});

expect(flat.getKeys()).toEqual([
"foo",
"foo__bar",
"foo__bar__0",
"foo__bar__1",
]);
});

it("should get keys on nested object", () => {
const flat = Flat.from({
foo: {
bar: {
baz: "qux",
},
},
});

expect(flat.getKeys()).toEqual(["foo", "foo__bar", "foo__bar__baz"]);
});
});

0 comments on commit e5a9576

Please sign in to comment.