Best JavaScript code snippet using ava
functions.js
Source:functions.js
1$(document).ready(function() {2 module("Functions");3 test("bind", function() {4 var context = {name : 'moe'};5 var func = function(arg) { return "name: " + (this.name || arg); };6 var bound = _.bind(func, context);7 equal(bound(), 'name: moe', 'can bind a function to a context');8 bound = _(func).bind(context);9 equal(bound(), 'name: moe', 'can do OO-style binding');10 bound = _.bind(func, null, 'curly');11 equal(bound(), 'name: curly', 'can bind without specifying a context');12 func = function(salutation, name) { return salutation + ': ' + name; };13 func = _.bind(func, this, 'hello');14 equal(func('moe'), 'hello: moe', 'the function was partially applied in advance');15 func = _.bind(func, this, 'curly');16 equal(func(), 'hello: curly', 'the function was completely applied in advance');17 func = function(salutation, firstname, lastname) { return salutation + ': ' + firstname + ' ' + lastname; };18 func = _.bind(func, this, 'hello', 'moe', 'curly');19 equal(func(), 'hello: moe curly', 'the function was partially applied in advance and can accept multiple arguments');20 func = function(context, message) { equal(this, context, message); };21 _.bind(func, 0, 0, 'can bind a function to `0`')();22 _.bind(func, '', '', 'can bind a function to an empty string')();23 _.bind(func, false, false, 'can bind a function to `false`')();24 // These tests are only meaningful when using a browser without a native bind function25 // To test this with a modern browser, set underscore's nativeBind to undefined26 var F = function () { return this; };27 var Boundf = _.bind(F, {hello: "moe curly"});28 var newBoundf = new Boundf();29 equal(newBoundf.hello, undefined, "function should not be bound to the context, to comply with ECMAScript 5");30 equal(Boundf().hello, "moe curly", "When called without the new operator, it's OK to be bound to the context");31 ok(newBoundf instanceof F, "a bound instance is an instance of the original function");32 });33 test("partial", function() {34 var obj = {name: 'moe'};35 var func = function() { return this.name + ' ' + _.toArray(arguments).join(' '); };36 obj.func = _.partial(func, 'a', 'b');37 equal(obj.func('c', 'd'), 'moe a b c d', 'can partially apply');38 });39 test("bindAll", function() {40 var curly = {name : 'curly'}, moe = {41 name : 'moe',42 getName : function() { return 'name: ' + this.name; },43 sayHi : function() { return 'hi: ' + this.name; }44 };45 curly.getName = moe.getName;46 _.bindAll(moe, 'getName', 'sayHi');47 curly.sayHi = moe.sayHi;48 equal(curly.getName(), 'name: curly', 'unbound function is bound to current object');49 equal(curly.sayHi(), 'hi: moe', 'bound function is still bound to original object');50 curly = {name : 'curly'};51 moe = {52 name : 'moe',53 getName : function() { return 'name: ' + this.name; },54 sayHi : function() { return 'hi: ' + this.name; }55 };56 raises(function() { _.bindAll(moe); }, Error, 'throws an error for bindAll with no functions named');57 _.bindAll(moe, 'sayHi');58 curly.sayHi = moe.sayHi;59 equal(curly.sayHi(), 'hi: moe');60 });61 test("memoize", function() {62 var fib = function(n) {63 return n < 2 ? n : fib(n - 1) + fib(n - 2);64 };65 equal(fib(10), 55, 'a memoized version of fibonacci produces identical results');66 fib = _.memoize(fib); // Redefine `fib` for memoization67 equal(fib(10), 55, 'a memoized version of fibonacci produces identical results');68 var o = function(str) {69 return str;70 };71 var fastO = _.memoize(o);72 equal(o('toString'), 'toString', 'checks hasOwnProperty');73 equal(fastO('toString'), 'toString', 'checks hasOwnProperty');74 });75 asyncTest("delay", 2, function() {76 var delayed = false;77 _.delay(function(){ delayed = true; }, 100);78 setTimeout(function(){ ok(!delayed, "didn't delay the function quite yet"); }, 50);79 setTimeout(function(){ ok(delayed, 'delayed the function'); start(); }, 150);80 });81 asyncTest("defer", 1, function() {82 var deferred = false;83 _.defer(function(bool){ deferred = bool; }, true);84 _.delay(function(){ ok(deferred, "deferred the function"); start(); }, 50);85 });86 asyncTest("throttle", 2, function() {87 var counter = 0;88 var incr = function(){ counter++; };89 var throttledIncr = _.throttle(incr, 32);90 throttledIncr(); throttledIncr();91 equal(counter, 1, "incr was called immediately");92 _.delay(function(){ equal(counter, 2, "incr was throttled"); start(); }, 64);93 });94 asyncTest("throttle arguments", 2, function() {95 var value = 0;96 var update = function(val){ value = val; };97 var throttledUpdate = _.throttle(update, 32);98 throttledUpdate(1); throttledUpdate(2);99 _.delay(function(){ throttledUpdate(3); }, 64);100 equal(value, 1, "updated to latest value");101 _.delay(function(){ equal(value, 3, "updated to latest value"); start(); }, 96);102 });103 asyncTest("throttle once", 2, function() {104 var counter = 0;105 var incr = function(){ return ++counter; };106 var throttledIncr = _.throttle(incr, 32);107 var result = throttledIncr();108 _.delay(function(){109 equal(result, 1, "throttled functions return their value");110 equal(counter, 1, "incr was called once"); start();111 }, 64);112 });113 asyncTest("throttle twice", 1, function() {114 var counter = 0;115 var incr = function(){ counter++; };116 var throttledIncr = _.throttle(incr, 32);117 throttledIncr(); throttledIncr();118 _.delay(function(){ equal(counter, 2, "incr was called twice"); start(); }, 64);119 });120 asyncTest("more throttling", 3, function() {121 var counter = 0;122 var incr = function(){ counter++; };123 var throttledIncr = _.throttle(incr, 30);124 throttledIncr(); throttledIncr();125 ok(counter == 1);126 _.delay(function(){127 ok(counter == 2);128 throttledIncr();129 ok(counter == 3);130 start();131 }, 85);132 });133 asyncTest("throttle repeatedly with results", 6, function() {134 var counter = 0;135 var incr = function(){ return ++counter; };136 var throttledIncr = _.throttle(incr, 100);137 var results = [];138 var saveResult = function() { results.push(throttledIncr()); };139 saveResult(); saveResult();140 _.delay(saveResult, 50);141 _.delay(saveResult, 150);142 _.delay(saveResult, 160);143 _.delay(saveResult, 230);144 _.delay(function() {145 equal(results[0], 1, "incr was called once");146 equal(results[1], 1, "incr was throttled");147 equal(results[2], 1, "incr was throttled");148 equal(results[3], 2, "incr was called twice");149 equal(results[4], 2, "incr was throttled");150 equal(results[5], 3, "incr was called trailing");151 start();152 }, 300);153 });154 asyncTest("throttle triggers trailing call when invoked repeatedly", 2, function() {155 var counter = 0;156 var limit = 48;157 var incr = function(){ counter++; };158 var throttledIncr = _.throttle(incr, 32);159 var stamp = new Date;160 while ((new Date - stamp) < limit) {161 throttledIncr();162 }163 var lastCount = counter;164 ok(counter > 1);165 _.delay(function() {166 ok(counter > lastCount);167 start();168 }, 96);169 });170 asyncTest("throttle does not trigger leading call when leading is set to false", 2, function() {171 var counter = 0;172 var incr = function(){ counter++; };173 var throttledIncr = _.throttle(incr, 60, {leading: false});174 throttledIncr(); throttledIncr();175 ok(counter === 0);176 _.delay(function() {177 ok(counter == 1);178 start();179 }, 96);180 });181 asyncTest("more throttle does not trigger leading call when leading is set to false", 3, function() {182 var counter = 0;183 var incr = function(){ counter++; };184 var throttledIncr = _.throttle(incr, 100, {leading: false});185 throttledIncr();186 _.delay(throttledIncr, 50);187 _.delay(throttledIncr, 60);188 _.delay(throttledIncr, 200);189 ok(counter === 0);190 _.delay(function() {191 ok(counter == 1);192 }, 250);193 _.delay(function() {194 ok(counter == 2);195 start();196 }, 350);197 });198 asyncTest("one more throttle with leading: false test", 2, function() {199 var counter = 0;200 var incr = function(){ counter++; };201 var throttledIncr = _.throttle(incr, 100, {leading: false});202 var time = new Date;203 while (new Date - time < 350) throttledIncr();204 ok(counter <= 3);205 _.delay(function() {206 ok(counter <= 4);207 start();208 }, 200);209 });210 asyncTest("throttle does not trigger trailing call when trailing is set to false", 4, function() {211 var counter = 0;212 var incr = function(){ counter++; };213 var throttledIncr = _.throttle(incr, 60, {trailing: false});214 throttledIncr(); throttledIncr(); throttledIncr();215 ok(counter === 1);216 _.delay(function() {217 ok(counter == 1);218 throttledIncr(); throttledIncr();219 ok(counter == 2);220 _.delay(function() {221 ok(counter == 2);222 start();223 }, 96);224 }, 96);225 });226 asyncTest("debounce", 1, function() {227 var counter = 0;228 var incr = function(){ counter++; };229 var debouncedIncr = _.debounce(incr, 32);230 debouncedIncr(); debouncedIncr();231 _.delay(debouncedIncr, 16);232 _.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 96);233 });234 asyncTest("debounce asap", 4, function() {235 var a, b;236 var counter = 0;237 var incr = function(){ return ++counter; };238 var debouncedIncr = _.debounce(incr, 64, true);239 a = debouncedIncr();240 b = debouncedIncr();241 equal(a, 1);242 equal(b, 1);243 equal(counter, 1, 'incr was called immediately');244 _.delay(debouncedIncr, 16);245 _.delay(debouncedIncr, 32);246 _.delay(debouncedIncr, 48);247 _.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 128);248 });249 asyncTest("debounce asap recursively", 2, function() {250 var counter = 0;251 var debouncedIncr = _.debounce(function(){252 counter++;253 if (counter < 10) debouncedIncr();254 }, 32, true);255 debouncedIncr();256 equal(counter, 1, "incr was called immediately");257 _.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 96);258 });259 test("once", function() {260 var num = 0;261 var increment = _.once(function(){ num++; });262 increment();263 increment();264 equal(num, 1);265 });266 test("Recursive onced function.", 1, function() {267 var f = _.once(function(){268 ok(true);269 f();270 });271 f();272 });273 test("wrap", function() {274 var greet = function(name){ return "hi: " + name; };275 var backwards = _.wrap(greet, function(func, name){ return func(name) + ' ' + name.split('').reverse().join(''); });276 equal(backwards('moe'), 'hi: moe eom', 'wrapped the salutation function');277 var inner = function(){ return "Hello "; };278 var obj = {name : "Moe"};279 obj.hi = _.wrap(inner, function(fn){ return fn() + this.name; });280 equal(obj.hi(), "Hello Moe");281 var noop = function(){};282 var wrapped = _.wrap(noop, function(fn){ return Array.prototype.slice.call(arguments, 0); });283 var ret = wrapped(['whats', 'your'], 'vector', 'victor');284 deepEqual(ret, [noop, ['whats', 'your'], 'vector', 'victor']);285 });286 test("compose", function() {287 var greet = function(name){ return "hi: " + name; };288 var exclaim = function(sentence){ return sentence + '!'; };289 var composed = _.compose(exclaim, greet);290 equal(composed('moe'), 'hi: moe!', 'can compose a function that takes another');291 composed = _.compose(greet, exclaim);292 equal(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative');293 });294 test("after", function() {295 var testAfter = function(afterAmount, timesCalled) {296 var afterCalled = 0;297 var after = _.after(afterAmount, function() {298 afterCalled++;299 });300 while (timesCalled--) after();301 return afterCalled;302 };303 equal(testAfter(5, 5), 1, "after(N) should fire after being called N times");304 equal(testAfter(5, 4), 0, "after(N) should not fire unless called N times");305 equal(testAfter(0, 0), 0, "after(0) should not fire immediately");306 equal(testAfter(0, 1), 1, "after(0) should fire when first invoked");307 });...
Using AI Code Generation
1var counter = require('./counter');2console.log(counter.counter(['shaun', 'crystal', 'ryu']));3console.log(counter.adder(5,6));4console.log(counter.adder(counter.pi, 5));5var counter = function(arr){6 return 'There are ' + arr.length + ' elements in this array';7};8var adder = function(a,b){9 return `The sum of the 2 numbers is ${a+b}`;10};11var pi = 3.142;12module.exports = {13};
Using AI Code Generation
1var avail = require('./avail');2console.log(avail.incr());3console.log(avail.incr());4console.log(avail.incr());5console.log(avail.incr());6var count = 0;7exports.incr = function() {8 return ++count;9};10var count = 0;11exports.incr = function() {12 return ++count;13};14var count = 0;15exports.count = count;16var count = 0;17exports.incr = function() {18 return ++count;19};20exports.count = count;
Using AI Code Generation
1var available = require('./available');2available.incr();3var count = 0;4exports.incr = function() {5 count += 1;6 console.log(count);7};8var available = require('./available');9available.incr();10var count = 0;11exports.count = count;12exports.incr = function() {13 exports.count += 1;14 console.log(exports.count);15};16var available = require('./available');17available.incr();18var count = 0;19module.exports.count = count;20module.exports.incr = function() {21 module.exports.count += 1;22 console.log(module.exports.count);23};24var available = require('./available');25available.incr();26var count = 0;27module.exports = {28 incr: function() {29 module.exports.count += 1;30 console.log(module.exports.count);31 }32};33var available = require('./available');34available.incr();
Using AI Code Generation
1var available = require('./available');2available.incr();3var counter = 0;4function incr() {5 counter++;6}7exports.incr = incr;8var counter = 0;9exports.incr = function () {10 counter++;11};12var counter = 0;13exports = {14 incr: function () {15 counter++;16 }17};18var counter = 0;19exports = {20 incr: function () {21 counter++;22 }23};24module.exports = exports;25var counter = 0;26exports = {27 incr: function () {28 counter++;29 }30};31module.exports = exports;32exports = {33 incr: function () {34 counter++;35 }36};37var counter = 0;38module.exports = {39 incr: function () {40 counter++;41 }42};43var counter = 0;44module.exports = {45 incr: function () {46 counter++;47 }48};49exports = module.exports;50var counter = 0;51module.exports = {52 incr: function () {53 counter++;54 }55};56exports = module.exports;57exports = {58 incr: function () {59 counter++;60 }61};62var counter = 0;63module.exports = {64 incr: function () {65 counter++;66 }67};68exports = {69 incr: function () {70 counter++;71 }72};73var counter = 0;74module.exports = {75 incr: function () {76 counter++;77 }78};79exports = {80 incr: function () {81 counter++;82 }83};84var counter = 0;85exports = {86 incr: function () {87 counter++;88 }89};90module.exports = exports;91var counter = 0;92exports = {93 incr: function () {94 counter++;95 }96};97module.exports = exports;98exports = {99 incr: function () {100 counter++;101 }102};103var counter = 0;104exports = {105 incr: function () {106 counter++;107 }108};109module.exports = exports;110exports = {111 incr: function () {112 counter++;113 }114};
Using AI Code Generation
1var myModule = require('./myModule');2myModule.incr();3var count = 0;4exports.incr = function () {5 count++;6 console.log(count);7};8var myModule = require('./myModule');9myModule.incr();10myModule.incr();11myModule.incr();12var count = 0;13exports.incr = function () {14 count++;15 console.log(count);16};17console.log(module.filename);18console.log(module.id);19console.log(module.loaded);20console.log(module.parent);21Module {22 exports: {},23}
Using AI Code Generation
1var available = require('./available');2exports.incr = function (number) {3 return number + 1;4};5var available = require('./available');6module.exports = {7 incr: function (number) {8 return number + 1;9 }10};11module.exports = {12 incr: function (number) {13 return number + 1;14 },15 decr: function (number) {16 return number - 1;17 }18};19module.exports = function (number) {20 return number + 1;21};22We also learned how to export a method from a module in Node.js. We also learned how to export a function and an object with methods. We also learned how to import a method from a module
Using AI Code Generation
1var counter = require('./counter');2console.log(counter.incr());3var count = 0;4exports.incr = function() {5 return count += 1;6};7var http = require('http');8http.createServer(function (req, res) {9 res.writeHead(200, {'Content-Type': 'text/plain'});10 res.end('Hello World');11}).listen(8080);
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!!