All files / src/lib/attributes normalize.ts

14.29% Statements 2/14
0% Branches 0/8
0% Functions 0/1
14.29% Lines 2/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 281x             1x                                        
import { snakeCase } from "lodash";
import { Attributes } from ".";
 
/**
 * Walks through attribute keys and makes them snakeCase if needed
 * @param attributes
 */
export default function(attributes: Attributes): Attributes {
  if (typeof attributes !== "object") {
    return {};
  }
  let changed = false;
  const result = { ...attributes };
 
  for (const key in attributes) {
    if (attributes.hasOwnProperty(key)) {
      const normalizedKey = snakeCase(key);
      if (normalizedKey !== key) {
        result[normalizedKey] = result[key];
        delete result[key];
        changed = true;
      }
    }
  }
 
  return changed ? result : attributes;
}