react-pedigree-table
Version:
React pedigree generator
59 lines (44 loc) • 2.44 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var PedigreeUtil = function () {
function PedigreeUtil(topField, bottomField) {
_classCallCheck(this, PedigreeUtil);
this.pedigreeData = [];
this.topField = topField;
this.bottomField = bottomField;
}
_createClass(PedigreeUtil, [{
key: 'generatePedigreeData',
value: function generatePedigreeData(obj, depth) {
if (this.pedigreeData.length) return this.pedigreeData;
this.process(obj, depth);
return this.pedigreeData;
}
}, {
key: 'getMockObject',
value: function getMockObject() {
var _ref;
return _ref = {}, _defineProperty(_ref, this.topField, null), _defineProperty(_ref, this.bottomField, null), _ref;
}
}, {
key: 'process',
value: function process(obj, depth) {
if (depth <= 0) return;
if (this.pedigreeData.length == 0) this.pedigreeData.push([]);
var father = obj[this.topField] || this.getMockObject();
var mother = obj[this.bottomField] || this.getMockObject();
var row = this.pedigreeData[this.pedigreeData.length - 1];
row.push(father);
this.process(father, depth - 1);
this.pedigreeData.push([mother]);
this.process(mother, depth - 1);
}
}]);
return PedigreeUtil;
}();
exports.default = PedigreeUtil;