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