@storybook/csf 0.0.2--canary.50.794a0bf.0 → 0.0.2--canary.36.ecfc79e.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ import { Args, Globals, InputType, Conditional } from './story';
2
+ export declare const testValue: (cond: Pick<Conditional, never>, value: any) => boolean;
3
+ export declare const includeConditionalArg: (argType: InputType, args: Args, globals: Globals) => boolean;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.includeConditionalArg = exports.testValue = void 0;
7
+
8
+ var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
+
12
+ var count = function count(vals) {
13
+ return vals.map(function (v) {
14
+ return typeof v !== 'undefined';
15
+ }).filter(Boolean).length;
16
+ };
17
+
18
+ var testValue = function testValue(cond, value) {
19
+ var _ref = cond,
20
+ exists = _ref.exists,
21
+ eq = _ref.eq,
22
+ neq = _ref.neq,
23
+ truthy = _ref.truthy;
24
+
25
+ if (count([exists, eq, neq, truthy]) > 1) {
26
+ throw new Error("Invalid conditional test ".concat(JSON.stringify({
27
+ exists: exists,
28
+ eq: eq,
29
+ neq: neq
30
+ })));
31
+ }
32
+
33
+ if (typeof eq !== 'undefined') {
34
+ return (0, _isEqual["default"])(value, eq);
35
+ }
36
+
37
+ if (typeof neq !== 'undefined') {
38
+ return !(0, _isEqual["default"])(value, neq);
39
+ }
40
+
41
+ if (typeof exists !== 'undefined') {
42
+ var valueExists = typeof value !== 'undefined';
43
+ return exists ? valueExists : !valueExists;
44
+ }
45
+
46
+ var shouldBeTruthy = typeof truthy === 'undefined' ? true : truthy;
47
+ return shouldBeTruthy ? !!value : !value;
48
+ };
49
+ /**
50
+ * Helper function to include/exclude an arg based on the value of other other args
51
+ * aka "conditional args"
52
+ */
53
+
54
+
55
+ exports.testValue = testValue;
56
+
57
+ var includeConditionalArg = function includeConditionalArg(argType, args, globals) {
58
+ if (!argType["if"]) return true;
59
+ var _ref2 = argType["if"],
60
+ arg = _ref2.arg,
61
+ global = _ref2.global;
62
+
63
+ if (count([arg, global]) !== 1) {
64
+ throw new Error("Invalid conditional value ".concat(JSON.stringify({
65
+ arg: arg,
66
+ global: global
67
+ })));
68
+ }
69
+
70
+ var value = arg ? args[arg] : globals[global];
71
+ return testValue(argType["if"], value);
72
+ };
73
+
74
+ exports.includeConditionalArg = includeConditionalArg;
@@ -0,0 +1,325 @@
1
+ "use strict";
2
+
3
+ var _includeConditionalArg = require("./includeConditionalArg");
4
+
5
+ /* eslint-disable @typescript-eslint/ban-ts-ignore */
6
+ describe('testValue', function () {
7
+ describe('truthy', function () {
8
+ it.each([['implicit true', {}, true, true], ['implicit truthy', {}, 1, true], ['implicit falsey', {}, 0, false], ['truthy true', {
9
+ truthy: true
10
+ }, true, true], ['truthy truthy', {
11
+ truthy: true
12
+ }, 1, true], ['truthy falsey', {
13
+ truthy: true
14
+ }, 0, false], ['falsey true', {
15
+ truthy: false
16
+ }, true, false], ['falsey truthy', {
17
+ truthy: false
18
+ }, 1, false], ['falsey falsey', {
19
+ truthy: false
20
+ }, 0, true]])('%s', function (_name, cond, value, expected) {
21
+ // @ts-ignore
22
+ expect((0, _includeConditionalArg.testValue)(cond, value)).toBe(expected);
23
+ });
24
+ });
25
+ describe('exists', function () {
26
+ it.each([['exist', {
27
+ exists: true
28
+ }, 1, true], ['exist false', {
29
+ exists: true
30
+ }, undefined, false], ['nexist', {
31
+ exists: false
32
+ }, undefined, true], ['nexist false', {
33
+ exists: false
34
+ }, 1, false]])('%s', function (_name, cond, value, expected) {
35
+ // @ts-ignore
36
+ expect((0, _includeConditionalArg.testValue)(cond, value)).toBe(expected);
37
+ });
38
+ });
39
+ describe('eq', function () {
40
+ it.each([['true', {
41
+ eq: 1
42
+ }, 1, true], ['false', {
43
+ eq: 1
44
+ }, 2, false], ['undefined', {
45
+ eq: undefined
46
+ }, undefined, false], ['undefined false', {
47
+ eq: 1
48
+ }, undefined, false], ['object true', {
49
+ eq: {
50
+ x: 1
51
+ }
52
+ }, {
53
+ x: 1
54
+ }, true], ['object true', {
55
+ eq: {
56
+ x: 1
57
+ }
58
+ }, {
59
+ x: 2
60
+ }, false]])('%s', function (_name, cond, value, expected) {
61
+ // @ts-ignore
62
+ expect((0, _includeConditionalArg.testValue)(cond, value)).toBe(expected);
63
+ });
64
+ });
65
+ describe('neq', function () {
66
+ it.each([['true', {
67
+ neq: 1
68
+ }, 2, true], ['false', {
69
+ neq: 1
70
+ }, 1, false], ['undefined true', {
71
+ neq: 1
72
+ }, undefined, true], ['undefined false', {
73
+ neq: undefined
74
+ }, undefined, false], ['object true', {
75
+ neq: {
76
+ x: 1
77
+ }
78
+ }, {
79
+ x: 2
80
+ }, true], ['object false', {
81
+ neq: {
82
+ x: 1
83
+ }
84
+ }, {
85
+ x: 1
86
+ }, false]])('%s', function (_name, cond, value, expected) {
87
+ // @ts-ignore
88
+ expect((0, _includeConditionalArg.testValue)(cond, value)).toBe(expected);
89
+ });
90
+ });
91
+ });
92
+ describe('includeConditionalArg', function () {
93
+ describe('errors', function () {
94
+ it('should throw if neither arg nor global is specified', function () {
95
+ expect(function () {
96
+ return (0, _includeConditionalArg.includeConditionalArg)({
97
+ "if": {}
98
+ }, {}, {});
99
+ }).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional value {}\"");
100
+ });
101
+ it('should throw if arg and global are both specified', function () {
102
+ expect(function () {
103
+ return (0, _includeConditionalArg.includeConditionalArg)({
104
+ "if": {
105
+ arg: 'a',
106
+ global: 'b'
107
+ }
108
+ }, {}, {});
109
+ }).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional value {\\\"arg\\\":\\\"a\\\",\\\"global\\\":\\\"b\\\"}\"");
110
+ });
111
+ it('should throw if mulitiple exists / eq / neq are specified', function () {
112
+ expect(function () {
113
+ return (0, _includeConditionalArg.includeConditionalArg)({
114
+ "if": {
115
+ arg: 'a',
116
+ exists: true,
117
+ eq: 1
118
+ }
119
+ }, {}, {});
120
+ }).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional test {\\\"exists\\\":true,\\\"eq\\\":1}\"");
121
+ expect(function () {
122
+ return (0, _includeConditionalArg.includeConditionalArg)({
123
+ "if": {
124
+ arg: 'a',
125
+ exists: false,
126
+ neq: 0
127
+ }
128
+ }, {}, {});
129
+ }).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional test {\\\"exists\\\":false,\\\"neq\\\":0}\"");
130
+ expect(function () {
131
+ return (0, _includeConditionalArg.includeConditionalArg)({
132
+ "if": {
133
+ arg: 'a',
134
+ eq: 1,
135
+ neq: 0
136
+ }
137
+ }, {}, {});
138
+ }).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional test {\\\"eq\\\":1,\\\"neq\\\":0}\"");
139
+ });
140
+ });
141
+ describe('args', function () {
142
+ describe('implicit', function () {
143
+ it.each([['implicit true', {
144
+ "if": {
145
+ arg: 'a'
146
+ }
147
+ }, {
148
+ a: 1
149
+ }, {}, true], ['truthy true', {
150
+ "if": {
151
+ arg: 'a',
152
+ truthy: true
153
+ }
154
+ }, {
155
+ a: 0
156
+ }, {}, false], ['truthy false', {
157
+ "if": {
158
+ arg: 'a',
159
+ truthy: false
160
+ }
161
+ }, {}, {}, true]])('%s', function (_name, argType, args, globals, expected) {
162
+ // @ts-ignore
163
+ expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
164
+ });
165
+ });
166
+ describe('exists', function () {
167
+ it.each([['exist', {
168
+ "if": {
169
+ arg: 'a',
170
+ exists: true
171
+ }
172
+ }, {
173
+ a: 1
174
+ }, {}, true], ['exist false', {
175
+ "if": {
176
+ arg: 'a',
177
+ exists: true
178
+ }
179
+ }, {}, {}, false]])('%s', function (_name, argType, args, globals, expected) {
180
+ // @ts-ignore
181
+ expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
182
+ });
183
+ });
184
+ describe('eq', function () {
185
+ it.each([['scalar true', {
186
+ "if": {
187
+ arg: 'a',
188
+ eq: 1
189
+ }
190
+ }, {
191
+ a: 1
192
+ }, {}, true], ['scalar false', {
193
+ "if": {
194
+ arg: 'a',
195
+ eq: 1
196
+ }
197
+ }, {
198
+ a: 2
199
+ }, {
200
+ a: 1
201
+ }, false]])('%s', function (_name, argType, args, globals, expected) {
202
+ // @ts-ignore
203
+ expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
204
+ });
205
+ });
206
+ describe('neq', function () {
207
+ it.each([['scalar true', {
208
+ "if": {
209
+ arg: 'a',
210
+ neq: 1
211
+ }
212
+ }, {
213
+ a: 2
214
+ }, {}, true], ['scalar false', {
215
+ "if": {
216
+ arg: 'a',
217
+ neq: 1
218
+ }
219
+ }, {
220
+ a: 1
221
+ }, {
222
+ a: 2
223
+ }, false]])('%s', function (_name, argType, args, globals, expected) {
224
+ // @ts-ignore
225
+ expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
226
+ });
227
+ });
228
+ });
229
+ describe('globals', function () {
230
+ describe('truthy', function () {
231
+ it.each([['implicit true', {
232
+ "if": {
233
+ global: 'a'
234
+ }
235
+ }, {}, {
236
+ a: 1
237
+ }, true], ['implicit undefined', {
238
+ "if": {
239
+ global: 'a'
240
+ }
241
+ }, {}, {}, false], ['truthy true', {
242
+ "if": {
243
+ global: 'a',
244
+ truthy: true
245
+ }
246
+ }, {}, {
247
+ a: 0
248
+ }, false], ['truthy false', {
249
+ "if": {
250
+ global: 'a',
251
+ truthy: false
252
+ }
253
+ }, {}, {
254
+ a: 0
255
+ }, true]])('%s', function (_name, argType, args, globals, expected) {
256
+ // @ts-ignore
257
+ expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
258
+ });
259
+ });
260
+ describe('exists', function () {
261
+ it.each([['implicit exist true', {
262
+ "if": {
263
+ global: 'a',
264
+ exists: true
265
+ }
266
+ }, {}, {
267
+ a: 1
268
+ }, true], ['implicit exist false', {
269
+ "if": {
270
+ global: 'a',
271
+ exists: true
272
+ }
273
+ }, {
274
+ a: 1
275
+ }, {}, false]])('%s', function (_name, argType, args, globals, expected) {
276
+ // @ts-ignore
277
+ expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
278
+ });
279
+ });
280
+ describe('eq', function () {
281
+ it.each([['scalar true', {
282
+ "if": {
283
+ global: 'a',
284
+ eq: 1
285
+ }
286
+ }, {}, {
287
+ a: 1
288
+ }, true], ['scalar false', {
289
+ "if": {
290
+ arg: 'a',
291
+ eq: 1
292
+ }
293
+ }, {
294
+ a: 2
295
+ }, {
296
+ a: 1
297
+ }, false]])('%s', function (_name, argType, args, globals, expected) {
298
+ // @ts-ignore
299
+ expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
300
+ });
301
+ });
302
+ describe('neq', function () {
303
+ it.each([['scalar true', {
304
+ "if": {
305
+ global: 'a',
306
+ neq: 1
307
+ }
308
+ }, {}, {
309
+ a: 2
310
+ }, true], ['scalar false', {
311
+ "if": {
312
+ global: 'a',
313
+ neq: 1
314
+ }
315
+ }, {
316
+ a: 2
317
+ }, {
318
+ a: 1
319
+ }, false]])('%s', function (_name, argType, args, globals, expected) {
320
+ // @ts-ignore
321
+ expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
322
+ });
323
+ });
324
+ });
325
+ });
package/dist/index.d.ts CHANGED
@@ -15,4 +15,5 @@ export declare const parseKind: (kind: string, { rootSeparator, groupSeparator }
15
15
  root: string | null;
16
16
  groups: string[];
17
17
  };
