Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

@storybook/csf

Package Overview
Dependencies
Maintainers
29
Versions
169
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@storybook/csf - npm Package Compare versions

Comparing version
0.0.2--canary.50.794a0bf.0
to
0.0.2--canary.51.768ca23.0
+3
dist/includeConditionalArg.d.ts
import { Args, Globals, InputType, Conditional } from './story';
export declare const testValue: (cond: Omit<Conditional, 'arg' | 'global'>, value: any) => boolean;
export declare const includeConditionalArg: (argType: InputType, args: Args, globals: Globals) => boolean;
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.includeConditionalArg = exports.testValue = void 0;
var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var count = function count(vals) {
return vals.map(function (v) {
return typeof v !== 'undefined';
}).filter(Boolean).length;
};
var testValue = function testValue(cond, value) {
var _ref = cond,
exists = _ref.exists,
eq = _ref.eq,
neq = _ref.neq,
truthy = _ref.truthy;
if (count([exists, eq, neq, truthy]) > 1) {
throw new Error("Invalid conditional test ".concat(JSON.stringify({
exists: exists,
eq: eq,
neq: neq
})));
}
if (typeof eq !== 'undefined') {
return (0, _isEqual["default"])(value, eq);
}
if (typeof neq !== 'undefined') {
return !(0, _isEqual["default"])(value, neq);
}
if (typeof exists !== 'undefined') {
var valueExists = typeof value !== 'undefined';
return exists ? valueExists : !valueExists;
}
var shouldBeTruthy = typeof truthy === 'undefined' ? true : truthy;
return shouldBeTruthy ? !!value : !value;
};
/**
* Helper function to include/exclude an arg based on the value of other other args
* aka "conditional args"
*/
exports.testValue = testValue;
var includeConditionalArg = function includeConditionalArg(argType, args, globals) {
if (!argType["if"]) return true;
var _ref2 = argType["if"],
arg = _ref2.arg,
global = _ref2.global;
if (count([arg, global]) !== 1) {
throw new Error("Invalid conditional value ".concat(JSON.stringify({
arg: arg,
global: global
})));
}
var value = arg ? args[arg] : globals[global];
return testValue(argType["if"], value);
};
exports.includeConditionalArg = includeConditionalArg;
"use strict";
var _includeConditionalArg = require("./includeConditionalArg");
/* eslint-disable @typescript-eslint/ban-ts-ignore */
describe('testValue', function () {
describe('truthy', function () {
it.each([['implicit true', {}, true, true], ['implicit truthy', {}, 1, true], ['implicit falsey', {}, 0, false], ['truthy true', {
truthy: true
}, true, true], ['truthy truthy', {
truthy: true
}, 1, true], ['truthy falsey', {
truthy: true
}, 0, false], ['falsey true', {
truthy: false
}, true, false], ['falsey truthy', {
truthy: false
}, 1, false], ['falsey falsey', {
truthy: false
}, 0, true]])('%s', function (_name, cond, value, expected) {
// @ts-ignore
expect((0, _includeConditionalArg.testValue)(cond, value)).toBe(expected);
});
});
describe('exists', function () {
it.each([['exist', {
exists: true
}, 1, true], ['exist false', {
exists: true
}, undefined, false], ['nexist', {
exists: false
}, undefined, true], ['nexist false', {
exists: false
}, 1, false]])('%s', function (_name, cond, value, expected) {
// @ts-ignore
expect((0, _includeConditionalArg.testValue)(cond, value)).toBe(expected);
});
});
describe('eq', function () {
it.each([['true', {
eq: 1
}, 1, true], ['false', {
eq: 1
}, 2, false], ['undefined', {
eq: undefined
}, undefined, false], ['undefined false', {
eq: 1
}, undefined, false], ['object true', {
eq: {
x: 1
}
}, {
x: 1
}, true], ['object true', {
eq: {
x: 1
}
}, {
x: 2
}, false]])('%s', function (_name, cond, value, expected) {
// @ts-ignore
expect((0, _includeConditionalArg.testValue)(cond, value)).toBe(expected);
});
});
describe('neq', function () {
it.each([['true', {
neq: 1
}, 2, true], ['false', {
neq: 1
}, 1, false], ['undefined true', {
neq: 1
}, undefined, true], ['undefined false', {
neq: undefined
}, undefined, false], ['object true', {
neq: {
x: 1
}
}, {
x: 2
}, true], ['object false', {
neq: {
x: 1
}
}, {
x: 1
}, false]])('%s', function (_name, cond, value, expected) {
// @ts-ignore
expect((0, _includeConditionalArg.testValue)(cond, value)).toBe(expected);
});
});
});
describe('includeConditionalArg', function () {
describe('errors', function () {
it('should throw if neither arg nor global is specified', function () {
expect(function () {
return (0, _includeConditionalArg.includeConditionalArg)({
"if": {}
}, {}, {});
}).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional value {}\"");
});
it('should throw if arg and global are both specified', function () {
expect(function () {
return (0, _includeConditionalArg.includeConditionalArg)({
"if": {
arg: 'a',
global: 'b'
}
}, {}, {});
}).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional value {\\\"arg\\\":\\\"a\\\",\\\"global\\\":\\\"b\\\"}\"");
});
it('should throw if mulitiple exists / eq / neq are specified', function () {
expect(function () {
return (0, _includeConditionalArg.includeConditionalArg)({
"if": {
arg: 'a',
exists: true,
eq: 1
}
}, {}, {});
}).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional test {\\\"exists\\\":true,\\\"eq\\\":1}\"");
expect(function () {
return (0, _includeConditionalArg.includeConditionalArg)({
"if": {
arg: 'a',
exists: false,
neq: 0
}
}, {}, {});
}).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional test {\\\"exists\\\":false,\\\"neq\\\":0}\"");
expect(function () {
return (0, _includeConditionalArg.includeConditionalArg)({
"if": {
arg: 'a',
eq: 1,
neq: 0
}
}, {}, {});
}).toThrowErrorMatchingInlineSnapshot("\"Invalid conditional test {\\\"eq\\\":1,\\\"neq\\\":0}\"");
});
});
describe('args', function () {
describe('implicit', function () {
it.each([['implicit true', {
"if": {
arg: 'a'
}
}, {
a: 1
}, {}, true], ['truthy true', {
"if": {
arg: 'a',
truthy: true
}
}, {
a: 0
}, {}, false], ['truthy false', {
"if": {
arg: 'a',
truthy: false
}
}, {}, {}, true]])('%s', function (_name, argType, args, globals, expected) {
// @ts-ignore
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
});
});
describe('exists', function () {
it.each([['exist', {
"if": {
arg: 'a',
exists: true
}
}, {
a: 1
}, {}, true], ['exist false', {
"if": {
arg: 'a',
exists: true
}
}, {}, {}, false]])('%s', function (_name, argType, args, globals, expected) {
// @ts-ignore
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
});
});
describe('eq', function () {
it.each([['scalar true', {
"if": {
arg: 'a',
eq: 1
}
}, {
a: 1
}, {}, true], ['scalar false', {
"if": {
arg: 'a',
eq: 1
}
}, {
a: 2
}, {
a: 1
}, false]])('%s', function (_name, argType, args, globals, expected) {
// @ts-ignore
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
});
});
describe('neq', function () {
it.each([['scalar true', {
"if": {
arg: 'a',
neq: 1
}
}, {
a: 2
}, {}, true], ['scalar false', {
"if": {
arg: 'a',
neq: 1
}
}, {
a: 1
}, {
a: 2
}, false]])('%s', function (_name, argType, args, globals, expected) {
// @ts-ignore
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
});
});
});
describe('globals', function () {
describe('truthy', function () {
it.each([['implicit true', {
"if": {
global: 'a'
}
}, {}, {
a: 1
}, true], ['implicit undefined', {
"if": {
global: 'a'
}
}, {}, {}, false], ['truthy true', {
"if": {
global: 'a',
truthy: true
}
}, {}, {
a: 0
}, false], ['truthy false', {
"if": {
global: 'a',
truthy: false
}
}, {}, {
a: 0
}, true]])('%s', function (_name, argType, args, globals, expected) {
// @ts-ignore
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
});
});
describe('exists', function () {
it.each([['implicit exist true', {
"if": {
global: 'a',
exists: true
}
}, {}, {
a: 1
}, true], ['implicit exist false', {
"if": {
global: 'a',
exists: true
}
}, {
a: 1
}, {}, false]])('%s', function (_name, argType, args, globals, expected) {
// @ts-ignore
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
});
});
describe('eq', function () {
it.each([['scalar true', {
"if": {
global: 'a',
eq: 1
}
}, {}, {
a: 1
}, true], ['scalar false', {
"if": {
arg: 'a',
eq: 1
}
}, {
a: 2
}, {
a: 1
}, false]])('%s', function (_name, argType, args, globals, expected) {
// @ts-ignore
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
});
});
describe('neq', function () {
it.each([['scalar true', {
"if": {
global: 'a',
neq: 1
}
}, {}, {
a: 2
}, true], ['scalar false', {
"if": {
global: 'a',
neq: 1
}
}, {
a: 2
}, {
a: 1
}, false]])('%s', function (_name, argType, args, globals, expected) {
// @ts-ignore
expect((0, _includeConditionalArg.includeConditionalArg)(argType, args, globals)).toBe(expected);
});
});
});
});
+2
-1
export declare const sanitize: (string: string) => string;
export declare const toId: (kind: string, name?: string | undefined) => string;
export declare const toId: (kind: string, name?: string) => string;
export declare const storyNameFromExport: (key: string) => string;

