Best JavaScript code snippet using istanbul
yallist.js
Source:yallist.js
1'use strict'2module.exports = Yallist3Yallist.Node = Node4Yallist.create = Yallist5function Yallist (list) {6 var self = this7 if (!(self instanceof Yallist)) {8 self = new Yallist()9 }10 self.tail = null11 self.head = null12 self.length = 013 if (list && typeof list.forEach === 'function') {14 list.forEach(function (item) {15 self.push(item)16 })17 } else if (arguments.length > 0) {18 for (var i = 0, l = arguments.length; i < l; i++) {19 self.push(arguments[i])20 }21 }22 return self23}24Yallist.prototype.removeNode = function (node) {25 if (node.list !== this) {26 throw new Error('removing node which does not belong to this list')27 }28 var next = node.next29 var prev = node.prev30 if (next) {31 next.prev = prev32 }33 if (prev) {34 prev.next = next35 }36 if (node === this.head) {37 this.head = next38 }39 if (node === this.tail) {40 this.tail = prev41 }42 node.list.length--43 node.next = null44 node.prev = null45 node.list = null46 return next47}48Yallist.prototype.unshiftNode = function (node) {49 if (node === this.head) {50 return51 }52 if (node.list) {53 node.list.removeNode(node)54 }55 var head = this.head56 node.list = this57 node.next = head58 if (head) {59 head.prev = node60 }61 this.head = node62 if (!this.tail) {63 this.tail = node64 }65 this.length++66}67Yallist.prototype.pushNode = function (node) {68 if (node === this.tail) {69 return70 }71 if (node.list) {72 node.list.removeNode(node)73 }74 var tail = this.tail75 node.list = this76 node.prev = tail77 if (tail) {78 tail.next = node79 }80 this.tail = node81 if (!this.head) {82 this.head = node83 }84 this.length++85}86Yallist.prototype.push = function () {87 for (var i = 0, l = arguments.length; i < l; i++) {88 push(this, arguments[i])89 }90 return this.length91}92Yallist.prototype.unshift = function () {93 for (var i = 0, l = arguments.length; i < l; i++) {94 unshift(this, arguments[i])95 }96 return this.length97}98Yallist.prototype.pop = function () {99 if (!this.tail) {100 return undefined101 }102 var res = this.tail.value103 this.tail = this.tail.prev104 if (this.tail) {105 this.tail.next = null106 } else {107 this.head = null108 }109 this.length--110 return res111}112Yallist.prototype.shift = function () {113 if (!this.head) {114 return undefined115 }116 var res = this.head.value117 this.head = this.head.next118 if (this.head) {119 this.head.prev = null120 } else {121 this.tail = null122 }123 this.length--124 return res125}126Yallist.prototype.forEach = function (fn, thisp) {127 thisp = thisp || this128 for (var walker = this.head, i = 0; walker !== null; i++) {129 fn.call(thisp, walker.value, i, this)130 walker = walker.next131 }132}133Yallist.prototype.forEachReverse = function (fn, thisp) {134 thisp = thisp || this135 for (var walker = this.tail, i = this.length - 1; walker !== null; i--) {136 fn.call(thisp, walker.value, i, this)137 walker = walker.prev138 }139}140Yallist.prototype.get = function (n) {141 for (var i = 0, walker = this.head; walker !== null && i < n; i++) {142 // abort out of the list early if we hit a cycle143 walker = walker.next144 }145 if (i === n && walker !== null) {146 return walker.value147 }148}149Yallist.prototype.getReverse = function (n) {150 for (var i = 0, walker = this.tail; walker !== null && i < n; i++) {151 // abort out of the list early if we hit a cycle152 walker = walker.prev153 }154 if (i === n && walker !== null) {155 return walker.value156 }157}158Yallist.prototype.map = function (fn, thisp) {159 thisp = thisp || this160 var res = new Yallist()161 for (var walker = this.head; walker !== null;) {162 res.push(fn.call(thisp, walker.value, this))163 walker = walker.next164 }165 return res166}167Yallist.prototype.mapReverse = function (fn, thisp) {168 thisp = thisp || this169 var res = new Yallist()170 for (var walker = this.tail; walker !== null;) {171 res.push(fn.call(thisp, walker.value, this))172 walker = walker.prev173 }174 return res175}176Yallist.prototype.reduce = function (fn, initial) {177 var acc178 var walker = this.head179 if (arguments.length > 1) {180 acc = initial181 } else if (this.head) {182 walker = this.head.next183 acc = this.head.value184 } else {185 throw new TypeError('Reduce of empty list with no initial value')186 }187 for (var i = 0; walker !== null; i++) {188 acc = fn(acc, walker.value, i)189 walker = walker.next190 }191 return acc192}193Yallist.prototype.reduceReverse = function (fn, initial) {194 var acc195 var walker = this.tail196 if (arguments.length > 1) {197 acc = initial198 } else if (this.tail) {199 walker = this.tail.prev200 acc = this.tail.value201 } else {202 throw new TypeError('Reduce of empty list with no initial value')203 }204 for (var i = this.length - 1; walker !== null; i--) {205 acc = fn(acc, walker.value, i)206 walker = walker.prev207 }208 return acc209}210Yallist.prototype.toArray = function () {211 var arr = new Array(this.length)212 for (var i = 0, walker = this.head; walker !== null; i++) {213 arr[i] = walker.value214 walker = walker.next215 }216 return arr217}218Yallist.prototype.toArrayReverse = function () {219 var arr = new Array(this.length)220 for (var i = 0, walker = this.tail; walker !== null; i++) {221 arr[i] = walker.value222 walker = walker.prev223 }224 return arr225}226Yallist.prototype.slice = function (from, to) {227 to = to || this.length228 if (to < 0) {229 to += this.length230 }231 from = from || 0232 if (from < 0) {233 from += this.length234 }235 var ret = new Yallist()236 if (to < from || to < 0) {237 return ret238 }239 if (from < 0) {240 from = 0241 }242 if (to > this.length) {243 to = this.length244 }245 for (var i = 0, walker = this.head; walker !== null && i < from; i++) {246 walker = walker.next247 }248 for (; walker !== null && i < to; i++, walker = walker.next) {249 ret.push(walker.value)250 }251 return ret252}253Yallist.prototype.sliceReverse = function (from, to) {254 to = to || this.length255 if (to < 0) {256 to += this.length257 }258 from = from || 0259 if (from < 0) {260 from += this.length261 }262 var ret = new Yallist()263 if (to < from || to < 0) {264 return ret265 }266 if (from < 0) {267 from = 0268 }269 if (to > this.length) {270 to = this.length271 }272 for (var i = this.length, walker = this.tail; walker !== null && i > to; i--) {273 walker = walker.prev274 }275 for (; walker !== null && i > from; i--, walker = walker.prev) {276 ret.push(walker.value)277 }278 return ret279}280Yallist.prototype.splice = function (start, deleteCount, ...nodes) {281 if (start > this.length) {282 start = this.length - 1283 }284 if (start < 0) {285 start = this.length + start;286 }287 for (var i = 0, walker = this.head; walker !== null && i < start; i++) {288 walker = walker.next289 }290 var ret = []291 for (var i = 0; walker && i < deleteCount; i++) {292 ret.push(walker.value)293 walker = this.removeNode(walker)294 }295 if (walker === null) {296 walker = this.tail297 }298 if (walker !== this.head && walker !== this.tail) {299 walker = walker.prev300 }301 for (var i = 0; i < nodes.length; i++) {302 walker = insert(this, walker, nodes[i])303 }304 return ret;305}306Yallist.prototype.reverse = function () {307 var head = this.head308 var tail = this.tail309 for (var walker = head; walker !== null; walker = walker.prev) {310 var p = walker.prev311 walker.prev = walker.next312 walker.next = p313 }314 this.head = tail315 this.tail = head316 return this317}318function insert (self, node, value) {319 var inserted = node === self.head ?320 new Node(value, null, node, self) :321 new Node(value, node, node.next, self)322 if (inserted.next === null) {323 self.tail = inserted324 }325 if (inserted.prev === null) {326 self.head = inserted327 }328 self.length++329 return inserted330}331function push (self, item) {332 self.tail = new Node(item, self.tail, null, self)333 if (!self.head) {334 self.head = self.tail335 }336 self.length++337}338function unshift (self, item) {339 self.head = new Node(item, null, self.head, self)340 if (!self.tail) {341 self.tail = self.head342 }343 self.length++344}345function Node (value, prev, next, list) {346 if (!(this instanceof Node)) {347 return new Node(value, prev, next, list)348 }349 this.list = list350 this.value = value351 if (prev) {352 prev.next = this353 this.prev = prev354 } else {355 this.prev = null356 }357 if (next) {358 next.prev = this359 this.next = next360 } else {361 this.next = null362 }363}364try {365 // add if support for Symbol.iterator is present366 require('./iterator.js')(Yallist)...
traversal-skip.js
Source:traversal-skip.js
...16 return NodeFilter.FILTER_ACCEPT;17 }18}19debug("<br>Testing nextNode")20walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, skipB1Filter, false);21shouldBe("walker.nextNode(); walker.currentNode.id", "'A1'");22shouldBe("walker.nextNode(); walker.currentNode.id", "'C1'");23shouldBe("walker.nextNode(); walker.currentNode.id", "'B2'");24shouldBe("walker.nextNode(); walker.currentNode.id", "'B3'");25debug("<br>Testing firstChild")26walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, skipB1Filter, false);27shouldBe("walker.firstChild(); walker.currentNode.id", "'A1'");28shouldBe("walker.firstChild(); walker.currentNode.id", "'C1'");29debug("<br>Testing nextSibling")30walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, skipB2Filter, false);31shouldBe("walker.firstChild(); walker.currentNode.id", "'A1'");32shouldBe("walker.firstChild(); walker.currentNode.id", "'B1'");33shouldBe("walker.nextSibling(); walker.currentNode.id", "'B3'");34debug("<br>Testing parentNode")35walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, skipB1Filter, false);36walker.currentNode = testElement.querySelectorAll('#C1')[0];37shouldBe("walker.parentNode(); walker.currentNode.id", "'A1'");38debug("<br>Testing previousSibling")39walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, skipB2Filter, false);40walker.currentNode = testElement.querySelectorAll('#B3')[0];41shouldBe("walker.previousSibling(); walker.currentNode.id", "'B1'");42debug("<br>Testing previousNode")43walker = document.createTreeWalker(testElement, NodeFilter.SHOW_ELEMENT, skipB1Filter, false);44walker.currentNode = testElement.querySelectorAll('#B3')[0];45shouldBe("walker.previousNode(); walker.currentNode.id", "'B2'");46shouldBe("walker.previousNode(); walker.currentNode.id", "'C1'");47shouldBe("walker.previousNode(); walker.currentNode.id", "'A1'");...
Using AI Code Generation
1var walk = require('walk');2var fs = require('fs');3var walker = walk.walk('./', { followLinks: false });4walker.on('file', function(root, stat, next) {5 console.log(root + '/' + stat.name);6 next();7});8walker.on('end', function() {9 console.log(allFiles);10});
Using AI Code Generation
1var istanbul = require('istanbul');2var collector = new istanbul.Collector();3var reporter = new istanbul.Reporter();4reporter.add('text');5var walker = istanbul.utils.walker('lib');6walker.files.forEach(function (f) {7 console.log(f);8 collector.add(istanbul.utils.produceCoverage(require(f), f));9});10reporter.write(collector, true, function () {11 console.log('All reports generated');12});13var instrumenter = new istanbul.Instrumenter();14instrumenter.instrument('lib/sum.js', function (err, instrumented) {15 console.log(instrumented);16});
Using AI Code Generation
1var istanbul = require('istanbul');2var instrumenter = new istanbul.Instrumenter();3var fs = require('fs');4var code = fs.readFileSync('app.js', 'utf8');5var instrumentedCode = instrumenter.instrumentSync(code, 'app.js');6fs.writeFileSync('instrumented.js', instrumentedCode);7var assert = require('assert');8var app = require('./instrumented.js');9describe('Test for the add function', function () {10 it('should return 5', function () {11 assert.equal(5, app.add(2, 3));12 });13});14describe('Test for the multiply function', function () {15 it('should return 6', function () {16 assert.equal(6, app.multiply(2, 3));17 });18});19describe('Test for the divide function', function () {20 it('should return 2', function () {21 assert.equal(2, app.divide(6, 3));22 });23});
Using AI Code Generation
1var istanbul = require('istanbul');2var collector = new istanbul.Collector();3var reporter = new istanbul.Reporter();4var walk = istanbul.utils.walk;5walk('lib', sync, function (file, stat) {6 if (stat.isFile() && /\.(js)$/.test(file)) {7 return file;8 }9}, function (err, files) {10 if (err) {11 console.error(err);12 process.exit(1);13 }14 files.forEach(function (f) {15 console.log('Processing ' + f);16 collector.add(istanbul.utils.produceCoverage(require('./' + f)));17 });18 reporter.add('text');19 reporter.write(collector, true, function () {20 console.log('All done!');21 });22});
Using AI Code Generation
1var istanbul = require('istanbul');2var collector = new istanbul.Collector();3var reporter = new istanbul.Reporter();4collector.add(JSON.parse(process.env.coverage || '{}'));5var instrumenter = new istanbul.Instrumenter();6var fs = require('fs');7var path = require('path');8var source = fs.readFileSync(path.join(__dirname, '../lib/parse.js'), 'utf8');9var instrumentedSource = instrumenter.instrumentSync(source, path.join(__dirname, '../lib/parse.js'));10fs.writeFileSync(path.join(__dirname, '../lib/parse.js'), instrumentedSource);11var parse = require('../lib/parse');12var assert = require('assert');13var coverage = instrumenter.lastFileCoverage();14collector.add(coverage);15process.send(JSON.stringify(collector.getFinalCoverage()));16reporter.addAll(['text-summary', 'lcov']);17reporter.write(collector, sync, function () {18 console.log('All reports generated');19});20var istanbul = require('istanbul');21var collector = new istanbul.Collector();22var reporter = new istanbul.Reporter();23collector.add(JSON.parse(process.env.coverage || '{}'));24var instrumenter = new istanbul.Instrumenter();25var fs = require('fs');26var path = require('path');27var source = fs.readFileSync(path.join(__dirname, '../lib/parse.js'), 'utf8');28var instrumentedSource = instrumenter.instrumentSync(source, path.join(__dirname, '../lib/parse.js'));29fs.writeFileSync(path.join(__dirname, '../lib/parse.js'), instrumentedSource);30var parse = require('../lib/parse');31var assert = require('assert');32var coverage = instrumenter.lastFileCoverage();33collector.add(coverage);34process.send(JSON.stringify(collector.getFinalCoverage()));35reporter.addAll(['text-summary', 'lcov']);
Using AI Code Generation
1var istanbul = require('istanbul');2var collector = new istanbul.Collector();3var reporter = new istanbul.Reporter();4var sync = false;5var walk = require('walk');6var files = [];7var walker = walk.walk('./', { followLinks: false });8walker.on('file', function(root, stat, next) {9 if (stat.name.match(/\.js$/)) {10 files.push(root + '/' + stat.name);11 }12 next();13});14walker.on('end', function() {15 var instrumenter = new istanbul.Instrumenter();16 var instrumented = files.map(function(file) {17 return instrumenter.instrumentSync(require('fs').readFileSync(file, 'utf8'), file);18 });19 instrumented.forEach(function(code, index) {20 require('vm').runInThisContext(code, files[index]);21 });22 require('./test1.js');23 collector.add(global.__coverage__);24 reporter.add('text');25 reporter.add('html');26 reporter.write(collector, sync, function() {27 console.log('All reports generated');28 });29});
Using AI Code Generation
1var istanbul = require('istanbul');2var walker = istanbul.utils.walk;3walker('test', function (err, files) {4 if (err) {5 throw err;6 }7 console.log(files);8});9var istanbul = require('istanbul');10var collector = new istanbul.Collector();11var reporter = new istanbul.Reporter();12reporter.add('lcov');13reporter.write(collector, sync, function () {14 console.log('All reports generated');15});
Using AI Code Generation
1var fs = require('fs');2var istanbul = require('istanbul');3var walker = istanbul.utils.walker;4var collector = new istanbul.Collector();5var reporter = new istanbul.Reporter();6var sync = false;7var excludes = [];8var includes = [];9var verbose = false;10var reportType = 'lcovonly';11var outputFile = 'coverage.lcov';12var dir = './';13var files = [];14var instrumenter = new istanbul.Instrumenter();15var instrumentedFiles = {};16var instrumentedFileIds = {};17var instrumentedFileId = 0;18var instrumentedCode = '';19var instrumentedLineCount = 0;20var instrumentedLineMap = {};21var instrumentedLineMapReverse = {};22var instrumentedLineCount = 0;23var instrumentedLine = 0;24var instrumentedFile = '';25var instrumentedFileId = 0;26var instrumentedFileLineCount = 0;27var instrumentedFileLine = 0;28var instrumentedFileLineId = 0;29var instrumentedFileLineIdMap = {};30var instrumentedFileLineIdMapReverse = {};31var instrumentedFileLineIdCount = 0;32var instrumentedFileLineIdCountMap = {};33var instrumentedFileLineIdCountMapReverse = {};34var instrumentedFileLineIdCountMapMax = 0;35var instrumentedFileLineIdCountMapMaxLine = 0;36var instrumentedFileLineIdCountMapMaxLineId = 0;37var instrumentedFileLineIdCountMapMaxLineIdCount = 0;38var instrumentedFileLineIdCountMapMaxLineIdCountFile = '';39var instrumentedFileLineIdCountMapMaxLineIdCountFileId = 0;40var instrumentedFileLineIdCountMapMaxLineIdCountFileIdLine = 0;41var instrumentedFileLineIdCountMapMaxLineIdCountFileIdLineId = 0;42var instrumentedFileLineIdCountMapMaxLineIdCountFileIdLineIdCount = 0;43var instrumentedFileLineIdCountMapMaxLineIdCountFileIdLineIdCountLine = 0;44var instrumentedFileLineIdCountMapMaxLineIdCountFileIdLineIdCountLineId = 0;
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!!