Skip to content

Commit

Permalink
Release v0.2.0 (#2)
Browse files Browse the repository at this point in the history
- [fix: append and delete bugs](d463be6)
- feat: add more tests
maxswjeon authored Apr 7, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents cdd3381 + d463be6 commit 9aab1ff
Showing 7 changed files with 192 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -12,6 +12,8 @@ jobs:
packages: write
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
with:
registry-url: "https://registry.npmjs.org"
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!dist/**
Empty file added .yarn/versions/26f35860.yml
Empty file.
59 changes: 59 additions & 0 deletions src/Flat.ts
Original file line number Diff line number Diff line change
@@ -138,6 +138,32 @@ export default class Flat {
return array;
}

private static createNextKey(
key: string,
index: number,
depth: number
): string {
const keys = key.split("__");

if (keys.length < depth) {
return key;
}

const prevIndex = Number(keys[depth - 1]);

if (prevIndex < index) {
return key;
}

keys[depth - 1] = (prevIndex - 1).toString();

return keys.join("__");
}

static getParentKey(key: string): string {
return key.split("__").slice(0, -1).join("__");
}

static from(data: object) {
return new Flat(data);
}
@@ -178,6 +204,39 @@ export default class Flat {
}
});

const parent = Flat.getParentKey(key);
if (
(parent === "" && this.type === "array") ||
this.flat[parent]?.type === "array"
) {
const removedIndex = Number(
key.slice(parent.length === 0 ? 0 : parent.length + 2)
);
const updatedDepth = key.split("__").length;

Object.keys(this.flat).forEach((flatKey) => {
const newKey = Flat.createNextKey(flatKey, removedIndex, updatedDepth);

const type = this.flat[flatKey].type;
const value = this.flat[flatKey].value;
delete this.flat[flatKey];

if (type === "value") {
this.flat[newKey] = {
type,
value,
};
} else {
const newValue = value.slice(flatKey.length);

this.flat[newKey] = {
type,
value: newKey + newValue,
};
}
});
}

return this;
}

3 changes: 0 additions & 3 deletions src/utils.ts

This file was deleted.

125 changes: 125 additions & 0 deletions test/delete.test.ts
Original file line number Diff line number Diff line change
@@ -92,4 +92,129 @@ describe("delete(key: string) function", () => {
foo: {},
});
});

it("should delete element in array on root", () => {
const flat = Flat.from(["foo", "bar", "baz"]);
flat.delete("1");
expect(flat.getData()).toEqual(["foo", "baz"]);
});

it("should delete element in array on nested", () => {
const flat = Flat.from({
foo: ["bar", "baz", "qux"],
});
flat.delete("foo__1");
expect(flat.getData()).toEqual({
foo: ["bar", "qux"],
});
});

it("should delete element in array with objects", () => {
const flat = Flat.from({
foo: [
{
bar: "baz",
},
{
bar: "qux",
},
{
bar: "tes",
},
],
});

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

it("should delete element in array with complex objects", () => {
const flat = Flat.from({
foo: [
{
bar: {
baz: "qux",
},
},
{
bar: {
baz: "tes",
},
},
{
bar: {
baz: "foo",
},
},
],
});

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

it("should able to append new value after delete on complex array", () => {
const flat = Flat.from({
foo: [
{
bar: {
baz: "qux",
},
},
{
bar: {
baz: "tes",
},
},
{
bar: {
baz: "foo",
},
},
],
});

flat.delete("foo__1");
flat.append("foo", {
bar: {
baz: "tar",
},
});
expect(flat.getData()).toEqual({
foo: [
{
bar: {
baz: "qux",
},
},
{
bar: {
baz: "tar",
},
},
],
});
});
});
8 changes: 4 additions & 4 deletions test/utils/getParentkey.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { describe, expect, it } from "vitest";
import { getParentKey } from "../../src/utils";
import Flat from "../../src/Flat";

describe("getParentKey", () => {
it("should return parent key for object", () => {
const key = "foo__bar__baz";
const parentKey = "foo__bar";

expect(getParentKey(key)).toBe(parentKey);
expect(Flat.getParentKey(key)).toBe(parentKey);
});

it("should return parent key for array", () => {
const key = "foo__bar__10";
const parentKey = "foo__bar";

expect(getParentKey(key)).toBe(parentKey);
expect(Flat.getParentKey(key)).toBe(parentKey);
});

it("should return empty string when key is root", () => {
const key = "foo";

expect(getParentKey(key)).toBe("");
expect(Flat.getParentKey(key)).toBe("");
});
});

0 comments on commit 9aab1ff

Please sign in to comment.