all files / ember-custom-actions/utils/ normalize-payload.js

100% Statements 13/13
100% Branches 6/6
100% Functions 2/2
100% Lines 13/13
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                    12×   11×        
import Ember from 'ember';
 
const { assert, String, isArray } = Ember;
 
function transformObject(object, operation) {
  if (object instanceof Object && !isArray(object)) {
    let data = {};
 
    Object.keys(object).forEach((key) => {
      data[String[operation](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!", !!String[operation]);
    return transformObject(payload, operation);
  } else {
    return payload;
  }
}