UNPKG

@tvkitchen/countertop

Version:

The entry point for developers who want to set up a TV Kitchen.

130 lines (99 loc) 4.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalizeStreams = exports.normalizeStream = exports.normalizeTributaryMaps = exports.normalizeTributaryMap = exports.generateMockAppliance = exports.loadTestData = void 0; var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _baseInterfaces = require("@tvkitchen/base-interfaces"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * Loads a file containing JSON from the test's `data` directory and returns the resulting object. * * @param {String} fileName The name of the file located in the `data` directory */ const loadTestData = (testDirectory, fileName) => JSON.parse(_fs.default.readFileSync(_path.default.join(testDirectory, 'data', fileName))); /** * Creates a mock appliance class * * @param {String[]} options.inputTypes The mocked input types value. * @param {String[]} options.outputTypes The mocked output types value. * @return {Class} The resulting mock class. */ exports.loadTestData = loadTestData; const generateMockAppliance = ({ inputTypes, outputTypes, getInputTypes = () => inputTypes, getOutputTypes = () => outputTypes, audit = async () => true, start = async () => true, stop = async () => true }) => { class MockAppliance extends _baseInterfaces.IAppliance { constructor(...args) { super(...args); _defineProperty(this, "on", () => true); _defineProperty(this, "audit", audit); _defineProperty(this, "start", start); _defineProperty(this, "stop", stop); } } _defineProperty(MockAppliance, "getInputTypes", getInputTypes); _defineProperty(MockAppliance, "getOutputTypes", getOutputTypes); return MockAppliance; }; /** * Normalize a tributary map so that all stations are replaced with a normalized * index. * * @param {Map} streamMap The stream map to normalize. * @param {CountertopStation[]} stations The stations being normalized. * @return {Map} The normalized stream map. */ exports.generateMockAppliance = generateMockAppliance; const normalizeTributaryMap = (streamMap, stations) => { const normalizedTributaryMap = new Map(); streamMap.forEach((value, key) => { normalizedTributaryMap.set(key, // https://github.com/eslint/eslint/issues/12473 // eslint-disable-next-line no-use-before-define normalizeStream(value, stations)); }); return normalizedTributaryMap; }; /** * Normalize an array of tributary maps so that all stations are replaced with a * normalized index. * * @param {Map[]} streamMap The stream maps to normalize. * @param {CountertopStation[]} stations The stations being normalized. * @return {Map[]} The normalized stream maps. */ exports.normalizeTributaryMap = normalizeTributaryMap; const normalizeTributaryMaps = (streamMaps, stations) => streamMaps.map(streamMap => normalizeTributaryMap(streamMap, stations)); /** * Normalizes a stream so that all station references are replaced with a normalized * index. * * @param {CountertopStream} stream The stream to normalize. * @param {CountertopStation[]} stations The stations being normalized. * @return {CountertopStream} The normalized stream. */ exports.normalizeTributaryMaps = normalizeTributaryMaps; const normalizeStream = (stream, stations) => ({ source: stations.findIndex(station => station.id === stream.getSource().id), mouth: stations.findIndex(station => station.id === stream.getMouth().id), tributaryMap: normalizeTributaryMap(stream.getTributaryMap(), stations) }); /** * Normalizes an array of streams so that all station references are replaced with a normalized * index. * * @param {CountertopStream[]} stream The streams to normalize. * @param {CountertopStation[]} stations The stations being normalized. * @return {CountertopStream[]} The normalized streams. */ exports.normalizeStream = normalizeStream; const normalizeStreams = (streams, stations) => streams.map(stream => normalizeStream(stream, stations)); exports.normalizeStreams = normalizeStreams;