all files / pivotview/flat/ flat-data.js

94.68% Statements 89/94
93.55% Branches 87/93
100% Functions 19/19
94.38% Lines 84/89
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 1361×   1× 1× 1× 384× 384× 384× 384× 384× 384× 384× 384× 384× 7237×     7237×     1× 3×     3× 3× 5×       3× 6×     6× 6× 10× 10× 10×   6×     1× 7253× 7253× 2×   7251× 18796× 18796× 18805× 1×   18795× 1×   18794× 18794× 2×   18792× 18792×   18776× 18776×   4×   18772× 759× 759×   18772× 18607× 18607× 18607× 18607× 18607× 781×             16×       1× 12080×       1× 16240× 16240×     16240× 16240× 16240× 16260× 16260×   16260×   16240×     1× 985× 985× 1888× 1×     984× 984× 5147× 5147× 10099× 10099×         5147×     1×   1×    
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var FlatRecordStore = (function () {
        function FlatRecordStore(records, excluded, csv) {
            if (excluded === void 0) { excluded = []; }
            if (csv === void 0) { csv = false; }
            var _this = this;
            this.fields = [];
            this.types = Object.create(null);
            this.paths = new Map();
            this.records = csv ? this.normalizeCsv(records) : records;
            this.excluded = excluded;
            this.records.forEach(function (record) {
                Iif (!record || typeof record !== 'object' || Array.isArray(record) || record instanceof Date) {
                    throw new Error('Flat layout requires a complete relational record source.');
                }
                _this.scan(record, [], []);
            });
        }
        FlatRecordStore.prototype.normalizeCsv = function (records) {
            Iif (!records.length) {
                return [];
            }
            var header = records[0];
            Iif (!Array.isArray(header) || header.some(function (name, index) {
                return typeof name !== 'string' || !name || header.indexOf(name) !== index;
            })) {
                throw new Error('Flat layout settings contain an invalid value.');
            }
            return records.slice(1).map(function (values) {
                Iif (!Array.isArray(values)) {
                    throw new Error('Flat layout settings contain an invalid value.');
                }
                var record = Object.create(null);
                header.forEach(function (name, index) {
                    var descriptor = Object.getOwnPropertyDescriptor(values, String(index));
                    var value = descriptor && Object.prototype.hasOwnProperty.call(descriptor, 'value') ? descriptor.value : null;
                    Object.defineProperty(record, name, { value: value, writable: true, enumerable: true, configurable: true });
                });
                return record;
            });
        };
        FlatRecordStore.prototype.scan = function (record, prefix, ancestors) {
            var _this = this;
            if (!record || typeof record !== 'object' || Array.isArray(record) || ancestors.indexOf(record) !== -1) {
                return;
            }
            Object.keys(record).forEach(function (key) {
                var parts = prefix.concat(key);
                var field = parts.join('.');
                if (key.split('.').some(function (part) { return ['__proto__', 'prototype', 'constructor'].indexOf(part) !== -1; })) {
                    throw new Error('Flat layout settings contain an invalid value.');
                }
                if (_this.excluded.some(function (name) { return field === name || field.indexOf(name + '.') === 0; })) {
                    return;
                }
                var descriptor = Object.getOwnPropertyDescriptor(record, key);
                if (!descriptor || !Object.prototype.hasOwnProperty.call(descriptor, 'value')) {
                    return;
                }
                var value = descriptor.value;
                if (value === null || value === undefined || value instanceof Date ||
                    ['string', 'number', 'boolean'].indexOf(typeof value) !== -1) {
                    var previous = _this.paths.get(field);
                    if (previous && (previous.length !== parts.length ||
                        (parts.length > 1 && previous.join('\0') !== parts.join('\0')))) {
                        throw new Error('Flat layout settings contain an invalid value.');
                    }
                    if (!previous) {
                        _this.paths.set(field, parts);
                        _this.fields.push(field);
                    }
                    if (value !== null && value !== undefined) {
                        var type = value instanceof Date ? 'date' : typeof value;
                        var inferred = Object.getOwnPropertyDescriptor(_this.types, field);
                        var previousType = inferred && inferred.value;
                        var nextType = previousType && previousType !== type ? 'mixed' : type;
                        if (previousType !== nextType) {
                            Object.defineProperty(_this.types, field, {
                                value: nextType, writable: true, enumerable: true, configurable: true
                            });
                        }
                    }
                }
                else {
                    _this.scan(descriptor.value, parts, ancestors.concat(record));
                }
            });
        };
        Object.defineProperty(FlatRecordStore.prototype, "length", {
            get: function () { return this.records.length; },
            enumerable: true,
            configurable: true
        });
        FlatRecordStore.prototype.read = function (ordinal, field) {
            var path = this.paths.get(field);
            Iif (!path) {
                throw new Error('Flat layout ignored an unavailable or unsupported field.');
            }
            var record = Object.getOwnPropertyDescriptor(this.records, String(ordinal));
            var current = record && record.value;
            for (var _i = 0, path_1 = path; _i < path_1.length; _i++) {
                var part = path_1[_i];
                var descriptor = current && typeof current === 'object'
                    ? Object.getOwnPropertyDescriptor(current, part) : undefined;
                current = descriptor && Object.prototype.hasOwnProperty.call(descriptor, 'value') ? descriptor.value : undefined;
            }
            return current instanceof Date || ['string', 'number', 'boolean'].indexOf(typeof current) !== -1
                ? current : null;
        };
        FlatRecordStore.prototype.project = function (fields, ordinals) {
            var _this = this;
            fields.forEach(function (field) {
                if (!_this.paths.has(field)) {
                    throw new Error('Flat layout ignored an unavailable or unsupported field.');
                }
            });
            var indexes = ordinals || this.records.map(function (_record, index) { return index; });
            return indexes.map(function (ordinal) {
                var projected = {};
                fields.forEach(function (field) {
                    var value = _this.read(ordinal, field);
                    Object.defineProperty(projected, field, {
                        value: value instanceof Date ? new Date(value.getTime()) : value,
                        writable: true, enumerable: true, configurable: true
                    });
                });
                return projected;
            });
        };
        return FlatRecordStore;
    }());
    exports.FlatRecordStore = FlatRecordStore;
});