All files / lib/internal prepareModuleExports.js

75.41% Statements 46/61
20% Branches 1/5
100% Functions 1/1
75.41% Lines 46/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 621x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x   6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x             6x 6x                 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x  
'use strict';
 
const assert = require('assert');
 
/**
 * @typedef {object} ModuleConfig
 * @property {string} name
 * @property {string} [indexFile]
 */
 
/**
 * @param {unknown[]} input
 * @returns {ModuleConfig[]}
 */
module.exports = function prepareModuleExports(input) {
  /** @type {ModuleConfig[]} */
  const configs = [];
 
  for (const config of input) {
    if (typeof config == 'string') {
      configs.push({ name: config });
    } else if (typeof config == 'object' && config) {
      const {
        name,
        indexFile,
        ...unknownOptions
      } = /** @type {ModuleConfig} */ (config);
 
      assert.ok(typeof name == 'string', '{ name } expected to be a string');
      assert.ok(name, '{ name } is empty');
 
      if (indexFile != null) {
        assert.ok(
          typeof indexFile == 'string',
          '{ indexFile } expected to be a string',
        );
        assert.ok(indexFile, '{ indexFile } is empty');
      }
 
      if (indexFile != null && indexFile !== name) {
        const [nameID] = name.split('/');
        const [indexFileID] = indexFile.split('/');

        assert.ok(
          nameID === indexFileID,
          `index file '${indexFile}' must belong to '${name}' package`,
        );
      }
 
      const unknownOptionsKeys = Object.keys(unknownOptions);
      assert.ok(
        !unknownOptionsKeys.length,
        `contains unknown keys { ${unknownOptionsKeys.join(', ')} }`,
      );
 
      configs.push({ name, indexFile });
    }
  }
 
  return configs;
};