all files / addon/utils/ normalize-payload.js

100% Statements 14/14
100% Branches 6/6
100% Functions 2/2
100% Lines 14/14
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 42 43 44 45                                                    23×   20×        
import { assert } from '@ember/debug';
import { isArray } from '@ember/array';
import {
  camelize,
  capitalize,
  classify,
  dasherize,
  decamelize,
  underscore
} from '@ember/string';
 
const transformFunctions = {
  camelize,
  capitalize,
  classify,
  dasherize,
  decamelize,
  underscore
};
 
function transformObject(object, operation) {
  if (object instanceof Object && !isArray(object)) {
    let data = {};
 
    Object.keys(object).forEach((key) => {
      let transform = transformFunctions[operation];
      data[transform(key)] = transformObject(object[key], operation);
    });
 
    return data;
  } else {
    return object;
  }
}
 
export default function(payload, operation) {
  if (operation) {
    assert("This normalize method of custom action's payload does not exist. Check Ember.String documentation!", !!transformFunctions[operation]);
    return transformObject(payload, operation);
  } else {
    return payload;
  }
}