Best JavaScript code snippet using storybook-root
navigation.js
Source:navigation.js
1define(['jquery', 'backbone'], function (jquery, Backbone) {2 3 var Navigation = function() {4 this.d = $.Deferred();5 this.breadcrumbs = new Backbone.Collection();6 this.routers = {};7 this.tree = {};8 var that = this;9 this.appendRouter = function(router) {10 if (typeof(router.navigation) != "undefined") {11 _.extend(that.tree, router.navigation.pages);12 that.routers[router.navigation.prefix] = router;13 }14 };15 this.mapRouters = function() {16 for (var i in that.routers) {17 that.routers[i].bind('route', function (route, args) {18 var router = this;19 that.breadcrumbs.reset();20 var url = that._getRouteUrl(router, route);21 var mappedArgs = that._getMappedArgs(args, url);22 that._mapNavigation(router.navigation.prefix + '.' + route, mappedArgs);23 that.getBreadcrumbs();24 });25 }26 };27 this._getMappedArgs = function(args, link) {28 var route = Backbone.Router.prototype._routeToRegExp(link);29 var argNames = Backbone.Router.prototype._extractParameters(route, link);30 var namedArgs = {};31 for (var i in argNames) {32 namedArgs[argNames[i]] = args[i];33 }34 return namedArgs;35 };36 this._getMappedUrl = function(url, mappedArgs) {37 for (var argName in mappedArgs) {38 var arg = mappedArgs[argName];39 url = url.replace(argName, arg);40 }41 return url;42 };43 this._getProcessedText = function(template, mappedArgs) {44 if (typeof(template) == "function") {45 // return template({ args: mappedArgs });46 return template(mappedArgs).done(function(newText) {47 });48 }49 return template;50 };51 this._mapNavigation = function(route, mappedArgs) {52 if (!that.tree[route]) {53 return;54 }55 56 var isFunction = typeof(that.tree[route].template) == "function";57 var routeParts = route.split('.');58 var router = that.routers[routeParts[0]];59 var shortRoute = routeParts[1];60 var page = new Backbone.Model({61 url: this._getMappedUrl(this._getRouteUrl(router, shortRoute), mappedArgs),62 text: isFunction ? that.tree[route].template(mappedArgs): that.tree[route].template,63 mappedArgs: mappedArgs,64 isResolved: false65 });66 var isDeferred = (typeof(page.get('text') == 'Object') && page.get('text').resolve);67 if (isDeferred) {68 page.get('text').done(function(template){69 page.set({'text': template, 'isResolved': true});70 that.checkBreadcrumbs();71 });72 } else {73 page.set('isResolved', true);74 }75 this.breadcrumbs.unshift(page);76 if (typeof(that.tree[route].parent) != "undefined") {77 this._mapNavigation(that.tree[route].parent, mappedArgs);78 }79 };80 this._getRouteUrl = function(router, route) {81 for (var url in router.routes) {82 if (route == router.routes[url]) {83 return url;84 }85 }86 };87 this.getBreadcrumbs = function() {88 this.checkBreadcrumbs();89 return this.d;90 };91 this.isAllPagesResolved = function () {92 for (var i = 0; i < this.breadcrumbs.size(); i++) {93 if (!this.breadcrumbs.at(i).get('isResolved')) {94 return false;95 }96 }97 return true;98 };99 this.checkBreadcrumbs = function() {100 if (this.isAllPagesResolved() && this.breadcrumbs.size() > 0) {101 this.d.resolve(this.breadcrumbs);102 }103 };104 };105 return Navigation;...
electron.js
Source:electron.js
1//https://dev.to/mandiwise/electron-apps-made-easy-with-create-react-app-and-electron-forge-560e2const path = require("path");3const fs = require("fs");4const { app, BrowserWindow } = require("electron");5const isDev = require("electron-is-dev");6const convertScript = require('funscript-utils').getHalfSpeedScript;7if(process.argv.findIndex(s => s === "-i") !== -1) {8 const allArgs = process.argv.slice(1);9 //map the args to key-value pairs10 const mappedArgs = {};11 for(let i = 0; i < allArgs.length; i++) {12 if(allArgs[i].substr(0, 1) === "-") {13 if(allArgs[i + 1] && allArgs[i + 1].substr(0, 1) !== "-") {14 mappedArgs[allArgs[i].substr(1)] = allArgs[i + 1];15 } else {16 mappedArgs[allArgs[i].substr(1)] = true;17 }18 }19 }20 //combine default options with passed-in options JSON21 const options = {22 resetAfterPause: false,23 removeShortPauses: true,24 matchFirstDownstroke: false,25 matchGroupEndPosition: true,26 shortPauseDuration: 2000,27 debugMode: false,28 };29 if(mappedArgs.options) {30 mappedArgs.options = JSON.parse(mappedArgs.options);31 Object.keys(mappedArgs.options).forEach(key => {32 options[key] = mappedArgs.options[key];33 })34 }35 if(!mappedArgs.i || !mappedArgs.i.includes(".funscript")) {36 console.log("Input funscript file path required");37 } else {38 //read source funscript file39 fs.readFile(mappedArgs.i, "utf8", (err, data) => {40 if(err) {41 console.log("Failed reading source script: ", err);42 return;43 }44 if(!mappedArgs.o) mappedArgs.o = mappedArgs.i.replace(".funscript", "_HALF.funscript");45 //convert script46 const script = JSON.parse(data);47 const halfScript = convertScript(script, options);48 //save output file49 fs.writeFile(mappedArgs.o, JSON.stringify(halfScript), (err, data) => {50 if(err) {51 console.log("Failed writing target script: ", err);52 return;53 }54 console.log("Half-speed script saved successfully");55 })56 })57 }58 app.quit();59} else {60 console.log("Running FunHalver not in command-line mode - no -i flag found");61}62if(require("electron-squirrel-startup")) {63 app.quit();64}65function createWindow() {66 // Create the browser window.67 const win = new BrowserWindow({68 width: 1280,69 height: 720,70 webPreferences: {71 nodeIntegration: true72 },73 resizable: false,74 fullscreenable: false,75 title: "FunHalver",76 titleBarStyle: "hiddenInset",77 });78 win.setMenuBarVisibility(false);79 // and load the index.html of the app.80 // win.loadFile("index.html");81 win.loadURL(82 isDev83 ? "http://localhost:3000"84 : `file://${path.join(__dirname, "../build/index.html")}`85 );86 // Open the DevTools.87 if (isDev) {88 win.webContents.openDevTools({ mode: "detach" });89 }90}91// This method will be called when Electron has finished92// initialization and is ready to create browser windows.93// Some APIs can only be used after this event occurs.94app.whenReady().then(createWindow);95// Quit when all windows are closed, except on macOS. There, it's common96// for applications and their menu bar to stay active until the user quits97// explicitly with Cmd + Q.98app.on("window-all-closed", () => {99 if (process.platform !== "darwin") {100 app.quit();101 }102});103app.on("activate", () => {104 // On macOS it's common to re-create a window in the app when the105 // dock icon is clicked and there are no other windows open.106 if (BrowserWindow.getAllWindows().length === 0) {107 createWindow();108 }109});110// In this file you can include the rest of your app's specific main process...
get-glamor-classname.js
Source:get-glamor-classname.js
1import {css, styleSheet} from 'glamor'2/**3 * This function takes a className string and gets all the4 * associated glamor styles. It's used to merge glamor styles5 * from a className to make sure that specificity is not6 * a problem when passing a className to a component.7 * @param {String} [className=''] the className string8 * @return {Object} { glamorStyles, glamorlessClassName }9 * - glamorStyles is an array of all the glamor styles objects10 * - glamorlessClassName is the rest of the className string11 * without the glamor classNames12 */13function extractGlamorStyles(className) {14 const glamorlessClassName = []15 const glamorStyles = []16 className17 .toString()18 .split(' ')19 .forEach(name => {20 if (styleSheet.registered[name.substring(4)] === undefined) {21 glamorlessClassName.push(name)22 } else {23 const style = buildGlamorSrcFromClassName(name)24 glamorStyles.push(style)25 }26 })27 return {glamorlessClassName, glamorStyles}28}29/** Glamor's css function returns an object with the shape30 *31 * {32 * [`data-css-${hash}`]: '',33 * toString() { return `css-${hash}` }34 * }35 *36 * Whenever glamor's build function encounters an object with37 * this shape it just pulls the resulting styles from the cache.38 *39 * note: the toString method is not needed to qualify the shape40 **/41function buildGlamorSrcFromClassName(className) {42 return {[`data-${className}`]: ''}43}44export default getGlamorClassName45function getGlamorClassName({46 styles,47 props,48 cssOverrides,49 cssProp,50 context,51 displayName,52}) {53 const {mappedArgs, nonGlamorClassNames} = handleStyles(54 [...styles, props.className, cssOverrides, cssProp],55 props,56 context,57 )58 // eslint-disable-next-line max-len59 const isDev = process.env.NODE_ENV === 'development' || !process.env.NODE_ENV60 const devRules = isDev ? {label: displayName} : null61 const glamorClassName = css(devRules, ...mappedArgs).toString()62 const extras = nonGlamorClassNames.join(' ').trim()63 return `${glamorClassName} ${extras}`.trim()64}65// this next function is on a "hot" code-path66// so it's pretty complex to make sure it's fast.67// eslint-disable-next-line complexity68function handleStyles(styles, props, context) {69 let current70 const mappedArgs = []71 const nonGlamorClassNames = []72 for (let i = 0; i < styles.length; i++) {73 current = styles[i]74 while (typeof current === 'function') {75 current = current(props, context)76 }77 if (typeof current === 'string') {78 const {glamorStyles, glamorlessClassName} = extractGlamorStyles(current)79 mappedArgs.push(...glamorStyles)80 nonGlamorClassNames.push(...glamorlessClassName)81 } else if (Array.isArray(current)) {82 const recursed = handleStyles(current, props, context)83 mappedArgs.push(...recursed.mappedArgs)84 nonGlamorClassNames.push(...recursed.nonGlamorClassNames)85 } else {86 mappedArgs.push(current)87 }88 }89 return {mappedArgs, nonGlamorClassNames}...
Using AI Code Generation
1const { mappedArgs } = require('storybook-root');2module.exports = {3 argTypes: {4 backgroundColor: { control: 'color' },5 },6};7const Template = (args) => <Example {...mappedArgs(args)} />;8export const Primary = Template.bind({});9Primary.args = {10};11export const Secondary = Template.bind({});12Secondary.args = {13};14export const Large = Template.bind({});15Large.args = {16};17export const Small = Template.bind({});18Small.args = {19};20import React from 'react';21import PropTypes from 'prop-types';22import './styles.css';23const Example = ({ primary, backgroundColor, size, label }) => {24 const mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';25 return (26 className={['storybook-button', `storybook-button--${size}`, mode].join(' ')}27 style={{ backgroundColor }}28 {label}29 );30};31Example.propTypes = {32 size: PropTypes.oneOf(['small', 'medium', 'large']),33};34Example.defaultProps = {35};36export default Example;37@import '../../styles/variables.css';38.storybook-button {39 border: 0;40 border-radius: 3em;41 cursor: pointer;42 font-family: var(--font-family);43 font-size: 1rem;44 font-weight: 700;45 line-height: 1;46 padding: 0.75em 1.5em;47 text-align: center;48 text-transform: uppercase;49 transition: all 150ms ease-out;50 white-space: nowrap;51 &--small {52 font-size: 0.75rem;
Using AI Code Generation
1import { mappedArgs } from 'storybook-root';2export default {3 argTypes: {4 test: { control: 'text' },5 },6 args: {7 test: mappedArgs('test'),8 },9};10export const Default = (args) => <Test {...args} />;11Default.args = {12};13import { addDecorator } from '@storybook/react';14import { withMappedArgs } from 'storybook-root';15addDecorator(withMappedArgs);
Using AI Code Generation
1const mappedArgs = require('storybook-root').mappedArgs;2const { name, age } = mappedArgs(['name', 'age']);3const mappedArgs = require('storybook-root').mappedArgs;4const { name, age } = mappedArgs(['name', 'age']);5const mappedArgs = require('storybook-root').mappedArgs;6const { name, age } = mappedArgs(['name', 'age']);7const mappedArgs = require('storybook-root').mappedArgs;8const { name, age } = mappedArgs(['name', 'age']);9const mappedArgs = require('storybook-root').mappedArgs;10const { name, age } = mappedArgs(['name', 'age']);11const mappedArgs = require('storybook-root').mappedArgs;12const { name, age } = mappedArgs(['name', 'age']);13const mappedArgs = require('storybook-root').mappedArgs;14const { name, age } = mappedArgs(['name', 'age']);15const mappedArgs = require('storybook-root').mappedArgs;16const { name, age } = mappedArgs(['name', 'age']);17const mappedArgs = require('storybook-root').mappedArgs;18const { name, age } = mappedArgs(['name', 'age']);19const mappedArgs = require('storybook-root').mappedArgs;20const { name, age } = mappedArgs(['name', 'age']);21const mappedArgs = require('storybook-root').mappedArgs;22const { name, age } = mappedArgs(['name', 'age']);23const mappedArgs = require('storybook-root').mappedArgs;24const { name, age } = mappedArgs(['name', 'age']);25const mappedArgs = require('storybook-root').mappedArgs;26const { name, age } = mappedArgs(['name', 'age']);
Using AI Code Generation
1import { mappedArgs } from 'storybook-root';2const { arg1, arg2 } = mappedArgs();3import { addParameters } from '@storybook/react';4addParameters({5 mappedArgs: {6 },7});8import { mappedArgs } from 'storybook-root';9const { arg1, arg2 } = mappedArgs();10export const MyComponent = (args) => {11 return <MyComponent {...args} />12}13MyComponent.args = {14}
Using AI Code Generation
1const { mappedArgs } = require('storybook-root');2const { args } = mappedArgs('test');3const { args } = require('storybook-root');4const { mappedArgs } = require('storybook-root');5const { args } = mappedArgs('test');6const { args } = require('storybook-root');7const { mappedArgs } = require('storybook-root');8const { args } = mappedArgs('test');9const { args } = require('storybook-root');10const { mappedArgs } = require('storybook-root');11const { args } = mappedArgs('test');12const { args } = require('storybook-root');13const { mappedArgs } = require('storybook-root');14const { args } = mappedArgs('test');15const { args } = require('storybook-root');16const { mappedArgs } = require('storybook-root');17const { args } = mappedArgs('test');18const { args } = require('storybook-root');19const { mappedArgs } = require('storybook-root');20const { args } = mappedArgs('test');21const { args } = require('storybook-root');22const { mappedArgs } = require('storybook-root');23const { args } = mappedArgs('test');24const { args } = require('storybook-root');25const { mappedArgs } = require('storybook-root');26const { args } = mappedArgs('test');27const { args } = require('storybook-root');28const { mappedArgs } = require('storybook-root');29const {
Using AI Code Generation
1import { mappedArgs } from 'storybook-root';2const args = mappedArgs();3export default {4 args: {5 },6};7export const TestStory = (args) => <Test {...args} />;8TestStory.storyName = 'Test';9const Test = ({ name, age }) => {10 return (11 <span>{name}</span>12 <span>{age}</span>13 );14};15export default Test;
Using AI Code Generation
1import { mappedArgs } from 'storybook-root';2const { args } = mappedArgs();3export const MyStory = (args) => {4 return <MyComponent {...args} />;5};6MyStory.parameters = {7 mappedArgs: {8 args: {9 },10 },11};12export const MyStory = (args) => {13 return <MyComponent {...args} />;14};15MyStory.parameters = {16 mappedArgs: {17 args: {18 },19 },20};21export const MyStory = (args) => {22 return <MyComponent {...args} />;23};24MyStory.parameters = {25 mappedArgs: {26 args: {27 },28 },29};30export const MyStory = (args) => {31 return <MyComponent {...args} />;32};33MyStory.parameters = {34 mappedArgs: {35 args: {36 },37 },38};39export const MyStory = (args) => {40 return <MyComponent {...args} />;41};42MyStory.parameters = {43 mappedArgs: {44 args: {45 },46 },47};48export const MyStory = (args) => {49 return <MyComponent {...args} />;50};51MyStory.parameters = {52 mappedArgs: {53 args: {54 },55 },56};57export const MyStory = (args) => {58 return <MyComponent {...args} />;59};60MyStory.parameters = {61 mappedArgs: {62 args: {63 },64 },65};
Using AI Code Generation
1const mappedArgs = storybookRoot.mappedArgs();2const mappedArgs = storybookRoot.mappedArgs();3const mappedArgs = storybookRoot.mappedArgs();4const mappedArgs = storybookRoot.mappedArgs();5const mappedArgs = storybookRoot.mappedArgs();6const mappedArgs = storybookRoot.mappedArgs();7const mappedArgs = storybookRoot.mappedArgs();8const mappedArgs = storybookRoot.mappedArgs();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!