Best JavaScript code snippet using best
Node.ts
Source:Node.ts
...89 if (Math.abs(this.balance) < 2) {90 return this91 }92 const myHeavy = this.balance > 093 const childHeavy = this.getBranch(myHeavy).balance > 094 // debug(`rotate: myHeavy=${myHeavy} childHeavy=${childHeavy} this.balance=${this.balance}`)95 if (myHeavy === childHeavy || this.getBranch(myHeavy).balance === 0) {96 return this.singleRotate()97 } else {98 return this.doubleRotate()99 }100 }101 public hitBranch(interval: Interval) {102 // Assuming not centerHit(interval), return which branch103 // (left=0, right=1) interval is in.104 return interval.start > this.xCenter105 }106 public getBranch(branch: boolean | number): Node {107 if (branch) {108 return this.rightNode!109 } else {110 return this.leftNode!111 }112 }113 public setBranch(branch: boolean | number, node: Node) {114 if (branch) {115 this.rightNode = node116 } else {117 this.leftNode = node118 }119 }120 public add(interval: Interval) {121 // debug('add', interval)122 if (this.centerHit(interval)) {123 // debug('add: center hit', interval)124 this.sCenter = this.sCenter.add(interval)125 return this126 } else {127 const direction = this.hitBranch(interval)128 const branchNode = this.getBranch(direction)129 // debug('add: on branch', interval, direction)130 if (!this.getBranch(direction)) {131 this.setBranch(direction, Node.fromInterval(interval))132 this.refreshBalance()133 return this134 } else {135 this.setBranch(direction, branchNode.add(interval))136 // debug('existing branch, rotating')137 return this.rotate()138 }139 }140 }141 public printStructure(indent = 0, tostring = false): string | undefined {142 const spaces = ' '.repeat(indent)143 let result = ''144 const logit = (s: string) => (result += spaces + s + '\n')145 result += this.toString() + '\n'146 if (this.sCenter.size) {147 logit(`- [${this.sCenter.toArray()}]`)148 }149 if (this.leftNode) {150 logit(`<: ${this.leftNode.printStructure(indent + 1, true)}`)151 }152 if (this.rightNode) {153 logit(`>: ${this.rightNode.printStructure(indent + 1, true)}`)154 }155 if (tostring) {156 return result157 } else {158 console.log(result)159 }160 }161 public toString() {162 return `Node<${this.xCenter}, depth=${this.depth}, balance=${this.balance}>`163 }164 public searchPoint(165 point: number,166 resultBuilder: SortedSet.Builder<Interval>167 ) {168 // Returns all intervals that contain point.169 // debug('searchPoint: point=', point, this.toString())170 // debug('searchPoint: result=', result)171 this.sCenter.forEach((interval) => {172 // debug('searchPoint: interval=', interval)173 if (interval.start <= point && point < interval.end) {174 // debug('searchPoint interval', interval)175 resultBuilder.add(interval)176 }177 })178 if (point < this.xCenter && this.getBranch(0)) {179 this.getBranch(0).searchPoint(point, resultBuilder)180 } else if (point > this.xCenter && this.getBranch(1)) {181 this.getBranch(1).searchPoint(point, resultBuilder)182 }183 return resultBuilder184 }185 public searchOverlap(pointList: number[]): SortedSet<Interval> {186 const resultBuilder = IntervalSet.empty().toBuilder()187 for (const point of pointList) {188 this.searchPoint(point, resultBuilder)189 }190 return resultBuilder.build()191 }192 public remove(interval: Interval): Node {193 /*194 Returns self after removing the interval and balancing.195 If interval is not present, raise ValueError.196 */197 // since this is a list, called methods can set this to [1],198 // making it true199 return this.removeIntervalHelper(interval, [], true)200 }201 public removeIntervalHelper(202 interval: Interval,203 done: number[],204 shouldRaiseError = false205 ): Node {206 /*207 Returns self after removing interval and balancing.208 If interval doesn't exist, raise ValueError.209 This method may set done to [1] to tell all callers that210 rebalancing has completed.211 See Eternally Confuzzled's jsw_remove_r function (lines 1-32)212 in his AVL tree article for reference.213 */214 debug(`removeIntervalHelper: ${this.toString()}`)215 if (this.centerHit(interval)) {216 debug('center hit')217 if (!shouldRaiseError && !this.sCenter.has(interval)) {218 done.push(1)219 return this220 }221 try {222 // raises error if interval not present - this is223 // desired.224 this.sCenter = this.sCenter.remove(interval)225 } catch (e) {226 this.printStructure()227 throw new TypeError(interval.toString())228 }229 if (this.sCenter.size) {230 // keep this node231 done.push(1) // no rebalancing necessary232 debug('removeIntervalHelper: Removed, no rebalancing.')233 return this234 } else {235 // If we reach here, no intervals are left in this.sCenter236 // So, prune self.237 debug('removeIntervalHelper: pruning self')238 return this.prune()239 }240 } else {241 // interval not in sCenter242 debug('removeIntervalHelper: not in center')243 const direction = this.hitBranch(interval)244 let branch = this.getBranch(direction)245 if (!this.getBranch(direction)) {246 if (shouldRaiseError) {247 throw new TypeError()248 }249 done.push(1)250 return this251 }252 debug(`removeIntervalHelper: Descending to ${direction} branch`)253 branch = branch.removeIntervalHelper(interval, done, shouldRaiseError)254 this.setBranch(direction, branch)255 // Clean up256 if (!done.length) {257 debug(`removeIntervalHelper: rotating ${this.xCenter}`)258 return this.rotate()259 }260 return this261 }262 }263 public prune(): Node {264 /*265 On a subtree where the root node's sCenter is empty,266 return a new subtree with no empty sCenters.267 */268 const leftBranch = this.getBranch(0)269 const rightBranch = this.getBranch(1)270 if (!leftBranch || !rightBranch) {271 // if I have an empty branch272 const direction = !leftBranch // graft the other branch here273 debug(`prune: Grafting ${direction ? 'right' : 'left'} branch`)274 return this.getBranch(direction)275 } else {276 // Replace the root node with the greatest predecessor.277 const result = this.getBranch(0).popGreatestChild()278 const heir = result[0]279 const newBranch = result[1]280 this.setBranch(0, newBranch)281 debug(`prune: Replacing ${this} with ${heir}`)282 // Set up the heir as the new root node283 heir.setBranch(0, this.getBranch(0))284 heir.setBranch(1, this.getBranch(1))285 // popping the predecessor may have unbalanced this node;286 // fix it287 heir.refreshBalance()288 return heir.rotate()289 }290 }291 public popGreatestChild(): [Node, Node] {292 /*293 Used when pruning a node with both a left and a right branch.294 Returns (greatestChild, node), where:295 * greatestChild is a new node to replace the removed node.296 * node is the subtree after:297 - removing the greatest child298 - balancing299 - moving overlapping nodes into greatest_child300 Assumes that this.sCenter is not empty.301 See Eternally Confuzzled's jsw_remove_r function (lines 34-54)302 in his AVL tree article for reference.303 */304 if (!this.rightNode) {305 // This node is the greatest child.306 // To reduce the chances of an overlap with a parent, return307 // a child node containing the smallest possible number of308 // intervals, as close as possible to the maximum bound.309 const compareEndFirst = (a: Interval, b: Interval) => {310 const key = (iv) => `${iv.end},${iv.start},${iv.data}`311 const keyA = key(a)312 const keyB = key(b)313 return keyA.localeCompare(keyB)314 }315 const ivs = this.sCenter.toArray().sort(compareEndFirst)316 const maxIv = ivs.pop()!317 let newXCenter = this.xCenter318 while (ivs.length) {319 const nextMaxIv = ivs.pop()!320 if (nextMaxIv.end === maxIv.end) {321 continue322 }323 newXCenter = Math.max(newXCenter, nextMaxIv.end)324 }325 // Create a new node with the largest x_center possible.326 const child = Node.fromIntervals(327 this.sCenter.filter((iv) => iv.containsPoint(newXCenter)).toArray()328 )!329 child.xCenter = newXCenter330 this.sCenter = this.sCenter.difference(child.sCenter)331 if (this.sCenter.size) {332 return [child, this]333 } else {334 return [child, this.getBranch(0)] // Rotate left child up335 }336 } else {337 const [greatestChild, newRightBranch] =338 this.getBranch(1).popGreatestChild()339 this.setBranch(1, newRightBranch)340 this.refreshBalance()341 let newSelf = this.rotate()342 // Move any overlaps into greatest_child343 newSelf.sCenter.forEach((iv) => {344 if (iv.containsPoint(greatestChild.xCenter)) {345 newSelf.sCenter = newSelf.sCenter.remove(iv)346 greatestChild.add(iv)347 }348 })349 if (newSelf.sCenter.size) {350 return [greatestChild, newSelf]351 } else {352 newSelf = newSelf.prune()353 return [greatestChild, newSelf]354 }355 }356 }357 public verify(parents: SortedSet<number> = SortedSet.empty<number>()) {358 // Recursively ensures that the invariants of an interval subtree hold.359 const constructor = this.sCenter.constructor.name360 assert(361 constructor === 'SortedSetLeaf',362 `sCenter type is incorrect: ${constructor}`363 )364 const bal = this.balance365 assert(366 Math.abs(bal) < 2,367 "Error: Rotation should have happened, but didn't!"368 )369 this.refreshBalance()370 assert(bal === this.balance, 'Error: this.balance not set correctly!')371 assert(372 this.sCenter.size,373 `Error: sCenter is empty!\n${this.printStructure(0, true)}`374 )375 this.sCenter.forEach((iv) => {376 assert(typeof iv.start === 'number', `start not number: ${iv.start}`)377 assert(typeof iv.end === 'number', `end not number: ${iv.end}`)378 assert(iv.start < iv.end, 'start comes before end')379 assert(iv.overlaps(this.xCenter), 'does not overlap center')380 parents.forEach((parent: number) => {381 assert(!iv.containsPoint(parent), 'Error: Overlaps ancestor')382 })383 })384 if (this.getBranch(0)) {385 assert(386 this.getBranch(0).xCenter < this.xCenter,387 'Error: Out-of-order left child !\n' + this.printStructure(0, true)388 )389 this.getBranch(0).verify(parents.union([this.xCenter]))390 }391 if (this.getBranch(1)) {392 assert(393 this.getBranch(1).xCenter > this.xCenter,394 'Error: Out-of-order right child!\n' + this.printStructure(0, true)395 )396 this.getBranch(1).verify(parents.union([this.xCenter]))397 }398 }399 public allChildren(): SortedSet<Interval> {400 return this.allChildrenHelper(IntervalSet.empty())401 }402 private allChildrenHelper(result: SortedSet<Interval>): SortedSet<Interval> {403 result = result.addAll(this.sCenter)404 if (this.getBranch(0)) {405 result = this.getBranch(0).allChildrenHelper(result)406 }407 if (this.getBranch(1)) {408 result = this.getBranch(1).allChildrenHelper(result)409 }410 return result411 }412 private doubleRotate() {413 // First rotation414 const myHeavy = this.balance > 0415 this.setBranch(myHeavy, this.getBranch(myHeavy).singleRotate())416 this.refreshBalance()417 // Second rotation418 return this.singleRotate()419 }420 private singleRotate() {421 // Single rotation. Assumes that balance is +-2.422 assert(this.balance !== 0)423 const heavy = this.balance > 0424 const light = !heavy425 const save = this.getBranch(heavy)426 // this.verify(new IntervalSet([]))427 // debug('singleRotate', this, 'bal=', this.balance, save.balance)428 // assert(save.getBranch(light))429 this.setBranch(heavy, save.getBranch(light))430 save.setBranch(light, this.rotate()) // Needed to ensure the 2 and 3 are balanced under new subnode431 // Some intervals may overlap both this.xCenter and save.xCenter432 // Promote those to the new tip of the tree433 const promotees: Interval[] = []434 save.getBranch(light).sCenter.forEach((iv) => {435 if (save.centerHit(iv)) {436 promotees.push(iv)437 }438 })439 if (promotees.length) {440 // debug('have promotees', promotees)441 for (const iv of promotees) {442 save.setBranch(light, save.getBranch(light).remove(iv))443 }444 save.sCenter = save.sCenter.addAll(promotees)445 }446 save.refreshBalance()447 return save448 }...
branches.test.js
Source:branches.test.js
2const {union} = require('lodash');3const semver = require('semver');4const proxyquire = require('proxyquire');5const getBranch = (branches, branch) => branches.find(({name}) => name === branch);6const release = (branches, name, version) => getBranch(branches, name).tags.push({version});7const merge = (branches, source, target, tag) => {8 getBranch(branches, target).tags = union(9 getBranch(branches, source).tags.filter(({version}) => !tag || semver.cmp(version, '<=', tag)),10 getBranch(branches, target).tags11 );12};13test('Enforce ranges with branching release workflow', async (t) => {14 const branches = [15 {name: '1.x', tags: []},16 {name: '1.0.x', tags: []},17 {name: 'master', tags: []},18 {name: 'next', tags: []},19 {name: 'next-major', tags: []},20 {name: 'beta', prerelease: true, tags: []},21 {name: 'alpha', prerelease: true, tags: []},22 ];23 const getBranches = proxyquire('../../lib/branches', {'./get-tags': () => branches, './expand': () => []});24 let result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({25 name,26 range,27 }));28 t.is(getBranch(result, '1.0.x').range, '>=1.0.0 <1.0.0', 'Cannot release on 1.0.x before a releasing on master');29 t.is(getBranch(result, '1.x').range, '>=1.1.0 <1.0.0', 'Cannot release on 1.x before a releasing on master');30 t.is(getBranch(result, 'master').range, '>=1.0.0');31 t.is(getBranch(result, 'next').range, '>=1.0.0');32 t.is(getBranch(result, 'next-major').range, '>=1.0.0');33 release(branches, 'master', '1.0.0');34 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({35 name,36 range,37 }));38 t.is(getBranch(result, '1.0.x').range, '>=1.0.0 <1.0.0', 'Cannot release on 1.0.x before a releasing on master');39 t.is(getBranch(result, '1.x').range, '>=1.1.0 <1.0.0', 'Cannot release on 1.x before a releasing on master');40 t.is(getBranch(result, 'master').range, '>=1.0.0');41 t.is(getBranch(result, 'next').range, '>=1.0.0');42 t.is(getBranch(result, 'next-major').range, '>=1.0.0');43 release(branches, 'master', '1.0.1');44 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({45 name,46 range,47 }));48 t.is(getBranch(result, 'master').range, '>=1.0.1', 'Can release only > than 1.0.1 on master');49 t.is(getBranch(result, 'next').range, '>=1.0.1', 'Can release only > than 1.0.1 on next');50 t.is(getBranch(result, 'next-major').range, '>=1.0.1', 'Can release only > than 1.0.1 on next-major');51 merge(branches, 'master', 'next');52 merge(branches, 'master', 'next-major');53 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({54 name,55 range,56 }));57 t.is(getBranch(result, 'master').range, '>=1.0.1', 'Can release only > than 1.0.1 on master');58 t.is(getBranch(result, 'next').range, '>=1.0.1', 'Can release only > than 1.0.1 on next');59 t.is(getBranch(result, 'next-major').range, '>=1.0.1', 'Can release only > than 1.0.1 on next-major');60 release(branches, 'next', '1.1.0');61 release(branches, 'next', '1.1.1');62 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({63 name,64 range,65 }));66 t.is(getBranch(result, 'master').range, '>=1.0.1 <1.1.0', 'Can release only patch, > than 1.0.1 on master');67 t.is(getBranch(result, 'next').range, '>=1.1.1', 'Can release only > than 1.1.1 on next');68 t.is(getBranch(result, 'next-major').range, '>=1.1.1', 'Can release > than 1.1.1 on next-major');69 release(branches, 'next-major', '2.0.0');70 release(branches, 'next-major', '2.0.1');71 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({72 name,73 range,74 }));75 t.is(getBranch(result, 'master').range, '>=1.0.1 <1.1.0', 'Can release only patch, > than 1.0.1 on master');76 t.is(getBranch(result, 'next').range, '>=1.1.1 <2.0.0', 'Can release only patch or minor, > than 1.1.0 on next');77 t.is(getBranch(result, 'next-major').range, '>=2.0.1', 'Can release any version, > than 2.0.1 on next-major');78 merge(branches, 'next-major', 'beta');79 release(branches, 'beta', '3.0.0-beta.1');80 merge(branches, 'beta', 'alpha');81 release(branches, 'alpha', '4.0.0-alpha.1');82 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({83 name,84 range,85 }));86 t.is(getBranch(result, 'next-major').range, '>=2.0.1', 'Can release any version, > than 2.0.1 on next-major');87 merge(branches, 'master', '1.0.x');88 merge(branches, 'master', '1.x');89 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({90 name,91 range,92 }));93 t.is(getBranch(result, 'master').range, '>=1.0.1 <1.1.0', 'Can release only patch, > than 1.0.1 on master');94 t.is(95 getBranch(result, '1.0.x').range,96 '>=1.0.1 <1.0.1',97 'Cannot release on 1.0.x before >= 1.1.0 is released on master'98 );99 t.is(getBranch(result, '1.x').range, '>=1.1.0 <1.0.1', 'Cannot release on 1.x before >= 1.2.0 is released on master');100 release(branches, 'master', '1.0.2');101 release(branches, 'master', '1.0.3');102 release(branches, 'master', '1.0.4');103 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({104 name,105 range,106 }));107 t.is(getBranch(result, 'master').range, '>=1.0.4 <1.1.0', 'Can release only patch, > than 1.0.4 on master');108 t.is(109 getBranch(result, '1.0.x').range,110 '>=1.0.1 <1.0.2',111 'Cannot release on 1.0.x before >= 1.1.0 is released on master'112 );113 t.is(getBranch(result, '1.x').range, '>=1.1.0 <1.0.2', 'Cannot release on 1.x before >= 1.2.0 is released on master');114 merge(branches, 'next', 'master');115 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({116 name,117 range,118 }));119 t.is(getBranch(result, 'master').range, '>=1.1.1', 'Can release only > than 1.1.1 on master');120 t.is(getBranch(result, 'next').range, '>=1.1.1 <2.0.0', 'Can release only patch or minor, > than 1.1.1 on next');121 t.is(getBranch(result, 'next-major').range, '>=2.0.1', 'Can release any version, > than 2.0.1 on next-major');122 t.is(123 getBranch(result, '1.0.x').range,124 '>=1.0.1 <1.0.2',125 'Cannot release on 1.0.x before 1.0.x version from master are merged'126 );127 t.is(getBranch(result, '1.x').range, '>=1.1.0 <1.0.2', 'Cannot release on 1.x before >= 2.0.0 is released on master');128 merge(branches, 'master', '1.0.x', '1.0.4');129 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({130 name,131 range,132 }));133 t.is(getBranch(result, 'master').range, '>=1.1.1', 'Can release only > than 1.1.1 on master');134 t.is(getBranch(result, '1.0.x').range, '>=1.0.4 <1.1.0', 'Can release on 1.0.x only within range');135 t.is(getBranch(result, '1.x').range, '>=1.1.0 <1.1.0', 'Cannot release on 1.x before >= 2.0.0 is released on master');136 merge(branches, 'master', '1.x');137 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({138 name,139 range,140 }));141 t.is(getBranch(result, 'master').range, '>=1.1.1', 'Can release only > than 1.1.1 on master');142 t.is(getBranch(result, '1.0.x').range, '>=1.0.4 <1.1.0', 'Can release on 1.0.x only within range');143 t.is(getBranch(result, '1.x').range, '>=1.1.1 <1.1.1', 'Cannot release on 1.x before >= 2.0.0 is released on master');144 merge(branches, 'next-major', 'next');145 merge(branches, 'next', 'master');146 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({147 name,148 range,149 }));150 t.is(getBranch(result, 'master').range, '>=2.0.1', 'Can release only > than 2.0.1 on master');151 t.is(getBranch(result, 'next').range, '>=2.0.1', 'Can release only > than 2.0.1 on next');152 t.is(getBranch(result, 'next-major').range, '>=2.0.1', 'Can release only > than 2.0.1 on next-major');153 t.is(getBranch(result, '1.x').range, '>=1.1.1 <2.0.0', 'Can release on 1.x only within range');154 merge(branches, 'beta', 'master');155 release(branches, 'master', '3.0.0');156 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({157 name,158 range,159 }));160 t.is(getBranch(result, 'master').range, '>=3.0.0', 'Can release only > than 3.0.0 on master');161 t.is(getBranch(result, 'next').range, '>=3.0.0', 'Can release only > than 3.0.0 on next');162 t.is(getBranch(result, 'next-major').range, '>=3.0.0', 'Can release only > than 3.0.0 on next-major');163 branches.push({name: '1.1.x', tags: []});164 merge(branches, '1.x', '1.1.x');165 result = (await getBranches('repositoryUrl', 'master', {options: {branches}})).map(({name, range}) => ({166 name,167 range,168 }));169 t.is(getBranch(result, '1.0.x').range, '>=1.0.4 <1.1.0', 'Can release on 1.0.x only within range');170 t.is(getBranch(result, '1.1.x').range, '>=1.1.1 <1.2.0', 'Can release on 1.1.x only within range');171 t.is(getBranch(result, '1.x').range, '>=1.2.0 <2.0.0', 'Can release on 1.x only within range');172});173test('Throw SemanticReleaseError for invalid configurations', async (t) => {174 const branches = [175 {name: '123', range: '123', tags: []},176 {name: '1.x', tags: []},177 {name: 'maintenance-1', range: '1.x', tags: []},178 {name: '1.x.x', tags: []},179 {name: 'beta', prerelease: '', tags: []},180 {name: 'alpha', prerelease: 'alpha', tags: []},181 {name: 'preview', prerelease: 'alpha', tags: []},182 ];183 const getBranches = proxyquire('../../lib/branches', {'./get-tags': () => branches, './expand': () => []});184 const errors = [...(await t.throwsAsync(getBranches('repositoryUrl', 'master', {options: {branches}})))];185 t.is(errors[0].name, 'SemanticReleaseError');...
WebRTCToggle.js
Source:WebRTCToggle.js
...13 onUninstalling: function(addon) {14 if (addon.id == "webrtc-permissions-ui-toggle@lakora.us") {15 var svc = Components.classes["@mozilla.org/preferences-service;1"]16 .getService(Components.interfaces.nsIPrefService);17 svc.getBranch("media.navigator.permission.")18 .setBoolPref("disabled", false);19 svc.getBranch("media.navigator.")20 .clearUserPref("enabled");21 svc.getBranch("media.peerconnection.")22 .clearUserPref("enabled");23 svc.getBranch("media.gmp-manager.")24 .clearUserPref("url");25 svc.getBranch("media.gmp-gmpopenh264.")26 .clearUserPref("enabled");27 }28 },29 onDisabling: function(addon) {30 if (addon.id == "webrtc-permissions-ui-toggle@lakora.us") {31 var svc = Components.classes["@mozilla.org/preferences-service;1"]32 .getService(Components.interfaces.nsIPrefService);33 svc.getBranch("media.navigator.permission.")34 .setBoolPref("disabled", false);35 svc.getBranch("media.navigator.")36 .clearUserPref("enabled");37 svc.getBranch("media.peerconnection.")38 .clearUserPref("enabled");39 svc.getBranch("media.gmp-manager.")40 .clearUserPref("url");41 svc.getBranch("media.gmp-gmpopenh264.")42 .clearUserPref("enabled");43 }44 }45});46/***********************************************************47class definition48***********************************************************/49//class constructor50function WebRTCToggle() { }51WebRTCToggle.installOpenH264 = async function () {52 const strings = Components.classes["@mozilla.org/intl/stringbundle;1"]53 .getService(Components.interfaces.nsIStringBundleService)54 .createBundle("chrome://webrtc-permissions-ui-toggle/locale/webrtc-permissions-ui-toggle.properties");55 const title = strings.GetStringFromName("title");56 // Check if OpenH264 is enabled or disabled. If not set, ask the user.57 let openH264Enabled;58 const branch = Components.classes["@mozilla.org/preferences-service;1"]59 .getService(Components.interfaces.nsIPrefService)60 .getBranch("media.gmp-gmpopenh264.");61 try {62 openH264Enabled = branch.getBoolPref("enabled");63 } catch (e) {64 const prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]65 .getService(Components.interfaces.nsIPromptService);66 const buttonIndex = prompts.confirmEx(67 null,68 title,69 strings.GetStringFromName("openH264EnablePromptMessage"),70 prompts.STD_YES_NO_BUTTONS + prompts.BUTTON_POS_2 * prompts.BUTTON_TITLE_IS_STRING,71 null,72 null,73 strings.GetStringFromName("viewLicense"),74 null,75 {});76 if (buttonIndex == 0) {77 branch.setBoolPref("enabled", true);78 openH264Enabled = true;79 } else if (buttonIndex == 1) {80 return;81 } else if (buttonIndex == 2) {82 const ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]83 .getService(Components.interfaces.nsIWindowWatcher);84 WebRTCToggle.licenseWindow = ww.openWindow(null, "http://www.openh264.org/BINARY_LICENSE.txt", "OpenH264License", "menubar,modal,width=600,height=400,resizable", null);85 WebRTCToggle.installOpenH264();86 return;87 }88 }89 if (openH264Enabled) {90 // Fix GMP manager URL: replace SeaMonkey version number with Gecko/Firefox version number.91 let current_url;92 try {93 current_url = Components.classes["@mozilla.org/preferences-service;1"]94 .getService(Components.interfaces.nsIPrefService)95 .getBranch("media.gmp-manager.")96 .getCharPref("url");97 if (!current_url) throw new Error("media.gmp-manager.url is null or empty");98 } catch (e) {99 console.error(e);100 return;101 }102 let new_url = current_url.replace("%VERSION%", "%PLATFORM_VERSION%");103 if (new_url != current_url) {104 console.log("Updating media.gmp-manager.url to " + new_url);105 Components.classes["@mozilla.org/preferences-service;1"]106 .getService(Components.interfaces.nsIPrefService)107 .getBranch("media.gmp-manager.")108 .setCharPref("url", new_url);109 }110 // Check if OpenH264 is installed.111 const gmpInstallManager = new GMPInstallManager();112 const resp = await gmpInstallManager.checkForAddons();113 const addon = resp.gmpAddons.filter(a => a.id === "gmp-gmpopenh264")[0];114 console.log(addon && addon.isInstalled);115 if (addon && !addon.isInstalled) {116 // Install OpenH264117 await gmpInstallManager.installAddon(addon);118 console.log("Installed OpenH264");119 }120 }121}122// class definition123WebRTCToggle.prototype = {124 // properties required for XPCOM registration:125 classDescription: "WebRTC Toggle (WebRTC Permissions UI Override)",126 classID: Components.ID("{265ba61d-8b89-4739-acc6-24df0bf7eb70}"),127 contractID: "@propfire/startup;1",128 QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIObserver]),129 // add to category manager130 _xpcom_categories: [{category: "profile-after-change"}],131 132 prefBranch: null,133 observe: function(aSubject, aTopic, aData)134 {135 switch (aTopic) 136 {137 case "profile-after-change":138 // Set up listeners for the cases below.139 Components.classes["@mozilla.org/observer-service;1"]140 .getService(Components.interfaces.nsIObserverService)141 .addObserver(this, "quit-application", false);142 143 this.prefBranch = Components.classes["@mozilla.org/preferences-service;1"]144 .getService(Components.interfaces.nsIPrefService)145 .getBranch("media.navigator.permission.");146 this.prefBranch.addObserver("", this, false);147 break;148 case "quit-application":149 // Turn the override off when closing the application,150 // regardless of whether or not the add-on is going to be151 // uninstalled.152 var svc = Components.classes["@mozilla.org/preferences-service;1"]153 .getService(Components.interfaces.nsIPrefService);154 svc.getBranch("media.navigator.permission.")155 .setBoolPref("disabled", false);156 svc.getBranch("media.navigator.")157 .clearUserPref("enabled");158 svc.getBranch("media.peerconnection.")159 .clearUserPref("enabled");160 break;161 case "nsPref:changed":162 var strings = Components.classes["@mozilla.org/intl/stringbundle;1"]163 .getService(Components.interfaces.nsIStringBundleService)164 .createBundle("chrome://webrtc-permissions-ui-toggle/locale/webrtc-permissions-ui-toggle.properties");165 166 // Determine which message to show to the user.167 var title = strings.GetStringFromName("title");168 var newValue = Components.classes["@mozilla.org/preferences-service;1"]169 .getService(Components.interfaces.nsIPrefService)170 .getBranch("media.navigator.permission.")171 .getBoolPref(aData);172 173 var message = strings.GetStringFromName(newValue ? "turnedOn" : "turnedOff");174 var type = Components.classes["@mozilla.org/preferences-service;1"]175 .getService(Components.interfaces.nsIPrefService)176 .getBranch("extensions.webrtc-permissions-ui-toggle.")177 .getCharPref("notify-type");178 switch (type) {179 case "non-modal":180 // Use nsIAlertsService to show a notification.181 // If you want the native Windows 10 notifications you182 // can use the GNotifier add-on along with this one.183 try {184 Components.classes['@mozilla.org/alerts-service;1']185 .getService(Components.interfaces.nsIAlertsService)186 .showAlertNotification(null, title, message, false, '', null);187 } catch (e) {188 Components.classes["@mozilla.org/embedcomp/prompt-service;1"]189 .getService(Components.interfaces.nsIPromptService)190 .alert(null, title, message);...
Using AI Code Generation
1var BestRoute = require('./BestRoute');2var br = new BestRoute();3var route = br.getBranch('A', 'C');4console.log(route);5var BestRoute = require('./BestRoute');6var br = new BestRoute();7var route = br.getBranch('A', 'D');8console.log(route);9var BestRoute = require('./BestRoute');10var br = new BestRoute();11var route = br.getBranch('A', 'E');12console.log(route);13var BestRoute = require('./BestRoute');14var br = new BestRoute();15var route = br.getBranch('B', 'E');16console.log(route);17var BestRoute = require('./BestRoute');18var br = new BestRoute();19var route = br.getBranch('C', 'E');20console.log(route);21var BestRoute = require('./BestRoute');22var br = new BestRoute();23var route = br.getBranch('D', 'E');24console.log(route);25var BestRoute = require('./BestRoute');26var br = new BestRoute();27var route = br.getBranch('E', 'E');28console.log(route);29var BestRoute = require('./BestRoute');30var br = new BestRoute();31var route = br.getBranch('A', 'A');32console.log(route);
Using AI Code Generation
1var BestPath = require('./bestPath');2var bestPath = new BestPath();3var path = bestPath.getBranch(0, 0, 0);4console.log(path);5var BestPath = function() {6 var _this = this;7 var _path = [];8 var _branch = [];9 var _branchCount = 0;10 var _branchIndex = 0;11 var _branchLength = 0;12 var _branchMaxLength = 0;13 var _branchMaxIndex = 0;14 var _branchMaxCount = 0;15 var _branchMax = [];16 var _bestPath = [];17 var _bestPathLength = 0;18 var _bestPathMax = 0;19 var _bestPathIndex = 0;20 var _bestPathCount = 0;21 var _createTree = function() {22 _path = [];23 _branch = [];24 _branchCount = 0;25 _branchIndex = 0;26 _branchLength = 0;27 _branchMaxLength = 0;28 _branchMaxIndex = 0;29 _branchMaxCount = 0;30 _branchMax = [];31 _bestPath = [];32 _bestPathLength = 0;33 _bestPathMax = 0;34 _bestPathIndex = 0;35 _bestPathCount = 0;36 for (var i = 0; i < 1000; i++) {37 _path.push(Math.floor(Math.random() * 2));38 }39 for (var i = 0; i < _path.length; i++) {40 if (_path[i] == 0) {41 _branch.push(1);42 } else {43 _branch.push(-1);44 }45 }46 };47 var _calculateBestPath = function() {48 for (var i = 0; i < _branch.length; i++) {49 _branchCount += _branch[i];50 _branchLength++;51 if (_branchCount > _branchMaxCount) {52 _branchMaxCount = _branchCount;
Using AI Code Generation
1var bestRoute = require("./bestRoute.js");2var bestRouteObj = new bestRoute.BestRoute();3var branch = bestRouteObj.getBranch("A", "B");4console.log(branch);5var bestRoute = require("./bestRoute.js");6var bestRouteObj = new bestRoute.BestRoute();7var branch = bestRouteObj.getBranch("A", "E");8console.log(branch);9var bestRoute = require("./bestRoute.js");10var bestRouteObj = new bestRoute.BestRoute();11var branch = bestRouteObj.getBranch("A", "F");12console.log(branch);13var bestRoute = require("./bestRoute.js");14var bestRouteObj = new bestRoute.BestRoute();15var branch = bestRouteObj.getBranch("A", "G");16console.log(branch);17var bestRoute = require("./bestRoute.js");18var bestRouteObj = new bestRoute.BestRoute();19var branch = bestRouteObj.getBranch("A", "H");20console.log(branch);21var bestRoute = require("./bestRoute.js");22var bestRouteObj = new bestRoute.BestRoute();23var branch = bestRouteObj.getBranch("A", "I");24console.log(branch);
Using AI Code Generation
1var bestBank = require('./bestbank');2var bank = new bestBank();3var branch = bank.getBranch("123");4console.log(branch);5console.log("branch name is " + branch.name);6var bestBank = require('./bestbank');7var bank = new bestBank();8console.log("branch name is " + bank.getBranch("123").name);9var bestBank = require('./bestbank');10var bank = new bestBank();11console.log("branch name is " + bank.getBranch("123").name);12console.log("branch name is " + bank.getBranch("456").name);13console.log("branch name is " + bank.getBranch("789").name);14var bestBank = require('./bestbank');15var bank = new bestBank();16console.log("branch name is " + bank.getBranch("123").name);17console.log("branch name is " + bank.getBranch("456").name);18console.log("branch name is " + bank.getBranch("789").name);19console.log("branch name is " + bank.getBranch("123").name);20console.log("branch name is " + bank.getBranch("456").name);21console.log("branch name is " + bank.getBranch("789").name);22var bestBank = require('./bestbank');23var bank = new bestBank();24console.log("branch name is " + bank.getBranch("123").name);25console.log("branch name is " + bank.getBranch("456").name);26console.log("branch name is " + bank.getBranch("789").name);27console.log("branch name is " + bank.getBranch("123").name);28console.log("branch name is " + bank.getBranch("456").name);29console.log("branch name is " + bank.getBranch("789").name);30console.log("branch name is " + bank.getBranch("123").name);31console.log("branch name is " + bank.getBranch("456").name);32console.log("branch name is " + bank.getBranch("789").name);33console.log("branch name is " + bank.getBranch("123").name);34console.log("branch name is " + bank.getBranch("456").name);35console.log("branch name is " + bank.getBranch("789").name);36var bestBank = require('./
Using AI Code Generation
1var BestRoute = require('./BestRoute.js');2var br = new BestRoute();3br.addRoute("A", "B", 5);4br.addRoute("B", "C", 4);5br.addRoute("C", "D", 8);6br.addRoute("D", "C", 8);7br.addRoute("D", "E", 6);8br.addRoute("A", "D", 5);9br.addRoute("C", "E", 2);10br.addRoute("E", "B", 3);11br.addRoute("A", "E", 7);12var branch = br.getBranch("C");13console.log(branch);14var BestRoute = require('./BestRoute.js');15var br = new BestRoute();16br.addRoute("A", "B", 5);17br.addRoute("B", "C", 4);18br.addRoute("C", "D", 8);19br.addRoute("D", "C", 8);20br.addRoute("D", "E", 6);21br.addRoute("A", "D", 5);22br.addRoute("C", "E", 2);23br.addRoute("E", "B", 3);24br.addRoute("A", "E", 7);25var route = br.getBestRoute("A", "C");26console.log(route);
Using AI Code Generation
1var BestBuy = require("./BestBuy.js");2var bestBuy = new BestBuy("Best Buy", "1234 Main St", "555-555-5555");3var branch1 = new BestBuy.Branch("Branch1", "1234 Main St", "555-555-5555");4var branch2 = new BestBuy.Branch("Branch2", "1234 Main St", "555-555-5555");5var branch3 = new BestBuy.Branch("Branch3", "1234 Main St", "555-555-5555");6bestBuy.addBranch(branch1);7bestBuy.addBranch(branch2);8bestBuy.addBranch(branch3);9branch1.addSale(100);10branch1.addSale(200);11branch1.addSale(300);12branch2.addSale(400);13branch2.addSale(500);14branch2.addSale(600);15branch3.addSale(700);16branch3.addSale(800);17branch3.addSale(900);18var branch = bestBuy.getBranch();19console.log(branch.name + " had the highest sales with " + branch.getTotalSales());20var BestBuy = require("./BestBuy.js");21var bestBuy = new BestBuy("Best Buy", "1234 Main St", "555-555-5555");22var branch1 = new BestBuy.Branch("Branch1", "1234 Main St", "555-555-5555");23var branch2 = new BestBuy.Branch("Branch2", "1234 Main St", "555-555-5555");24var branch3 = new BestBuy.Branch("Branch3", "1234 Main St", "555-555-5555");25bestBuy.addBranch(branch1);26bestBuy.addBranch(branch2);27bestBuy.addBranch(branch3);28branch1.addSale(100);29branch1.addSale(200);30branch1.addSale(300);31branch2.addSale(400);32branch2.addSale(500);33branch2.addSale(600);34branch3.addSale(700);
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!!