@@ -18,2 +18,3 @@ declare type StoryDescriptor = string[] | RegExp;

};
export { includeConditionalArg } from './includeConditionalArg';
export * from './story';

@@ -11,5 +11,12 @@ "use strict";

isExportStory: true,
parseKind: true
parseKind: true,
includeConditionalArg: true
};
exports.isExportStory = isExportStory;
Object.defineProperty(exports, "includeConditionalArg", {
enumerable: true,
get: function get() {
return _includeConditionalArg.includeConditionalArg;
}
});
exports.parseKind = exports.storyNameFromExport = exports.toId = exports.sanitize = void 0;

@@ -19,2 +26,4 @@

var _includeConditionalArg = require("./includeConditionalArg");
var _story = require("./story");

@@ -21,0 +30,0 @@

@@ -0,1 +1,2 @@

import { Simplify, UnionToIntersection } from 'type-fest';
import { SBType, SBScalarType } from './SBType';

@@ -19,2 +20,17 @@ export * from './SBType';

};
declare type ConditionalTest = {
truthy?: boolean;
} | {
exists: boolean;
} | {
eq: any;
} | {
neq: any;
};
declare type ConditionalValue = {
arg: string;
} | {
global: string;
};
export declare type Conditional = ConditionalValue & ConditionalTest;
export interface InputType {

@@ -25,2 +41,3 @@ name?: string;

type?: SBType | SBScalarType['name'];
if?: Conditional;
[key: string]: any;

@@ -53,6 +70,11 @@ }

