linearAlgorithm: add test, fix selection order

This commit is contained in:
Alex Gleason 2022-09-13 11:57:28 -05:00
parent 4ff9918fe0
commit 474d7da02a
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 20 additions and 1 deletions

View file

@ -0,0 +1,19 @@
import { linearAlgorithm } from '../linear';
const DATA = Object.freeze(['a', 'b', 'c', 'd']);
test('linearAlgorithm', () => {
const result = Array(50).fill('').map((_, i) => {
return linearAlgorithm(DATA, i, { interval: 5 });
});
// console.log(result);
expect(result[0]).toBe(undefined);
expect(result[4]).toBe('a');
expect(result[8]).toBe(undefined);
expect(result[9]).toBe('b');
expect(result[10]).toBe(undefined);
expect(result[14]).toBe('c');
expect(result[15]).toBe(undefined);
expect(result[19]).toBe('d');
});

View file

@ -8,7 +8,7 @@ type Opts = {
/** Picks the next item every iteration. */
const linearAlgorithm: PickAlgorithm = (items, iteration, rawOpts) => {
const opts = normalizeOpts(rawOpts);
const itemIndex = items ? Math.floor((iteration + 1) / opts.interval) % items.length : 0;
const itemIndex = items ? Math.floor(iteration / opts.interval) % items.length : 0;
const item = items ? items[itemIndex] : undefined;
const showItem = (iteration + 1) % opts.interval === 0;