All files / src/utils queue.ts

100% Statements 27/27
100% Branches 6/6
100% Functions 4/4
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 281x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 9x 9x 5x 5x 1x 1x 7x 6x 7x 1x 1x 7x 1x 1x 2x 2x  
import { Option, none, some } from 'fp-ts/Option';
 
type Queue<T> = {
  length: number;
  value: T[];
};
 
export const fromArray = <T>(arr: T[]): Queue<T> => {
  return {
    value: arr,
    get length() {
      return this.value.length;
    },
  };
};
 
export const dequeue = <T>(queue: Queue<T>): Option<T> => {
  if (queue.value.length > 0) {
    return some(queue.value.shift() as T);
  } else {
    return none;
  }
};
 
export const enqueue = <T>(queue: Queue<T>, el: T) => {
  queue.value.push(el);
};