Skip to content

Commit

Permalink
feat: add "values" method
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasnix committed Sep 14, 2024
1 parent 04cd540 commit 830faff
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
22 changes: 11 additions & 11 deletions src/object-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
}
}

/**
* @description Returns the length of the object graph.
* @since 0.1.0
*/
public get length() {
return this.nodes.size;
}

/**
* @description Returns the keys of the object graph.
* @since 0.1.0
Expand All @@ -31,11 +39,11 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
}

/**
* @description Returns the length of the object graph.
* @description Returns the values of the object graph.
* @since 0.1.0
*/
public get length() {
return this.nodes.size;
public values() {
return this.nodes.values();
}

/**
Expand All @@ -56,14 +64,6 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
return nodeValue;
}

/**
* @description Returns all nodes of the object graph.
* @since 0.1.0
*/
public getAll() {
return Array.from(this.nodes.values());
}

/**
* @description Returns a copy of the original object graph.
* @since 0.1.0
Expand Down
40 changes: 21 additions & 19 deletions tests/object-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ import {
type Size,
} from "../mocks/object-graph.mock";

describe("keys", () => {
test("get the keys of the object graph", () => {
const shirtsObjectGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);

expect(Array.from(shirtsObjectGraph.keys())).toEqual(["1", "2", "3", "4", "5", "6", "7", "8"]);
});
});

describe("length", () => {
test("get the length of the object graph", () => {
const shirtToAdd: Shirt = { sku: "9", color: "orange", size: "small" };
Expand All @@ -30,6 +22,27 @@ describe("length", () => {
});
});

describe("keys()", () => {
test("get the keys of the object graph", () => {
const shirtsObjectGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);

const shirtsObjectGraphKeys = Array.from(shirtsObjectGraph.keys());

expect(shirtsObjectGraphKeys).toEqual(["1", "2", "3", "4", "5", "6", "7", "8"]);
});
});

describe("values()", () => {
test("get the values of the object graph", () => {
const shirtsObjectGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);

const shirtsObjectGraphValues = Array.from(shirtsObjectGraph.values());

expect(shirtsObjectGraphValues).toHaveLength(8);
expect(shirtsObjectGraphValues[0]).toStrictEqual(shirtsMock[0]);
});
});

describe("get()", () => {
test("logs an error when there is no node with the provided key in the object graph", () => {
const consoleErrorSpy = vi.spyOn(console, "error");
Expand All @@ -51,17 +64,6 @@ describe("get()", () => {
});
});

describe("getAll()", () => {
test("get all nodes of the object graph", () => {
const shirtsObjectGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);

const returnedShirtsObjectGraph = shirtsObjectGraph.getAll();

expect(shirtsMock.length).toBe(returnedShirtsObjectGraph.length);
expect(shirtsMock[0]).toEqual(returnedShirtsObjectGraph[0]);
});
});

describe("copy()", () => {
test("get a copy of the original object graph", () => {
const shirtsObjectGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
Expand Down

0 comments on commit 830faff

Please sign in to comment.