object-keys-mapping
Version:
mapping object keys
154 lines (118 loc) • 3.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Operator = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.toCamelcase = toCamelcase;
exports.reverseCamelcase = reverseCamelcase;
var _lodash = require('lodash.camelcase');
var _lodash2 = _interopRequireDefault(_lodash);
var _util = require('./util');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var isArray = Array.isArray;
var defaultOpts = {
camelcase: true,
mapping: {}
};
var Operator = exports.Operator = function () {
/**
* @param {Object} opts
*/
function Operator() {
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultOpts;
_classCallCheck(this, Operator);
this.camelcase = opts.camelcase;
this.mapping = opts.mapping || {};
}
/**
* @param {Object|Array} origin
*/
_createClass(Operator, [{
key: 'map',
value: function map(origin) {
if (!(0, _util.isObject)(origin)) {
return {};
}
if (isArray(origin)) {
return this.mapArray(origin, '');
} else {
return this.mapObject(origin, '');
}
}
/**
* @param {String} path
* @return {String}
*/
}, {
key: 'mapField',
value: function mapField(path) {
if (this.mapping[path]) {
return this.mapping[path];
}
path = path.split('.').pop();
if (this.camelcase === 'reverse') {
return path.replace(/[A-Z]/g, function (s) {
return '_' + s.toLowerCase();
});
} else if (this.camelcase) {
return (0, _lodash2.default)(path);
}
return path;
}
/**
* @param {Object} origin
* @param {String} path
* @return {Object}
*/
}, {
key: 'mapObject',
value: function mapObject(origin, path) {
var _this = this;
var result = {};
Object.keys(origin).forEach(function (key) {
var value = origin[key];
var keyPath = path + '.' + key;
if (isArray(value)) {
result[_this.mapField(keyPath)] = _this.mapArray(value, keyPath);
} else if ((0, _util.isObject)(value)) {
result[_this.mapField(keyPath)] = _this.mapObject(value, keyPath);
} else {
result[_this.mapField(keyPath)] = value;
}
});
return result;
}
/**
* @param {Array} origin
* @param {String} path
* @return {Array}
*/
}, {
key: 'mapArray',
value: function mapArray(origin, path) {
var _this2 = this;
var result = [];
origin.forEach(function (item) {
if (isArray(item)) {
result.push(_this2.mapArray(item, path));
} else if ((0, _util.isObject)(item)) {
result.push(_this2.mapObject(item, path));
} else {
result.push(item);
}
});
return result;
}
}]);
return Operator;
}();
function toCamelcase(origin) {
return new Operator().map(origin);
}
function reverseCamelcase(origin) {
return new Operator({
camelcase: 'reverse'
}).map(origin);
}