@storybook/csf 0.0.2--canary.507502b.0 → 0.0.2--canary.7c6c115.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/includeConditionalArg.d.ts +3 -0
- package/dist/includeConditionalArg.js +74 -0
- package/dist/includeConditionalArg.test.d.ts +1 -0
- package/dist/includeConditionalArg.test.js +318 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +10 -17
- package/dist/index.test.js +0 -20
- package/dist/story.d.ts +16 -2
- package/package.json +5 -2
@@ -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 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,318 @@
|
|
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 arg and global are both specified', function () {
|
95
|
+
expect(function () {
|
96
|
+
return (0, _includeConditionalArg.includeConditionalArg)({
|
97
|
+
"if": {
|
98
|
+
arg: 'a',
|
99
|
+
global: 'b'
|
100
|
+
}
|
101
|
+
}, {}, {});
|
102
|
+
}).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional value {\\\"arg\\\":\\\"a\\\",\\\"global\\\":\\\"b\\\"}\"");
|
103
|
+
});
|
104
|
+
it('should throw if mulitiple exists / eq / neq are specified', function () {
|
105
|
+
expect(function () {
|
106
|
+
return (0, _includeConditionalArg.includeConditionalArg)({
|
107
|
+
"if": {
|
108
|
+
arg: 'a',
|
109
|
+
exists: true,
|
110
|
+
eq: 1
|
111
|
+
}
|
112
|
+
}, {}, {});
|
113
|
+
}).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional test {\\\"exists\\\":true,\\\"eq\\\":1}\"");
|
114
|
+
expect(function () {
|
115
|
+
return (0, _includeConditionalArg.includeConditionalArg)({
|
116
|
+
"if": {
|
117
|
+
arg: 'a',
|
118
|
+
exists: false,
|
119
|
+
neq: 0
|
120
|
+
}
|
121
|
+
}, {}, {});
|
122
|
+
}).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional test {\\\"exists\\\":false,\\\"neq\\\":0}\"");
|
123
|
+
expect(function () {
|
124
|
+
return (0, _includeConditionalArg.includeConditionalArg)({
|
125
|
+
"if": {
|
126
|
+
arg: 'a',
|
127
|
+
eq: 1,
|
128
|
+
neq: 0
|
129
|
+
}
|
130
|
+
}, {}, {});
|
131
|
+
}).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional test {\\\"eq\\\":1,\\\"neq\\\":0}\"");
|
132
|
+
});
|
133
|
+
});
|
134
|
+
describe('args', function () {
|
135
|
+
describe('implicit', function () {
|
136
|
+
it.each([['implicit true', {
|
137
|
+
"if": {
|
138
|
+
arg: 'a'
|
139
|
+
}
|
140
|
+
}, {
|
141
|
+
a: 1
|
142
|
+
}, {}, true], ['truthy true', {
|
143
|
+
"if": {
|
144
|
+
arg: 'a',
|
145
|
+
truthy: true
|
146
|
+
}
|
147
|
+
}, {
|
148
|
+
a: 0
|
149
|
+
}, {}, false], ['truthy false', {
|
150
|
+
"if": {
|
151
|
+
arg: 'a',
|
152
|
+
truthy: false
|
153
|
+
}
|
154
|
+
}, {}, {}, true]])('%s', function (_name, argType, args, globals, expected) {
|
155
|
+
// @ts-ignore
|
156
|
+
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
|
157
|
+
});
|
158
|
+
});
|
159
|
+
describe('exists', function () {
|
160
|
+
it.each([['exist', {
|
161
|
+
"if": {
|
162
|
+
arg: 'a',
|
163
|
+
exists: true
|
164
|
+
}
|
165
|
+
}, {
|
166
|
+
a: 1
|
167
|
+
}, {}, true], ['exist false', {
|
168
|
+
"if": {
|
169
|
+
arg: 'a',
|
170
|
+
exists: true
|
171
|
+
}
|
172
|
+
}, {}, {}, false]])('%s', function (_name, argType, args, globals, expected) {
|
173
|
+
// @ts-ignore
|
174
|
+
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
|
175
|
+
});
|
176
|
+
});
|
177
|
+
describe('eq', function () {
|
178
|
+
it.each([['scalar true', {
|
179
|
+
"if": {
|
180
|
+
arg: 'a',
|
181
|
+
eq: 1
|
182
|
+
}
|
183
|
+
}, {
|
184
|
+
a: 1
|
185
|
+
}, {}, true], ['scalar false', {
|
186
|
+
"if": {
|
187
|
+
arg: 'a',
|
188
|
+
eq: 1
|
189
|
+
}
|
190
|
+
}, {
|
191
|
+
a: 2
|
192
|
+
}, {
|
193
|
+
a: 1
|
194
|
+
}, false]])('%s', function (_name, argType, args, globals, expected) {
|
195
|
+
// @ts-ignore
|
196
|
+
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
|
197
|
+
});
|
198
|
+
});
|
199
|
+
describe('neq', function () {
|
200
|
+
it.each([['scalar true', {
|
201
|
+
"if": {
|
202
|
+
arg: 'a',
|
203
|
+
neq: 1
|
204
|
+
}
|
205
|
+
}, {
|
206
|
+
a: 2
|
207
|
+
}, {}, true], ['scalar false', {
|
208
|
+
"if": {
|
209
|
+
arg: 'a',
|
210
|
+
neq: 1
|
211
|
+
}
|
212
|
+
}, {
|
213
|
+
a: 1
|
214
|
+
}, {
|
215
|
+
a: 2
|
216
|
+
}, false]])('%s', function (_name, argType, args, globals, expected) {
|
217
|
+
// @ts-ignore
|
218
|
+
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
|
219
|
+
});
|
220
|
+
});
|
221
|
+
});
|
222
|
+
describe('globals', function () {
|
223
|
+
describe('truthy', function () {
|
224
|
+
it.each([['implicit true', {
|
225
|
+
"if": {
|
226
|
+
global: 'a'
|
227
|
+
}
|
228
|
+
}, {}, {
|
229
|
+
a: 1
|
230
|
+
}, true], ['implicit undefined', {
|
231
|
+
"if": {
|
232
|
+
global: 'a'
|
233
|
+
}
|
234
|
+
}, {}, {}, false], ['truthy true', {
|
235
|
+
"if": {
|
236
|
+
global: 'a',
|
237
|
+
truthy: true
|
238
|
+
}
|
239
|
+
}, {}, {
|
240
|
+
a: 0
|
241
|
+
}, false], ['truthy false', {
|
242
|
+
"if": {
|
243
|
+
global: 'a',
|
244
|
+
truthy: false
|
245
|
+
}
|
246
|
+
}, {}, {
|
247
|
+
a: 0
|
248
|
+
}, true]])('%s', function (_name, argType, args, globals, expected) {
|
249
|
+
// @ts-ignore
|
250
|
+
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
|
251
|
+
});
|
252
|
+
});
|
253
|
+
describe('exists', function () {
|
254
|
+
it.each([['implicit exist true', {
|
255
|
+
"if": {
|
256
|
+
global: 'a',
|
257
|
+
exists: true
|
258
|
+
}
|
259
|
+
}, {}, {
|
260
|
+
a: 1
|
261
|
+
}, true], ['implicit exist false', {
|
262
|
+
"if": {
|
263
|
+
global: 'a',
|
264
|
+
exists: true
|
265
|
+
}
|
266
|
+
}, {
|
267
|
+
a: 1
|
268
|
+
}, {}, false]])('%s', function (_name, argType, args, globals, expected) {
|
269
|
+
// @ts-ignore
|
270
|
+
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
|
271
|
+
});
|
272
|
+
});
|
273
|
+
describe('eq', function () {
|
274
|
+
it.each([['scalar true', {
|
275
|
+
"if": {
|
276
|
+
global: 'a',
|
277
|
+
eq: 1
|
278
|
+
}
|
279
|
+
}, {}, {
|
280
|
+
a: 1
|
281
|
+
}, true], ['scalar false', {
|
282
|
+
"if": {
|
283
|
+
arg: 'a',
|
284
|
+
eq: 1
|
285
|
+
}
|
286
|
+
}, {
|
287
|
+
a: 2
|
288
|
+
}, {
|
289
|
+
a: 1
|
290
|
+
}, false]])('%s', function (_name, argType, args, globals, expected) {
|
291
|
+
// @ts-ignore
|
292
|
+
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
|
293
|
+
});
|
294
|
+
});
|
295
|
+
describe('neq', function () {
|
296
|
+
it.each([['scalar true', {
|
297
|
+
"if": {
|
298
|
+
global: 'a',
|
299
|
+
neq: 1
|
300
|
+
}
|
301
|
+
}, {}, {
|
302
|
+
a: 2
|
303
|
+
}, true], ['scalar false', {
|
304
|
+
"if": {
|
305
|
+
global: 'a',
|
306
|
+
neq: 1
|
307
|
+
}
|
308
|
+
}, {
|
309
|
+
a: 2
|
310
|
+
}, {
|
311
|
+
a: 1
|
312
|
+
}, false]])('%s', function (_name, argType, args, globals, expected) {
|
313
|
+
// @ts-ignore
|
314
|
+
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
|
315
|
+
});
|
316
|
+
});
|
317
|
+
});
|
318
|
+
});
|
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
|
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
|
-
|
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) {
|
@@ -126,20 +134,5 @@ var parseKind = function parseKind(kind, _ref2) {
|
|
126
134
|
groups: groups
|
127
135
|
};
|
128
136
|
};
|
129
|
-
/**
|
130
|
-
* Helper function to include/exclude an arg based on the value of other other args
|
131
|
-
* aka "conditional args"
|
132
|
-
*/
|
133
|
-
|
134
|
-
|
135
|
-
exports.parseKind = parseKind;
|
136
|
-
|
137
|
-
var includeConditionalArg = function includeConditionalArg(argType, args) {
|
138
|
-
var addIf = argType.addIf,
|
139
|
-
removeIf = argType.removeIf;
|
140
|
-
if (addIf) return !!args[addIf];
|
141
|
-
if (removeIf) return !args[removeIf];
|
142
|
-
return true;
|
143
|
-
};
|
144
137
|
|
145
|
-
exports.
|
138
|
+
exports.parseKind = parseKind;
|
package/dist/index.test.js
CHANGED
@@ -139,24 +139,4 @@ describe('isExportStory', function () {
|
|
139
139
|
excludeStories: /b/
|
140
140
|
})).toBeTruthy();
|
141
141
|
});
|
142
|
-
});
|
143
|
-
describe('includeConditionalArg', function () {
|
144
|
-
it('dynamic values', function () {
|
145
|
-
expect((0, _.includeConditionalArg)({
|
146
|
-
addIf: 'foo'
|
147
|
-
}, {
|
148
|
-
foo: true
|
149
|
-
})).toBe(true);
|
150
|
-
expect((0, _.includeConditionalArg)({
|
151
|
-
addIf: 'bar'
|
152
|
-
}, {})).toBe(false);
|
153
|
-
expect((0, _.includeConditionalArg)({
|
154
|
-
removeIf: 'foo'
|
155
|
-
}, {
|
156
|
-
foo: true
|
157
|
-
})).toBe(false);
|
158
|
-
expect((0, _.includeConditionalArg)({
|
159
|
-
removeIf: 'bar'
|
160
|
-
}, {})).toBe(true);
|
161
|
-
});
|
162
142
|
});
|
package/dist/story.d.ts
CHANGED
@@ -16,13 +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'];
|
24
|
-
|
25
|
-
removeIf?: string;
|
39
|
+
if?: Conditional;
|
26
40
|
[key: string]: any;
|
27
41
|
}
|
28
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.
|
3
|
+
"version": "0.0.2--canary.7c6c115.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"
|