storyResult: unknown;
T?: unknown;
};
export declare type StoryContextForEnhancers<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = StoryIdentifier & {
component?: TFramework['component'];
subcomponents?: Record<string, TFramework['component']>;
component?: (TFramework & {
T: any;
})['component'];
subcomponents?: Record<string, (TFramework & {
T: any;
})['component']>;
parameters: Parameters;

@@ -83,16 +105,24 @@ initialArgs: TArgs;

};
export declare type PlayFunction<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = (context: StoryContext<TFramework, TArgs>) => Promise<void> | void;
export declare type PartialStoryFn<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = (update?: StoryContextUpdate<TArgs>) => TFramework['storyResult'];
export declare type StepLabel = string;
export declare type StepFunction<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = (label: StepLabel, play: PlayFunction<TFramework, TArgs>) => Promise<void> | void;
export declare type PlayFunctionContext<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = StoryContext<TFramework, TArgs> & {
step: StepFunction<TFramework, TArgs>;
};
export declare type PlayFunction<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = (context: PlayFunctionContext<TFramework, TArgs>) => Promise<void> | void;
export declare type PartialStoryFn<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = (update?: StoryContextUpdate<Partial<TArgs>>) => TFramework['storyResult'];
export declare type LegacyStoryFn<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = (context: StoryContext<TFramework, TArgs>) => TFramework['storyResult'];
export declare type ArgsStoryFn<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = (args: TArgs, context: StoryContext<TFramework, TArgs>) => TFramework['storyResult'];
export declare type ArgsStoryFn<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = (args: TArgs, context: StoryContext<TFramework, TArgs>) => (TFramework & {
T: TArgs;
})['storyResult'];
export declare type StoryFn<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = LegacyStoryFn<TFramework, TArgs> | ArgsStoryFn<TFramework, TArgs>;
export declare type DecoratorFunction<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = (fn: PartialStoryFn<TFramework, TArgs>, c: StoryContext<TFramework, TArgs>) => TFramework['storyResult'];
export declare type DecoratorApplicator<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = (storyFn: LegacyStoryFn<TFramework, TArgs>, decorators: DecoratorFunction<TFramework, TArgs>[]) => LegacyStoryFn<TFramework, TArgs>;
export declare type StepRunner<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = (label: StepLabel, play: PlayFunction<TFramework, TArgs>, context: PlayFunctionContext<TFramework, TArgs>) => Promise<void>;
export declare type BaseAnnotations<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = {
decorators?: DecoratorFunction<TFramework, Args>[];
decorators?: DecoratorFunction<TFramework, TArgs>[];
parameters?: Parameters;
args?: Partial<TArgs>;
argTypes?: Partial<ArgTypes<TArgs>>;
loaders?: LoaderFunction<TFramework, Args>[];
render?: ArgsStoryFn<TFramework, Args>;
loaders?: LoaderFunction<TFramework, TArgs>[];
render?: ArgsStoryFn<TFramework, TArgs>;
};

