Best JavaScript code snippet using storybook-root
parse.js
Source:parse.js
1import absolutize from "./absolutize.js";2import reduce from "./reduce.js";3const commandsMap = {Z: "Z", M: "M", L: "L", C: "C", Q: "Q", A: "A", H: "H", V: "V", S: "S", T: "T", z: "Z", m: "m", l: "l", c: "c", q: "q", a: "a", h: "h", v: "v", s: "s", t: "t"};4export default function parse(string, {normalize = false} = {}) {5 if (!(string += "") || string.length === 0) return [];6 const pathData = [];7 let currentIndex = 0;8 let endIndex = string.length;9 let prevCommand = null;10 skipOptionalSpaces();11 function parseSegment() {12 var char = string[currentIndex];13 var command = commandsMap[char] ? commandsMap[char] : null;14 if (command === null) {15 // Possibly an implicit command. Not allowed if this is the first command.16 if (prevCommand === null) {17 return null;18 }19 // Check for remaining coordinates in the current command.20 if (21 (char === "+" || char === "-" || char === "." || (char >= "0" && char <= "9")) && prevCommand !== "Z"22 ) {23 if (prevCommand === "M") {24 command = "L";25 }26 else if (prevCommand === "m") {27 command = "l";28 }29 else {30 command = prevCommand;31 }32 }33 else {34 command = null;35 }36 if (command === null) {37 return null;38 }39 }40 else {41 currentIndex += 1;42 }43 prevCommand = command;44 var values = null;45 var cmd = command.toUpperCase();46 if (cmd === "H" || cmd === "V") {47 values = [parseNumber()];48 }49 else if (cmd === "M" || cmd === "L" || cmd === "T") {50 values = [parseNumber(), parseNumber()];51 }52 else if (cmd === "S" || cmd === "Q") {53 values = [parseNumber(), parseNumber(), parseNumber(), parseNumber()];54 }55 else if (cmd === "C") {56 values = [57 parseNumber(),58 parseNumber(),59 parseNumber(),60 parseNumber(),61 parseNumber(),62 parseNumber()63 ];64 }65 else if (cmd === "A") {66 values = [67 parseNumber(),68 parseNumber(),69 parseNumber(),70 parseArcFlag(),71 parseArcFlag(),72 parseNumber(),73 parseNumber()74 ];75 }76 else if (cmd === "Z") {77 skipOptionalSpaces();78 values = [];79 }80 if (values === null || values.indexOf(null) >= 0) {81 // Unknown command or known command with invalid values82 return null;83 }84 else {85 return {type: command, values: values};86 }87 }88 function hasMoreData() {89 return currentIndex < endIndex;90 }91 function peekSegmentType() {92 var char = string[currentIndex];93 return commandsMap[char] ? commandsMap[char] : null;94 }95 function initialCommandIsMoveTo() {96 // If the path is empty it is still valid, so return true.97 if (!hasMoreData()) {98 return true;99 }100 var command = peekSegmentType();101 // Path must start with moveTo.102 return command === "M" || command === "m";103 }104 function isCurrentSpace() {105 var char = string[currentIndex];106 return char <= " " && (char === " " || char === "\n" || char === "\t" || char === "\r" || char === "\f");107 }108 function skipOptionalSpaces() {109 while (currentIndex < endIndex && isCurrentSpace()) {110 currentIndex += 1;111 }112 return currentIndex < endIndex;113 }114 function skipOptionalSpacesOrDelimiter() {115 if (116 currentIndex < endIndex &&117 !isCurrentSpace() &&118 string[currentIndex] !== ","119 ) {120 return false;121 }122 if (skipOptionalSpaces()) {123 if (currentIndex < endIndex && string[currentIndex] === ",") {124 currentIndex += 1;125 skipOptionalSpaces();126 }127 }128 return currentIndex < endIndex;129 }130 // Parse a number from an SVG path. This very closely follows genericParseNumber(...) from131 // Source/core/svg/SVGParserUtilities.cpp.132 // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-PathDataBNF133 function parseNumber() {134 var exponent = 0;135 var integer = 0;136 var frac = 1;137 var decimal = 0;138 var sign = 1;139 var expsign = 1;140 var startIndex = currentIndex;141 skipOptionalSpaces();142 // Read the sign.143 if (currentIndex < endIndex && string[currentIndex] === "+") {144 currentIndex += 1;145 }146 else if (currentIndex < endIndex && string[currentIndex] === "-") {147 currentIndex += 1;148 sign = -1;149 }150 if (151 currentIndex === endIndex ||152 (153 (string[currentIndex] < "0" || string[currentIndex] > "9") &&154 string[currentIndex] !== "."155 )156 ) {157 // The first character of a number must be one of [0-9+-.].158 return null;159 }160 // Read the integer part, build right-to-left.161 var startIntPartIndex = currentIndex;162 while (163 currentIndex < endIndex &&164 string[currentIndex] >= "0" &&165 string[currentIndex] <= "9"166 ) {167 currentIndex += 1; // Advance to first non-digit.168 }169 if (currentIndex !== startIntPartIndex) {170 var scanIntPartIndex = currentIndex - 1;171 var multiplier = 1;172 while (scanIntPartIndex >= startIntPartIndex) {173 integer += multiplier * (string[scanIntPartIndex] - "0");174 scanIntPartIndex -= 1;175 multiplier *= 10;176 }177 }178 // Read the decimals.179 if (currentIndex < endIndex && string[currentIndex] === ".") {180 currentIndex += 1;181 // There must be a least one digit following the .182 if (183 currentIndex >= endIndex ||184 string[currentIndex] < "0" ||185 string[currentIndex] > "9"186 ) {187 return null;188 }189 while (190 currentIndex < endIndex &&191 string[currentIndex] >= "0" &&192 string[currentIndex] <= "9"193 ) {194 frac *= 10;195 decimal += (string.charAt(currentIndex) - "0") / frac;196 currentIndex += 1;197 }198 }199 // Read the exponent part.200 if (201 currentIndex !== startIndex &&202 currentIndex + 1 < endIndex &&203 (string[currentIndex] === "e" || string[currentIndex] === "E") &&204 (string[currentIndex + 1] !== "x" && string[currentIndex + 1] !== "m")205 ) {206 currentIndex += 1;207 // Read the sign of the exponent.208 if (string[currentIndex] === "+") {209 currentIndex += 1;210 }211 else if (string[currentIndex] === "-") {212 currentIndex += 1;213 expsign = -1;214 }215 // There must be an exponent.216 if (217 currentIndex >= endIndex ||218 string[currentIndex] < "0" ||219 string[currentIndex] > "9"220 ) {221 return null;222 }223 while (224 currentIndex < endIndex &&225 string[currentIndex] >= "0" &&226 string[currentIndex] <= "9"227 ) {228 exponent *= 10;229 exponent += (string[currentIndex] - "0");230 currentIndex += 1;231 }232 }233 var number = integer + decimal;234 number *= sign;235 if (exponent) {236 number *= Math.pow(10, expsign * exponent);237 }238 if (startIndex === currentIndex) {239 return null;240 }241 skipOptionalSpacesOrDelimiter();242 return number;243 }244 function parseArcFlag() {245 if (currentIndex >= endIndex) {246 return null;247 }248 var flag = null;249 var flagChar = string[currentIndex];250 currentIndex += 1;251 if (flagChar === "0") {252 flag = 0;253 }254 else if (flagChar === "1") {255 flag = 1;256 }257 else {258 return null;259 }260 skipOptionalSpacesOrDelimiter();261 return flag;262 }263 if (initialCommandIsMoveTo()) {264 while (hasMoreData()) {265 const pathSeg = parseSegment();266 if (pathSeg === null) break;267 pathData.push(pathSeg);268 }269 }270 return normalize ? reduce(absolutize(pathData)) : pathData;...
script.js
Source:script.js
1//arrays:2let grassCute = ["img/bulbasaur.jpg", "img/tangela.jpg", "img/bellsprout.jpg"];3let grassScary = ["img/rillaboom.jpg", "img/rayquaza.jpg", "img/zarude.jpg"];4let fireWeak = ["img/charmeleon.jpg", "img/litten.jpg", "img/vulpix.jpg"];5let fireStrong = ["img/blaziken.jpg", "img/emboar.jpg", "img/typhlosion.jpg"];6let waterSmall = ["img/dratini.jpg", "img/poliwhirl.jpg", "img/squirtle.jpg"];7let waterBig = ["img/greninja.jpg", "img/kygore.jpg", "img/swampert.jpg"];8let currentIndex = 2;9let carouselIMG = document.getElementById("carouselIMG");10let groupSelect = document.getElementById("groupSelect");11let categorySelect= document.getElementById("categorySelect");12let grassOptions = ['Cute', 'Scary'];13let fireOptions = ['Weak', 'Strong'];14let waterOptions = ['Small', 'Big'];15//carousels:16let changeGrassCute = function(){17 if(currentIndex === 0){18 carouselIMG.src = grassCute[1];19 currentIndex = 1;20 }21 else if(currentIndex === 1){22 carouselIMG.src = grassCute[2];23 currentIndex = 2;24 }25 else {26 carouselIMG.src = grassCute[0];27 currentIndex = 0;28 }29 if (currentIndex === 2){30 currentIndex=0;31 }32 else{33 currentIndex = currentIndex+1;34 }35}36let changeGrassScary = function(){37 if(currentIndex === 0){38 carouselIMG.src = grassScary[1];39 currentIndex = 1;40 }41 else if(currentIndex === 1){42 carouselIMG.src = grassScary[2];43 currentIndex = 2;44 }45 else {46 carouselIMG.src = grassScary[0];47 currentIndex = 0;48 }49 if (currentIndex === 2){50 currentIndex=0;51 }52 else{53 currentIndex = currentIndex+1;54 }55}56let changeWaterBig = function(){57 if(currentIndex === 0){58 carouselIMG.src = waterBig[1];59 currentIndex = 1;60 }61 else if(currentIndex === 1){62 carouselIMG.src = waterBig[2];63 currentIndex = 2;64 }65 else {66 carouselIMG.src = waterBig[0];67 currentIndex = 0;68 }69 if (currentIndex === 2){70 currentIndex=0;71 }72 else{73 currentIndex = currentIndex+1;74 }75}76let changeWaterSmall = function(){77 if(currentIndex === 0){78 carouselIMG.src = waterSmall[1];79 currentIndex = 1;80 }81 else if(currentIndex === 1){82 carouselIMG.src = waterSmall[2];83 currentIndex = 2;84 }85 else {86 carouselIMG.src = waterSmall[0];87 currentIndex = 0;88 }89 if (currentIndex === 2){90 currentIndex=0;91 }92 else{93 currentIndex = currentIndex+1;94 }95}96let changeFireWeak = function(){97 if(currentIndex === 0){98 carouselIMG.src = fireWeak[1];99 currentIndex = 1;100 }101 else if(currentIndex === 1){102 carouselIMG.src = fireWeak[2];103 currentIndex = 2;104 }105 else {106 carouselIMG.src = fireWeak[0];107 currentIndex = 0;108 }109 if (currentIndex === 2){110 currentIndex=0;111 }112 else{113 currentIndex = currentIndex+1;114 }115}116let changeFireStrong = function(){117 if(currentIndex === 0){118 carouselIMG.src = fireStrong[1];119 currentIndex = 1;120 }121 else if(currentIndex === 1){122 carouselIMG.src = fireStrong[2];123 currentIndex = 2;124 }125 else {126 carouselIMG.src = fireStrong[0];127 currentIndex = 0;128 }129 if (currentIndex === 2){130 currentIndex=0;131 }132 else{133 currentIndex = currentIndex+1;134 }135}136//category137let updateCategory = function(){138 console.log(groupSelect);139 categorySelect.innerHTML = '';140 let selectedValue = groupSelect.value;141 if(selectedValue === "grass"){142 grassOptions.forEach(function(elem){143 categorySelect.innerHTML += '<option value="' + elem + '">' + elem + '</option>';144 });145 }146 else if(selectedValue === "fire"){147 fireOptions.forEach(function(elem){148 categorySelect.innerHTML += '<option value="' + elem + '">' + elem + '</option>';149 });150 }151 else if(selectedValue === "water"){152 waterOptions.forEach(function(elem){153 categorySelect.innerHTML += '<option value="' + elem + '">' + elem + '</option>';154 });155 } 156}157//category update158let toggleCarousel = function(){159 let selectedGroup = groupSelect.value;160 let selectedCategory = categorySelect.value;161 if (selectedCategory === "Cute"){162 console.log("cute grass"); 163 changeGrassCute();164 }165 else if (selectedCategory === "Scary"){166 console.log("scary grass");167 changeGrassScary();168 }169 else if (selectedCategory === "Small"){170 console.log("small water");171 changeWaterSmall(); 172 }173 else if (selectedCategory === "Big"){174 console.log("big water");175 changeWaterBig();176 }177 else if (selectedCategory === "Weak"){178 console.log("weak fire");179 changeFireWeak();180 }181 182 else if (selectedCategory === "Strong"){183 console.log("weak fire");184 changeFireWeak();185 }186}187//events188window.addEventListener("load", function(){189 setInterval(toggleCarousel, 4000);190})191groupSelect.addEventListener("change", updateCategory);...
NextPrevious.js
Source:NextPrevious.js
1import React from 'react';2import Link from './link';3import { StyledNextPrevious } from './styles/PageNavigationButtons';4const NextPrevious = ({ mdx, nav }) => {5 let currentIndex;6 const currentPaginationInfo = nav.map((el, index) => {7 if (el && el.url === mdx.fields.slug) {8 currentIndex = index;9 }10 });11 const nextInfo = {};12 const previousInfo = {};13 if (currentIndex === undefined) {14 // index15 if (nav[0]) {16 nextInfo.url = nav[0].url;17 nextInfo.title = nav[0].title;18 }19 previousInfo.url = null;20 previousInfo.title = null;21 currentIndex = -1;22 } else if (currentIndex === 0) {23 // first page24 nextInfo.url = nav[currentIndex + 1] ? nav[currentIndex + 1].url : null;25 nextInfo.title = nav[currentIndex + 1] ? nav[currentIndex + 1].title : null;26 previousInfo.url = null;27 previousInfo.title = null;28 } else if (currentIndex === nav.length - 1) {29 // last page30 nextInfo.url = null;31 nextInfo.title = null;32 previousInfo.url = nav[currentIndex - 1] ? nav[currentIndex - 1].url : null;33 previousInfo.title = nav[currentIndex - 1] ? nav[currentIndex - 1].title : null;34 } else if (currentIndex) {35 // any other page36 nextInfo.url = nav[currentIndex + 1].url;37 nextInfo.title = nav[currentIndex + 1].title;38 if (nav[currentIndex - 1]) {39 previousInfo.url = nav[currentIndex - 1].url;40 previousInfo.title = nav[currentIndex - 1].title;41 }42 }43 return (44 <StyledNextPrevious>45 {previousInfo.url && currentIndex >= 0 ? (46 <Link to={nav[currentIndex - 1].url} className={'previousBtn'}>47 <div className={'leftArrow'}>48 <svg49 preserveAspectRatio="xMidYMid meet"50 height="1em"51 width="1em"52 fill="none"53 xmlns="http://www.w3.org/2000/svg"54 viewBox="0 0 24 24"55 strokeWidth="2"56 strokeLinecap="round"57 strokeLinejoin="round"58 stroke="currentColor"59 className="_13gjrqj"60 >61 <g>62 <line x1="19" y1="12" x2="5" y2="12" />63 <polyline points="12 19 5 12 12 5" />64 </g>65 </svg>66 </div>67 <div className={'preRightWrapper'}>68 <div className={'smallContent'}>69 <span>Previous</span>70 </div>71 <div className={'nextPreviousTitle'}>72 <span>{nav[currentIndex - 1].title}</span>73 </div>74 </div>75 </Link>76 ) : null}77 {nextInfo.url && currentIndex >= 0 ? (78 <Link to={nav[currentIndex + 1].url} className={'nextBtn'}>79 <div className={'nextRightWrapper'}>80 <div className={'smallContent'}>81 <span>Next</span>82 </div>83 <div className={'nextPreviousTitle'}>84 <span>{nav[currentIndex + 1] && nav[currentIndex + 1].title}</span>85 </div>86 </div>87 <div className={'rightArrow'}>88 <svg89 preserveAspectRatio="xMidYMid meet"90 height="1em"91 width="1em"92 fill="none"93 xmlns="http://www.w3.org/2000/svg"94 viewBox="0 0 24 24"95 strokeWidth="2"96 strokeLinecap="round"97 strokeLinejoin="round"98 stroke="currentColor"99 className="_13gjrqj"100 >101 <g>102 <line x1="5" y1="12" x2="19" y2="12" />103 <polyline points="12 5 19 12 12 19" />104 </g>105 </svg>106 </div>107 </Link>108 ) : null}109 </StyledNextPrevious>110 );111};...
Using AI Code Generation
1describe('test', () => {2 it('test', () => {3 });4});5describe('test', () => {6 it('test', () => {7 currentIndex = 1;8 console.log(currentIndex);9 expect(currentIndex).toBe(1);10 });11});12describe('test', () => {13 it('test', () => {14 currentIndex = 1;15 console.log(currentIndex);16 expect(currentIndex).toBe(1);17 });18});19describe('test', () => {20 it('test', () => {21 currentIndex = 1;22 console.log(currentIndex);23 expect(currentIndex).toBe(1);24 });25});
Using AI Code Generation
1export const getCarouselIndex = async () => {2 const storybookRoot = await $(`storybook-root`);3 const index = await storybookRoot.getCurrentIndex();4 return index;5};6export const nextCarouselSlide = async () => {7 const storybookRoot = await $(`storybook-root`);8 await storybookRoot.next();9};10export const previousCarouselSlide = async () => {11 const storybookRoot = await $(`storybook-root`);12 await storybookRoot.previous();13};14export const goToSlide = async (index) => {15 const storybookRoot = await $(`storybook-root`);16 await storybookRoot.goToSlide(index);17};18export const getSlidesCount = async () => {19 const storybookRoot = await $(`storybook-root`);20 const count = await storybookRoot.getSlidesCount();21 return count;22};23export const getSlideWidth = async () => {24 const storybookRoot = await $(`storybook-root`);25 const width = await storybookRoot.getSlideWidth();26 return width;27};28export const getSlideHeight = async () => {29 const storybookRoot = await $(`storybook-root`);
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!!