UNPKG

@synthart/synthlite

Version:

A fast, lightweight Gen AI powered synthetic data generator written in TypeScript. 🌞

59 lines (58 loc) • 2.39 kB
"use strict"; /** * * @file generated-dataset.ts * @description 🚀 GeneratedDataset is a class that handles saving generated data to files. * @date January 2024 * @version 1.0.0 * @license Affero General Public License v3.0 * ✨ "Data is the new oil." — Clive Humby * */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GeneratedDataset = void 0; const promises_1 = __importDefault(require("fs/promises")); const json_2_csv_1 = require("json-2-csv"); /** * GeneratedDataset: Handles saving generated data to files. * @class GeneratedDataset */ class GeneratedDataset { /** * Constructor for GeneratedDataset. * @param {any[]} data - The data to be saved. */ constructor(data) { this.data = data; } /** * Saves the data to a file in the specified format. * @param {string} outputPath - The path to save the file. * @param {OutputFormat} format - The format to save the file in. * @returns {Promise<void>} - A promise that resolves when the file is saved. */ saveToFile(outputPath, format) { return __awaiter(this, void 0, void 0, function* () { if (format === "json") { yield promises_1.default.writeFile(outputPath, JSON.stringify(this.data, null, 2)); } else if (format === "csv") { const csv = yield (0, json_2_csv_1.json2csv)(this.data); yield promises_1.default.writeFile(outputPath, csv); } }); } } exports.GeneratedDataset = GeneratedDataset;