@@ -105,5 +135,6 @@ export declare type ProjectAnnotations<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = BaseAnnotations<TFramework, TArgs> & {

applyDecorators?: DecoratorApplicator<TFramework, Args>;
runStep?: StepRunner<TFramework, TArgs>;
};
declare type StoryDescriptor = string[] | RegExp;
export declare type ComponentAnnotations<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = BaseAnnotations<TFramework, TArgs> & {
export interface ComponentAnnotations<TFramework extends AnyFramework = AnyFramework, TArgs = Args> extends BaseAnnotations<TFramework, TArgs> {
title?: ComponentTitle;

@@ -113,6 +144,8 @@ id?: ComponentId;

excludeStories?: StoryDescriptor;
component?: TFramework['component'];
component?: (TFramework & {
T: Args extends TArgs ? any : TArgs;
})['component'];
subcomponents?: Record<string, TFramework['component']>;
};
export declare type StoryAnnotations<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = BaseAnnotations<TFramework, TArgs> & {
}
export declare type StoryAnnotations<TFramework extends AnyFramework = AnyFramework, TArgs = Args, TRequiredArgs = Partial<TArgs>> = BaseAnnotations<TFramework, TArgs> & {
name?: StoryName;

@@ -122,3 +155,7 @@ storyName?: StoryName;

story?: Omit<StoryAnnotations<TFramework, TArgs>, 'story'>;
};
} & ({} extends TRequiredArgs ? {
args?: TRequiredArgs;
} : {
args: TRequiredArgs;
});
export declare type LegacyAnnotatedStoryFn<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = StoryFn<TFramework, TArgs> & StoryAnnotations<TFramework, TArgs>;

