Best JavaScript code snippet using playwright-internal
line-navigator.js
Source:line-navigator.js  
1;2/* https://github.com/anpur/line-navigator Anton Purin MIT 2016 */3var getLineNavigatorClass = function() {4    function LineNavigator (file, options) {5        var self = this;6        // options init 7        options =       options           ? options           : {};        8        var encoding =  options.encoding  ? options.encoding  : 'utf8';9        var chunkSize = options.chunkSize ? options.chunkSize : 1024 * 4;10        var milestones = [];11        var wrapper = new FileWrapper(file, encoding);12        var oldFileSize = wrapper.getSize();13        var getFileSize = function (position) {14            return oldFileSize = oldFileSize > position15                ? oldFileSize 16                : wrapper.getSize(file);17        }18        var getProgressSimple = function(position) {19            var size = getFileSize(position);20            return Math.round(100 * position / size);21        }22        self.readSomeLines = function(index, callback) {23            var place = self.getPlaceToStart(index, milestones);     24            wrapper.readChunk(place.offset, chunkSize, function readChunkCallback(err, buffer, bytesRead) {                25                if (err) return callback(err, index);26                var isEof = bytesRead < chunkSize;27  28                var chunkContent = self.examineChunk(buffer, bytesRead, isEof); 29                if (chunkContent === undefined)30                    return callback('Line ' + index + ' is out of index, last available: ' + (milestones.length > 0 ? milestones[milestones.length - 1].lastLine : "none"), index);31                var inChunk = { 32                    firstLine: place.firstLine, 33                    lastLine: place.firstLine + chunkContent.lines - 1, 34                    offset: place.offset,35                    length: chunkContent.length + 136                };37                if (place.isNew) 38                    milestones.push(inChunk);               39                var targetInChunk = inChunk.firstLine <= index && index <= inChunk.lastLine;40                if (targetInChunk) {41                    var bomOffset = place.offset !== 0 ? 0 : self.getBomOffset(buffer, encoding);42                    wrapper.decode(buffer.slice(bomOffset, inChunk.length), function(text) {43                        var expectedLinesCount = inChunk.lastLine - inChunk.firstLine + (isEof ? 2 : 1);44                        45                        var lines = text.split(self.splitLinesPattern);                            46                        if (!isEof)47                            lines = lines.slice(0, lines.length - 1);                   48                        if (index != inChunk.firstLine)49                            lines = lines.splice(index - inChunk.firstLine); 50                          51                        callback(undefined, index, lines, isEof, getProgressSimple(inChunk.offset + inChunk.length), inChunk);52                    });53                } else {54                    if (!isEof) {                        55                        place = self.getPlaceToStart(index, milestones);56                        wrapper.readChunk(place.offset, chunkSize, readChunkCallback);57                    } else {58                        return callback('Line ' + index + ' is out of index, last available: ' + inChunk.lastLine, index);59                    }60                }                61            });62        };        63        self.readLines = function(index, count, callback) {64            if (count === 0) 65                return callback(undefined, index, [], false, 0);            66            var result = [];67            self.readSomeLines(index, function readLinesCallback(err, partIndex, lines, isEof, progress, inChunk) {68                if (err) return callback(err, index);69                var resultEof = !isEof70                    ? false71                    :  partIndex + lines.length <= index + count;72                result = result.concat(lines);73                if (result.length >= count || isEof) {74                    result = result.splice(0, count);75                    var progress = self.getProgress(inChunk, index + result.length - 1, getFileSize(inChunk.offset + inChunk.length));76                    return callback(undefined, index, result, resultEof, progress);77                }78                self.readSomeLines(partIndex + lines.length, readLinesCallback);79            });80        };81        self.find = function(regex, index, callback) {82            self.readSomeLines(index, function readSomeLinesHandler(err, firstLine, lines, isEof, progress) {83                if (err) return callback(err);84                for (var i = 0; i < lines.length; i++) {85                    var match = self.searchInLine(regex, lines[i]);86                    if (match)       87                        return callback(undefined, firstLine + i, match);                    88                }89                if (isEof) 90                    return callback();91                self.readSomeLines(firstLine + lines.length + 1, readSomeLinesHandler);92            });93        };94        self.findAll = function(regex, index, limit, callback) {95            var results = [];96            self.readSomeLines(index, function readSomeLinesHandler(err, firstLine, lines, isEof) {97                if (err) return callback(err, index);98                for (var i = 0; i < lines.length; i++) {99                    var match = self.searchInLine(regex, lines[i]);100                    if (match) {101                        match.index = firstLine + i;102                        results.push(match);103                        if (results.length >= limit)104                            return callback(undefined, index, true, results);105                    }106                }107                if (isEof)108                    return callback(undefined, index, false, results);109                self.readSomeLines(firstLine + lines.length, readSomeLinesHandler);110            });111        };112    }113    LineNavigator.prototype.splitLinesPattern = /\r\n|\n|\r/;114    LineNavigator.prototype.getProgress = function (milestone, index, fileSize) {115        var linesInMilestone = milestone.lastLine - milestone.firstLine + 1;116        var indexNumberInMilestone = index - milestone.firstLine;117        var indexLineAssumablePosition = index !== milestone.lastLine 118            ? milestone.offset + milestone.length / linesInMilestone * indexNumberInMilestone119            : milestone.offset + milestone.length;120        return Math.floor(100 * (indexLineAssumablePosition / fileSize));121    }122    LineNavigator.prototype.searchInLine = function(regex, line) {123        var match = regex.exec(line);124        return !match 125            ? null 126            : {127                    offset: line.indexOf(match[0]),128                    length: match[0].length,129                    line: line130              };131    }132    LineNavigator.prototype.getPlaceToStart = function (index, milestones) {133        for (var i = milestones.length - 1; i >= 0; i--) {134            if (milestones[i].lastLine < index) 135                return { 136                    firstLine: milestones[i].lastLine + 1, 137                    offset: milestones[i].offset + milestones[i].length,138                    isNew: i === milestones.length - 1139                };140        }141        return { firstLine: 0, offset: 0, isNew: milestones.length === 0 };142    }143    // searches for line end, which can be \r\n, \n or \r (Windows, *nix, MacOs line endings)144    // returns line end postion including all line ending145    LineNavigator.prototype.getLineEnd = function (buffer, start, end, isEof) {146        var newLineCode = '\n'.charCodeAt(0);147        var caretReturnCode = '\r'.charCodeAt(0);148        for (var i = start; i < end; i++) {149            var char = buffer[i];150            if (char === newLineCode) {151                return i;152            } else if (char === caretReturnCode) {153                // \r is a last character in a given buffer and it is not the end of file yet, so it could be \r\n sequence which was separated154                var canBeSplitted = (i == end - 1) && !isEof; 155                if (!canBeSplitted) {156                    return buffer[i + 1] === newLineCode 157                        ? i + 1 158                        : i;159                } else {160                    return undefined;161                }162            }163        }164    }165    LineNavigator.prototype.examineChunk = function(buffer, bytesRead, isEof) {166        var lines = 0;167        var length = 0;168        169        do {170            var position = LineNavigator.prototype.getLineEnd(buffer, length, bytesRead, isEof);171            if (position !== undefined) {172                lines++;173                length = position + 1;174            }175        } while (position !== undefined);176        if (isEof) {177            lines++;178            length = bytesRead;179        }180        return length > 0 181            ? { lines: lines, length: length - 1 } 182            : undefined;183    };    184    var bomUtf8 = [239, 187, 191];185    var bomUtf16le = [255, 254];186    var arrayStartsWith = function (array, startsWith) {187        for (var i = 0; i < array.length && i < startsWith.length; i++) {188            if (array[i] !== startsWith[i])189                return false;190            if (i == startsWith.length - 1) 191                return true;192        }193        return false;194    };195    LineNavigator.prototype.getBomOffset = function(buffer, encoding) {196        switch (encoding.toLowerCase()) {197            case 'utf8':198                return arrayStartsWith(buffer, bomUtf8) ? bomUtf8.length : 0;199            case 'utf16le':200                return arrayStartsWith(buffer, bomUtf16le) ? bomUtf16le.length : 0;201            default:202                return 0;203        }204    }205    return LineNavigator;    206};207// Node.js208if (typeof module !== 'undefined' && module.exports) {209    FileWrapper = require('./file-wrapper.js');210    module.exports = getLineNavigatorClass();211}212// AMD213else if (typeof define === 'function') {214    define(['./file-wrapper'], function(fileWrapper){215        FileWrapper = fileWrapper;216        return getLineNavigatorClass();    217    });218}219// Vanilla220else {221    if (typeof FileWrapper === 'undefined') {222        throw "For vanilla JS please add 'file-wrapper.js' script tag before this one."223    }224    LineNavigator = getLineNavigatorClass();...StreamParser.js
Source:StreamParser.js  
1export function parse(buffer, generatorFunction) {2  return (new StreamParser(generatorFunction)).addFinalChunk(buffer);3}4export class StreamParser {5  constructor(generatorFunction) {6    this._chunkConsumer = makeChunkConsumer(generatorFunction);7    this._chunkConsumer.next();8    this.done = false;9    this.result = null;10  }11  addChunk(buffer) {12    if (! Buffer.isBuffer(buffer)) {13      throw new Error("Chunk must be a Buffer in StreamParser");14    }15    if (this.done) {16      return this.result;17    } else {18      const {value, done} = this._chunkConsumer.next(buffer);19      if (done) {20        this.done = true;21        this.result = value;22        return this.result;23      } else {24        return null;25      }26    }27  }28  addEOF() {29    if (this.done) {30      return this.result;31    } else {32      const result = this._chunkConsumer.next('EOF').value;33      this.result = result;34      this.done = true;35      return result;36    }37  }38  addFinalChunk(buffer) {39    this.addChunk(buffer);40    return this.addEOF();41  }42}43// An extractor is a function that takes:44//45// * `chunks` - non-empty array of non-empty Buffers46// * `c` - index into `chunks`47// * `offset` - byte index into `chunks[c]`48// * `available` - total number of available bytes,49//     including remaining bytes in `chunks[c]` starting at `offset`,50//     and bytes in subsequent chunks51// * `isEOF` - true/false; true if there are no more chunks.52//     note that there still may be bytes available.53//     most extractors can ignore this argument and simply return `null`54//     if the bytes they need aren't available, and then an appropriate55//     error will be thrown automatically.56//57// An extractor returns one of the following:58// * `null` - the extractor will be called again when more bytes are available59// * `{result: any, totalBytesConsumed: int}` - the extractor returns a result60//   and consumes some number of bytes, which may be 0, or may span multiple61//   chunks.62//63// When an extractor is called:64// * Either `available > 0` or `available === 0` and we are at EOF.65//   (Returning `null` at EOF will cause an "Unexpected EOF" error, which66//   is presumably what you want in this case.)67// * `chunks[c]` exists and `offset < chunks[c].length`68//69// When an extractor is called multiple times as a result of returning `null`,70// the `c` and `offset` arguments will be the same each time.  The `available`71// argument will be a greater number each time, and the `chunks` array will72// contain the same chunks plus at least one new chunk each time -- unless73// isEOF is newly set to true.74export const extractors = {75  EOF: (chunks, c, offset, available, isEOF) => {76    return { result: (available === 0),77             totalBytesConsumed: 0 };78  },79  byte: (chunks, c, offset, available) => {80    if (available < 1) {81      return null;82    }83    return { result: chunks[c].readUInt8(offset),84             totalBytesConsumed: 1 };85  },86  bytes(n) {87    if (! (n >= 1)) {88      throw new Error("Positive number of bytes required");89    }90    return (chunks, c, offset, available) => {91      if (available < n) {92        // call us back when there are more bytes available93        return null;94      } else {95        const curChunk = chunks[c];96        const curChunkAvailable = curChunk.length - offset;97        if (curChunkAvailable >= n) {98          // bytes are available in the current chunk99          return { result: curChunk.slice(offset, offset+n),100                   totalBytesConsumed: n };101        } else {102          if (c === (chunks.length - 1)) {103            throw new Error("assertion fail: more bytes should be available");104          }105          // bytes are at a chunk boundary; allocate new Buffer106          const result = new Buffer(n);107          // copy the rest of this chunk into the buffer108          let bytesNeeded = n - curChunkAvailable; // > 0109          curChunk.copy(result, 0, offset, curChunk.length);110          // copy data from more chunks111          let j = c+1;112          while (bytesNeeded > 0 && j < chunks.length) {113            const newChunk = chunks[j];114            const bytesToTake = Math.min(newChunk.length, bytesNeeded);115            newChunk.copy(result, n-bytesNeeded, 0, bytesToTake);116            bytesNeeded -= bytesToTake;117            j++;118          }119          if (bytesNeeded) {120            throw new Error("assertion fail: more bytes should be available");121          }122          return { result: result, totalBytesConsumed: n };123        }124      }125    };126  }127};128const bytes4 = extractors.bytes(4);129Object.assign(extractors, {130  uint32: (chunks, c, offset, available) => {131    if (available < 4) {132      return null;133    } else if (chunks[c].length - offset >= 4) {134      // fast path for performance; bytes are in same chunk135      return { result: chunks[c].readUInt32BE(offset),136               totalBytesConsumed: 4 };137    } else {138      // bytes are split across chunks139      const ret = bytes4(chunks, c, offset, available);140      ret.result = ret.result.readUInt32BE(0);141      return ret;142    }143  }144});145export class TestParser extends StreamParser {146  constructor() {147    super(function* () {148      const {byte, uint32} = extractors;149      return [(yield uint32).toString(16),150              (yield byte).toString(16),151              (yield uint32).toString(16)];152    });153  }154}155// Pass in Buffer chunks, or the string 'EOF' to indicate EOF.156function* makeChunkConsumer(parser) {157  const chunks = []; // Buffers of non-zero length158  let available = 0;159  let c = 0; // which chunk we're on160  let offset = 0; // current offset into `chunks[c]`161  let isEOF = false;162  let totalPastChunksLength = 0;163  function receiveChunk(newChunk) {164    if (newChunk === 'EOF') {165      isEOF = true;166    } else if (newChunk.length) {167      chunks.push(newChunk);168      available += newChunk.length;169    }170  }171  const generator = parser();172  let info = generator.next();173  while (! info.done) {174    const extractor = info.value;175    // make sure we have at least one byte available,176    // which means we have at least one chunk, and a byte177    // to point to in that chunk, when we invoke an extractor.178    // At this point, isEOF is false.179    while (available === 0 && !isEOF) {180      receiveChunk((yield));181    }182    // Invariants (if not isEOF):183    // - `available > 0`184    // - `chunks[c]` exists and `offset < chunks[c].length`.185    //186    // If and only if isEOF, `available === 0`.187    let result, bytesConsumed;188    if (available && extractor === extractors.byte) {189      // this case is purely a performance optimization, to avoid an190      // extra function call and object allocation.191      result = chunks[c].readUInt8(offset);192      bytesConsumed = 1;193    } else if (extractor === 'offset') {194      result = totalPastChunksLength + offset;195      bytesConsumed = 0;196    } else {197      let extracted = extractor(chunks, c, offset, available, isEOF);198      while (!extracted && !isEOF) {199        const oldAvailable = available;200        while (available === oldAvailable && !isEOF) {201          receiveChunk((yield));202        }203        extracted = extractor(chunks, c, offset, available, isEOF);204      }205      if (!extracted) {206        generator.throw(new Error("Unexpected EOF"));207      }208      result = extracted.result;209      bytesConsumed = extracted.totalBytesConsumed;210      if (typeof bytesConsumed !== 'number') {211        throw new Error("extractor must return totalBytesConsumed");212      }213      if (bytesConsumed > available) {214        throw new Error("Parser tried to consume more bytes than available");215      }216    }217    available -= bytesConsumed;218    offset += bytesConsumed;219    while (c < chunks.length && offset >= chunks[c].length) {220      offset -= chunks[c].length;221      totalPastChunksLength += chunks[c].length;222      c++;223    }224    // Invariant: Either `offset < chunks[c].length`, meaning there225    // is another byte in the current chunk ready to be read, or we are226    // out of bytes but ready for the next chunk when it arrives, i.e.227    // `c === chunks.length` and `offset === 0` and `available === 0`.228    info = generator.next(result);229  }230  return info.value;...Sexpr.js
Source:Sexpr.js  
...6        this.column = 0;7    }89    // Check if we reached the end of the file10    isEOF() {11        return this.index >= this.string.length;12    }1314    // Peek at the next char15    peek() {16        return this.string[this.index];17    }1819    // Peek at a specified char20    peekOffset(offset) {21        return this.string[this.index + offset];22    }2324    // Read & consume the next char25    pop() {26        const char = this.string[this.index++];27        if (char == "\n") {28            this.line++;29            this.column = 0;30        } else {31            this.column++;32        }33        return char;34    }3536    pos() {37        return { line: this.line, column: this.column };38    }39};4041// Check if a char is a digit42function isDigit(char) {43    return char >= "0" && char <= "9";44}4546// Check if a char is an alphabet character47function isAlpha(char) {48    return (char >= "a" && char <= "z") || (char >= "A" && char <= "Z");49}5051// Lex a comment52function comment(lexer) {53    if (!lexer.isEOF() && lexer.peek() == ";") {54        lexer.pop();55        while (!lexer.isEOF()) {56            const char = lexer.pop();57            if (char === '\n') break;58        }59        return true;60    }61    return false;62}6364// Lex a number65function num(lexer) {66    let num = 0;67    let digitsCount = 0;68    let negate = false;69    if (!lexer.isEOF() && lexer.peek() == "-" && isDigit(lexer.peekOffset(1))) {70        negate = true;71        lexer.pop();72    }73    while (!lexer.isEOF() && isDigit(lexer.peek())) {74        digitsCount++;75        num *= 10;76        num += parseInt(lexer.pop());77    }78    if (digitsCount === 0) {79        // Didn't find any digits80        return false;81    } else {82        // Found digits, so return the number83        return negate ? -num : num;84    }85}8687// Lex a string88function string(lexer) {89    let contents = "";90    if (lexer.isEOF() || lexer.peek() !== '"') {91        return false;92    }93    lexer.pop();94    while (!lexer.isEOF()) {95        const char = lexer.pop();96        if (char === '"') return contents;97        else contents += char;98    }99    return false;100}101102// Lex an open paren103function openParen(lexer) {104    if (!lexer.isEOF() && lexer.peek() == "(") {105        lexer.pop();106        return "(";107    } else {108        return false;109    }110}111112// Lex a close paren113function closeParen(lexer) {114    if (!lexer.isEOF() && lexer.peek() == ")") {115        lexer.pop();116        return ")";117    } else {118        return false;119    }120}121122// Lex white space123function whiteSpace(lexer) {124    const whiteSpaceChars = [" ", "\t", "\r", "\n"];125    while (!lexer.isEOF()) {126        if (whiteSpaceChars.includes(lexer.peek())) lexer.pop();127        else if (comment(lexer)) continue;128        else break;129    }130    return false;131}132133// Lex an identifier134function ident(lexer) {135    const validOperators = [136        "+",137        "-",138        "*",139        "/",140        "%",141        "?",142        "=",143        "&",144        "|",145        "@",146        "!",147        "~",148        "_"149    ];150    if (lexer.isEOF()) {151        return false;152    }153    const firstChar = lexer.pop();154    let buffer = "";155    if (isAlpha(firstChar) || validOperators.includes(firstChar)) {156        buffer += firstChar;157    }158    while (!lexer.isEOF()) {159        const char = lexer.peek();160        if (isAlpha(char) || isDigit(char) || validOperators.includes(char)) {161            buffer += lexer.pop();162        } else {163            break;164        }165    }166    return buffer;167}168169// Construct a token as the position + item170function token(pos, item) {171    return { pos: pos, token: item };172}173174// Lex an entire program175function lexProgram(text) {176    let buffer = [];177    let lexer = new Lexer(text);178    while (!lexer.isEOF()) {179        whiteSpace(lexer);180        let startPos = lexer.pos();181182        const isOpenParen = openParen(lexer);183        if (isOpenParen !== false) {184            buffer.push(token(startPos, isOpenParen));185            continue;186        }187188        const isCloseParen = closeParen(lexer);189        if (isCloseParen !== false) {190            buffer.push(token(startPos, isCloseParen));191            continue;192        }193194        const isString = string(lexer);195        if (isString !== false) {196            buffer.push(token(startPos, isString));197            continue;198        }199200        const isNum = num(lexer);201        if (isNum !== false) {202            buffer.push(token(startPos, isNum));203            continue;204        }205206        const isIdent = ident(lexer);207        if (isIdent !== false) {208            buffer.push(token(startPos, { id: isIdent }));209            continue;210        }211212        // Didn't find a match, so let's throw an error213        const line = startPos["line"];214        const col = startPos["column"];215        const found = lexer.peek();216        throw `Unexpected character at line ${line}, column ${col}: '${found}'`;217    }218219    return buffer;220}221222class Parser {223    constructor(tokenStream) {224        this.tokenStream = tokenStream;225        this.index = 0;226    }227228    // Check if we reached the end of the file229    isEOF() {230        return this.index >= this.tokenStream.length;231    }232233    // Peek at the next token234    peek() {235        return this.tokenStream[this.index]["token"];236    }237238    // Fetch & consume the next token239    pop() {240        return this.tokenStream[this.index++]["token"];241    }242243    // Get the current position of the parser244    pos() {245        return this.tokenStream[this.index]["pos"];246    }247}248249function parseExpr(parser) {250    let buffer = [];251    while (!parser.isEOF()) {252        if (parser.peek() === ")") {253            parser.pop();254            return buffer;255        } else if (parser.peek() === "(") {256            parser.pop();257            buffer.push(parseExpr(parser));258        } else {259            buffer.push(parser.pop());260        }261    }262    const line = parser.pos()["line"];263    const col = parser.pos()["column"];264    throw `Expected ')' but found end of file at line ${line}, column ${col}`;265}266267function parseProgram(tokenStream) {268    let parser = new Parser(tokenStream);269    let result = null;270    if (!parser.isEOF() && parser.peek() == "(") {271        parser.pop();272        result = parseExpr(parser);273    } else if (!parser.isEOF() && parser.peek() != ")") {274        result = parser.pop();275    }276277    if (!parser.isEOF()) {278        const found = parser.peek();279        const line = parser.pos()["line"];280        const col = parser.pos()["column"];281        throw `Expected end of file but found '${found}' at line ${line}, column ${col}`;282    } else {283        return result;284    }285}286287export function readSexpr(string) {288    return parseProgram(lexProgram(string));289}290291export function formatSexpr(sexpr) {
...parser.js
Source:parser.js  
1/**2 * Simple parser for tokenizing strings.3 *4 * Sample:5 *6 * ```7 * const parser = Parser('10 myinput,');8 *9 * console.log(parser.nextInt());   // Prints 1010 * console.log(parser.nextIdent()); // Prints myinput11 * console.log(parser.next());      // Prints ','12 * ```13 *14 * If `.next` or `.peek` methods fail to parse, an error is thrown.15 */16class Parser {17  /**18   *19   * @param {String} input Input to be parsed.20   * @param {Boolean} autoskipWhitespace Whether to automatically skip whitespaces21   * when parsing input. Defaults to `true`.22   */23  constructor (input, autoskipWhitespace = true) {24    this.input = input;25    this.index = 0;26    this.autoskipWhitespace = autoskipWhitespace;27  }2829  /**30   * Returns the current character without advancing the reading index.31   *32   * @returns {string} The current character in the input.33   * @throws If at end of input (i.e. `this.isEoF() == true`).34   */35  peek () {36    this.throwIfEoF();37    return this.input[this.index];38  }3940  /**41   * Returns the current character, and advances the reading index to the next42   * character of the input.43   *44   * @returns {string} The current character in the input.45   * @throws If at end of input (i.e. `this.isEoF() == true`).46   */47  next () {48    this.throwIfEoF();49    return this.input[this.index++];50  }5152  /**53   * @returns {number} The integer, parsed as a number.54   * @throws If at end of input (i.e. `this.isEoF() == true`).55   * @throws If the next char is not a digit.56   */57  nextInt () {58    if (this.autoskipWhitespace) {59      this.skipWhitespace();60    }6162    if (!this.isDigit(this.peek())) {63      throw Error('Expected digit at index ', this.index);64    }6566    var result = this.getStringAfterSkipping(() => {67      this.next(); // We already now this is a digit because of the `if` above6869      while (!this.isEoF() && this.isDigit(this.peek())) {70        this.next();71      }72    });7374    return Number.parseInt(result);75  }7677  /**78   * @returns {number} The float, parsed as a number.79   * @throws If at end of input (i.e. `this.isEoF() == true`).80   * @throws If the next char is not a digit, or the regex fails the match /\d+(\.\d+)?/81   */82  nextFloat () {83    const match = this.nextRegex(/\d+(\.\d+)?/);8485    return Number.parseFloat(match);86  }8788  /**89   * Parses an identifier. An identifier is any collection of non-whitespace90   * characters, up until the first whitespace char.91   * @returns {string} Parsed string up until first whitespace.92   * @throws If at end of input (i.e. `this.isEoF() == true`).93   */94  nextIdent () {95    if (this.autoskipWhitespace) {96      this.skipWhitespace();97    }9899    this.throwIfEoF();100101    return this.getStringAfterSkipping(() => {102      while (!this.isEoF() && !this.isWhitespace(this.peek())) {103        this.next();104      }105    });106  }107108  /**109   * Verifies the next identifier passed a given checking closure.110   * The closure receives in the next identifier string and returns true/false,111   * and the method throws if the return is `false`, with a given error message.112   * @param {(String) => Boolean} matching113   * @param {string} errorMessage114   * @returns {String} The identifier that was matches115   */116  validateNextIdent (matching, errorMessage = null) {117    const ident = this.nextIdent();118    if (!matching(ident)) {119      throw Error(errorMessage);120    }121122    return ident;123  }124125  /**126   * Matches a given regex from the current point in the string.127   * If the regex doesn't match, or the first match is not exactly at index `this.index`,128   * an error is thrown.129   * @param {string} regex130   * @returns {RegExpExecArray} The resulting match array.131   */132  nextRegex (regex) {133    if (this.autoskipWhitespace) {134      this.skipWhitespace();135    }136137    const matches = RegExp(regex).exec(this.input.substr(this.index));138139    if (matches.length === 0 || matches.index !== 0) {140      throw Error('Failed to match ' + regex + ' against remaining input.');141    }142143    this.index += matches[0].length;144145    return matches;146  }147148  /**149   * Collects all the remaining string from the current point in the input.150   * After this call, any further attempt to call a parsing method will result151   * in an EoF error.152   * @returns {String}153   */154  remaining () {155    const rem = this.input.substr(this.index);156157    this.index = this.input.length;158159    return rem;160  }161162  /**163   * Skips all whitespace characters until a non-whitespace char is hit, or end-of-file164   * is reached.165   */166  skipWhitespace () {167    while (!this.isEoF() && this.isWhitespace(this.peek())) {168      this.next();169    }170  }171172  /**173   * Returns whether char is a whitespace character.174   * Whitespace chars recognized: \s, \t, \r, \n.175   * @param {string} char176   */177  isWhitespace (char) {178    return char === ' ' || char === '\t' || char === '\r' || char === '\n';179  }180181  /**182   * Returns whether char is one of the 10 numerical digits.183   * @param {string} char184   */185  isDigit (char) {186    const code = char.charCodeAt(0);187    return code >= 48 /* = '0' */ && code <= 57;188  }189190  /**191   * Returns whether char is one of the 26 alphabetical letters, lowercase and192   * uppercase.193   * @param {string} char194   */195  isLetter (char) {196    const code = char.charCodeAt(0);197    return (code >= 97 /* = 'a' */ && code <= 122 /* = 'z' */) ||198      (code >= 65 /* = 'A' */ && code <= 90 /* = 'Z' */);199  }200201  /**202   * Returns whether char is either a letter or a digit.203   * @param {string} char204   */205  isAlpha (char) {206    return this.isLetter(char) || this.isDigit(char);207  }208209  /**210   * Whether the index to read points to the end of the string, that is, no more211   * string can be parsed.212   *213   * `EoF` comes from 'end-of-file' and is the common term used in programming214   * when a reading index points to the end of the readable region.215   *216   * @returns {bool} Returns if `this.index >= this.input.length`. Used to check217   * if the parser is at the end of the input.218   */219  isEoF () {220    return this.index >= this.input.length;221  }222223  /**224   * Helper method for checking end of input and throwing automatically.225   *226   * @throws If `this.isEoF() == true`.227   */228  throwIfEoF () {229    if (this.isEoF()) {230      throw Error('Unexpected end of input.');231    }232  }233234  /**235   * Used to return a string that starts at the current index, and ends at the236   * index after the closure finishes executing. Used to aid in collecting strings237   * without having to deal w/ locals.238   * @param {() => void} closure239   */240  getStringAfterSkipping (closure) {241    const startIndex = this.index;242    closure();243244    return this.input.substring(startIndex, this.index);245  }246}247
...message.test.js
Source:message.test.js  
...24  describe('#isEOF', () => {25    it('should be return true it message was "STORED"', () => {26      const reply = Buffer.from('STORED\r\n', 'utf8');27      // eslint-disable-next-line no-unused-expressions28      expect(Message.isEOF(reply)).to.be.true;29    });30    it('should be return true it message was "ERROR"', () => {31      const reply = Buffer.from('ERROR\r\n', 'utf8');32      // eslint-disable-next-line no-unused-expressions33      expect(Message.isEOF(reply)).to.be.true;34    });35    it('should be return true it message was "SERVER_ERROR"', () => {36      const reply = Buffer.from('SERVER_ERROR\r\n', 'utf8');37      // eslint-disable-next-line no-unused-expressions38      expect(Message.isEOF(reply)).to.be.true;39    });40    it('should be return true it message was "CLIENT_ERROR"', () => {41      const reply = Buffer.from('CLIENT_ERROR\r\n', 'utf8');42      // eslint-disable-next-line no-unused-expressions43      expect(Message.isEOF(reply)).to.be.true;44    });45    it('should be return true it message was "EXISTS"', () => {46      const reply = Buffer.from('EXISTS\r\n', 'utf8');47      // eslint-disable-next-line no-unused-expressions48      expect(Message.isEOF(reply)).to.be.true;49    });50    it('should be return true it message was "TOUCHED"', () => {51      const reply = Buffer.from('TOUCHED\r\n', 'utf8');52      // eslint-disable-next-line no-unused-expressions53      expect(Message.isEOF(reply)).to.be.true;54    });55    it('should be return true it message was "DELETED"', () => {56      const reply = Buffer.from('DELETED\r\n', 'utf8');57      // eslint-disable-next-line no-unused-expressions58      expect(Message.isEOF(reply)).to.be.true;59    });60    it('should be return true it message was "END"', () => {61      const reply = Buffer.from('END\r\n', 'utf8');62      // eslint-disable-next-line no-unused-expressions63      expect(Message.isEOF(reply)).to.be.true;64    });65    it('should be return false it message was value messages', () => {66      const reply = Buffer.from('VALUE foo 0 3 100\r\n', 'utf8');67      // eslint-disable-next-line no-unused-expressions68      expect(Message.isEOF(reply)).to.be.false;69    });70  });71  describe('#code', () => {72    beforeEach(() => {73      m.append(Buffer.from('STORED\r\n', 'utf8'));74      m.freeze();75    });76    it('should return trimmed code string', () => {77      expect(m.code).to.equal('STORED');78    });79  });80  describe('#rawData', () => {81    beforeEach(() => {82      m.append(Buffer.from('VALUE foo 0 3 100\r\nbar\r\nEND\r\n', 'utf8'));...driftbox.js
Source:driftbox.js  
1// Driftbox ãã°ãªã¼ã2LogReaderInfo.push({3	Caption:	"Driftbox (*.dbn)",4	Filter:		"*.dbn",5	ReaderFunc:	"Read_dbn"6});7// dbn ãã©ã¼ããã (ã»ã¨ãã© [HEADER] ã«æ¸ãã¦ãã)8// 1ã¬ã³ã¼ã: 33ãã¤ã  big endian9// '$'10// SATS(1)11// TIME(3)		GMT 0:00 ããã®çµéæé 1ç§=100 æ¥ä»ã¯å¤±ããã¦ãã(?)12// LATITUDE(4)	1度 = 600000013// LONGITUDE(4)14// VELOCITY(2)	1km/h = 10015// HEADING(2)	1度 = 10016// HEIGHT(4)17// YAW__(2)		以ä¸ãªãã®å¤ã䏿18// YAW_(2)19// YAW(2)20// SLIP(2)21// CHKSUM(2)22// 0x0D 0x0A23function Read_dbn( Files ){24	25	Log.Time		= [];26	Log.Speed		= [];27	Log.Longitude	= [];28	Log.Latitude	= [];29	30	var	Cnt = 0;31	var Line;32	33	for( var i = 0; i < Files.length; ++i ){34		var file = new File();35		try{36			file.Open( Files[ i ], "rb" );37		}catch( e ){38			MessageBox( "ãã¡ã¤ã«ãéãã¾ãã: " + Files[ i ] );39			return 0;40		}41		42		// [HEADER] ã¾ã§ã¹ããã43		while( !file.IsEOF()){44			if( file.ReadLine().match( /^\[HEADER\]/ )) break;45		}46		47		var ParamName = [];48		var ParamSize = [];49		50		// ãããè§£æ51		while( !file.IsEOF()){52			Line = file.ReadLine();53			if( Line.match( /^(.+)\((\d+)\)/ )){54				ParamName.push( RegExp.$1 );55				ParamSize.push( ~~RegExp.$2 );56			}else{57				break;58			}59		}60		if( file.IsEOF()) return INVALID_FORMAT;61		62		// [DATA] ã¾ã§ã¹ããã63		if( !Line.match( /^\[DATA\]/ )){64			while( !file.IsEOF()){65				if( file.ReadLine().match( /^\[DATA\]/ )) break;66			}67		}68		if( file.IsEOF()) return INVALID_FORMAT;69		70		var PrevTime = 0;71		72		// æ¥ä»æ
å ±ãç¡ãã®ã§æ«å®çã« 2012/1/173		var Time0 = Date.UTC( 2012, 0, 1, 0, 0, 0 );74		75		while( 1 ){76			file.Seek( 1, SEEK_CUR );	// '$' ã®ã¹ããã77			78			for( var j = 0; j < ParamName.length; ++j ){79				var Val;80				81				switch( ParamSize[ j ] ){82					case 1: Val = file.ReadUChar(); break;83					case 2: Val = file.ReadUShortB(); break;84					case 3: Val = file.ReadUShortB() * 256 + file.ReadUChar(); break;85					default:Val = file.ReadIntB();86				}87				88				switch( ParamName[ j ] ){89					case "TIME":		Time = Val * 10; break;90					case "LATITUDE":	Log.Latitude [ Cnt ] = Val / 6000000; break;91					case "LONGITUDE":	Log.Longitude[ Cnt ] = Val / 6000000; break;92					case "VELOCITY":	Log.Speed	 [ Cnt ] = Val / 100;93				}94			}95			96			if( file.IsEOF()) break;97			98			if( Time < PrevTime ){99				Time0 += 24 * 3600 * 1000;100			}101			PrevTime = Time;102			Log.Time[ Cnt ]	= Time + Time0;103			104			++Cnt;105			106			// 0D 0A ãã¹ããã107			file.Seek( 2, SEEK_CUR );108		}109		file.Close();110	}111	112	return Cnt;...Scanner.js
Source:Scanner.js  
...17      {18        return this._tokens$1.shift();19      }20      this.skipWhites();21      if (this.isEOF())22      {23        return new lsystem.parser.Token("eof", null);24      }25      var c/*:String*/ = this._source$1.charAt(this._pos$1++);26      var buf/*:String*/ = "";27      var code/*:int*/ = c.charCodeAt(0);28      if (this.isAlpha(code) || (c == '_'))29      {30        /*while (isAlpha(code) || (c == '_'))31           {32           buf += c;33           if (isEOF())34           {35           ++_pos;36           break;37           }38           c = _source.charAt(_pos++);39           code = c.charCodeAt(0);40           }41         --_pos;*/42        buf += c;43        return new lsystem.parser.Token("name", buf);44      }45      else if (c == "\n")46      {47        return new lsystem.parser.Token("eol", null);48      }49      else if (c == "-")50      {51        buf = c;52        while (!this.isEOF())53        {54          c = this._source$1.charAt(this._pos$1);55          if (c != '>')56          {57            break;58          }59          buf += c;60          this._pos$1++;61        }62        return new lsystem.parser.Token("operator", buf);63      }64      else65      {66        return new lsystem.parser.Token("operator", c);67      }68    },69    "public function pushBack",function pushBack(token/*:Token*/)/*:void*/70    {71      this._tokens$1.push(token);72    },73    "protected function isAlpha",function isAlpha(c/*:int*/)/*:Boolean*/74    {75      return ((c >= 97) && (c <= 122)) || ((c >= 65) && (c <= 90));76    },77    "protected function isEOF",function isEOF()/*:Boolean*/78    {79      return (this._pos$1 >= this._source$1.length);80    },81    "protected function skipWhites",function skipWhites()/*:void*/82    {83      while (!this.isEOF())84      {85        var c/*:String*/ = this._source$1.charAt(this._pos$1++);86        if ((c != " ") && (c != "\t"))87        {88          --this._pos$1;89          break;90        }91      }92    },93  ];},[],["Array","lsystem.parser.Token"], "0.8.0", "0.9.6"...reader.spec.js
Source:reader.spec.js  
1import createReader from "./reader";23test("Should correctly read input string", () => {4  const reader = createReader("hello");56  expect(reader.currentChar()).toBe("h");7  expect(reader.isEof()).toBeFalsy();8  reader.nextChar();9  expect(reader.currentChar()).toBe("e");10  expect(reader.isEof()).toBeFalsy();11  reader.nextChar();12  expect(reader.currentChar()).toBe("l");13  expect(reader.isEof()).toBeFalsy();14  reader.nextChar();15  expect(reader.currentChar()).toBe("l");16  expect(reader.isEof()).toBeFalsy();17  reader.nextChar();18  expect(reader.currentChar()).toBe("o");19  expect(reader.isEof()).toBeFalsy();20  reader.nextChar();21  expect(reader.isEof()).toBeTruthy();
...Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3  const browser = await chromium.launch();4  const context = await browser.newContext();5  const page = await context.newPage();6  await page.waitForSelector('input[name="q"]');7  await page.fill('input[name="q"]', 'Playwright');8  await page.keyboard.press('Enter');9  await page.waitForSelector('#search');10  console.log(await page.isEOF('#search'));11  await browser.close();12})();Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3  const browser = await chromium.launch();4  const context = await browser.newContext();5  const page = await context.newPage();6  await page.waitForSelector('input[name="q"]');7  await page.fill('input[name="q"]', 'playwright');8  await page.click('input[name="btnK"]');9  await page.waitForSelector('h3');10  await page.waitForSelector('text=Playwright');11  await page.click('text=Playwright');12  await page.waitForSelector('text=Getting Started');13  await page.click('text=Getting Started');14  await page.waitForSelector('text=Install Playwright');15  await page.click('text=Install Playwright');16  await page.waitForSelector('text=Playwright is a Node.js library to automate Chromium, Firefox and WebKit with a single API.');17  await page.click('text=Playwright is a Node.js library to automate Chromium, Firefox and WebKit with a single API.');18  await page.waitForSelector('text=Playwright is a Node.js library to automate Chromium, Firefox and WebKit with a single API.');19  await page.click('text=Playwright is a Node.js library to automate Chromium, Firefox and WebKit with a single API.');20  await page.waitForSelector('text=Playwright is a Node.js library to automate Chromium, Firefox and WebKit with a single API.');21  await page.click('text=Playwright is a Node.js library to automate Chromium, Firefox and WebKit with a single API.');22  await page.waitForSelector('text=Playwright is a Node.js library to automate Chromium, Firefox and WebKit with a single API.');23  await page.click('text=Playwright is a Node.js library to automate Chromium, Firefox and WebKit with a single API.');24  await page.waitForSelector('text=Playwright is a Node.js library to automate Chromium, Firefox and WebKit with a single API.');25  await page.click('text=Playwright is a Node.js library to automate Chromium, Firefox and WebKit with a single API.');26  await page.waitForSelector('text=Playwright is a Node.js library to automate Chromium, Firefox and WebKit with a single API.');Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3  const browser = await chromium.launch();4  const page = await browser.newPage();5  await page.waitForSelector('h1');6  const isEnd = await page.isEOF();7  console.log(isEnd);8  await browser.close();9})();10Playwright isEOF() method11page.isEOF()12const { chromium } = require('playwright');13(async () => {14  const browser = await chromium.launch();15  const page = await browser.newPage();16  await page.waitForSelector('h1');17  const isEnd = await page.isEOF();18  console.log(isEnd);19  await browser.close();20})();21Playwright isClosed() method22page.isClosed()23const { chromium } = require('playwright');24(async () => {25  const browser = await chromium.launch();26  const page = await browser.newPage();27  await page.waitForSelector('h1');28  const isClosed = await page.isClosed();29  console.log(isClosed);30  await browser.close();31})();32Playwright isJavaScriptDialogOpen() method33page.isJavaScriptDialogOpen()34const { chromium } = require('playwright');35(async () => {36  const browser = await chromium.launch();37  const page = await browser.newPage();Using AI Code Generation
1const { isEOF } = require('playwright/lib/server/frames');2console.log(isEOF);3const { isEOF } = require('playwright');4console.log(isEOF);5import { isEOF } from 'playwright';6console.log(isEOF);7import { isEOF } from 'playwright';8console.log(isEOF);9import { isEOF } from 'playwright';10console.log(isEOF);11import { isEOF } from 'playwright';12console.log(isEOF);Using AI Code Generation
1const { Stream } = require('playwright/lib/utils/stream');2const stream = new Stream();3stream.write('Hello');4stream.write('World');5stream.end();6(async () => {7  while (!stream.isEOF()) {8    console.log(stream.read());9  }10})();11stream.isEOF()12const { Stream } = require('playwright/lib/utils/stream');13const stream = new Stream();14stream.write('Hello');15stream.write('World');16stream.end();17console.log(stream.isEOF());18stream.isReadable()19const { Stream } = require('playwright/lib/utils/stream');20const stream = new Stream();21stream.write('Hello');22stream.write('World');23stream.end();24console.log(stream.isReadable());25stream.isWritable()26const { Stream } = require('playwright/lib/utils/stream');27const stream = new Stream();28stream.write('Hello');29stream.write('World');30stream.end();31console.log(stream.isWritable());32stream.read()33const { Stream } = require('playwright/lib/utils/stream');34const stream = new Stream();35stream.write('Hello');36stream.write('World');37stream.end();38(async () => {39  while (!LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!
