UNPKG

imdb-listimporter

Version:
75 lines (74 loc) 2.44 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import Variants from "./types/listVariants.js"; import * as CSV from 'csv-string'; /** * Detects which IMDb export template is being used * * @param header the header line of the input data * @returns the detected variant or null */ var detectVersion = function (header) { var stringifiedHeader = CSV.stringify(header).trim(); var foundVariant = null; Variants.every(function (variant) { var variantHeader = CSV.stringify(variant.header).trim(); if (stringifiedHeader === variantHeader) { foundVariant = variant; return false; } return true; }); return foundVariant; }; /** * Parses the input string into either an array of Film or a 2D array of strings * * @param input a string that must be a valid CSV * @param options parsing options * @returns either an array of Film or a 2D array of strings */ var parse = function (input, options) { if (options === void 0) { options = { marshal: true }; } var records = CSV.parse(input); var version = detectVersion(records.shift()); if (version) { if (options.marshal) { var films_1 = []; records.forEach(function (record) { var film = {}; version.mapper.forEach(function (key, index) { var _a; if ((options.only && !options.only.includes(key.localKey)) || (options.except && options.except.includes(key.localKey))) { return; } var value = record[index]; film = __assign(__assign({}, film), (_a = {}, _a[key.localKey] = key.converter ? key.converter(value) : value, _a)); }); films_1.push(film); }); return films_1; } else { return records; } } else { return []; } }; export default parse; // module.exports.default = parse export var exportsForTests = { detectVersion: detectVersion };