All files create-array.ts

100% Statements 49/49
100% Branches 10/10
100% Functions 16/16
100% Lines 42/42

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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 1291x 1x 1x         1x                                                               1x     16x 16x 1x   16x           1x 16x 16x 16x           16x 1x 5x 1x 4x 1x   1x 2x 1x                     16x       8x   8x   1x 1x 1x   1x   1x   1x   1x 1x 1x   1x   1x 1x         1x   1x       1x   31x   16x              
import { BehaviorSubject, Observable } from 'rxjs';
import { AnyAction, Constants, Dispatch, getType, AnyItem } from './utils';
import {
  CreateStateRxOpts,
  CreateStateRxApi,
  createStateRx
} from './create-staterx';
import { map } from 'rxjs/operators';
 
export interface CreateArrayOpts<T, E>
  extends CreateStateRxOpts<E, StateRxArray<T>, T[]> {}
 
export interface Actions<T> {
  pop: () => { type: string };
  push: (...data: T[]) => { type: string; data: T[] };
  reverse: () => { type: string };
  shift: () => { type: string };
  unshift: (...data: T[]) => { type: string; data: T[] };
  sort: (compareFn?: (a: T, b: T) => number) => { type: string; data: T[] };
  splice: (
    start: number,
    deleteCount?: number,
    ...items: T[]
  ) => {
    type: string;
    data: { start: number; deleteCount?: number; items: T[] };
  };
}
 
export interface Selectors<T> {
  byIndex: (index: number) => Observable<T | undefined>;
  slice: (start?: number, end?: number) => Observable<T[]>;
}
 
export interface StateRxArray<T>
  extends CreateStateRxApi<T[]>,
    Actions<T>,
    Selectors<T> {}
 
const createSelectors = <T extends AnyItem, S extends any[]>(
  state$: Observable<S>
): Selectors<T> => {
  const byIndex = (idx: number) => state$.pipe(map((state) => state[idx]));
  const slice = (start?: number, end?: number) =>
    state$.pipe(map((state) => state.slice(start, end)));
 
  return {
    byIndex,
    slice
  };
};
 
const createActions = <T>({
  constant,
  dispatch,
  get
}: {
  constant: Constants;
  dispatch: Dispatch;
  get: () => T[];
}): Actions<T> => {
  return {
    pop: () => dispatch({ type: constant.POP }),
    push: (...data) => dispatch({ type: constant.PUSH, data }),
    shift: () => dispatch({ type: constant.SHIFT }),
    unshift: (...data) => dispatch({ type: constant.UNSHIFT, data }),
    reverse: () => dispatch({ type: constant.REVERSE }),
    sort: (compareFn) =>
      dispatch({ type: constant.SORT, data: get().slice().sort(compareFn) }),
    splice: (start, deleteCount, ...items) =>
      dispatch({
        type: constant.SPLICE,
        data: {
          start,
          deleteCount,
          items
        }
      })
  };
};
 
const createReducer = ({ constant }: { constant: Constants }) => (
  state: any[],
  action: AnyAction
) => {
  const type = getType(action);
 
  switch (type) {
    case constant.POP:
      const popClone = state.slice();
      popClone.pop();
      return popClone;
    case constant.PUSH:
      return [...state, ...action.data];
    case constant.UNSHIFT:
      return [...action.data, ...state];
    case constant.REVERSE:
      return state.slice().reverse();
    case constant.SHIFT:
      const shiftClone = state.slice();
      shiftClone.shift();
      return shiftClone;
    case constant.SORT:
      return action.data;
    case constant.SPLICE:
      const spliceClone = state.slice();
      spliceClone.splice(
        action.data.start,
        action.data.deleteCount,
        ...action.data.items
      );
      return spliceClone;
    default:
      return state;
  }
};
 
export const createArray = <T, E>(
  initialState: T[],
  options: CreateArrayOpts<T, E> = {}
) =>
  createStateRx(initialState, options, {
    state$: new BehaviorSubject<T[]>(initialState),
    constants: ['POP', 'PUSH', 'REVERSE', 'UNSHIFT', 'SHIFT', 'SORT', 'SPLICE'],
    createActions,
    createReducer,
    createSelectors
  });