| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | /**
|
|---|
| 9 | * Defines the item type used by this module.
|
|---|
| 10 | * @template T
|
|---|
| 11 | * @typedef {Record<string, string | string[] | T>} Item
|
|---|
| 12 | */
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Defines the container options format type used by this module.
|
|---|
| 16 | * @template T
|
|---|
| 17 | * @typedef {(string | Item<T>)[] | Item<T>} ContainerOptionsFormat
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Processes the provided t.
|
|---|
| 22 | * @template T
|
|---|
| 23 | * @template N
|
|---|
| 24 | * @param {ContainerOptionsFormat<T>} options options passed by the user
|
|---|
| 25 | * @param {(item: string | string[], itemOrKey: string) => N} normalizeSimple normalize a simple item
|
|---|
| 26 | * @param {(value: T, key: string) => N} normalizeOptions normalize a complex item
|
|---|
| 27 | * @param {(item: string, normalized: N) => void} fn processing function
|
|---|
| 28 | * @returns {void}
|
|---|
| 29 | */
|
|---|
| 30 | const process = (options, normalizeSimple, normalizeOptions, fn) => {
|
|---|
| 31 | /**
|
|---|
| 32 | * Processes the provided item.
|
|---|
| 33 | * @param {(string | Item<T>)[]} items items
|
|---|
| 34 | */
|
|---|
| 35 | const array = (items) => {
|
|---|
| 36 | for (const item of items) {
|
|---|
| 37 | if (typeof item === "string") {
|
|---|
| 38 | fn(item, normalizeSimple(item, item));
|
|---|
| 39 | } else if (item && typeof item === "object") {
|
|---|
| 40 | object(item);
|
|---|
| 41 | } else {
|
|---|
| 42 | throw new Error("Unexpected options format");
|
|---|
| 43 | }
|
|---|
| 44 | }
|
|---|
| 45 | };
|
|---|
| 46 | /**
|
|---|
| 47 | * Processes the provided obj.
|
|---|
| 48 | * @param {Item<T>} obj an object
|
|---|
| 49 | */
|
|---|
| 50 | const object = (obj) => {
|
|---|
| 51 | for (const [key, value] of Object.entries(obj)) {
|
|---|
| 52 | if (typeof value === "string" || Array.isArray(value)) {
|
|---|
| 53 | fn(key, normalizeSimple(value, key));
|
|---|
| 54 | } else {
|
|---|
| 55 | fn(key, normalizeOptions(value, key));
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | };
|
|---|
| 59 | if (!options) {
|
|---|
| 60 | // Do nothing
|
|---|
| 61 | } else if (Array.isArray(options)) {
|
|---|
| 62 | array(options);
|
|---|
| 63 | } else if (typeof options === "object") {
|
|---|
| 64 | object(options);
|
|---|
| 65 | } else {
|
|---|
| 66 | throw new Error("Unexpected options format");
|
|---|
| 67 | }
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Returns parsed options.
|
|---|
| 72 | * @template T
|
|---|
| 73 | * @template R
|
|---|
| 74 | * @param {ContainerOptionsFormat<T>} options options passed by the user
|
|---|
| 75 | * @param {(item: string | string[], itemOrKey: string) => R} normalizeSimple normalize a simple item
|
|---|
| 76 | * @param {(value: T, key: string) => R} normalizeOptions normalize a complex item
|
|---|
| 77 | * @returns {[string, R][]} parsed options
|
|---|
| 78 | */
|
|---|
| 79 | const parseOptions = (options, normalizeSimple, normalizeOptions) => {
|
|---|
| 80 | /** @type {[string, R][]} */
|
|---|
| 81 | const items = [];
|
|---|
| 82 | process(options, normalizeSimple, normalizeOptions, (key, value) => {
|
|---|
| 83 | items.push([key, value]);
|
|---|
| 84 | });
|
|---|
| 85 | return items;
|
|---|
| 86 | };
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Returns options to spread or pass.
|
|---|
| 90 | * @template T
|
|---|
| 91 | * @param {string} scope scope name
|
|---|
| 92 | * @param {ContainerOptionsFormat<T>} options options passed by the user
|
|---|
| 93 | * @returns {Record<string, string | string[] | T>} options to spread or pass
|
|---|
| 94 | */
|
|---|
| 95 | const scope = (scope, options) => {
|
|---|
| 96 | /** @type {Record<string, string | string[] | T>} */
|
|---|
| 97 | const obj = {};
|
|---|
| 98 | process(
|
|---|
| 99 | options,
|
|---|
| 100 | (item) => /** @type {string | string[] | T} */ (item),
|
|---|
| 101 | (item) => /** @type {string | string[] | T} */ (item),
|
|---|
| 102 | (key, value) => {
|
|---|
| 103 | obj[
|
|---|
| 104 | key.startsWith("./") ? `${scope}${key.slice(1)}` : `${scope}/${key}`
|
|---|
| 105 | ] = value;
|
|---|
| 106 | }
|
|---|
| 107 | );
|
|---|
| 108 | return obj;
|
|---|
| 109 | };
|
|---|
| 110 |
|
|---|
| 111 | module.exports.parseOptions = parseOptions;
|
|---|
| 112 | module.exports.scope = scope;
|
|---|