From 7254d6ef23011444f3c17ec20071e36c345e754a Mon Sep 17 00:00:00 2001 From: Benny Neugebauer Date: Mon, 13 Jan 2025 23:34:38 +0100 Subject: [PATCH] refactor: Return values from multiple updates --- src/Indicator.test.ts | 8 ++++++++ src/Indicator.ts | 5 ++--- src/util/getLastFromForEach.test.ts | 9 --------- src/util/getLastFromForEach.ts | 12 ------------ src/util/index.ts | 1 - 5 files changed, 10 insertions(+), 25 deletions(-) delete mode 100644 src/util/getLastFromForEach.test.ts delete mode 100644 src/util/getLastFromForEach.ts diff --git a/src/Indicator.test.ts b/src/Indicator.test.ts index 77fe4215..0a7975fb 100644 --- a/src/Indicator.test.ts +++ b/src/Indicator.test.ts @@ -107,4 +107,12 @@ describe('Indicator', () => { expect(itc.highest?.toString()).toBe('97'); }); }); + + describe('updates', () => { + it('returns all values from individual updates', () => { + const itc = new IndicatorTestClass(); + const results = itc.updates([100, 1_000, 10_000]); + expect(results.map(big => big?.toString())).toEqual(['100', '550', '3700']); + }); + }); }); diff --git a/src/Indicator.ts b/src/Indicator.ts index d6914418..fefb71be 100644 --- a/src/Indicator.ts +++ b/src/Indicator.ts @@ -1,6 +1,5 @@ import type {BigSource} from 'big.js'; import {NotEnoughDataError} from './error/NotEnoughDataError.js'; -import {getLastFromForEach} from './util/getLastFromForEach.js'; type Nullable = Result | null; @@ -11,7 +10,7 @@ interface Indicator { getResultOrThrow(): Result; replace(input: Input): Nullable; update(input: Input, replace: boolean): Nullable; - updates(input: Input[], replace: boolean): Nullable; + updates(input: Input[], replace: boolean): Nullable[]; } export abstract class TechnicalIndicator implements Indicator { @@ -48,7 +47,7 @@ export abstract class TechnicalIndicator implements Indicator this.update(input, replace)); + return inputs.map(input => this.update(input, replace)); } } diff --git a/src/util/getLastFromForEach.test.ts b/src/util/getLastFromForEach.test.ts deleted file mode 100644 index c207bd09..00000000 --- a/src/util/getLastFromForEach.test.ts +++ /dev/null @@ -1,9 +0,0 @@ -import {getLastFromForEach} from './getLastFromForEach.js'; - -describe('getLastFromForEach', () => { - it('returns the last value from an forEach execution', () => { - const array = [1, 2, 3, 4]; - const result = getLastFromForEach(array, value => value * 2); - expect(result).toBe(8); - }); -}); diff --git a/src/util/getLastFromForEach.ts b/src/util/getLastFromForEach.ts deleted file mode 100644 index ad5552a0..00000000 --- a/src/util/getLastFromForEach.ts +++ /dev/null @@ -1,12 +0,0 @@ -export function getLastFromForEach( - array: readonly T[], - callback: (value: T, index: number, array: readonly T[]) => R -): R | null { - let lastValue: R | null = null; - - array.forEach((item, index) => { - lastValue = callback(item, index, array); - }); - - return lastValue; -} diff --git a/src/util/index.ts b/src/util/index.ts index c36bc2bf..4c584c8d 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -1,6 +1,5 @@ export * from './BandsResult.js'; export * from './getAverage.js'; -export * from './getLastFromForEach.js'; export * from './getMaximum.js'; export * from './getMinimum.js'; export * from './getStandardDeviation.js';