linearAlgorithm: add test, fix selection order
This commit is contained in:
parent
4ff9918fe0
commit
474d7da02a
2 changed files with 20 additions and 1 deletions
|
@ -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');
|
||||||
|
});
|
|
@ -8,7 +8,7 @@ type Opts = {
|
||||||
/** Picks the next item every iteration. */
|
/** Picks the next item every iteration. */
|
||||||
const linearAlgorithm: PickAlgorithm = (items, iteration, rawOpts) => {
|
const linearAlgorithm: PickAlgorithm = (items, iteration, rawOpts) => {
|
||||||
const opts = normalizeOpts(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 item = items ? items[itemIndex] : undefined;
|
||||||
const showItem = (iteration + 1) % opts.interval === 0;
|
const showItem = (iteration + 1) % opts.interval === 0;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue