UNPKG

8.8 kBJavaScriptView Raw
1"use strict";
2
3require("core-js/modules/es.symbol.js");
4
5require("core-js/modules/es.symbol.description.js");
6
7require("core-js/modules/es.symbol.iterator.js");
8
9require("core-js/modules/es.array.concat.js");
10
11require("core-js/modules/es.array.filter.js");
12
13require("core-js/modules/es.array.find-index.js");
14
15require("core-js/modules/es.array.includes.js");
16
17require("core-js/modules/es.array.iterator.js");
18
19require("core-js/modules/es.array.join.js");
20
21require("core-js/modules/es.array.map.js");
22
23require("core-js/modules/es.array.reduce.js");
24
25require("core-js/modules/es.array.some.js");
26
27require("core-js/modules/es.function.name.js");
28
29require("core-js/modules/es.number.constructor.js");
30
31require("core-js/modules/es.object.assign.js");
32
33require("core-js/modules/es.object.entries.js");
34
35require("core-js/modules/es.object.keys.js");
36
37require("core-js/modules/es.object.to-string.js");
38
39require("core-js/modules/es.string.includes.js");
40
41require("core-js/modules/es.string.iterator.js");
42
43require("core-js/modules/web.dom-collections.iterator.js");
44
45Object.defineProperty(exports, "__esModule", {
46 value: true
47});
48exports.validateOptions = exports.combineArgs = exports.mapArgsToTypes = void 0;
49
50var _clientLogger = require("@storybook/client-logger");
51
52var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject"));
53
54var _tsDedent = _interopRequireDefault(require("ts-dedent"));
55
56var _templateObject, _templateObject2;
57
58function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
59
60function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
61
62function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
63
64function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
65
66function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
67
68function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
69
70function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
71
72function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
73
74function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
75
76function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
77
78function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
79
80function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
81
82function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
83
84function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
85
86var INCOMPATIBLE = Symbol('incompatible');
87
88var map = function map(arg, type) {
89 if (arg === undefined || arg === null || !type) return arg;
90
91 switch (type.name) {
92 case 'string':
93 return String(arg);
94
95 case 'enum':
96 return arg;
97
98 case 'number':
99 return Number(arg);
100
101 case 'boolean':
102 return arg === 'true';
103
104 case 'array':
105 if (!type.value || !Array.isArray(arg)) return INCOMPATIBLE;
106 return arg.reduce(function (acc, item, index) {
107 var mapped = map(item, type.value);
108 if (mapped !== INCOMPATIBLE) acc[index] = mapped;
109 return acc;
110 }, new Array(arg.length));
111
112 case 'object':
113 if (typeof arg === 'string' || typeof arg === 'number') return arg;
114 if (!type.value || _typeof(arg) !== 'object') return INCOMPATIBLE;
115 return Object.entries(arg).reduce(function (acc, _ref) {
116 var _ref2 = _slicedToArray(_ref, 2),
117 key = _ref2[0],
118 val = _ref2[1];
119
120 var mapped = map(val, type.value[key]);
121 return mapped === INCOMPATIBLE ? acc : Object.assign(acc, _defineProperty({}, key, mapped));
122 }, {});
123
124 default:
125 return INCOMPATIBLE;
126 }
127};
128
129var mapArgsToTypes = function mapArgsToTypes(args, argTypes) {
130 return Object.entries(args).reduce(function (acc, _ref3) {
131 var _ref4 = _slicedToArray(_ref3, 2),
132 key = _ref4[0],
133 value = _ref4[1];
134
135 if (!argTypes[key]) return acc;
136 var mapped = map(value, argTypes[key].type);
137 return mapped === INCOMPATIBLE ? acc : Object.assign(acc, _defineProperty({}, key, mapped));
138 }, {});
139};
140
141exports.mapArgsToTypes = mapArgsToTypes;
142
143var combineArgs = function combineArgs(value, update) {
144 if (Array.isArray(value) && Array.isArray(update)) {
145 return update.reduce(function (acc, upd, index) {
146 acc[index] = combineArgs(value[index], update[index]);
147 return acc;
148 }, _toConsumableArray(value)).filter(function (v) {
149 return v !== undefined;
150 });
151 }
152
153 if (!(0, _isPlainObject.default)(value) || !(0, _isPlainObject.default)(update)) return update;
154 return Object.keys(Object.assign({}, value, update)).reduce(function (acc, key) {
155 if (key in update) {
156 var combined = combineArgs(value[key], update[key]);
157 if (combined !== undefined) acc[key] = combined;
158 } else {
159 acc[key] = value[key];
160 }
161
162 return acc;
163 }, {});
164};
165
166exports.combineArgs = combineArgs;
167
168var validateOptions = function validateOptions(args, argTypes) {
169 return Object.entries(argTypes).reduce(function (acc, _ref5) {
170 var _ref6 = _slicedToArray(_ref5, 2),
171 key = _ref6[0],
172 options = _ref6[1].options;
173
174 if (!options) {
175 acc[key] = args[key];
176 return acc;
177 }
178
179 if (!Array.isArray(options)) {
180 _clientLogger.once.error((0, _tsDedent.default)(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n Invalid argType: '", ".options' should be an array.\n\n More info: https://storybook.js.org/docs/react/api/argtypes\n "])), key));
181
182 acc[key] = args[key];
183 return acc;
184 }
185
186 if (options.some(function (opt) {
187 return opt && ['object', 'function'].includes(_typeof(opt));
188 })) {
189 _clientLogger.once.error((0, _tsDedent.default)(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n Invalid argType: '", ".options' should only contain primitives. Use a 'mapping' for complex values.\n\n More info: https://storybook.js.org/docs/react/writing-stories/args#mapping-to-complex-arg-values\n "])), key));
190
191 acc[key] = args[key];
192 return acc;
193 }
194
195 var isArray = Array.isArray(args[key]);
196 var invalidIndex = isArray && args[key].findIndex(function (val) {
197 return !options.includes(val);
198 });
199 var isValidArray = isArray && invalidIndex === -1;
200
201 if (args[key] === undefined || options.includes(args[key]) || isValidArray) {
202 acc[key] = args[key];
203 return acc;
204 }
205
206 var field = isArray ? "".concat(key, "[").concat(invalidIndex, "]") : key;
207 var supportedOptions = options.map(function (opt) {
208 return typeof opt === 'string' ? "'".concat(opt, "'") : String(opt);
209 }).join(', ');
210
211 _clientLogger.once.warn("Received illegal value for '".concat(field, "'. Supported options: ").concat(supportedOptions));
212
213 return acc;
214 }, {});
215};
216
217exports.validateOptions = validateOptions;
\No newline at end of file