UNPKG

react-native-web-headroom

Version:
88 lines (78 loc) 2.38 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); var _typeof2 = require('babel-runtime/helpers/typeof'); var _typeof3 = _interopRequireDefault(_typeof2); exports.default = getHeaders; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /* * Polyfill for running tests * `includes` is an ES2016 feature */ if (!Array.prototype.includes) { Array.prototype.includes = function (searchElement /*, fromIndex*/) { var O = Object(this); var len = parseInt(O.length) || 0; if (len === 0) { return false; } var n = parseInt(arguments[1]) || 0; var k; if (n >= 0) { k = n; } else { k = len + n; if (k < 0) { k = 0; } } var currentElement; while (k < len) { currentElement = O[k]; if (searchElement === currentElement || searchElement !== searchElement && currentElement !== currentElement) { // NaN !== NaN return true; } k++; } return false; }; } function getHeaders(data) { if ((typeof data === 'undefined' ? 'undefined' : (0, _typeof3.default)(data)) === 'object') { var rowHeaders = void 0; // is an array if (Array.isArray(data)) { var nRows = data.length; rowHeaders = [].concat((0, _toConsumableArray3.default)(Array(nRows).keys())); } else if (data !== null) { // is an object // keys are row indexes rowHeaders = Object.keys(data); } // Time: O(nRows * nCols) var colHeaders = rowHeaders.reduce(function (colHeaders, rowHeader) { var row = data[rowHeader]; if ((typeof row === 'undefined' ? 'undefined' : (0, _typeof3.default)(row)) === 'object' && row !== null) { /* O(nCols) Could optimize `includes` here */ var cols = Object.keys(row); cols.reduce(function (xs, x) { if (!xs.includes(x)) { /* xs is the colHeaders to be filled by searching the row's indexes */ xs.push(x); } return xs; }, colHeaders); } return colHeaders; }, []); return { rowHeaders: rowHeaders, colHeaders: colHeaders }; } return undefined; }