f7
f7 is a spreadsheet formula execution library
git clone https://git.vogt.world/f7.git
Log | Files | README.md | LICENSE.md
← All files
name: src/main/js/utils/Mappers.ts
-rw-r--r--
3380
  1import { F7Exception } from "../errors/F7Exception";
  2import { Grid } from "../models/common/Grid";
  3import { Complex } from "../models/common/Types";
  4import { Converters } from "./Converters";
  5import { Predicates } from "./Predicates";
  6
  7export class Mappers {
  8  /**
  9   * Flatten possibly nested lists into a flat list of objects that are either not a list, or a list of size 0.
 10   *
 11   * @param value - to return as a stream.
 12   * @return - stream of objects that are Complex.
 13   */
 14  static toFlatStream(value: Complex): Array<Complex> {
 15    if (value instanceof Grid) {
 16      const grid: Grid<Complex> = Converters.castAsGrid(value);
 17      const values: Array<Complex> = [];
 18      for (let row = 0; row < grid.getRows(); row++) {
 19        for (let column = 0; column < grid.getColumns(); column++) {
 20          values.push(grid.get(column, row));
 21        }
 22      }
 23      return values;
 24    }
 25    return [value];
 26  }
 27
 28  /**
 29   * Flatten possibly nested lists into a flat list of objects that are either not a list, or a list of size 0.
 30   *
 31   * @param value - to return as a stream.
 32   * @return - stream of objects that are Complex.
 33   */
 34  static flattenGridsToArrays(value: Complex): Array<Complex> | Complex {
 35    if (value instanceof Grid) {
 36      const grid: Grid<Complex> = Converters.castAsGrid(value);
 37      const values: Array<Complex> = [];
 38      for (let row = 0; row < grid.getRows(); row++) {
 39        for (let column = 0; column < grid.getColumns(); column++) {
 40          values.push(grid.get(column, row));
 41        }
 42      }
 43      return values;
 44    }
 45    return value;
 46  }
 47
 48  /**
 49   * If the value is an Array, filter to numeric-coercable values, else return value.
 50   * @param value - value to possibly filter.
 51   */
 52  static filterArrayValuesByIsCoercableToNumeric(
 53    value: Array<Complex> | Complex
 54  ): Array<Complex> | Complex {
 55    if (Array.isArray(value)) {
 56      return (value as Array<Complex>).filter(Predicates.isCoercableToNumeric);
 57    }
 58    return value;
 59  }
 60
 61  /**
 62   * If the value is Array, check for errors and throw them where found. Else return value.
 63   * @param value
 64   */
 65  static checkArrayValuesForErrors(value: Array<Complex> | Complex): Array<Complex> | Complex {
 66    if (Array.isArray(value)) {
 67      return (value as Array<Complex>).map(Mappers.throwErrors);
 68    }
 69    return value;
 70  }
 71
 72  /**
 73   * If the values is an Array, return. If the value sis not an Array, attempt to coerce to number.
 74   * @param value - to use.
 75   */
 76  static coerceNonArrayValuesToNumberOrThrow(
 77    value: Array<Complex> | Complex
 78  ): Array<Complex> | Complex {
 79    if (Array.isArray(value)) {
 80      return value as Array<Complex>;
 81    }
 82    return Converters.toNumber(value);
 83  }
 84
 85  /**
 86   * When we have an array of possible nested arrays (because we treat grids differently), we need to at ensure all
 87   * values are arrays before reduce them to a single array.
 88   * @param value - value to ensure.
 89   */
 90  static ensureAllAreArrays(value: Array<Complex> | Complex): Array<Complex> {
 91    if (Array.isArray(value)) {
 92      return value as Array<Complex>;
 93    }
 94    return [value as Complex];
 95  }
 96
 97  /**
 98   * If the value is an error, throw.
 99   * @param value - value to check.
100   */
101  static throwErrors(value: Complex): Complex {
102    if (value instanceof F7Exception) {
103      throw Converters.castAsF7Exception(value);
104    }
105    return value;
106  }
107}