All files helpers.ts

84.61% Statements 11/13
71.42% Branches 10/14
100% Functions 1/1
84.61% Lines 11/13

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          2x 20x 20x       20x   6x   2x   2x   4x   2x   2x     2x              
import type { VariableType, VariableValue } from "@featurevisor/types";
 
type FieldType = string | VariableType;
type ValueType = VariableValue;
 
export function getValueByType(value: ValueType, fieldType: FieldType): ValueType {
  try {
    Iif (value === undefined) {
      return null;
    }
 
    switch (fieldType) {
      case "string":
        return typeof value === "string" ? value : null;
      case "integer":
        return parseInt(value as string, 10);
      case "double":
        return parseFloat(value as string);
      case "boolean":
        return value === true;
      case "array":
        return Array.isArray(value) ? value : null;
      case "object":
        return typeof value === "object" ? value : null;
      // @NOTE: `json` is not handled here intentionally
      default:
        return value;
    }
    // eslint-disable-next-line
  } catch (e) {
    return null;
  }
}