From 99a2e97260812254fade68fb47799b17b64795d0 Mon Sep 17 00:00:00 2001 From: diy0r Date: Wed, 4 Sep 2024 00:56:29 +0500 Subject: [PATCH] feat: add public forEachVertex method --- lib/graph/file.graph.ts | 9 ++++++++- lib/graph/storage.file.ts | 3 ++- lib/interfaces/graph.interface.ts | 13 +++++++++++++ lib/interfaces/params.interface.ts | 3 +++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/graph/file.graph.ts b/lib/graph/file.graph.ts index 8fcf806..86e5c2c 100644 --- a/lib/graph/file.graph.ts +++ b/lib/graph/file.graph.ts @@ -1,5 +1,6 @@ import { FileGraphAbstract, + ICallbackVertex, IFindVertex, IPredicate, IUpdater, @@ -47,7 +48,7 @@ class FileGraphIml implements FileGraphAbstract { ); } - public async findOne( + public findOne( predicate: IPredicate, ): Promise | null> { return this.storageFile.searchLine(predicate); @@ -64,6 +65,12 @@ class FileGraphIml implements FileGraphAbstract { return vertices; } + public async forEachVertex( + callbackVertex: ICallbackVertex, + ): Promise { + await this.storageFile.searchLine(callbackVertex); + } + public async createEdge(ids: IUuidArray): Promise { const updater = (vertex: IVertex) => { const index = ids.indexOf(vertex.id); diff --git a/lib/graph/storage.file.ts b/lib/graph/storage.file.ts index 648fccf..7e77cc9 100644 --- a/lib/graph/storage.file.ts +++ b/lib/graph/storage.file.ts @@ -8,6 +8,7 @@ import { finished } from 'stream/promises'; import readline from 'readline'; import { mergeVertices, uuid } from '../utils'; import { + ICallbackVertex, IFindVertex, ILineReturn, IPredicate, @@ -27,7 +28,7 @@ export class StorageFile { } public async searchLine( - predicate: IFindVertex | IPredicate, + predicate: IFindVertex | IPredicate | ICallbackVertex, ): Promise> { const fileStream = this.createLineStream(true); return await fileStream(async line => { diff --git a/lib/interfaces/graph.interface.ts b/lib/interfaces/graph.interface.ts index 3800126..2b52b22 100644 --- a/lib/interfaces/graph.interface.ts +++ b/lib/interfaces/graph.interface.ts @@ -1,4 +1,5 @@ import { + ICallbackVertex, IPredicate, IUpdater, IUuidArray, @@ -80,6 +81,18 @@ export abstract class FileGraphAbstract { predicate: IPredicate, ): Promise[]>; + /** + * Iterates over each vertex in the graph and applies the provided callback function. + * + * @template T - The type of the vertex object. + * @param {ICallbackVertex} callbackVertex - A callback function that is invoked for each vertex. + * If it returns `true`, the iteration stops. + * @returns {Promise} - A promise that resolves when the iteration is complete or has been stopped. + */ + public abstract forEachVertex( + callbackVertex: ICallbackVertex, + ): Promise; + /** * Creates edges (links) between the specified vertices. * diff --git a/lib/interfaces/params.interface.ts b/lib/interfaces/params.interface.ts index c325701..347ed0a 100644 --- a/lib/interfaces/params.interface.ts +++ b/lib/interfaces/params.interface.ts @@ -12,6 +12,9 @@ export interface IVertexTree extends IVertex { export type IUuidArray = [uuidType, ...uuidType[]]; export type IPredicate = (vertex: IVertex) => boolean; +export type ICallbackVertex = ( + vertex: IVertex, +) => void | boolean; export type IUpdater = ( vertex: IVertex, ) => IVertex | object;