All files / normalize normalize-dictionaries.ts

81.25% Statements 13/16
70% Branches 7/10
100% Functions 6/6
78.57% Lines 11/14

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        32x 27x 5x 5x     16x 12x       5x 12x         1x 3x   4x                                
import { isDictionary } from "../utils";
import type { NormalizedData } from "./types";
 
export function normalizeDictionaries(data: any): NormalizedData {
  if (Array.isArray(data)) {
    return data;
  } else if (isDictionary(data)) {
    const types = Array.from(
      new Set(
        Object.values(data)
          .filter((value) => value !== null)
          .map((value) => typeof value),
      ),
    );
 
    if (types.length === 1) {
      return Object.entries(data).map(([key, value]: [string, any]) => ({
        key,
        value,
      }));
    } else {
      const baseRow = Object.fromEntries(
        types.map((type) => [`value_${type}`, null]),
      );
      return Object.entries(data).map(([key, value]) => ({
        key,
        ...baseRow,
        ...(value === null
          ? {}
          : {
              [`value_${typeof value}`]: value,
            }),
      }));
    }
  E} else if (Object(data) === data) {
    return [data];
  } else {
    return [];
  }
}