UNPKG

9.53 kBJavaScriptView Raw
1import "core-js/modules/es.symbol.iterator.js";
2import "core-js/modules/es.string.iterator.js";
3import "core-js/modules/es.array.iterator.js";
4import "core-js/modules/web.dom-collections.iterator.js";
5import "core-js/modules/es.function.name.js";
6import "core-js/modules/es.array.from.js";
7import "core-js/modules/es.object.freeze.js";
8
9var _templateObject;
10
11function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
12
13function _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; }
14
15function _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); }
16
17import "core-js/modules/es.string.match.js";
18import "core-js/modules/es.regexp.exec.js";
19import "core-js/modules/es.object.assign.js";
20import "core-js/modules/es.symbol.js";
21import "core-js/modules/es.symbol.description.js";
22import "core-js/modules/es.object.to-string.js";
23import "core-js/modules/es.array.concat.js";
24import "core-js/modules/es.array.fill.js";
25import "core-js/modules/es.object.keys.js";
26import "core-js/modules/es.object.entries.js";
27import "core-js/modules/es.array.slice.js";
28import "core-js/modules/es.string.replace.js";
29import "core-js/modules/es.array.map.js";
30import "core-js/modules/es.array.join.js";
31import "core-js/modules/es.string.split.js";
32import "core-js/modules/es.string.search.js";
33import "core-js/modules/es.string.starts-with.js";
34
35function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
36
37function _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."); }
38
39function _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); }
40
41function _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; }
42
43function _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; }
44
45function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
46
47import { once } from '@storybook/client-logger';
48import deepEqual from 'fast-deep-equal';
49import isPlainObject from 'lodash/isPlainObject';
50import memoize from 'memoizerific';
51import qs from 'qs';
52import dedent from 'ts-dedent';
53var splitPathRegex = /\/([^/]+)\/(?:(.*)_)?([^/]+)?/;
54export var parsePath = memoize(1000)(function (path) {
55 var result = {
56 viewMode: undefined,
57 storyId: undefined,
58 refId: undefined
59 };
60
61 if (path) {
62 var _ref = path.toLowerCase().match(splitPathRegex) || [],
63 _ref2 = _slicedToArray(_ref, 4),
64 viewMode = _ref2[1],
65 refId = _ref2[2],
66 storyId = _ref2[3];
67
68 if (viewMode) {
69 Object.assign(result, {
70 viewMode: viewMode,
71 storyId: storyId,
72 refId: refId
73 });
74 }
75 }
76
77 return result;
78});
79export var DEEPLY_EQUAL = Symbol('Deeply equal');
80export var deepDiff = function deepDiff(value, update) {
81 if (_typeof(value) !== _typeof(update)) return update;
82 if (deepEqual(value, update)) return DEEPLY_EQUAL;
83
84 if (Array.isArray(value) && Array.isArray(update)) {
85 var res = update.reduce(function (acc, upd, index) {
86 var diff = deepDiff(value[index], upd);
87 if (diff !== DEEPLY_EQUAL) acc[index] = diff;
88 return acc;
89 }, new Array(update.length));
90 if (update.length >= value.length) return res;
91 return res.concat(new Array(value.length - update.length).fill(undefined));
92 }
93
94 if (isPlainObject(value) && isPlainObject(update)) {
95 return Object.keys(Object.assign({}, value, update)).reduce(function (acc, key) {
96 var diff = deepDiff(value === null || value === void 0 ? void 0 : value[key], update === null || update === void 0 ? void 0 : update[key]);
97 return diff === DEEPLY_EQUAL ? acc : Object.assign(acc, _defineProperty({}, key, diff));
98 }, {});
99 }
100
101 return update;
102}; // Keep this in sync with validateArgs in core-client/src/preview/parseArgsParam.ts
103
104var VALIDATION_REGEXP = /^[a-zA-Z0-9 _-]*$/;
105var NUMBER_REGEXP = /^-?[0-9]+(\.[0-9]+)?$/;
106var HEX_REGEXP = /^#([a-f0-9]{3,4}|[a-f0-9]{6}|[a-f0-9]{8})$/i;
107var COLOR_REGEXP = /^(rgba?|hsla?)\(([0-9]{1,3}),\s?([0-9]{1,3})%?,\s?([0-9]{1,3})%?,?\s?([0-9](\.[0-9]{1,2})?)?\)$/i;
108
109var validateArgs = function validateArgs() {
110 var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
111 var value = arguments.length > 1 ? arguments[1] : undefined;
112 if (key === null) return false;
113 if (key === '' || !VALIDATION_REGEXP.test(key)) return false;
114 if (value === null || value === undefined) return true; // encoded as `!null` or `!undefined`
115
116 if (value instanceof Date) return true; // encoded as modified ISO string
117
118 if (typeof value === 'number' || typeof value === 'boolean') return true;
119
120 if (typeof value === 'string') {
121 return VALIDATION_REGEXP.test(value) || NUMBER_REGEXP.test(value) || HEX_REGEXP.test(value) || COLOR_REGEXP.test(value);
122 }
123
124 if (Array.isArray(value)) return value.every(function (v) {
125 return validateArgs(key, v);
126 });
127 if (isPlainObject(value)) return Object.entries(value).every(function (_ref3) {
128 var _ref4 = _slicedToArray(_ref3, 2),
129 k = _ref4[0],
130 v = _ref4[1];
131
132 return validateArgs(k, v);
133 });
134 return false;
135};
136
137var encodeSpecialValues = function encodeSpecialValues(value) {
138 if (value === undefined) return '!undefined';
139 if (value === null) return '!null';
140
141 if (typeof value === 'string') {
142 if (HEX_REGEXP.test(value)) return "!hex(".concat(value.slice(1), ")");
143 if (COLOR_REGEXP.test(value)) return "!".concat(value.replace(/[\s%]/g, ''));
144 return value;
145 }
146
147 if (Array.isArray(value)) return value.map(encodeSpecialValues);
148
149 if (isPlainObject(value)) {
150 return Object.entries(value).reduce(function (acc, _ref5) {
151 var _ref6 = _slicedToArray(_ref5, 2),
152 key = _ref6[0],
153 val = _ref6[1];
154
155 return Object.assign(acc, _defineProperty({}, key, encodeSpecialValues(val)));
156 }, {});
157 }
158
159 return value;
160};
161
162var QS_OPTIONS = {
163 encode: false,
164 // we handle URL encoding ourselves
165 delimiter: ';',
166 // we don't actually create multiple query params
167 allowDots: true,
168 // encode objects using dot notation: obj.key=val
169 format: 'RFC1738',
170 // encode spaces using the + sign
171 serializeDate: function serializeDate(date) {
172 return "!date(".concat(date.toISOString(), ")");
173 }
174};
175export var buildArgsParam = function buildArgsParam(initialArgs, args) {
176 var update = deepDiff(initialArgs, args);
177 if (!update || update === DEEPLY_EQUAL) return '';
178 var object = Object.entries(update).reduce(function (acc, _ref7) {
179 var _ref8 = _slicedToArray(_ref7, 2),
180 key = _ref8[0],
181 value = _ref8[1];
182
183 if (validateArgs(key, value)) return Object.assign(acc, _defineProperty({}, key, value));
184 once.warn(dedent(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n Omitted potentially unsafe URL args.\n\n More info: https://storybook.js.org/docs/react/writing-stories/args#setting-args-through-the-url\n "]))));
185 return acc;
186 }, {});
187 return qs.stringify(encodeSpecialValues(object), QS_OPTIONS).replace(/ /g, '+').split(';').map(function (part) {
188 return part.replace('=', ':');
189 }).join(';');
190};
191export var queryFromString = memoize(1000)(function (s) {
192 return qs.parse(s, {
193 ignoreQueryPrefix: true
194 });
195});
196export var queryFromLocation = function queryFromLocation(location) {
197 return queryFromString(location.search);
198};
199export var stringifyQuery = function stringifyQuery(query) {
200 return qs.stringify(query, {
201 addQueryPrefix: true,
202 encode: false
203 });
204};
205export var getMatch = memoize(1000)(function (current, target) {
206 var startsWith = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
207 var startsWithTarget = current && startsWith && current.startsWith(target);
208 var currentIsTarget = typeof target === 'string' && current === target;
209 var matchTarget = current && target && current.match(target);
210
211 if (startsWithTarget || currentIsTarget || matchTarget) {
212 return {
213 path: current
214 };
215 }
216
217 return null;
218});
\No newline at end of file