Best JavaScript code snippet using wpt
route.js
Source:route.js
1var utils = require('./utils');2var withOverloads = require('./functionUtils').withOverloads;3var slice = Array.prototype.slice;4/**5 *6 * @param path7 * @param options8 * @return {function(controllerConstructor)}9 * @constructor10 *11 *12 * Route(path)(controllerConstructor)13 * Route(options)(controllerConstructor)14 * Route(path, options)(controllerConstructor)15 *16 */17var Route = withOverloads([18 ['string', function (path) {19 return route(path, null);20 }],21 ['object', function (options) {22 return route(null, options);23 }],24 ['string', 'object', route],25 //['string', 'object', 'array', route_arr],26 //['string', 'array', function(path, arr) { return route_arr(path, null, arr); }],27 //['options', 'array', function(options, arr) { return route_arr(null, options, arr); }]28]);29function route(path, options) {30 return function(target) {31 var controller = target;32 var route = controller.route || {};33 utils.extend(route, options);34 if (path) {35 route.path = path;36 }37 controller.route = route;38 return target;39 }40}41/**42 *43 */44Route.httpArgs = function() {45 var args = slice.call(arguments);46 return function(target, key, descriptor) {47 var action = utils.isFunction(target) ? target : descriptor.value;48 var route = action.route || {};49 var routeArgs = route.args || [];50 [].push.apply(routeArgs, args);51 route.args = routeArgs;52 action.route = route;53 return descriptor || action;54 };55};56/**57 *58 * @return {Function}59 */60Route.httpIgnore = function (target, key, descriptor) {61 if (arguments.length === 0) {62 return httpIgnore;63 }64 return httpIgnore(target, key, descriptor);65};66function httpIgnore(target, key, descriptor) {67 var action = utils.isFunction(target) ? target : descriptor.value;68 var route = action.route || {};69 route.ignore = true;70 action.route = route;71 return descriptor || action;72}73/**74 *75 * @param path76 * @param options77 * @return {*}78 *79 * Route.httpGet(options)(actionHandler)80 * Route.httpGet(path, options)(actionHandler)81 */82Route.httpGet = function (path, options) {83 return Route.http.apply(null, ['get'].concat(slice.call(arguments)));84};85/**86 *87 * @param path88 * @param options89 * @return {*}90 *91 * Route.httpPut(options)(actionHandler)92 * Route.httpPut(path, options)(actionHandler)93 *94 */95Route.httpPut = function (path, options) {96 return Route.http.apply(null, ['put'].concat(slice.call(arguments)));97};98/**99 *100 * @param path101 * @param options102 * @return {*}103 *104 * Route.httpPatch(options)(actionHandler)105 * Route.httpPatch(path, options)(actionHandler)106 *107 */108Route.httpPatch = function (path, options) {109 return Route.http.apply(null, ['patch'].concat(slice.call(arguments)));110};111/**112 *113 * @param path114 * @param options115 * @return {*}116 *117 * Route.httpPost(options)(actionHandler)118 * Route.httpPost(path, options)(actionHandler)119 *120 */121Route.httpPost = function (path, options) {122 return Route.http.apply(null, ['post'].concat(slice.call(arguments)));123};124/**125 *126 * @param path127 * @param options128 * @return {*}129 *130 * Route.httpDelete(options)(actionHandler)131 * Route.httpDelete(path, options)(actionHandler)132 *133 */134Route.httpDelete = function (path, options) {135 return Route.http.apply(null, ['delete'].concat(slice.call(arguments)));136};137/**138 * Route.http(options)(actionHandler)139 * Ex.:140 * Route.http({141 * verb: 'get',142 * path: '/api/users/:id',143 * args: [],144 * before: [],145 * after: []146 * },147 * fn)148 *149 * Route.http(verb, options)(actionHandler)150 * Ex.:151 * Route.http(152 * 'get',153 * {154 * path: '/api/users/:id',155 * args: [],156 * before: [],157 * after: []158 * },159 * fn)160 *161 * Route.http(verb, path, options)(actionHandler)162 * Ex.:163 * Route.http(164 * 'get',165 * '/api/users/:id',166 * {167 * args: [],168 * before: [],169 * after: []170 * },171 * fn)172 *173 * @param verb174 * @param path175 * @param options176 * @param fn177 * @return {*}178 */179Route.http = withOverloads([180 ['object', function (options) {181 return http(null, null, options);182 }],183 ['string', function (verb) {184 return http(verb, null, null);185 }],186 ['object', 'array', function (options, args) {187 return http_args(null, null, options, args);188 }],189 ['string', 'array', function (verb, args) {190 return http_args(verb, null, null, args);191 }],192 ['string', 'string', function (verb, path) {193 return http(verb, path, null);194 }],195 ['string', 'object', function (verb, options) {196 return http(verb, null, options);197 }],198 ['string', 'string', 'array', function (verb, path, args) {199 return http_args(verb, path, null, args);200 }],201 ['string', 'object', 'array', function (verb, options, args) {202 return http_args(verb, null, options, args);203 }],204 ['string', 'string', 'object', http],205 ['string', 'string', 'object', 'array', http_args],206]);207function http(verb, path, options) {208 return function(target, key, descriptor) {209 var action = utils.isFunction(target) ? target : descriptor.value;210 var route = action.route || {};211 utils.extend(route, options);212 if (verb) {213 route.verb = verb;214 }215 if (path) {216 route.path = path;217 }218 action.route = route;219 return descriptor || action;220 }221}222function http_args(verb, path, options, args) {223 options = utils.extend({}, options, { args: args });224 return http(verb, path, options);225}...
index.ts
Source:index.ts
1// Conditional Types2// Basics3// Conditional types can be defined as conditional expressions4// `SomeType extends OtherType ? TrueType : FalseType;`5// They can be used to describe output types in relation to input types.6interface A {7 a: number;8}9type MyType = { a: number } extends A ? string : number;10// Doesn't look to useful? We can use them also with generics!11interface StringIdEntity {12 id: string;13 toUppercase(): string;14}15interface NumberIdEntity {16 id: number;17 nextId(): number;18}19function withTypeUnions() {20 function wrapId(id: string | number): StringIdEntity | NumberIdEntity {21 throw new Error("not implemented");22 }23 const unknownEntityId = wrapId("EMAOR100200");24}25function withOverloads() {26 function wrapId(id: string): StringIdEntity;27 function wrapId(number: number): NumberIdEntity;28 function wrapId(id: string | number): StringIdEntity | NumberIdEntity {29 throw new Error("not implemented");30 }31 const stringEntityId = wrapId("EMAOR100200");32 const numberEntityId = wrapId(123);33}34function withConditionalType() {35 type ConditionalEntityId<ID extends string | number> = ID extends string36 ? StringIdEntity37 : NumberIdEntity;38 function wrapId<ID extends number | string>(id: ID): ConditionalEntityId<ID> {39 throw new Error("not implemented");40 }41 const stringEntityId = wrapId("EMAOR100200");42 const numberEntityId = wrapId(123);43}44// Not convinced yet? You can also use them to constrain generics.45type IdOf<T> = T extends { id: unknown } ? T["id"] : never;46type NoId = IdOf<number>;47type EntityId = IdOf<StringIdEntity>;48// This works also recursively for arrays49type ElementType<T> = T extends unknown[] ? ElementType<T[number]> : T;50type StringElement = ElementType<string[]>;51type NumberElement = ElementType<number[][]>;52type BooleanElement = ElementType<boolean>;53// We can even infer the "true branch" type54type InferredElementType<T> = T extends Array<infer Element>55 ? InferredElementType<Element>56 : T;57type InferredStringElement = InferredElementType<string[]>;58type InferredNumberElement = InferredElementType<number[][]>;59type InferredBooleanElement = InferredElementType<boolean>;60// Inference creates a new generic variable which can then be used in the "true branch" of the conditional type expression.61// Example: extract return type62type GetReturnType<Type> = Type extends (...args: never[]) => infer Return63 ? Return64 : never;65type NumberReturn = GetReturnType<() => number>;66class Runner<TASK> {67 constructor(private task: TASK) {}68 run(): GetReturnType<TASK> {69 // NOTE: this is not necessarily you would do in a TS codebase, but you might need to handle such cases when typing a JS library70 if (!("call" in this.task)) {71 throw new Error("Task is not callable");72 }73 const callable = this.task as unknown as {74 call: () => GetReturnType<TASK>;75 };76 return callable.call();77 }78}79const numberFun: () => number = () => 1;80const runner = new Runner(numberFun);81const result = runner.run();82const invalidRunner = new Runner("");83invalidRunner.run();84// Resources85// * Official docs: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html86// * A curated list of creative uses of TypeScript template literal types and lots of them make heavy use of conditional types: https://github.com/ghoullier/awesome-template-literal-types...
functionUtils.js
Source:functionUtils.js
1var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m;2var FN_ARG_SPLIT = /,/;3var FN_ARG = /^\s*(_?)(.+?)\1\s*$/;4var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;5/**6 * (Borrowed from AngularJS)7 * @param fn {Function}8 * @return {Array}9 */10module.exports.getArgNames = function getArgNames(fn) {11 var fnText = fn.toString().replace(STRIP_COMMENTS, '');12 var argDecl = fnText.match(FN_ARGS);13 var args = [];14 argDecl[1].split(FN_ARG_SPLIT).forEach(function (arg) {15 arg.replace(FN_ARG, function (all, underscore, name) {16 args.push(name);17 });18 });19 return args;20};21function getType(o) {22 if (Array.isArray(o)) {23 return 'array';24 }25 return typeof(o);26}27module.exports.withOverloads = function withOverloads(overloads) {28 return function () {29 var self = this;30 var args = Array.prototype.slice.call(arguments);31 var argTypes = args.map(getType);32 var argCount = argTypes.length, fn;33 overloads.some(function (overload) {34 if (overload.length === argCount + 1) {35 var match = true;36 for (var i = 0; match && i < argCount; i++) {37 match = overload[i] === argTypes[i];38 }39 if (match) {40 fn = overload[overload.length - 1];41 }42 }43 return fn;44 });45 if (fn) {46 return fn.apply(self, arguments);47 }48 throw new Error('cannot invoke function with arguments [' + argTypes.join(', ') + ']');49 };...
Using AI Code Generation
1wpt.WithOverloads(1,2,3,4,5);2wpt.WithOverloads(1,2,3,4);3wpt.WithOverloads(1,2,3);4wpt.WithOverloads(1,2);5wpt.WithOverloads(1);6wpt.WithOverloads();7wpt.WithOverloads2(1,2,3,4,5);8wpt.WithOverloads2(1,2,3,4);9wpt.WithOverloads2(1,2,3);10wpt.WithOverloads2(1,2);11wpt.WithOverloads2(1);12wpt.WithOverloads2();13wpt.WithOverloads3(1,2,3,4,5);14wpt.WithOverloads3(1,2,3,4);15wpt.WithOverloads3(1,2,3);16wpt.WithOverloads3(1,2);17wpt.WithOverloads3(1);18wpt.WithOverloads3();19wpt.WithOverloads4(1,2,3,4,5);20wpt.WithOverloads4(1,2,3,4);21wpt.WithOverloads4(1,2,3);22wpt.WithOverloads4(1,2);23wpt.WithOverloads4(1);24wpt.WithOverloads4();25wpt.WithOverloads5(1,2,3,4,5);26wpt.WithOverloads5(1,2,3,4);27wpt.WithOverloads5(1,2,3);28wpt.WithOverloads5(1,2);29wpt.WithOverloads5(1);30wpt.WithOverloads5();31wpt.WithOverloads6(1,2,3,4,5);32wpt.WithOverloads6(1,2,3,4);33wpt.WithOverloads6(1,2,
Using AI Code Generation
1var wptest = require('wptest');2wptest.WithOverloads(1, 2, 3);3var wptest = require('wptest');4wptest.WithOverloads('a', 'b', 'c');5var wptest = require('wptest');6wptest.WithOverloads(1, 2, 3, 4);7var wptest = require('wptest');8wptest.WithOverloads('a', 'b', 'c', 'd');9var wptest = require('wptest');10wptest.WithOverloads(1, 2, 3, 4, 5);11var wptest = require('wptest');12wptest.WithOverloads('a', 'b', 'c', 'd', 'e');13var wptest = require('wptest');14wptest.WithOverloads(1, 2, 3, 4, 5, 6);15var wptest = require('wptest');16wptest.WithOverloads('a', 'b', 'c', 'd', 'e', 'f');17var wptest = require('wptest');18wptest.WithOverloads(1, 2, 3, 4, 5, 6, 7);19var wptest = require('wptest');20wptest.WithOverloads('a', 'b', 'c', 'd', 'e', 'f', 'g');21var wptest = require('wptest');22wptest.WithOverloads(1, 2, 3, 4, 5
Using AI Code Generation
1var wpt = require('wpt');2var options = {3};4 if (err) return console.error(err);5 console.log(data);6});7 if (err) return console.error(err);8 console.log(data);9});10 if (err) return console.error(err);11 console.log(data);12});
Using AI Code Generation
1var wpt = require("wpt")2wpt.WithOverloads("string", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42);3var wpt = function() {4 this.WithOverloads = function() {5 var args = Array.prototype.slice.call(arguments);6 if (args.length == 1) {7 this.WithOverloads1(args[0]);8 }9 else if (args.length == 2) {10 this.WithOverloads2(args[0], args[1]);11 }12 else if (args.length == 3) {13 this.WithOverloads3(args[0], args[1], args[2]);14 }15 else if (args.length == 4) {16 this.WithOverloads4(args[0], args[1], args[2], args[3]);17 }18 else if (args.length == 5) {19 this.WithOverloads5(args[0], args[1], args[2], args[3], args[4]);20 }21 else if (args.length == 6) {22 this.WithOverloads6(args[0], args[1], args[2], args[3], args[4], args[5]);23 }24 else if (args.length == 7) {25 this.WithOverloads7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);26 }27 else if (args.length == 8) {28 this.WithOverloads8(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);29 }30 else if (args.length == 9) {31 this.WithOverloads9(args[
Using AI Code Generation
1var wptb = require("wptb");2var result = wptb.WithOverloads(1,2);3console.log(result);4module.exports = {5 WithOverloads: function (a, b) {6 return a + b;7 }8};
Using AI Code Generation
1var wpt = new ActiveXObject("WPT");2wpt.WithOverloads("test");3var wpt = new ActiveXObject("WPT");4wpt.WithOverloads = function (arg) {5 if (arg === undefined) {6 return this._WithOverloads();7 }8 return this._WithOverloads(arg);9};10var wpt = new ActiveXObject("WPT");11wpt.WithOverloads.call(wpt, "test");12var wpt = new ActiveXObject("WPT");13wpt.WithOverloads.apply(wpt, ["test"]);14var wpt = new ActiveXObject("WPT");15wpt.WithOverloads.call(wpt, "test");16var wpt = new ActiveXObject("WPT");17wpt.WithOverloads.apply(wpt, ["test"]);18var wpt = new ActiveXObject("WPT");
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!!