Best JavaScript code snippet using root
exec_env.js
Source:exec_env.js
...14 } else {15 return value;16 }17}18function asNumber(value) {19 const result = Number(value);20 if (isNaN(result)) {21 throw new TypeError("Expecting a number");22 } else {23 return result;24 }25}26function assertArray(value) {27 if (value instanceof Array) {28 return value;29 } else {30 throw new TypeError("Expecting an array");31 }32}33function assertColor(value) {34 if (value instanceof Color) {35 return value;36 } else {37 throw new TypeError("Expecting a color");38 }39}40async function sleep(time) {41 time = asNumber(time);42 return new Promise(accept => {43 setTimeout(accept, time * 1000);44 });45}46function time() {47 return Date.now() / 1000;48}49function* range(first, second = undefined, third = undefined) {50 var begin = 0,51 end,52 step = 1;53 if (second != undefined) {54 begin = asNumber(first);55 end = asNumber(second);56 if (third != undefined) {57 step = asNumber(third);58 }59 } else {60 end = asNumber(first);61 }62 for (var i = begin; step >= 0 ? i < end : i > end; i += step) {63 yield i;64 }65}66function print(obj) {67 webkit.messageHandlers.system.postMessage({68 type: "log",69 message: String(obj)70 });71}72const ArrayUtils = {73 length(list) {74 assertArray(list);75 return list.length;76 },77 append(list, element) {78 assertArray(list);79 list.push(element);80 },81 insert(list, index, element) {82 assertArray(list);83 index = asNumber(index);84 list.splice(index, 0, element);85 },86 remove(list, index) {87 assertArray(list);88 index = asNumber(index);89 list.splice(index, 1);90 }91};92const MathUtils = {93 RAD_PER_DEG: Math.PI / 180,94 random(min, max) {95 min = asNumber(min);96 max = asNumber(max);97 return min + Math.random() * (max - min);98 },99 abs(value) {100 value = asNumber(value);101 return Math.abs(value);102 },103 sqrt(value) {104 value = asNumber(value);105 return Math.sqrt(value);106 },107 sin(degree) {108 degree = asNumber(degree);109 return Math.sin(degree * this.RAD_PER_DEG);110 },111 cos(degree) {112 degree = asNumber(degree);113 return Math.cos(degree * this.RAD_PER_DEG);114 },115 tan(degree) {116 degree = asNumber(degree);117 return Math.tan(degree * this.RAD_PER_DEG);118 },119 asin(value) {120 value = asNumber(value);121 return Math.asin(value) / this.RAD_PER_DEG;122 },123 acos(value) {124 value = asNumber(value);125 return Math.acos(value) / this.RAD_PER_DEG;126 },127 atan(value) {128 value = asNumber(value);129 return Math.atan(value) / this.RAD_PER_DEG;130 },131 ln(value) {132 value = asNumber(value);133 return Math.log(value);134 },135 log(value) {136 value = asNumber(value);137 return Math.log10(value);138 },139 round(value) {140 value = asNumber(value);141 return Math.round(value);142 },143 floor(value) {144 value = asNumber(value);145 return Math.floor(value);146 },147 ceil(value) {148 value = asNumber(value);149 return Math.ceil(value);150 }151};152const ColorUtils = {153 rgb(red, green, blue) {154 red = restrict(asNumber(red), 0, 255);155 green = restrict(asNumber(green), 0, 255);156 blue = restrict(asNumber(blue), 0, 255);157 return new Color("rgb(" + red + "," + green + "," + blue + ")");158 },159 rgba(red, green, blue, alpha) {160 red = restrict(asNumber(red), 0, 255);161 green = restrict(asNumber(green), 0, 255);162 blue = restrict(asNumber(blue), 0, 255);163 alpha = restrict(asNumber(alpha), 0, 1);164 return new Color(165 "rgba(" + red + "," + green + "," + blue + "," + alpha + ")"166 );167 },168 hsl(hue, saturation, lightness) {169 hue = restrict(asNumber(hue), 0, 360);170 saturation = restrict(asNumber(saturation), 0, 1) * 100;171 lightness = restrict(asNumber(lightness), 0, 1) * 100;172 return new Color("hsl(" + hue + "," + saturation + "%," + lightness + "%)");173 },174 hsla(hue, saturation, lightness, alpha) {175 hue = restrict(asNumber(hue), 0, 360);176 saturation = restrict(asNumber(saturation), 0, 1) * 100;177 lightness = restrict(asNumber(lightness), 0, 1) * 100;178 alpha = restrict(asNumber(alpha), 0, 1);179 return new Color(180 "hsla(" + hue + "," + saturation + "%," + lightness + "%," + alpha + ")"181 );182 }183};184const Graphics = {185 stroke: true,186 fill: true,187 captureContext(canvas) {188 const newContext = canvas.getContext("2d");189 if (this.context == undefined) {190 // Default style191 newContext.strokeStyle = "rgba(255,255,255,1)";192 newContext.fillStyle = "rgba(0,0,0,1)";193 newContext.lineWidth = 1;194 } else {195 newContext.strokeStyle = this.context.strokeStyle;196 newContext.fillStyle = this.context.fillStyle;197 newContext.lineWidth = this.context.lineWidth;198 newContext.lineCap = this.context.lineCap;199 newContext.lineJoin = this.context.lineJoin;200 newContext.miterLimit = this.context.miterLimit;201 newContext.lineDashOffset = this.context.lineDashOffset;202 newContext.setLineDash(this.context.getLineDash());203 }204 this.context = newContext;205 },206 origin() {207 return {208 x: this.context.canvas.width / 2,209 y: this.context.canvas.height / 2210 };211 },212 async drawLine(startX, startY, endX, endY) {213 startX = asNumber(startX);214 startY = asNumber(startY);215 endX = asNumber(endX);216 endY = asNumber(endY);217 const origin = this.origin();218 this.context.beginPath();219 this.context.moveTo(startX + origin.x, startY + origin.y);220 this.context.lineTo(endX + origin.x, endY + origin.y);221 this.context.stroke();222 await sleep(0);223 },224 async drawArc(x, y, radius, startAngle, endAngle) {225 x = asNumber(x);226 y = asNumber(y);227 radius = asNumber(radius);228 startAngle = asNumber(startAngle) - 90;229 endAngle = asNumber(endAngle) - 90;230 const origin = this.origin();231 this.context.beginPath();232 this.context.arc(233 x + origin.x,234 y + origin.y,235 radius,236 startAngle * MathUtils.RAD_PER_DEG,237 endAngle * MathUtils.RAD_PER_DEG238 );239 this.context.stroke();240 await sleep(0);241 },242 async drawRect(x, y, width, height) {243 x = asNumber(x);244 y = asNumber(y);245 width = asNumber(width);246 height = asNumber(height);247 const origin = this.origin();248 this.context.beginPath();249 this.context.rect(x + origin.x, y + origin.y, width, height);250 this.context.stroke();251 this.context.fill();252 await sleep(0);253 },254 async drawEllipse(x, y, hRadius, vRadius) {255 x = asNumber(x);256 y = asNumber(y);257 hRadius = asNumber(hRadius);258 vRadius = asNumber(vRadius);259 const origin = this.origin();260 this.context.beginPath();261 this.context.ellipse(262 x + origin.x,263 y + origin.y,264 hRadius,265 vRadius,266 0,267 0,268 Math.PI * 2269 );270 this.context.stroke();271 this.context.fill();272 await sleep(0);273 },274 clearCanvas(color) {275 assertColor(color);276 document.body.style.backgroundColor = color.text;277 this.context.clearRect(278 0,279 0,280 this.context.canvas.width,281 this.context.canvas.height282 );283 },284 setLineWidth(width) {285 width = asNumber(width);286 this.context.lineWidth = width;287 },288 setLineColor(color) {289 assertColor(color);290 this.context.strokeStyle = color.text;291 },292 setFillColor(color) {293 assertColor(color);294 this.context.fillStyle = color.text;295 }296};297const Logo = {298 x: 0,299 y: 0,300 dir: Math.PI,301 isPenDown: true,302 async forward(length) {303 length = asNumber(length);304 const originalX = this.x;305 const originalY = this.y;306 this.x -= length * Math.sin(this.dir);307 this.y += length * Math.cos(this.dir);308 if (this.isPenDown) {309 await Graphics.drawLine(originalX, originalY, this.x, this.y);310 }311 },312 async backward(length) {313 await this.forward(-asNumber(length));314 },315 turnRight(degree) {316 degree = asNumber(degree);317 this.dir = (this.dir + degree * MathUtils.RAD_PER_DEG) % (Math.PI * 2);318 },319 turnLeft(degree) {320 this.turnRight(-asNumber(degree));321 },322 penUp() {323 this.isPenDown = false;324 },325 penDown() {326 this.isPenDown = true;327 },328 async goHome() {329 if (this.isPenDown) {330 await Graphics.drawLine(this.x, this.y, 0, 0);331 }332 this.x = 0;333 this.y = 0;334 this.dir = Math.PI;...
controlFlowIterationErrors.js
Source:controlFlowIterationErrors.js
...40 x = foo(x);41 }42 x;43}44function asNumber(x: string | number): number {45 return +x;46}47function h1() {48 let x: string | number | boolean;49 x = "0";50 while (cond) {51 x = +x + 1;52 x;53 }54}55function h2() {56 let x: string | number | boolean;57 x = "0";58 while (cond) {59 x = asNumber(x) + 1;60 x;61 }62}63function h3() {64 let x: string | number | boolean;65 x = "0";66 while (cond) {67 let y = asNumber(x);68 x = y + 1;69 x;70 }71}72function h4() {73 let x: string | number | boolean;74 x = "0";75 while (cond) {76 x;77 let y = asNumber(x);78 x = y + 1;79 x;80 }81}828384//// [controlFlowIterationErrors.js]85var cond;86function len(s) {87 return s.length;88}89function f1() {90 var x;91 x = "";92 while (cond) {93 x = len(x);94 x;95 }96 x;97}98function f2() {99 var x;100 x = "";101 while (cond) {102 x;103 x = len(x);104 }105 x;106}107function g1() {108 var x;109 x = "";110 while (cond) {111 x = foo(x);112 x;113 }114 x;115}116function g2() {117 var x;118 x = "";119 while (cond) {120 x;121 x = foo(x);122 }123 x;124}125function asNumber(x) {126 return +x;127}128function h1() {129 var x;130 x = "0";131 while (cond) {132 x = +x + 1;133 x;134 }135}136function h2() {137 var x;138 x = "0";139 while (cond) {140 x = asNumber(x) + 1;141 x;142 }143}144function h3() {145 var x;146 x = "0";147 while (cond) {148 var y = asNumber(x);149 x = y + 1;150 x;151 }152}153function h4() {154 var x;155 x = "0";156 while (cond) {157 x;158 var y = asNumber(x);159 x = y + 1;160 x;161 }
...
formatting.ts
Source:formatting.ts
1import { AbbreviateCurrency } from "abbreviate-currency";2import { getPreferredCurrency } from "../store/exchangeRates";3const americanEnglishUsd = new AbbreviateCurrency({4 language: "en-US",5 digitGroups: [6 { symbol: "M", digits: 7 }, // 1,000,000+ (7 digits or more.)7 { symbol: "B", digits: 10 }, // 1,000,000,000+ (10 digits or more.)8 { symbol: "T", digits: 13 }, // 1,000,000,000,000+ (13 digits or more.)],9 ],10});11export const formatAmount = (amount) => {12 const asNumber = Number(amount);13 const asString = `${amount}`;14 const dec = asString.split(".")[1];15 if (!dec) return asString;16 if (asNumber < 1 && asNumber > 0) {17 if (dec.length < 2) return `${asNumber.toFixed(dec.length)}`;18 return `${asNumber.toFixed(3)}`;19 }20 if (dec.length < 2) return `${asNumber.toFixed(dec.length)}`;21 return `${asNumber.toFixed(2)}`;22};23export const formatCurrency = (amount) => {24 const asNumber = parseFloat(amount);25 return asNumber > 100000 && getPreferredCurrency() === "USD"26 ? americanEnglishUsd.transform(amount)27 : `${new Intl.NumberFormat("en-US", {28 style: "currency",29 currency: getPreferredCurrency(),30 minimumFractionDigits: Math.abs(amount) > 1 ? 2 : 3,31 maximumFractionDigits: Math.abs(amount) > 1 ? 2 : 3,32 }).format(asNumber)}`;33};34export const formatPercentage = (percent, showPlus = false) => {35 const asNumber = parseFloat(percent) * 100;36 return `${asNumber > 0 && showPlus ? "+" : ""}${asNumber.toFixed(2)}%`;37};38export const formatDate = (timestamp) => new Date(timestamp).toDateString();39export const formatTime = (timestamp) =>40 new Date(timestamp).toLocaleTimeString();41export const normalize = (str = "") =>42 str43 .toLowerCase()44 .trim()...
Using AI Code Generation
1var asNumber = require('./root.js').asNumber;2var asNumber = require('./child.js').asNumber;3var asNumber = require('./parent.js').asNumber;4var asNumber = require('./grandparent.js').asNumber;5var asNumber = require('./greatgrandparent.js').asNumber;6var asNumber = require('./greatgreatgrandparent.js').asNumber;7var asNumber = require('./greatgreatgreatgrandparent.js').asNumber;8var asNumber = require('./greatgreatgreatgreatgrandparent.js').asNumber;9var asNumber = require('./greatgreatgreatgreatgreatgrandparent.js').asNumber;10var asNumber = require('./greatgreatgreatgreatgreatgreatgrandparent.js').asNumber;11var asNumber = require('./greatgreatgreatgreatgreatgreatgreatgrandparent.js').asNumber;12var asNumber = require('./greatgreatgreatgreatgreatgreatgreatgreatgrandparent.js').asNumber;
Using AI Code Generation
1var asNumber = require('as-number');2var n = asNumber('5');3console.log(n);4console.log(typeof n);5var asNumber = require('as-number');6var n = asNumber('5');7console.log(n);8console.log(typeof n);9var asNumber = require('as-number');10var n = asNumber('5');11console.log(n);12console.log(typeof n);13var asNumber = require('as-number');14var n = asNumber('5');15console.log(n);16console.log(typeof n);17var asNumber = require('as-number');18var n = asNumber('5');19console.log(n);20console.log(typeof n);21var asNumber = require('as-number');22var n = asNumber('5');23console.log(n);24console.log(typeof n);25var asNumber = require('as-number');26var n = asNumber('5');27console.log(n);28console.log(typeof n);29var asNumber = require('as-number');30var n = asNumber('5');31console.log(n);32console.log(typeof n);33var asNumber = require('as-number');34var n = asNumber('5');35console.log(n);36console.log(typeof n);37var asNumber = require('as-number');38var n = asNumber('5');39console.log(n);40console.log(typeof n);41var asNumber = require('as-number');42var n = asNumber('5');43console.log(n);44console.log(typeof n);45var asNumber = require('as-number');46var n = asNumber('5');47console.log(n);48console.log(typeof n);49var asNumber = require('as-number');50var n = asNumber('5');51console.log(n);52console.log(typeof n);53var asNumber = function (value)
Using AI Code Generation
1var root = this;2var result = root.asNumber("1234");3console.log(result);4var current = this;5var result2 = current.asNumber("1234");6console.log(result2);7var result3 = global.asNumber("1234");8console.log(result3);9var result4 = global.asNumber("1234");10console.log(result4);11var result5 = global.asNumber("1234");12console.log(result5);13var result6 = global.asNumber("1234");14console.log(result6);15var result7 = global.asNumber("1234");16console.log(result7);17var result8 = global.asNumber("1234");18console.log(result8);19var result9 = global.asNumber("1234");20console.log(result9);21var result10 = global.asNumber("1234");22console.log(result10);23var result11 = global.asNumber("1234");24console.log(result11);25var result12 = global.asNumber("1234");26console.log(result12);27var result13 = global.asNumber("1234");28console.log(result13);29var result14 = global.asNumber("1234");30console.log(result14);31var result15 = global.asNumber("1234");32console.log(result15);33var result16 = global.asNumber("1234");34console.log(result16);35var result17 = global.asNumber("1234");36console.log(result17);37var result18 = global.asNumber("1234");38console.log(result18);
Using AI Code Generation
1var root = require('root');2var num = root.asNumber('1234');3console.log(num);4var num = root.asNumber('1234', 8);5console.log(num);6var num = root.asNumber('1234', 16);7console.log(num);8var root = require('root');9var str = root.asString(1234);10console.log(str);11var str = root.asString(1234, 8);12console.log(str);13var str = root.asString(1234, 16);14console.log(str);15var root = require('root');16var bool = root.asBoolean('true');17console.log(bool);18var bool = root.asBoolean('false');19console.log(bool);20var bool = root.asBoolean('yes');21console.log(bool);22var bool = root.asBoolean('no');23console.log(bool);24var bool = root.asBoolean('on');25console.log(bool);26var bool = root.asBoolean('off');27console.log(bool);28var bool = root.asBoolean('1');29console.log(bool);30var bool = root.asBoolean('0');31console.log(bool);32var root = require('root');33var date = root.asDate('2016-12-31');34console.log(date);35var date = root.asDate('2016-12-31', 'yyyy-MM-dd');36console.log(date);
Using AI Code Generation
1var root = require('./root');2var x = root.asNumber(1);3console.log(x);4var root = require('./root');5var x = root.asNumber(1);6console.log(x);7var root = require('./root');8var x = root.asNumber(1);9console.log(x);10var root = require('./root');11var x = root.asNumber(1);12console.log(x);13var root = require('./root');14var x = root.asNumber(1);15console.log(x);16var root = require('./root');17var x = root.asNumber(1);18console.log(x);19var root = require('./root');20var x = root.asNumber(1);21console.log(x);22var root = require('./root');23var x = root.asNumber(1);24console.log(x);25var root = require('./root');26var x = root.asNumber(1);27console.log(x);28var root = require('./root');29var x = root.asNumber(1);30console.log(x);
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!!