Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adjust return types #53

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0.1",
"version": "1.0.2",
"repository": {
"type": "git",
"url": "https://github.com/luckasnix/structured-objects.git"
Expand Down
30 changes: 15 additions & 15 deletions src/object-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,31 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
* @description Returns the length of the object graph.
* @since 1.0.0
*/
public get length() {
public get length(): number {
return this.nodes.size;
}

/**
* @description Returns an iterator object that contains the keys of the object graph.
* @since 1.0.0
*/
public keys() {
public keys(): IterableIterator<string> {
return this.nodes.keys();
}

/**
* @description Returns an iterator object that contains the values of the object graph.
* @since 1.0.0
*/
public values() {
public values(): IterableIterator<NodeValue> {
return this.nodes.values();
}

/**
* @description Returns a node of the object graph.
* @since 1.0.0
*/
public get(nodeKey: string) {
public get(nodeKey: string): NodeValue | undefined {
if (!nodeKey) {
throw new Error("Provide a value for the 'nodeKey' parameter");
}
Expand All @@ -68,15 +68,15 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
* @description Returns a copy of the original object graph.
* @since 1.0.0
*/
public copy() {
public copy(): ObjectGraph<NodeValue> {
return new ObjectGraph(Array.from(this.nodes.values()), this.keyExtractor);
}

/**
* @description Adds a node to the object graph.
* @since 1.0.0
*/
public add(nodeValue: NodeValue) {
public add(nodeValue: NodeValue): void {
if (!nodeValue) {
throw new Error("Provide a value for the 'nodeValue' parameter");
}
Expand All @@ -91,7 +91,7 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
* @description Returns a copy of the original object graph with a received node added.
* @since 1.0.0
*/
public toAdded(nodeValue: NodeValue) {
public toAdded(nodeValue: NodeValue): ObjectGraph<NodeValue> {
const copiedObjectGraph = this.copy();
copiedObjectGraph.add(nodeValue);
return copiedObjectGraph;
Expand All @@ -101,7 +101,7 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
* @description Updates a node in the object graph.
* @since 1.0.0
*/
public update(nodeValue: NodeValue) {
public update(nodeValue: NodeValue): void {
if (!nodeValue) {
throw new Error("Provide a value for the 'nodeValue' parameter");
}
Expand All @@ -116,7 +116,7 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
* @description Returns a copy of the original object graph with a received node updated.
* @since 1.0.0
*/
public toUpdated(nodeValue: NodeValue) {
public toUpdated(nodeValue: NodeValue): ObjectGraph<NodeValue> {
const copiedObjectGraph = this.copy();
copiedObjectGraph.update(nodeValue);
return copiedObjectGraph;
Expand All @@ -126,7 +126,7 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
* @description Removes a node from the object graph.
* @since 1.0.0
*/
public remove(nodeKey: string) {
public remove(nodeKey: string): void {
if (!nodeKey) {
throw new Error("Provide a value for the 'nodeKey' parameter");
}
Expand All @@ -143,7 +143,7 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
* @description Returns a copy of the original object graph with a received node removed.
* @since 1.0.0
*/
public toRemoved(nodeKey: string) {
public toRemoved(nodeKey: string): ObjectGraph<NodeValue> {
const copiedObjectGraph = this.copy();
copiedObjectGraph.remove(nodeKey);
return copiedObjectGraph;
Expand All @@ -153,16 +153,16 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
* @description Returns all values of the provided property.
* @since 1.0.0
*/
public valuesOf(propertyKey: keyof NodeValue) {
public valuesOf(propertyKey: keyof NodeValue): Array<NodeValue[keyof NodeValue]> {
if (!propertyKey) {
throw new Error("Provide a value for the 'propertyKey' parameter");
}
if (typeof propertyKey !== "string") {
throw new TypeError("The parameter 'propertyKey' must be a string");
}
const propertyValues = new Set();
const propertyValues: Set<NodeValue[keyof NodeValue]> = new Set();
for (const [_, nodeValue] of this.nodes) {
propertyValues.add(nodeValue[propertyKey]);
propertyValues.add(nodeValue[propertyKey as keyof NodeValue]);
}
return Array.from(propertyValues);
}
Expand All @@ -171,7 +171,7 @@ export class ObjectGraph<NodeValue extends Record<string, unknown>> {
* @description Returns all nodes that match with the provided shape.
* @since 1.0.0
*/
public match(shape: Partial<Record<keyof NodeValue, Array<unknown>>>) {
public match(shape: Partial<Record<keyof NodeValue, Array<unknown>>>): Array<NodeValue> {
if (!shape) {
throw new Error("Provide a value for the 'shape' parameter");
}
Expand Down