Skip to content

Commit

Permalink
feat: add "size" property (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasnix authored Dec 5, 2024
1 parent b8ff555 commit 6edb2fe
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
4 changes: 4 additions & 0 deletions docs/object-graph.doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Returns an instance of ObjectGraph.

Returns the length of the object graph.

### size

Returns the size of the object graph.

## Instance Methods

### keys()
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kasnix/structured-objects",
"version": "1.1.0",
"version": "1.2.0",
"repository": {
"type": "git",
"url": "git+https://github.com/luckasnix/structured-objects.git"
Expand Down
9 changes: 9 additions & 0 deletions src/object-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,20 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
/**
* @description Returns the length of the object graph.
* @since 1.0.0
* @deprecated Since version 1.2.0. Will be removed in version 2.0.0. Use "size" instead.
*/
public get length(): number {
return this.nodes.size;
}

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

/**
* @description Returns an iterator object that contains the keys of the object graph.
* @since 1.0.0
Expand Down
21 changes: 17 additions & 4 deletions tests/object-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ describe("length", () => {
});
});

describe("size", () => {
test("gets the size of the object graph", () => {
const shirtToAdd: Shirt = { sku: "9", color: "orange", size: "small" };
const shirtsObjectGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);

expect(shirtsObjectGraph.size).toBe(8);

shirtsObjectGraph.add(shirtToAdd);

expect(shirtsObjectGraph.size).toBe(9);
});
});

describe("keys()", () => {
test("gets an iterator object that contains the keys of the object graph", () => {
const shirtsObjectGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
Expand Down Expand Up @@ -104,8 +117,8 @@ describe("toAdded()", () => {

const copiedShirtsObjectGraph = shirtsObjectGraph.toAdded(shirtsMock[0]);

expect(shirtsObjectGraph.length).toBe(0);
expect(copiedShirtsObjectGraph.length).toBe(1);
expect(shirtsObjectGraph.size).toBe(0);
expect(copiedShirtsObjectGraph.size).toBe(1);

const returnedNodeFromCopy = copiedShirtsObjectGraph.get("1");

Expand Down Expand Up @@ -191,8 +204,8 @@ describe("toRemoved()", () => {

const copiedShirtsObjectGraph = shirtsObjectGraph.toRemoved("1");

expect(shirtsObjectGraph.length).toBe(8);
expect(copiedShirtsObjectGraph.length).toBe(7);
expect(shirtsObjectGraph.size).toBe(8);
expect(copiedShirtsObjectGraph.size).toBe(7);

const returnedNodeFromOriginal = shirtsObjectGraph.get("1");

Expand Down

0 comments on commit 6edb2fe

Please sign in to comment.