@storybook/csf 0.0.2--canary.507502b.0 → 0.0.2--canary.d38f8f1.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,69 @@
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
+ var shouldExist = typeof exists !== 'undefined' ? exists : true;
41
+ var valueExists = typeof value !== 'undefined';
42
+ return shouldExist ? valueExists : !valueExists;
43
+ };
44
+ /**
45
+ * Helper function to include/exclude an arg based on the value of other other args
46
+ * aka "conditional args"
47
+ */
48
+
49
+
50
+ exports.testValue = testValue;
51
+
52
+ var includeConditionalArg = function includeConditionalArg(argType, args, globals) {
53
+ if (!argType["if"]) return true;
54
+ var _ref2 = argType["if"],
55
+ arg = _ref2.arg,
56
+ global = _ref2.global;
57
+
58
+ if (count([arg, global]) !== 1) {
59
+ throw new Error("Invalid conditional value ".concat(JSON.stringify({
60
+ arg: arg,
61
+ global: global
62
+ })));
63
+ }
64
+
65
+ var value = arg ? args[arg] : globals[global];
66
+ return testValue(argType["if"], value);
67
+ };
68
+
69
+ exports.includeConditionalArg = includeConditionalArg;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,255 @@
1
+ "use strict";
2
+
3
+ var _includeConditionalArg = require("./includeConditionalArg");
4
+
5
+ /* eslint-disable @typescript-eslint/ban-ts-ignore */
6
+ describe('testValue', function () {
7
+ describe('exists', function () {
8
+ it.each([['implicit exist true', {}, 1, true], ['implicit exist false', {}, undefined, false], ['explicit exist', {
9
+ exists: true
10
+ }, 1, true], ['explicit exist false', {
11
+ exists: true
12
+ }, undefined, false], ['explicit nexist', {
13
+ exists: false
14
+ }, undefined, true], ['explicit nexist false', {
15
+ exists: false
16
+ }, 1, false]])('%s', function (_name, cond, value, expected) {
17
+ // @ts-ignore
18
+ expect((0, _includeConditionalArg.testValue)(cond, value)).toBe(expected);
19
+ });
20
+ });
21
+ describe('eq', function () {
22
+ it.each([['true', {
23
+ eq: 1
24
+ }, 1, true], ['false', {
25
+ eq: 1
26
+ }, 2, false], ['undefined', {
27
+ eq: undefined
28
+ }, undefined, false], ['undefined false', {
29
+ eq: 1
30
+ }, undefined, false], ['object true', {
31
+ eq: {
32
+ x: 1
33
+ }
34
+ }, {
35
+ x: 1
36
+ }, true], ['object true', {
37
+ eq: {
38
+ x: 1
39
+ }
40
+ }, {
41
+ x: 2
42
+ }, false]])('%s', function (_name, cond, value, expected) {
43
+ // @ts-ignore
44
+ expect((0, _includeConditionalArg.testValue)(cond, value)).toBe(expected);
45
+ });
46
+ });
47
+ describe('neq', function () {
48
+ it.each([['true', {
49
+ neq: 1
50
+ }, 2, true], ['false', {
51
+ neq: 1
52
+ }, 1, false], ['undefined true', {
53
+ neq: 1
54
+ }, undefined, true], ['undefined false', {
55
+ neq: undefined
56
+ }, undefined, false], ['object true', {
57
+ neq: {
58
+ x: 1
59
+ }
60
+ }, {
61
+ x: 2
62
+ }, true], ['object false', {
63
+ neq: {
64
+ x: 1
65
+ }
66
+ }, {
67
+ x: 1
68
+ }, false]])('%s', function (_name, cond, value, expected) {
69
+ // @ts-ignore
70
+ expect((0, _includeConditionalArg.testValue)(cond, value)).toBe(expected);
71
+ });
72
+ });
73
+ });
74
+ describe('includeConditionalArg', function () {
75
+ describe('errors', function () {
76
+ it('should throw if arg and global are both specified', function () {
77
+ expect(function () {
78
+ return (0, _includeConditionalArg.includeConditionalArg)({
79
+ "if": {
80
+ arg: 'a',
81
+ global: 'b'
82
+ }
83
+ }, {}, {});
84
+ }).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional value {\\\"arg\\\":\\\"a\\\",\\\"global\\\":\\\"b\\\"}\"");
85
+ });
86
+ it('should throw if mulitiple exists / eq / neq are specified', function () {
87
+ expect(function () {
88
+ return (0, _includeConditionalArg.includeConditionalArg)({
89
+ "if": {
90
+ arg: 'a',
91
+ exists: true,
92
+ eq: 1
93
+ }
94
+ }, {}, {});
95
+ }).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional test {\\\"exists\\\":true,\\\"eq\\\":1}\"");
96
+ expect(function () {
97
+ return (0, _includeConditionalArg.includeConditionalArg)({
98
+ "if": {
99
+ arg: 'a',
100
+ exists: false,
101
+ neq: 0
102
+ }
103
+ }, {}, {});
104
+ }).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional test {\\\"exists\\\":false,\\\"neq\\\":0}\"");
105
+ expect(function () {
106
+ return (0, _includeConditionalArg.includeConditionalArg)({
107
+ "if": {
108
+ arg: 'a',
109
+ eq: 1,
110
+ neq: 0
111
+ }
112
+ }, {}, {});
113
+ }).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional test {\\\"eq\\\":1,\\\"neq\\\":0}\"");
114
+ });
115
+ });
116
+ describe('args', function () {
117
+ describe('exists', function () {
118
+ it.each([['implicit exist true', {
119
+ "if": {
120
+ arg: 'a'
121
+ }
122
+ }, {
123
+ a: 1
124
+ }, {}, true], ['implicit exist false', {
125
+ "if": {
126
+ arg: 'a'
127
+ }
128
+ }, {}, {}, false], ['explicit exist', {
129
+ "if": {
130
+ arg: 'a',
131
+ exists: true
132
+ }
133
+ }, {
134
+ a: 1
135
+ }, {}, true], ['explicit exist false', {
136
+ "if": {
137
+ arg: 'a',
138
+ exists: true
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('eq', function () {
146
+ it.each([['scalar true', {
147
+ "if": {
148
+ arg: 'a',
149
+ eq: 1
150
+ }
151
+ }, {
152
+ a: 1
153
+ }, {}, true], ['scalar false', {
154
+ "if": {
155
+ arg: 'a',
156
+ eq: 1
157
+ }
158
+ }, {
159
+ a: 2
160
+ }, {
161
+ a: 1
162
+ }, false]])('%s', function (_name, argType, args, globals, expected) {
163
+ // @ts-ignore
164
+ expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
165
+ });
166
+ });
167
+ describe('neq', function () {
168
+ it.each([['scalar true', {
169
+ "if": {
170
+ arg: 'a',
171
+ neq: 1
172
+ }
173
+ }, {
174
+ a: 2
175
+ }, {}, true], ['scalar false', {
176
+ "if": {
177
+ arg: 'a',
178
+ neq: 1
179
+ }
180
+ }, {
181
+ a: 1
182
+ }, {
183
+ a: 2
184
+ }, false]])('%s', function (_name, argType, args, globals, expected) {
185
+ // @ts-ignore
186
+ expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
187
+ });
188
+ });
189
+ });
190
+ describe('globals', function () {
191
+ describe('exists', function () {
192
+ it.each([// name, argType, args, globals, expected
193
+ ['implicit exist true', {
194
+ "if": {
195
+ global: 'a'
196
+ }
197
+ }, {}, {
198
+ a: 1
199
+ }, true], ['implicit exist false', {
200
+ "if": {
201
+ global: 'a'
202
+ }
203
+ }, {
204
+ a: 1
205
+ }, {}, false]])('%s', function (_name, argType, args, globals, expected) {
206
+ // @ts-ignore
207
+ expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
208
+ });
209
+ });
210
+ describe('eq', function () {
211
+ it.each([['scalar true', {
212
+ "if": {
213
+ global: 'a',
214
+ eq: 1
215
+ }
216
+ }, {}, {
217
+ a: 1
218
+ }, true], ['scalar false', {
219
+ "if": {
220
+ arg: 'a',
221
+ eq: 1
222
+ }
223
+ }, {
224
+ a: 2
225
+ }, {
226
+ a: 1
227
+ }, false]])('%s', function (_name, argType, args, globals, expected) {
228
+ // @ts-ignore
229
+ expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
230
+ });
231
+ });
232
+ describe('neq', function () {
233
+ it.each([['scalar true', {
234
+ "if": {
235
+ global: 'a',
236
+ neq: 1
237
+ }
238
+ }, {}, {
239
+ a: 2
240
+ }, true], ['scalar false', {
241
+ "if": {
242
+ global: 'a',
243
+ neq: 1
244
+ }
245
+ }, {
246
+ a: 2
247
+ }, {
248
+ a: 1
249
+ }, false]])('%s', function (_name, argType, args, globals, expected) {
250
+ // @ts-ignore
251
+ expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
252
+ });
253
+ });
254
+ });
255
+ });
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) {
@@ -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.includeConditionalArg = includeConditionalArg;
138
+ exports.parseKind = parseKind;
@@ -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,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
- addIf?: string;
25
- removeIf?: 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.507502b.0",
3
+ "version": "0.0.2--canary.d38f8f1.0",
4
4
  "description": "Component Story Format (CSF) utilities",
5
5
  "keywords": [
6
6
  "storybook",