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