source: trip-planner-front/node_modules/webpack/lib/container/options.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
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 */
19const 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 */
59const 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 */
74const 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
90exports.parseOptions = parseOptions;
91exports.scope = scope;
Note: See TracBrowser for help on using the repository browser.