Skip to content

Commit 6edb2fe

Browse files
authored
feat: add "size" property (#62)
1 parent b8ff555 commit 6edb2fe

5 files changed

+33
-7
lines changed

docs/object-graph.doc.md

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Returns an instance of ObjectGraph.
2020

2121
Returns the length of the object graph.
2222

23+
### size
24+
25+
Returns the size of the object graph.
26+
2327
## Instance Methods
2428

2529
### keys()

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kasnix/structured-objects",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/luckasnix/structured-objects.git"

src/object-graph.ts

+9
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
2525
/**
2626
* @description Returns the length of the object graph.
2727
* @since 1.0.0
28+
* @deprecated Since version 1.2.0. Will be removed in version 2.0.0. Use "size" instead.
2829
*/
2930
public get length(): number {
3031
return this.nodes.size;
3132
}
3233

34+
/**
35+
* @description Returns the size of the object graph.
36+
* @since 1.2.0
37+
*/
38+
public get size(): number {
39+
return this.nodes.size;
40+
}
41+
3342
/**
3443
* @description Returns an iterator object that contains the keys of the object graph.
3544
* @since 1.0.0

tests/object-graph.test.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ describe("length", () => {
2222
});
2323
});
2424

25+
describe("size", () => {
26+
test("gets the size of the object graph", () => {
27+
const shirtToAdd: Shirt = { sku: "9", color: "orange", size: "small" };
28+
const shirtsObjectGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
29+
30+
expect(shirtsObjectGraph.size).toBe(8);
31+
32+
shirtsObjectGraph.add(shirtToAdd);
33+
34+
expect(shirtsObjectGraph.size).toBe(9);
35+
});
36+
});
37+
2538
describe("keys()", () => {
2639
test("gets an iterator object that contains the keys of the object graph", () => {
2740
const shirtsObjectGraph = new ObjectGraph<Shirt>(shirtsMock, (shirt) => shirt.sku);
@@ -104,8 +117,8 @@ describe("toAdded()", () => {
104117

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

107-
expect(shirtsObjectGraph.length).toBe(0);
108-
expect(copiedShirtsObjectGraph.length).toBe(1);
120+
expect(shirtsObjectGraph.size).toBe(0);
121+
expect(copiedShirtsObjectGraph.size).toBe(1);
109122

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

@@ -191,8 +204,8 @@ describe("toRemoved()", () => {
191204

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

194-
expect(shirtsObjectGraph.length).toBe(8);
195-
expect(copiedShirtsObjectGraph.length).toBe(7);
207+
expect(shirtsObjectGraph.size).toBe(8);
208+
expect(copiedShirtsObjectGraph.size).toBe(7);
196209

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

0 commit comments

Comments
 (0)