camel-case.js 763 Bytes
/**
 * 对象键名驼峰化
 * @author: Bi Kai<kai.bi@yoho.cn>
 * @date: 2016/05/09
 */
'use strict';
const _ = require('lodash');

let camelCase,
    camelCaseObject,
    camelCaseArray;

camelCaseObject = (obj) => {
    _.forEach(Object.keys(obj), (k) => {
        obj[k] = camelCase(obj[k]);
        if (/[_-]/.test(k)) {
            obj[_.camelCase(k)] = obj[k];
            delete obj[k];
        }
    });
    return obj;
};

camelCaseArray = (list) => {
    _.forEach(list, (k) => {
        k = camelCase(k);
    });
    return list;
};

camelCase = (data) => {
    if (_.isArray(data)) {
        data = camelCaseArray(data);
    } else if (_.isObject(data)) {
        data = camelCaseObject(data);
    }

    return data;
};

module.exports = camelCase;