Source: DataTableSerializer.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var DataTableJsonSerializer_1 = require("./DataTableJsonSerializer");
var DataTableXmlSerializer_1 = require("./DataTableXmlSerializer");
var DataTableXlsSerializer_1 = require("./DataTableXlsSerializer");
var path = require("path");
var fs = require("fs");
/** Default constructor
 * @class DataTableSerializer
 * @classdesc Utility to load and save DataTable objects to different file formats.
 * More Info: {@link DataTableJsonSerializer} {@link DataTableXlsSerializer} {@link DataTableXmlSerializer}
 */
var DataTableSerializer = (function () {
    function DataTableSerializer() {
    }
    DataTableSerializer.getSerializer = function (extension) {
        var retVal = null;
        if (extension !== null && extension.length > 0) {
            // remove first '.' if the extension begins with it
            if (extension.indexOf(".") == 0)
                extension = extension.substr(1);
            for (var i = 0; i < this.AvailableSerializers.length; i++) {
                var serializer = this.AvailableSerializers[i];
                if (serializer.extension && serializer.extension.toLocaleLowerCase() == extension.toLowerCase()) {
                    retVal = serializer;
                    break;
                }
            }
        }
        return retVal;
    };
    DataTableSerializer.save = function (dataTable, fileName) {
        var ext = path.extname(fileName);
        var serializer = this.getSerializer(ext);
        if (serializer === null)
            throw new Error("Format '" + ext + "' not supported");
        var data = serializer.serialize(dataTable);
        fs.writeFileSync(fileName, data);
    };
    /** The path is relative to process.cwd() */
    DataTableSerializer.load = function (fileName) {
        var dt;
        var ext = path.extname(fileName);
        var serializer = this.getSerializer(ext);
        if (serializer === null)
            throw new Error("Format '" + ext + "' not supported");
        var buffer = fs.readFileSync(fileName);
        dt = serializer.deserialize(buffer);
        return dt;
    };
    return DataTableSerializer;
}());
/** @lends DataTableSerializer */
// register serializers here
DataTableSerializer.AvailableSerializers = [
    new DataTableJsonSerializer_1.DataTableJsonSerializer(),
    new DataTableXlsSerializer_1.DataTableXlsSerializer(),
    new DataTableXmlSerializer_1.DataTableXmlSerializer()
];
exports.DataTableSerializer = DataTableSerializer;
//# sourceMappingURL=DataTableSerializer.js.map