UNPKG

3.9 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.InvalidOptionError = undefined;
7
8var _typeof2 = require('babel-runtime/helpers/typeof');
9
10var _typeof3 = _interopRequireDefault(_typeof2);
11
12var _map = require('babel-runtime/core-js/map');
13
14var _map2 = _interopRequireDefault(_map);
15
16var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
17
18var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
19
20var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
21
22var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
23
24var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
25
26var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
27
28var _inherits2 = require('babel-runtime/helpers/inherits');
29
30var _inherits3 = _interopRequireDefault(_inherits2);
31
32exports.default = createPercyAddon;
33
34var _es6Error = require('es6-error');
35
36var _es6Error2 = _interopRequireDefault(_es6Error);
37
38var _constants = require('./constants');
39
40function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
41
42var InvalidOptionError = exports.InvalidOptionError = function (_ExtendableError) {
43 (0, _inherits3.default)(InvalidOptionError, _ExtendableError);
44
45 function InvalidOptionError() {
46 (0, _classCallCheck3.default)(this, InvalidOptionError);
47 return (0, _possibleConstructorReturn3.default)(this, (InvalidOptionError.__proto__ || (0, _getPrototypeOf2.default)(InvalidOptionError)).apply(this, arguments));
48 }
49
50 return InvalidOptionError;
51}(_es6Error2.default);
52
53function assertWidths(widths) {
54 if (typeof widths === 'undefined') {
55 return;
56 }
57 if (!Array.isArray(widths)) {
58 throw new InvalidOptionError('Given widths option is not an array');
59 }
60 if (!widths.length) {
61 throw new InvalidOptionError('Need at least one valid width');
62 }
63 widths.forEach(function (width) {
64 if (isNaN(width) || width !== ~~width) {
65 throw new InvalidOptionError("Given width '" + width + "' is invalid");
66 }
67 });
68}
69
70function assertRtl(rtl) {
71 if (typeof rtl === 'undefined') {
72 return;
73 }
74 if (typeof rtl !== 'boolean') {
75 throw new InvalidOptionError("Given rtl setting '" + rtl + "' is invalid");
76 }
77}
78
79function createPercyAddon() {
80 /**
81 * This map contains a mapping from stories for a kind to
82 * options of the respective story,
83 * looking like this:
84 * {
85 * kindA: { story1: options, storyX: options },
86 * kindB: { story...}
87 * ...
88 * }
89 */
90 var optionsTuples = new _map2.default();
91
92 var percyAddon = {
93 addWithPercyOptions: function addWithPercyOptions(storyName, options, storyFn) {
94 if (typeof options === 'function') {
95 storyFn = options;
96 options = undefined;
97 }
98
99 if (options) {
100 assertWidths(options.widths);
101 assertRtl(options.rtl);
102
103 optionsTuples.set(this.kind, optionsTuples.get(this.kind) || new _map2.default());
104 var tuplesForKind = optionsTuples.get(this.kind);
105 tuplesForKind.set(storyName, options);
106 }
107 return this.add(storyName, storyFn);
108 }
109 };
110
111 var serializeStories = function serializeStories(getStorybook) {
112 var storybook = getStorybook();
113 storybook.forEach(function (storyBucket) {
114 if (!optionsTuples.has(storyBucket.kind)) {
115 return;
116 }
117 var tuplesForKind = optionsTuples.get(storyBucket.kind);
118 storyBucket.stories.forEach(function (story) {
119 if (tuplesForKind.has(story.name)) {
120 story.options = tuplesForKind.get(story.name);
121 }
122 });
123 });
124 if ((typeof window === 'undefined' ? 'undefined' : (0, _typeof3.default)(window)) === 'object') window[_constants.storiesKey] = storybook;
125 return storybook;
126 };
127
128 return { percyAddon: percyAddon, serializeStories: serializeStories };
129}
\No newline at end of file