Best JavaScript code snippet using ava
tool.js
Source:tool.js
2 return {3 command: `${name} <domain>`,4 describe: `List all URLs: ${name}`,5 builder: (yargs) => {6 yargs.options('wildcard', {7 alias: ['w'],8 type: 'string',9 describe: 'Domain wildcard',10 default: '*.'11 })12 yargs.options('filter', {13 alias: ['f'],14 type: 'string',15 default: 'statuscode:200'16 })17 yargs.options('from', {18 alias: ['F'],19 type: 'string',20 describe: 'To date range',21 default: ''22 })23 yargs.options('to', {24 alias: ['T'],25 type: 'string',26 describe: 'To data range',27 default: ''28 })29 yargs.options('order', {30 alias: ['O'],31 type: 'string',32 describe: 'Order',33 choices: ['desc', 'asc'],34 default: 'desc'35 })36 yargs.options('header', {37 alias: ['H'],38 type: 'string',39 describe: 'Custom header'40 })41 yargs.options('retry', {42 alias: ['r'],43 type: 'number',44 default: 545 })46 yargs.options('timeout', {47 alias: ['t'],48 type: 'number',49 default: 3000050 })51 yargs.options('max-results', {52 alias: ['m', 'max'],53 type: 'number',54 default: Infinity55 })56 yargs.options('output-type', {57 alias: ['o', 'output'],58 type: 'string',59 choices: ['text', 'json', 'json-request'],60 default: 'text'61 })62 yargs.options('unique', {63 alias: ['u'],64 type: 'boolean',65 default: false66 })67 yargs.options('pdp', {68 alias: ['l'],69 type: 'boolean',70 default: false71 })72 yargs.options('summary', {73 alias: ['s'],74 type: 'boolean',75 default: false76 })77 yargs.options('concurrency', {78 alias: ['c'],79 type: 'number',80 default: Infinity81 })82 yargs.options('filter-extensions', {83 alias: ['extensions', 'filter-extension', 'extension'],84 type: 'string',85 default: ''86 })87 },88 handler: async(args) => {89 const { Scheduler } = require('@pown/request/lib/scheduler')90 const { wildcard, filter, from, to, order, header, retry, timeout, maxResults, outputType, unique, pdp, summary, concurrency, filterExtensions, domain: maybeDomain } = args91 let domain = maybeDomain.trim()92 if (/^https?:\/\//i.test(domain)) {93 domain = require('url').parse(domain).hostname94 }95 const headers = {}96 if (header) {...
prompt-middleware.spec.js
Source:prompt-middleware.spec.js
1const expect = require('chai').expect;2const inquirer = require('inquirer');3const questionModule = require('../../lib/cli/prompt-middleware');4describe('The prompt-middleware module', function () {5 beforeEach(function () {6 this.sut = questionModule;7 });8 describe('yargsToInquirer function', function () {9 it('returns an inquirer questions array', function () {10 const yargsOptions = {11 tenant: {12 type: 'string',13 describe: 'Specify a tenant ID',14 },15 };16 const result = this.sut.yargsToInquirer(yargsOptions);17 expect(result).to.be.an('array');18 expect(result[0]).to.be.an('object').with.property('name');19 expect(result[0].name).to.equal('tenant');20 });21 it('maps yargs description to inquirer message', function () {22 const yargsOptions = {23 tenant: {24 type: 'string',25 describe: 'Specify a tenant ID',26 },27 };28 const result = this.sut.yargsToInquirer(yargsOptions);29 expect(result[0]).to.be.an('object').with.property('message');30 expect(result[0].message).to.equal(yargsOptions.tenant.describe);31 });32 it('converts a yargs type to inquirer type', function () {33 const yargsOptions = {34 tenant: {35 type: 'string',36 describe: 'Specify a tenant ID',37 },38 sourcemap: {39 type: 'boolean',40 describe: 'Include sourcemaps in build output',41 },42 };43 const result = this.sut.yargsToInquirer(yargsOptions);44 expect(result[0].type).to.equal('input');45 expect(result[1].type).to.equal('confirm');46 });47 it('applies inquirer-specific overrides', function () {48 const yargsOptions = {49 password: {50 type: 'string',51 describe: 'Okapi tenant password',52 inquirer: {53 type: 'password',54 mask: '*',55 },56 },57 };58 const result = this.sut.yargsToInquirer(yargsOptions);59 expect(result[0]).to.be.an('object').with.property('mask');60 expect(result[0].type).to.equal('password');61 });62 });63 describe('askIfUndefined function', function () {64 beforeEach(function () {65 this.argv = {66 username: 'user',67 interactive: true,68 };69 this.yargsOptions = {70 password: {71 describe: 'Okapi tenant password',72 },73 };74 this.sandbox.stub(inquirer, 'prompt').resolves({ password: 'password input' });75 });76 afterEach(function () {77 delete this.argv;78 delete this.yargsOptions;79 delete this.commandStub;80 });81 it('generates inquirer questions for missing yargs options', function (done) {82 this.sut.askIfUndefined(this.argv, this.yargsOptions)83 .then(() => {84 expect(inquirer.prompt).to.have.been.calledOnce;85 const inquirerCall = inquirer.prompt.getCall(0);86 expect(inquirerCall.args[0][0].name).to.equal('password');87 done();88 })89 .catch(err => console.log(err));90 });91 it('does not pass populated yargs options to inquirer', function (done) {92 this.argv.password = 'password1';93 this.yargsOptions.somethingElse = { describe: 'another option for testing' };94 this.sut.askIfUndefined(this.argv, this.yargsOptions)95 .then(() => {96 expect(inquirer.prompt).to.have.been.calledOnce;97 const inquirerCall = inquirer.prompt.getCall(0);98 expect(inquirerCall.args[0].length).to.equal(1);99 expect(inquirerCall.args[0][0].name).to.equal('somethingElse');100 done();101 });102 });103 it('does not invoke inquirer when all options are populated', function (done) {104 this.argv.password = 'password1';105 this.sut.askIfUndefined(this.argv, this.yargsOptions)106 .then(() => {107 expect(inquirer.prompt).not.to.have.been.called;108 done();109 });110 });111 it('merges answers with argv', function (done) {112 this.sut.askIfUndefined(this.argv, this.yargsOptions)113 .then((result) => {114 expect(result).to.be.an('object').with.property('username', 'user');115 expect(result).to.be.an('object').with.property('password', 'password input');116 done();117 });118 });119 });120 describe('promptMiddleware', function () {121 beforeEach(function () {122 this.argv = {123 username: 'user',124 interactive: true,125 };126 this.yargsOptions = {127 password: {128 describe: 'Okapi tenant password',129 },130 };131 this.sandbox.stub(inquirer, 'prompt').resolves({ password: 'password input' });132 });133 afterEach(function () {134 delete this.argv;135 delete this.yargsOptions;136 });137 it('returns a function', function () {138 const result = this.sut.promptMiddleware({}, () => {});139 expect(result).is.a('function');140 });141 it('when invoked, returns answers', function (done) {142 const middleware = this.sut.promptMiddleware(this.yargsOptions);143 middleware(this.argv).then((response) => {144 expect(response).to.be.an('object').with.property('username', 'user');145 expect(response).to.be.an('object').with.property('password', 'password input');146 done();147 });148 });149 });...
jokes.js
Source:jokes.js
1#! /usr/bin/env node2const chalk = require('chalk');3const boxen = require('boxen');4const yargs = require('yargs');5const axios = require('axios');6const { option } = require('yargs');7const boxenOptions = {8 padding: 1,9 borderStyle: 'round'10}11const yargsOptions = yargs12.usage("Usage: -n <string>")13.option("n", { alias: "name", describe: "input your name", type: "string", demandOption: true})14.option("s", { alias: "search", describe: "search term", type: "string"})15.argv;16const greeting = `hello ${yargsOptions.name}`;17if(yargsOptions.search){18 console.log(`Searching for jokes about ${yargsOptions.search}`);19}else {20 console.log(`Lemme fetch a random joke:`)21}22const url = yargsOptions.search ? `https://icanhazdadjoke.com/search?term=${yargsOptions.search}&/limit=1`: `https://icanhazdadjoke.com/`;23axios.get(url, { headers: {Accept: "application/json"}} )24.then(res => {25 if (yargsOptions.search) {26 res.data.results.forEach( j => {27 console.log(chalk.blue.bold(`\n ${j.joke}`));28 })29 }else {30 console.log(chalk.red.bold(res.data.joke));31 }32})...
Using AI Code Generation
1const argv = require('yargs')2.options({3 a: {4 }5})6.help()7.alias('help', 'h')8.argv;9const request = require('request');10const fs = require('fs');11const _ = require('lodash');12const yargs = require('yargs');13const geocode = require('./geocode/geocode.js');14const weather = require('./weather/weather.js');15var encodedAddress = encodeURIComponent(argv.address);16geocode.geocodeAddress(encodedAddress, (errorMessage, results) => {17 if(errorMessage){18 console.log(errorMessage);19 } else {20 console.log(results.address);21 weather.getWeather(results.latitude, results.longitude, (errorMessage, weatherResults) => {22 if(errorMessage){23 console.log(errorMessage);24 } else {25 console.log(`It's currently ${weatherResults.temperature}. It feels like ${weatherResults.apparentTemperature}.`);26 }27 });28 }29});30const request = require('request');31var geocodeAddress = (address, callback) => {32 var encodedAddress = encodeURIComponent(address);33 request({34 }, (error, response, body) => {35 if(error){36 callback('Unable to connect to Google servers.');37 } else if(body.status === 'ZERO_RESULTS'){38 callback('Unable to find that address.');39 } else if(body.status === 'OK'){40 callback(undefined, {41 });42 }43 });44};45module.exports.geocodeAddress = geocodeAddress;46const request = require('request');47var getWeather = (latitude, longitude, callback) => {48 request({49 }, (error, response, body) => {50 if(!error && response.statusCode === 200){51 callback(undefined,
Using AI Code Generation
1const yargs = require('yargs');2yargs.command({3 builder: {4 title: {5 },6 body: {7 }8 },9 handler: function (argv) {10 console.log('Title: ' + argv.title);11 console.log('Body: ' + argv.body);12 }13})14const yargs = require('yargs');15yargs.command({16 handler: function () {17 console.log('Adding a new note!');18 }19})20yargs.command({21 handler: function () {22 console.log('Removing the note!');23 }24})25const yargs = require('yargs');26yargs.command({27 builder: {28 title: {29 }30 },31 handler: function (argv) {32 console.log('Title: ' + argv.title);33 }34})35yargs.command({
Using AI Code Generation
1const yargs = require('yargs');2const notes = require('./notes.js');3const chalk = require('chalk');4yargs.version('1.1.0');5yargs.command({6 title: {7 },8 body: {9 }10 },11 handler(argv) {12 notes.addNotes(argv.title, argv.body);13 }14});15yargs.command({16 title: {17 }18 },19 handler(argv) {20 notes.removeNotes(argv.title);21 }22});23yargs.command({24 handler() {25 notes.listNotes();26 }27});28yargs.command({29 title: {30 }31 },32 handler(argv) {33 notes.readNotes(argv.title);34 }35});36yargs.parse();
Using AI Code Generation
1const yargs = require('yargs');2yargs.options({3 command: {4 }5})6console.log(yargs.argv);
Using AI Code Generation
1var argv = require('yargs')2 .options({3 a: {4 }5 })6 .help()7 .alias('help', 'h')8 .argv;9var argv = require('yargs')10 .command('hello', 'Greet the user', function(yargs) {11 yargs.options({12 name: {13 },14 lastname: {15 }16 }).help('help');17 })18 .help()19 .argv;20var command = argv._[0];21if (command === 'hello' && typeof argv.name !== 'undefined' && typeof argv.lastname !== 'undefined') {22 console.log('Hello ' + argv.name + ' ' + argv.lastname + '!');23} else if (command === 'hello' && typeof argv.name !== 'undefined') {24 console.log('Hello ' + argv.name + '!');25} else if (command === 'hello') {26 console.log('Hello world!');27}28var argv = require('yargs')29 .command('hello', 'Greet the user', function(yargs) {30 yargs.options({31 name: {32 },33 lastname: {34 }35 }).help('help');36 })37 .help()38 .argv;39var command = argv._[0];40if (command === 'hello' && typeof argv.name !== 'undefined' && typeof argv.lastname !== 'undefined') {41 console.log('Hello ' + argv.name + ' ' + argv.lastname + '!');42} else if (command === 'hello' && typeof argv
Using AI Code Generation
1.options({2 a: {3 }4})5.help()6.alias('help', 'h')7.argv;8var encodedAddress = encodeURIComponent(argv.address);9request({10}, (error, response, body) => {11 if (error) {12 console.log('Unable to connect to Google servers.');13 } else if (body.status === 'ZERO_RESULTS') {14 console.log('Unable to find that address.');15 } else if (body.status === 'OK') {16 console.log(`Address: ${body.results[0].formatted_address}`);17 console.log(`Latitude: ${body.results[0].geometry.location.lat}`);18 console.log(`Longitude: ${body.results[0].geometry.location.lng}`);19 }20});21request({22}, (error, response, body) => {23 if (!error && response.statusCode === 200) {24 console.log(body.currently.temperature);25 } else {26 console.log('Unable to fetch weather.');27 }28});29request({30}, (error, response, body) => {31 if (!error && response.statusCode === 200) {32 console.log(body.currently.temperature);33 } else {34 console.log('Unable to fetch weather.');
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!!