From 38ef2f796fd4ca4ffafd11aab929539b197e7bcc Mon Sep 17 00:00:00 2001 From: Evyatar Date: Sat, 8 Jun 2024 14:42:29 +0300 Subject: [PATCH] patch(vest): reduceTests --- .../core/isolate/IsolateTest/TestWalker.ts | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/vest/src/core/isolate/IsolateTest/TestWalker.ts b/packages/vest/src/core/isolate/IsolateTest/TestWalker.ts index a71a90595..75b77c809 100644 --- a/packages/vest/src/core/isolate/IsolateTest/TestWalker.ts +++ b/packages/vest/src/core/isolate/IsolateTest/TestWalker.ts @@ -18,7 +18,7 @@ export class TestWalker { static someTests( predicate: (test: TIsolateTest) => boolean, - root: MaybeRoot = TestWalker.defaultRoot() + root: MaybeRoot = TestWalker.defaultRoot(), ): boolean { if (!root) return false; return Walker.some( @@ -28,13 +28,13 @@ export class TestWalker { return predicate(isolate); }, - VestTest.is + VestTest.is, ); } static everyTest( predicate: (test: TIsolateTest) => boolean, - root: MaybeRoot = TestWalker.defaultRoot() + root: MaybeRoot = TestWalker.defaultRoot(), ): boolean { if (!root) return false; return Walker.every( @@ -44,13 +44,13 @@ export class TestWalker { return predicate(isolate); }, - VestTest.is + VestTest.is, ); } static walkTests( callback: (test: TIsolateTest, breakout: () => void) => void, - root: MaybeRoot = TestWalker.defaultRoot() + root: MaybeRoot = TestWalker.defaultRoot(), ): void { if (!root) return; Walker.walk( @@ -58,13 +58,29 @@ export class TestWalker { (isolate, breakout) => { callback(VestTest.cast(isolate), breakout); }, - VestTest.is + VestTest.is, + ); + } + + static reduceTests( + callback: (acc: T, test: I, breakout: () => void) => T, + initialValue: T, + root: MaybeRoot = TestWalker.defaultRoot(), + ): T { + if (!root) return initialValue; + return Walker.reduce( + root, + (acc, isolate, breakout) => { + return callback(acc, VestTest.cast(isolate) as I, breakout); + }, + initialValue, + VestTest.is, ); } static pluckTests( predicate: (test: TIsolateTest) => boolean, - root: MaybeRoot = TestWalker.defaultRoot() + root: MaybeRoot = TestWalker.defaultRoot(), ): void { if (!root) return; Walker.pluck( @@ -74,7 +90,7 @@ export class TestWalker { return predicate(isolate); }, - VestTest.is + VestTest.is, ); } @@ -88,7 +104,7 @@ export class TestWalker { static removeTestByFieldName( fieldName: TFieldName, - root: MaybeRoot = TestWalker.defaultRoot() + root: MaybeRoot = TestWalker.defaultRoot(), ): void { TestWalker.pluckTests(testObject => { return matchingFieldName(VestTest.getData(testObject), fieldName);