18
+ export { includeConditionalArg } from './includeConditionalArg';
18
19
  export * from './story';
package/dist/index.js CHANGED
@@ -8,13 +8,22 @@ var _exportNames = {
8
8
  toId: true,
9
9
  storyNameFromExport: true,
10
10
  isExportStory: true,
11
- parseKind: true
11
+ parseKind: true,
12
+ includeConditionalArg: true
12
13
  };
13
14
  exports.isExportStory = isExportStory;
15
+ Object.defineProperty(exports, "includeConditionalArg", {
16
+ enumerable: true,
17
+ get: function get() {
18
+ return _includeConditionalArg.includeConditionalArg;
19
+ }
20
+ });
14
21
  exports.parseKind = exports.storyNameFromExport = exports.toId = exports.sanitize = void 0;
15
22
 
16
23
  var _startCase = _interopRequireDefault(require("lodash/startCase"));
17
24
 
25
+ var _includeConditionalArg = require("./includeConditionalArg");
26
+
18
27
  var _story = require("./story");
19
28
 
20
29
  Object.keys(_story).forEach(function (key) {
package/dist/story.d.ts CHANGED
@@ -16,11 +16,27 @@ export interface StoryIdentifier {
16
16
  export declare type Parameters = {
17
17
  [name: string]: any;
18
18
  };
19
+ declare type ConditionalTest = {
20
+ truthy?: boolean;
21
+ } | {
22
+ exists: boolean;
23
+ } | {
24
+ eq: any;
25
+ } | {
26
+ neq: any;
27
+ };
28
+ declare type ConditionalValue = {
29
+ arg: string;
30
+ } | {
31
+ global: string;
32
+ };
33
+ export declare type Conditional = ConditionalValue & ConditionalTest;
19
34
  export interface InputType {
20
35
  name?: string;
21
36
  description?: string;
22
37
  defaultValue?: any;
23
38
  type?: SBType | SBScalarType['name'];
39
+ if?: Conditional;
24
40
  [key: string]: any;
25
41
  }
26
42
  export interface StrictInputType extends InputType {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/csf",
3
- "version": "0.0.2--canary.50.794a0bf.0",
3
+ "version": "0.0.2--canary.36.ecfc79e.0",
4
4
  "description": "Component Story Format (CSF) utilities",
5
5
  "keywords": [
6
6
  "storybook",
@@ -38,7 +38,10 @@
38
38
  },
39
39
  "prettier": "@storybook/linter-config/prettier.config",
40
40
  "jest": {
41
- "testEnvironment": "node"
41
+ "testEnvironment": "node",
42
+ "roots": [
43
+ "<rootDir>/src"
44
+ ]
42
45
  },
43
46
  "dependencies": {
44
47
  "lodash": "^4.17.15"
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};