Best JavaScript code snippet using tracetest
parser.ts
Source:parser.ts
1/* eslint-disable no-continue */2import Renderer from './renderer';3import SapDocFile from './sapDocFile';4const Entities = require('html-entities').AllHtmlEntities;5const entities = new Entities();6interface Header {7 title: string;8 isMainTitle: boolean,9 render: (renderer: Renderer) => void;10}11export default class Parser {12 $: CheerioStatic;13 renderer: Renderer;14 allFiles: SapDocFile[];15 file: SapDocFile;16 constructor(file: SapDocFile, renderer: Renderer, allFiles: Array<SapDocFile>) {17 this.$ = file.cheerio;18 this.file = file;19 this.renderer = renderer;20 this.allFiles = allFiles;21 }22 parse(): string {23 const path: CheerioElement = this.$('.path')[0];24 if (path) {25 this.parsePath(path);26 }27 const root: CheerioElement = this.$('.all')[0];28 if (root === undefined) {29 // If there isn't a .all root, it's probably a old page. Just parse every thing as is30 this.parseText(this.$('body').html()!);31 } else {32 for (let index = 0; index < root.children.length; index++) {33 if (this.isBlock(root.children[index])) {34 this.parseBlock(root.children[index]);35 break;36 }37 }38 }39 return this.renderer.getContents();40 }41 parsePath(path: CheerioElement) {42 let pathHTML = this.$(path).html()!;43 // Remove last arrow44 pathHTML = pathHTML.replace(/ș([^_]*)$/, '');45 this.parseText(`<small>${pathHTML}</small>`);46 }47 parseBlock(element: CheerioElement) {48 const blockElements: Array<CheerioElement> = [];49 blockElements.push(element);50 let { next } = element;51 // eslint-disable-next-line no-constant-condition52 while (true) {53 blockElements.push(next);54 next = next.next;55 if (!next) { break; }56 if (this.isBlock(next)) { break; }57 }58 this.parseBlockElements(blockElements);59 if (next) { this.parseBlock(next); }60 }61 parseBlockElements(blockElements: CheerioElement[]) {62 const headerElement: Cheerio = this.$(blockElements[0]);63 const header = this.determineHeader(headerElement);64 switch (header.title) {65 case 'Syntax':66 header.render(this.renderer);67 this.parseSyntaxBlock(blockElements);68 break;69 case 'Note':70 case 'Notes':71 this.parseAdmonition('note', header.title, blockElements);72 break;73 case 'Example':74 this.parseAdmonition('example', header.title, blockElements);75 break;76 case 'Catchable Exceptions':77 case 'Non-Catchable Exceptions':78 this.parseAdmonition('tip', header.title, blockElements);79 break;80 default:81 header.render(this.renderer);82 this.regularParseBlockElements(blockElements);83 break;84 }85 if (header.isMainTitle) {86 this.parseVersioning();87 }88 }89 parseVersioning() {90 // Example:91 // Versions: <u>7.31</u> [7.40](../cds) [7.54](../cds)92 const version731 = this.renderVersion('7.31', this.findFile('7.31'), `../../7.31/${this.file.name}`);93 const version740 = this.renderVersion('7.40', this.findFile('7.40'), `../../7.40/${this.file.name}`);94 const version754 = this.renderVersion('7.54', this.findFile('7.54'), `../../7.54/${this.file.name}`);95 const versioning = `Other versions: \n ${version731} | ${version740} | ${version754}\n`;96 this.renderer.renderText(versioning);97 }98 findFile(version: string): number {99 const fileName = this.file.name;100 return this.allFiles.findIndex((x: SapDocFile) => x.version === version && x.name === fileName);101 }102 renderVersion(version: string, index: number, link: string) {103 if (version === this.file.version) {104 return `<b>${version}</b>`;105 }106 if (index === -1) {107 return `<s><i>${version}</i></s>`;108 }109 return `[${version}](${link})`;110 }111 parseAdmonition(type: string, title: string, contents: CheerioElement[]) {112 // This break line before an admonition is necessary, otherwise they get113 // wrongly merged by mkdocs. display: block is to reduce its size114 this.parseText('<br style="display: block">');115 this.parseText(`<div markdown="span" class="admonition ${type}">`);116 this.parseText(`<p class="admonition-title">${title}</p>`);117 this.regularParseBlockElements(contents);118 this.parseText('</div>');119 }120 parseSyntaxBlock(blockElements: CheerioElement[]) {121 const parsedElements: string[] = [];122 for (let index = 0; index < blockElements.length; index++) {123 const element = blockElements[index];124 let HTML = this.$(element).html() || '';125 // Skip empty first line126 HTML = this.replaceJavascriptLinks(HTML);127 HTML = HTML.replace(/<br><br>/gm, '');128 if (index === 0 && HTML === '') { continue; }129 if (index === blockElements.length - 1 && HTML === '') { continue; }130 parsedElements.push(HTML);131 }132 this.renderer.renderSyntaxBlock(parsedElements);133 }134 private regularParseBlockElements(blockElements: CheerioElement[]) {135 for (let index = 1; index < blockElements.length; index++) {136 const element = blockElements[index];137 if (this.$(element).is('ul')) {138 this.parseList(element);139 } else if (this.$(element).is('table')) {140 this.parseTable(element);141 } else if (this.$(element).hasClass('qtextml1')) {142 this.parseCodeExample(element);143 } else if (this.$(element).is('a')) {144 // parse outer HTML145 const outerHTML: string = this.$.html(this.$(element))!;146 this.parseText(outerHTML);147 } else if (this.$(element).is('br')) {148 this.parseText('\n');149 } else {150 this.parseText(this.$(element).html()!);151 }152 }153 }154 parseTable(element: CheerioElement) {155 const tableBody = this.$(element).find('tbody');156 let tableHeaderParsed = false;157 let markdownRow;158 let tableRow;159 let tableCell;160 let tableWidth;161 for (let i = 0; i < tableBody.children().length; i++) {162 tableRow = this.$(tableBody.children()[i]);163 tableWidth = tableRow.children().length;164 markdownRow = '';165 for (let j = 0; j < tableWidth; j++) {166 tableCell = this.$(tableRow.children()[j]);167 markdownRow += '|';168 // Don't use HTML in the header, so that we have pure text (e.g. sy-subrc is in a span)169 let cellContent = tableHeaderParsed ? this.$(tableCell).html()! : this.$(tableCell).text()!;170 cellContent = cellContent || '';171 // Remove new lines so that markdown tables do not break172 cellContent = cellContent.replace(/(\r\n|\n|\r)/gm, '');173 markdownRow += cellContent;174 }175 // Closes row176 markdownRow += '|';177 this.parseText(markdownRow);178 if (!tableHeaderParsed) {179 markdownRow = '';180 for (let j = 0; j < tableWidth; j++) {181 markdownRow += '|';182 markdownRow += '----';183 }184 markdownRow += '|';185 tableHeaderParsed = true;186 this.parseText(markdownRow);187 }188 }189 }190 parseList(element: CheerioElement) {191 let ulTag = '<ul>';192 // Standard abap documentation doesn't nest UL,193 // so we need to apply the same CSS style to ident194 if (this.$(element).hasClass('circlem2')) {195 ulTag = '<ul class="circlem2">';196 }197 this.parseText(ulTag);198 let html = this.$(element).html()!;199 if (html) {200 // Remove extra line break201 html = html.replace(/<br>\n<br>/gm, '\n');202 html = html.replace(/<br><br><\/li>/gm, '</li>');203 }204 this.parseText(html);205 this.parseText('</ul>');206 }207 private parseText(text: string) {208 if (!text) { return; }209 let parsedText = text;210 // https://regex101.com/r/1mionM/3211 // Show inline code212 parsedText = parsedText.replace(/<span class="qtext">.*?<\/span>/gm, (matched) => `<code style="display: inline;">${matched}</code>`);213 // Remove extra line break214 parsedText = parsedText.replace(/<br><br>/gm, ' \n ');215 parsedText = this.replaceJavascriptLinks(parsedText);216 this.renderer.renderText(parsedText);217 }218 private determineHeader(element: Cheerio): Header {219 const header: Header = {220 title: '',221 isMainTitle: false,222 // eslint-disable-next-line no-unused-vars223 render(renderer: Renderer) { },224 };225 let headerElement = element.find('.h1');226 let headerTitle = this.$(headerElement).text().trim();227 if (headerTitle !== '') {228 header.title = headerTitle;229 header.isMainTitle = true;230 // eslint-disable-next-line func-names231 header.render = function (renderer: Renderer) { renderer.renderTitle(headerTitle); };232 return header;233 }234 headerElement = element.find('.h2');235 const headerLink = this.$(headerElement).find('a');236 const headerLinkHTML = this.$.html(headerLink);237 headerTitle = this.$(headerElement).text().trim().replace(':', '');238 if (headerTitle !== '') {239 header.title = headerTitle;240 // eslint-disable-next-line func-names241 header.render = function (renderer: Renderer) {242 renderer.renderText(headerLinkHTML);243 renderer.renderH2(headerTitle);244 };245 return header;246 }247 headerElement = element.find('.h3');248 headerTitle = this.$(headerElement).text().trim().replace(':', '');249 if (headerTitle !== '') {250 header.title = headerTitle;251 // eslint-disable-next-line func-names252 header.render = function (renderer: Renderer) { renderer.renderH3(headerTitle); };253 return header;254 }255 headerElement = element.find('.h4');256 headerTitle = this.$(headerElement).text().trim().replace(':', '');257 if (headerTitle !== '') {258 header.title = headerTitle;259 // eslint-disable-next-line func-names260 header.render = function (renderer: Renderer) { renderer.renderH3(headerTitle); };261 return header;262 }263 if (element.hasClass('h4')) {264 headerTitle = this.$(element).text().trim().replace(':', '');265 if (headerTitle !== '') {266 header.title = headerTitle;267 // eslint-disable-next-line func-names268 header.render = function (renderer: Renderer) { renderer.renderH3(headerTitle); };269 return header;270 }271 }272 headerElement = element.find('.bold');273 headerTitle = this.$(headerElement).text().trim().replace(':', '');274 if (headerTitle !== '') {275 header.title = headerTitle;276 // eslint-disable-next-line func-names277 header.render = function (renderer: Renderer) { renderer.renderH3(headerTitle); };278 return header;279 }280 return header;281 }282 private isBlock(element: CheerioElement): boolean {283 const { isHeader } = this;284 if (isHeader(element)) return true;285 const children = (element) ? (element.children || []) : [];286 return children.some((e) => isHeader(e));287 }288 private isHeader(element: CheerioElement): boolean {289 const elementHeader = element || {};290 const attributes = elementHeader.attribs || {};291 const classes = attributes.class || '';292 return classes.split(/\s+/).some((c) => c === 'h1' || c === 'h2' || c === 'h3' || c === 'h4' || c === 'h5' || c === 'bold');293 }294 parseH2(element: CheerioElement) {295 const text: Cheerio = this.$(element).find('.bold');296 this.renderer.renderH2(text.text());297 this.parseBlock(element);298 }299 private parseCodeExample(element: CheerioElement) {300 const span: Cheerio = this.$(element).find('.qtext');301 let code = this.$(span).html()!;302 code = entities.decode(code);303 code = code.replace(/(\r\n|\n|\r)/gm, '');304 code = code.replace(/<br>/g, '\n');305 this.renderer.renderCodeBlock(code);306 }307 private replaceJavascriptLinks(text: string) {308 let replacedText = text;309 if (!text) return '';310 // https://regex101.com/r/rvTc8y/6311 // Replace bad abap docs js links with relative links312 // HTML313 replacedText = text.replace(/(<a href=")(javascript:call_link\((?:'|'))(.*?)\.html(?:'|')\)/gm, '$1../$3');314 // HTM315 replacedText = replacedText.replace(/(<a href=")(javascript:call_link\((?:'|'))(.*?)\.htm(?:'|')\)/gm, '$1../$3');316 return replacedText;317 // HTM318 }...
App.js
Source:App.js
1import React from 'react';2import {3 createAppContainer,4 createSwitchNavigator,5} from 'react-navigation';6import { createBottomTabNavigator } from 'react-navigation-tabs';7import { createDrawerNavigator } from 'react-navigation-drawer';8import { createStackNavigator } from 'react-navigation-stack';9import Example from './screens/Example';10const AuthStack = createStackNavigator({11 Landing: {12 screen: Example,13 navigationOptions: {14 headerTitle: 'Landing',15 },16 },17 SignIn: {18 screen: Example,19 navigationOptions: {20 headerTitle: 'Sign In',21 },22 },23 CreateAccount: {24 screen: Example,25 navigationOptions: {26 headerTitle: 'Create Account',27 },28 },29 ForgotPassword: {30 screen: Example,31 navigationOptions: {32 headerTitle: 'Forgot Password',33 },34 },35 ResetPassword: {36 screen: Example,37 navigationOptions: {38 headerTitle: 'Reset Password',39 },40 },41});42const FeedStack = createStackNavigator({43 Feed: {44 screen: Example,45 navigationOptions: {46 headerTitle: 'Feed',47 },48 },49 Details: {50 screen: Example,51 navigationOptions: {52 headerTitle: 'Details',53 },54 },55});56const SearchStack = createStackNavigator({57 Search: {58 screen: Example,59 navigationOptions: {60 headerTitle: 'Search',61 },62 },63 Details: {64 screen: Example,65 navigationOptions: {66 headerTitle: 'Details',67 },68 },69});70const DiscoverStack = createStackNavigator({71 Discover: {72 screen: Example,73 navigationOptions: {74 headerTitle: 'Discover',75 },76 },77 Details: {78 screen: Example,79 navigationOptions: {80 headerTitle: 'Details',81 },82 },83});84const MainTabs = createBottomTabNavigator({85 Feed: {86 screen: FeedStack,87 navigationOptions: {88 tabBarLabel: 'Feed',89 },90 },91 Search: {92 screen: SearchStack,93 navigationOptions: {94 tabBarLabel: 'Search',95 },96 },97 Discover: {98 screen: DiscoverStack,99 navigationOptions: {100 tabBarLabel: 'Discover',101 },102 },103});104const SettingsStack = createStackNavigator({105 SettingsList: {106 screen: Example,107 navigationOptions: {108 headerTitle: 'Settings List',109 },110 },111 Profile: {112 screen: Example,113 navigationOptions: {114 headerTitle: 'Profile',115 },116 },117});118const MainDrawer = createDrawerNavigator({119 MainTabs: MainTabs,120 Settings: SettingsStack,121});122const AppModalStack = createStackNavigator(123 {124 App: MainDrawer,125 Promotion1: {126 screen: Example,127 },128 },129 {130 mode: 'modal',131 headerMode: 'none',132 }133);134const App = createSwitchNavigator({135 Loading: {136 screen: Example,137 },138 Auth: {139 screen: AuthStack,140 },141 App: {142 screen: AppModalStack,143 },144});...
TabScreenData.ts
Source:TabScreenData.ts
1import { BottomTabNavigationOptions } from "@react-navigation/bottom-tabs";2import { TabBarIconProps } from "../../types/Navigation/NavigationTypes";3import { ScreenData } from "./ScreenData";4export class TabScreenData extends ScreenData {5 tabBarTitle: string;6 headerTitle?: string;7 enableIcon: any;8 disableIcon: any;9 constructor(id: string, tabBarTitle: string = "", enableIcon: any, disableIcon: any, headerTitle: string = "") {10 super(id);11 this.tabBarTitle = tabBarTitle;12 this.enableIcon = enableIcon;13 this.disableIcon = disableIcon;14 15 let hasHeaderTitleContent: boolean = (!!headerTitle && headerTitle != "");16 if (hasHeaderTitleContent) {17 this.headerTitle = headerTitle;18 }19 else {20 this.headerTitle = tabBarTitle;21 }22 }23 private selectIcon = (props: TabBarIconProps) => {24 return props.focused ? this.enableIcon : this.disableIcon;25 };26 getTabOptions = (): BottomTabNavigationOptions => {27 return {28 tabBarLabel: this.tabBarTitle, 29 title: this.headerTitle,30 tabBarIcon: this.selectIcon,31 };32 };...
Using AI Code Generation
1var tracetest = require('tracetest');2var header = tracetest.HeaderTitle();3console.log(header);4var tracetest = require('tracetest');5var header = tracetest.HeaderTitle();6console.log(header);7var tracetest = require('tracetest');8var header = tracetest.HeaderTitle();9console.log(header);10var tracetest = require('tracetest');11var header = tracetest.HeaderTitle();12console.log(header);13var tracetest = require('tracetest');14var header = tracetest.HeaderTitle();15console.log(header);16var tracetest = require('tracetest');17var header = tracetest.HeaderTitle();18console.log(header);19var tracetest = require('tracetest');20var header = tracetest.HeaderTitle();21console.log(header);22var tracetest = require('tracetest');23var header = tracetest.HeaderTitle();24console.log(header);25var tracetest = require('tracetest');26var header = tracetest.HeaderTitle();27console.log(header);28var tracetest = require('tracetest');29var header = tracetest.HeaderTitle();30console.log(header);31var tracetest = require('tracetest');32var header = tracetest.HeaderTitle();33console.log(header);34var tracetest = require('tracetest');
Using AI Code Generation
1var tracetest = require('./tracetest');2tracetest.HeaderTitle();3exports.HeaderTitle = function() {4 console.log('This is header title');5}6var tracetest = require('tracetest');7tracetest.HeaderTitle();
Using AI Code Generation
1var tracetest = require('./tracetest.js');2tracetest.HeaderTitle('test.js');3exports.HeaderTitle = function (str) {4 console.log('HeaderTitle');5 console.log(str);6}7app.get('/users/:id', function (req, res) {8 console.log(req.params.id);9 res.send('Hello World!');10});11app.use(express.static('public'));12app.use(express.static('public/website'));13app.get('/users', function (req, res) {14 console.log(req.params.id);15 res.send('Hello World!');16});
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!!