All files helpers.ts

100% Statements 11/11
85.71% Branches 12/14
100% Functions 1/1
100% Lines 11/11

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          3x 31x 1x     30x   10x   3x   3x   5x   3x   3x     3x      
import type { VariableType, VariableValue } from "@featurevisor/types";
 
type FieldType = string | VariableType;
export type ValueType = VariableValue;
 
export function getValueByType(value: ValueType, fieldType: FieldType): ValueType {
  if (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;
  }
}