bigbuffet-rw/app/soapbox/features/timeline-insertion/linear.ts

28 lines
795 B
TypeScript
Raw Normal View History

import type { PickAlgorithm } from './types';
type Opts = {
/** Number of iterations until the next item is picked. */
interval: number
};
2022-09-09 18:47:51 -07:00
/** Picks the next item every iteration. */
const linearAlgorithm: PickAlgorithm = (items, iteration, rawOpts) => {
const opts = normalizeOpts(rawOpts);
const itemIndex = items ? Math.floor(iteration / opts.interval) % items.length : 0;
const item = items ? items[itemIndex] : undefined;
2022-09-09 18:47:51 -07:00
const showItem = (iteration + 1) % opts.interval === 0;
return showItem ? item : undefined;
};
const normalizeOpts = (opts: unknown): Opts => {
const { interval } = (opts && typeof opts === 'object' ? opts : {}) as Record<any, unknown>;
return {
interval: typeof interval === 'number' ? interval : 20,
};
};
export {
linearAlgorithm,
};