Best JavaScript code snippet using best
scanner.ts
Source:scanner.ts
1import { Reader } from "./io/reader";2const TERMINALS = /((\s+)|([\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?)|(\w+))/;3const END = 04const EOL = 15const SPACING = 2;6const NUMBER = 3;7const WORD = 4;8const OTHER = 5;9const SYNC = -1;10const ERROR = -2;11class Token {12 readonly kind: number;13 readonly text: string;14 readonly lineNo?: number;15 readonly index?: number;16 constructor(kind: number, text: string, lineNo: number, index: number) {17 this.kind = kind;18 this.text = text;19 this.lineNo = lineNo;20 this.index = index;21 }22}23class Scanner {24 private currentLine?: string;25 private source: Reader;26 private maxConsecutiveEmptyLines?: number;27 private lineNo = 0;28 private index = 0;29 private pendingTokenKind?: number;30 private currentTokenKind?: number;31 private currentTokenText?: string;32 private currentTokenLineNo?: number;33 private currentTokenIndex?: number;34 constructor(reader: Reader, maxConsecutiveEmptyLines?: number) {35 this.source = reader;36 this.maxConsecutiveEmptyLines = maxConsecutiveEmptyLines;37 }38 get token() {39 if (!this.currentTokenKind) return undefined;40 return new Token(41 this.currentTokenKind,42 this.currentTokenText ?? '',43 this.currentTokenLineNo ?? 1,44 this.currentTokenIndex ?? 1);45 }46 private error(message: string): Error {47 const text = `${this.currentTokenLineNo ?? 1}:${this.currentTokenIndex ?? 1} ${message}`;48 return new Error(text);49 }50 private extract(size: number, kind: number): number {51 this.currentTokenText = this.currentLine!.substring(0, size);52 this.currentLine = this.currentLine!.substring(size);53 this.index += size;54 return kind;55 }56 private async advanceLine(): Promise<string | undefined> {57 const line = await this.source.readLine();58 if (line !== undefined) {59 this.lineNo++;60 this.index = 1;61 }62 return line;63 }64 private async scan(): Promise<number> {65 this.currentTokenText = '';66 if (this.currentTokenKind === SYNC) return SYNC;67 if (this.currentTokenKind === END) return END;68 if (this.currentTokenKind === ERROR) return SYNC;69 if (this.currentLine === undefined) {70 // If we are at the end of text, we remain there.71 if (this.source.endOfText ||72 undefined === (this.currentLine = await this.advanceLine())73 ) {74 return END;75 }76 }77 this.currentTokenLineNo = this.lineNo;78 this.currentTokenIndex = this.index;79 if (this.currentLine === '') {80 this.currentLine = await this.advanceLine();81 return EOL;82 }83 84 const match = TERMINALS.exec(this.currentLine);85 if (match) {86 if (match.index) {87 return this.extract(match.index, OTHER);88 }89 for (var kind = SPACING; kind <= WORD; kind++) {90 if (match[kind] !== undefined) {91 return this.extract(match[kind].length, kind);92 }93 }94 }95 return this.extract(this.currentLine.length, OTHER);96 }97 private async scanToken() : Promise<number> {98 let kind = await this.scan();99 let countEOL = (this.index === 1) ? 1 : 0;100 while (kind === SPACING || kind === EOL) {101 if (kind === EOL) {102 countEOL++;103 // We optionally can abort processing after observing empty lines.104 if (this.maxConsecutiveEmptyLines && countEOL > this.maxConsecutiveEmptyLines) {105 kind = END;106 break;107 }108 }109 kind = await this.scan();110 }111 this.currentTokenKind = kind;112 this.pendingTokenKind = kind;113 return kind;114 }115 private async nextToken(acceptIt: boolean): Promise<number> {116 if (this.pendingTokenKind === undefined) await this.scanToken();117 if (acceptIt) {118 this.pendingTokenKind = undefined;119 }120 return this.currentTokenKind!;121 }122 get endOfText(): boolean {123 return this.currentTokenKind === END;124 }125 async peek(): Promise<number> {126 return await this.nextToken(false);127 }128 async acceptAny(): Promise<string> {129 const kind = await this.nextToken(true);130 return this.currentTokenText ?? '';131 }132 async accept(text: string): Promise<string> {133 const kind = await this.nextToken(true);134 if (this.currentTokenText === text) return text;135 throw this.error(`expected \'${text.replace('\'', '\\\'')}\'`);136 }137 async acceptWord(): Promise<string> {138 const kind = await this.nextToken(true);139 if (this.currentTokenKind === WORD) return this.currentTokenText!;140 throw this.error(`expected word`);141 }142 async acceptNumber(): Promise<number> {143 const kind = await this.nextToken(true);144 if (this.currentTokenKind === NUMBER) return parseFloat(this.currentTokenText!);145 throw this.error(`expected number`);146 }147 async acceptEnd(): Promise<void> {148 const kind = await this.nextToken(true);149 if (this.currentTokenKind === END) return;150 throw this.error(`expected end of text`);151 }152 async sync(): Promise<void> {153 if (this.currentTokenKind === ERROR || this.currentTokenKind === SYNC) {154 while (this.currentTokenKind !== END && this.currentTokenKind !== EOL) {155 this.currentTokenKind = undefined;156 this.currentTokenKind = await this.scan();157 }158 }159 }160}161export {162 Scanner,163 END,164 EOL,165 SPACING,166 NUMBER,167 WORD,168 OTHER,169 SYNC,170 ERROR,...
hiddenRangeModel.js
Source:hiddenRangeModel.js
...21 get hiddenRanges() { return this._hiddenRanges; }22 notifyChangeModelContent(e) {23 if (this._hiddenRanges.length && !this._hasLineChanges) {24 this._hasLineChanges = e.changes.some(change => {25 return change.range.endLineNumber !== change.range.startLineNumber || countEOL(change.text)[0] !== 0;26 });27 }28 }29 updateHiddenRanges() {30 let updateHiddenAreas = false;31 let newHiddenAreas = [];32 let i = 0; // index into hidden33 let k = 0;34 let lastCollapsedStart = Number.MAX_VALUE;35 let lastCollapsedEnd = -1;36 let ranges = this._foldingModel.regions;37 for (; i < ranges.length; i++) {38 if (!ranges.isCollapsed(i)) {39 continue;...
output-stream.ts
Source:output-stream.ts
...6*/7import { Writable } from "stream";8import { isInteractive as globaIsInteractive, clearLine } from "@best/utils";9import { EOL } from "os";10function countEOL(buffer: string): number {11 let eol_count = 0;12 for (let i = 0; i < buffer.length; i++) {13 if (buffer[i] === EOL) {14 eol_count+= 1;15 }16 }17 return eol_count;18}19export default class OutputStream {20 stdout: Writable;21 isInteractive: boolean;22 _linesBuffer: number = 0;23 constructor(stream: Writable, isInteractive?: boolean) {24 this.stdout = stream;25 this.isInteractive = isInteractive || globaIsInteractive26 }27 write(str: string) {28 this._linesBuffer += countEOL(str);29 this.stdout.write(str);30 }31 writeln(str: string) {32 this.write(str + '\n');33 }34 clearLine() {35 if (this.isInteractive) {36 clearLine(this.stdout);37 }38 }39 clearAll() {40 if (this.isInteractive) {41 clearLine(this.stdout);42 this.stdout.write('\r\x1B[K\r\x1B[1A'.repeat(this._linesBuffer));...
Using AI Code Generation
1var BestLine = require('./BestLine.js');2var fs = require('fs');3var readline = require('readline');4var stream = require('stream');5var instream = fs.createReadStream('input.txt');6var outstream = new stream;7var rl = readline.createInterface(instream, outstream);8var line = new BestLine();9rl.on('line', function(line) {10 line.addLine(line);11});12rl.on('close', function() {13 console.log(line.countEOL());14});15var BestLine = function BestLine() {16 this.eol = 0;17 this.line = [];18};19BestLine.prototype.addLine = function(line) {20 this.line.push(line);21};22BestLine.prototype.countEOL = function() {23 for (var i = 0; i < this.line.length; i++) {24 this.eol += this.line[i].length;25 }26 return this.eol;27};28module.exports = BestLine;
Using AI Code Generation
1var BestLine = require('./BestLine');2var fs = require('fs');3var bestLine = new BestLine();4var input = fs.readFileSync('./input.txt', 'utf8');5var output = bestLine.countEOL(input);6console.log(output);7var BestLine = function() {8 function countEOL(input) {9 var lines = input.split('\n');10 return lines.length;11 }12 this.countEOL = function(input) {13 return countEOL(input);14 }15};16module.exports = BestLine;17var BestLine = require('./BestLine');18var fs = require('fs');19var bestLine = new BestLine();20var input = fs.readFileSync('./input.txt', 'utf8');21var output = bestLine.countEOL(input);22fs.writeFileSync('./output.txt', output);23var BestLine = function() {24 function countEOL(input) {25 var lines = input.split('\n');26 return lines.length;27 }28 this.countEOL = function(input) {29 return countEOL(input);30 }31};32module.exports = BestLine;33var BestLine = require('./BestLine');34var fs = require('fs');35var bestLine = new BestLine();36var input = fs.readFileSync('./input.txt', 'utf8');37var output = bestLine.countEOL(input);38fs.writeFileSync('./output.txt', output);39var BestLine = function() {40 function countEOL(input) {41 var lines = input.split('\n');42 return lines.length;43 }
Using AI Code Generation
1var BestLine = require('./BestLine.js');2var fs = require('fs');3var file = fs.readFileSync(process.argv[2], 'utf8');4var bl = new BestLine(file);5var lineCount = bl.countEOL();6console.log(lineCount);7var BestLine = function(fileContents) {8 this.fileContents = fileContents;9};10BestLine.prototype.countEOL = function() {11 var count = 0;12 for (var i = 0; i < this.fileContents.length; i++) {13 if (this.fileContents[i] === '\n') {14 count++;15 }16 }17 return count;18};19module.exports = BestLine;20var BestLine = require('./BestLine.js');21var fs = require('fs');22var file = fs.readFileSync(process.argv[2], 'utf8');23var bl = new BestLine(file);24var lineCount = bl.countEOL();25console.log(lineCount);26var BestLine = function(fileContents) {27 this.fileContents = fileContents;28};29BestLine.prototype.countEOL = function() {30 return this.fileContents.split('\n').length - 1;31};32module.exports = BestLine;33var BestLine = require('./BestLine.js');34var fs = require('fs');35var file = fs.readFileSync(process.argv[2], 'utf8');36var bl = new BestLine(file);37var lineCount = bl.countEOL();38console.log(lineCount);39var BestLine = function(fileContents) {40 this.fileContents = fileContents;41};42BestLine.prototype.countEOL = function() {43 return this.fileContents.split('\n').length - 1;44};45module.exports = BestLine;
Using AI Code Generation
1var BestLine = require('./bestline.js');2var bestLine = new BestLine();3var fs = require('fs');4var file = fs.readFileSync('./test4.txt', 'utf8');5var lines = bestLine.countEOL(file);6console.log(lines);
Using AI Code Generation
1var BestLine = require('./BestLine.js');2var fs = require('fs');3var file = fs.readFileSync('test4.txt');4var line = new BestLine(file);5console.log(line.countEOL());6var BestLine = require('./BestLine.js');7var fs = require('fs');8var file = fs.readFileSync('test5.txt');9var line = new BestLine(file);10console.log(line.countEOL());11var BestLine = require('./BestLine.js');12var fs = require('fs');13var file = fs.readFileSync('test6.txt');14var line = new BestLine(file);15console.log(line.countEOL());16var BestLine = require('./BestLine.js');17var fs = require('fs');18var file = fs.readFileSync('test7.txt');19var line = new BestLine(file);20console.log(line.countEOL());21var BestLine = require('./BestLine.js');22var fs = require('fs');23var file = fs.readFileSync('test8.txt');24var line = new BestLine(file);25console.log(line.countEOL());26var BestLine = require('./BestLine.js');27var fs = require('fs');28var file = fs.readFileSync('test9.txt');29var line = new BestLine(file);30console.log(line.countEOL());31var BestLine = require('./BestLine.js');32var fs = require('fs');33var file = fs.readFileSync('test10.txt');34var line = new BestLine(file);35console.log(line.countEOL());36var BestLine = require('./BestLine.js');37var fs = require('fs');
Using AI Code Generation
1var BestEditor = require('besteditor');2var editor = new BestEditor();3var count = editor.countEOL('test.txt');4console.log(count);5console.log('done');6var BestEditor = require('besteditor');7var editor = new BestEditor();8var count = editor.countEOL('test.txt', function(err, count) {9 if (err)10 console.log(err);11 console.log(count);12});13console.log('done');
Using AI Code Generation
1var BestLine = require('./bestline');2var bestline = new BestLine();3var line = "This is a line\nwith two lines\n";4console.log("Line: " + line);5console.log("Number of lines: " + bestline.countEOL(line));6console.log("Number of lines: " + bestline.countEOL("This is a line\nwith two lines\n"));7console.log("Number of lines: " + bestline.countEOL("This is a line\nwith two lines"));8console.log("Number of lines: " + bestline.countEOL("This is a line"));9console.log("Number of lines: " + bestline.countEOL("This is a line\n"));10console.log("Number of lines: " + bestline.countEOL("This is a line\r\n"));11console.log("Number of lines: " + bestline.countEOL("This is a line\r\nwith two lines\r\n"));12console.log("Number of lines: " + bestline.countEOL("This is a line\r\nwith two lines\r\n\r\n"));13console.log("Number of lines: " + bestline.countEOL("This is a line\r\nwith two lines\r\n\r\n\r\n"));14console.log("Number of lines: " + bestline.countEOL("This is a line\r\nwith two lines\r\n\r\n\r\n\r\n"));15console.log("Number of lines: " + bestline.countEOL("This is a line\r\nwith two lines\r\n\r\n\r\n\r\n\r\n"));16console.log("Number of lines: " + bestline.countEOL("This is a line\r\nwith two lines\r\n\r\n\r\n\r\n\r\n\r\n"));17console.log("Number of lines: " + bestline.countEOL("This is a line\r\nwith two lines\r\n\r\n\r\n\r\n\r\n\r\n\r\n"));18console.log("Number of lines: " + bestline.countEOL("This is a line\r\nwith two lines\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"));19console.log("Number of lines: " + bestline.countEOL("This is a line\r\nwith two lines\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"));20console.log("Number of lines: " + bestline.countE
Using AI Code Generation
1var BestLine = require("./BestLine");2var bestLine = new BestLine();3var lines = ["Hello", "World"];4var result = bestLine.countEOL(lines);5console.log(result);6BestLine.prototype.countEOL = function(lines) {7 var count = 0;8 for (var i = 0; i < lines.length; i++) {9 var line = lines[i];10 var length = line.length;11 var lastChar = line.charAt(length - 1);12 if (lastChar === "\r" || lastChar === "13") {14 count++;15 }16 }17 return count;18};19module.exports = BestLine;20BestLine.prototype.countEOL = function(lines) {21 var count = 0;22 for (var i =
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!!