Skip to content

Commit

Permalink
refactor: Return values from multiple updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed Jan 13, 2025
1 parent 2423543 commit 7254d6e
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 25 deletions.
8 changes: 8 additions & 0 deletions src/Indicator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});
});
5 changes: 2 additions & 3 deletions src/Indicator.ts
Original file line number Diff line number Diff line change
@@ -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> = Result | null;

Expand All @@ -11,7 +10,7 @@ interface Indicator<Result = Big, Input = BigSource> {
getResultOrThrow(): Result;
replace(input: Input): Nullable<Result>;
update(input: Input, replace: boolean): Nullable<Result>;
updates(input: Input[], replace: boolean): Nullable<Result>;
updates(input: Input[], replace: boolean): Nullable<Result>[];
}

export abstract class TechnicalIndicator<Result, Input> implements Indicator<Result, Input> {
Expand Down Expand Up @@ -48,7 +47,7 @@ export abstract class TechnicalIndicator<Result, Input> implements Indicator<Res
abstract update(input: Input, replace: boolean): Result | null;

updates(inputs: readonly Input[], replace: boolean = false) {
return getLastFromForEach(inputs, input => this.update(input, replace));
return inputs.map(input => this.update(input, replace));
}
}

Expand Down
9 changes: 0 additions & 9 deletions src/util/getLastFromForEach.test.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/util/getLastFromForEach.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/util/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit 7254d6e

Please sign in to comment.