@@ -128,1 +165,8 @@ export declare type LegacyStoryAnnotationsOrFn<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = LegacyAnnotatedStoryFn<TFramework, TArgs> | StoryAnnotations<TFramework, TArgs>;

export declare type StoryAnnotationsOrFn<TFramework extends AnyFramework = AnyFramework, TArgs = Args> = AnnotatedStoryFn<TFramework, TArgs> | StoryAnnotations<TFramework, TArgs>;
export declare type ArgsFromMeta<TFramework extends AnyFramework, Meta> = Meta extends {
render?: ArgsStoryFn<TFramework, infer RArgs>;
loaders?: (infer Loaders)[];
decorators?: (infer Decorators)[];
} ? Simplify<RArgs & DecoratorsArgs<TFramework, Decorators> & LoaderArgs<TFramework, Loaders>> : unknown;
declare type DecoratorsArgs<TFramework extends AnyFramework, Decorators> = UnionToIntersection<Decorators extends DecoratorFunction<TFramework, infer Args> ? Args : unknown>;
declare type LoaderArgs<TFramework extends AnyFramework, Loaders> = UnionToIntersection<Loaders extends LoaderFunction<TFramework, infer Args> ? Args : unknown>;
"use strict";
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
// NOTE Example of internal type definition for @storybook/<X> (where X is a framework)
// NOTE Examples of using types from @storybook/<X> in real project
var Button = function Button() {
var Button = function Button(props) {
return 'Button';
};
}; // NOTE Various kind usages
// NOTE Various kind usages
var simple = {

@@ -29,6 +33,6 @@ title: 'simple',

args: {
a: 1
x: '1'
},
argTypes: {
a: {
x: {
type: {

@@ -120,3 +124,3 @@ name: 'string'

var CSF3Story = {
render: function render() {
render: function render(args) {
return 'Named Story';

@@ -143,3 +147,3 @@ },

var CSF3StoryStrict = {
render: function render() {
render: function render(args) {
return 'Named Story';

@@ -163,2 +167,67 @@ },

x: '1'
},
play: function () {
var _play = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(_ref) {
var step;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
step = _ref.step;
_context2.next = 3;
return step('a step', /*#__PURE__*/function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref2) {
var substep;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
substep = _ref2.step;
_context.next = 3;
return substep('a substep', function () {});
case 3:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function (_x2) {
return _ref3.apply(this, arguments);
};
}());
case 3:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
function play(_x) {
return _play.apply(this, arguments);
}
return play;
}()
};
var project = {
runStep: function runStep(label, play, context) {
return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
return _context3.abrupt("return", play(context));
case 1:
case "end":
return _context3.stop();
}
}
}, _callee3);
}))();
}

@@ -165,0 +234,0 @@ }; // NOTE Jest forced to define at least one test in file

{
"name": "@storybook/csf",
"version": "0.0.2--canary.50.794a0bf.0",
"version": "0.0.2--canary.51.768ca23.0",
"description": "Component Story Format (CSF) utilities",

@@ -41,6 +41,10 @@ "keywords": [

"jest": {
"testEnvironment": "node"
"testEnvironment": "node",
"roots": [
"<rootDir>/src"
]
},
"dependencies": {
"lodash": "^4.17.15"
"lodash": "^4.17.15",
"type-fest": "^2.19.0"
},

@@ -62,4 +66,4 @@ "devDependencies": {

"jest": "^24.9.0",
"prettier": "^1.19.1",
"typescript": "^3.7.2"
"prettier": "^2.7.1",
"typescript": "^4.8.4"
},

@@ -66,0 +70,0 @@ "auto": {