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