Best JavaScript code snippet using chai
api.js
Source:api.js
1import $ from 'jquery';2const Api = {3 groupsPath: '/api/:version/groups.json',4 groupPath: '/api/:version/groups/:id.json',5 namespacesPath: '/api/:version/namespaces.json',6 groupProjectsPath: '/api/:version/groups/:id/projects.json',7 projectsPath: '/api/:version/projects.json?simple=true',8 labelsPath: '/:namespace_path/:project_path/labels',9 licensePath: '/api/:version/templates/licenses/:key',10 gitignorePath: '/api/:version/templates/gitignores/:key',11 gitlabCiYmlPath: '/api/:version/templates/gitlab_ci_ymls/:key',12 dockerfilePath: '/api/:version/templates/dockerfiles/:key',13 issuableTemplatePath: '/:namespace_path/:project_path/templates/:type/:key',14 usersPath: '/api/:version/users.json',15 group(groupId, callback) {16 const url = Api.buildUrl(Api.groupPath)17 .replace(':id', groupId);18 return $.ajax({19 url,20 dataType: 'json',21 })22 .done(group => callback(group));23 },24 // Return groups list. Filtered by query25 groups(query, options, callback) {26 const url = Api.buildUrl(Api.groupsPath);27 return $.ajax({28 url,29 data: Object.assign({30 search: query,31 per_page: 20,32 }, options),33 dataType: 'json',34 })35 .done(groups => callback(groups));36 },37 // Return namespaces list. Filtered by query38 namespaces(query, callback) {39 const url = Api.buildUrl(Api.namespacesPath);40 return $.ajax({41 url,42 data: {43 search: query,44 per_page: 20,45 },46 dataType: 'json',47 }).done(namespaces => callback(namespaces));48 },49 // Return projects list. Filtered by query50 projects(query, options, callback) {51 const url = Api.buildUrl(Api.projectsPath);52 return $.ajax({53 url,54 data: Object.assign({55 search: query,56 per_page: 20,57 membership: true,58 }, options),59 dataType: 'json',60 })61 .done(projects => callback(projects));62 },63 newLabel(namespacePath, projectPath, data, callback) {64 const url = Api.buildUrl(Api.labelsPath)65 .replace(':namespace_path', namespacePath)66 .replace(':project_path', projectPath);67 return $.ajax({68 url,69 type: 'POST',70 data: { label: data },71 dataType: 'json',72 })73 .done(label => callback(label))74 .fail(message => callback(message.responseJSON));75 },76 // Return group projects list. Filtered by query77 groupProjects(groupId, query, callback) {78 const url = Api.buildUrl(Api.groupProjectsPath)79 .replace(':id', groupId);80 return $.ajax({81 url,82 data: {83 search: query,84 per_page: 20,85 },86 dataType: 'json',87 })88 .done(projects => callback(projects));89 },90 // Return text for a specific license91 licenseText(key, data, callback) {92 const url = Api.buildUrl(Api.licensePath)93 .replace(':key', key);94 return $.ajax({95 url,96 data,97 })98 .done(license => callback(license));99 },100 gitignoreText(key, callback) {101 const url = Api.buildUrl(Api.gitignorePath)102 .replace(':key', key);103 return $.get(url, gitignore => callback(gitignore));104 },105 gitlabCiYml(key, callback) {106 const url = Api.buildUrl(Api.gitlabCiYmlPath)107 .replace(':key', key);108 return $.get(url, file => callback(file));109 },110 dockerfileYml(key, callback) {111 const url = Api.buildUrl(Api.dockerfilePath).replace(':key', key);112 $.get(url, callback);113 },114 issueTemplate(namespacePath, projectPath, key, type, callback) {115 const url = Api.buildUrl(Api.issuableTemplatePath)116 .replace(':key', key)117 .replace(':type', type)118 .replace(':project_path', projectPath)119 .replace(':namespace_path', namespacePath);120 $.ajax({121 url,122 dataType: 'json',123 })124 .done(file => callback(null, file))125 .fail(callback);126 },127 users(query, options) {128 const url = Api.buildUrl(this.usersPath);129 return Api.wrapAjaxCall({130 url,131 data: Object.assign({132 search: query,133 per_page: 20,134 }, options),135 dataType: 'json',136 });137 },138 buildUrl(url) {139 let urlRoot = '';140 if (gon.relative_url_root != null) {141 urlRoot = gon.relative_url_root;142 }143 return urlRoot + url.replace(':version', gon.api_version);144 },145 wrapAjaxCall(options) {146 return new Promise((resolve, reject) => {147 // jQuery 2 is not Promises/A+ compatible (missing catch)148 $.ajax(options) // eslint-disable-line promise/catch-or-return149 .then(data => resolve(data),150 (jqXHR, textStatus, errorThrown) => {151 const error = new Error(`${options.url}: ${errorThrown}`);152 error.textStatus = textStatus;153 reject(error);154 },155 );156 });157 },158};...
dead-module-elimination.js
Source:dead-module-elimination.js
1/**2 * Copyright (c) 2015-present, Facebook, Inc.3 * All rights reserved.4 *5 * This source code is licensed under the BSD-style license found in the6 * LICENSE file in the root directory of this source tree. An additional grant7 * of patent rights can be found in the PATENTS file in the same directory.8 */9'use strict';10const t = require('babel-types');11var globals = Object.create(null);12var requires = Object.create(null);13var _requires;14const hasDeadModules = modules =>15 Object.keys(modules).some(key => modules[key] === 0);16function CallExpression(path) {17 const { node } = path;18 const fnName = node.callee.name;19 if (fnName === 'require' || fnName === '__d') {20 var moduleName = node.arguments[0].value;21 if (fnName === '__d' && _requires && !_requires[moduleName]) {22 path.remove();23 } else if (fnName === '__d'){24 requires[moduleName] = requires[moduleName] || 0;25 } else {26 requires[moduleName] = (requires[moduleName] || 0) + 1;27 }28 }29}30function IfStatement(path) {31 const { node } = path;32 if (node.test.type === 'Identifier' && node.test.name in globals) {33 if (globals[node.test.name]) {34 if (node.consequent.type === 'BlockStatement') {35 path.replaceWithMultiple(node.consequent.body);36 } else {37 path.replaceWith(node.consequent);38 }39 } else if (node.alternate) {40 if (node.alternate.type === 'BlockStatement') {41 path.replaceWithMultiple(node.alternate.body);42 } else {43 path.replaceWith(node.alternate);44 }45 } else {46 path.remove();47 }48 }49 }50module.exports = function () {51 var firstPass = {52 AssignmentExpression(path) {53 const { node } = path;54 if (node.left.type === 'Identifier' && node.left.name === '__DEV__') {55 var value;56 if (node.right.type === 'BooleanLiteral') {57 value = node.right.value;58 } else if (59 node.right.type === 'UnaryExpression' &&60 node.right.operator === '!' &&61 node.right.argument.type === 'NumericLiteral'62 ) {63 value = !node.right.argument.value;64 } else {65 return;66 }67 globals[node.left.name] = value;68 // workaround babel/source map bug - the minifier should strip it69 path.replaceWith(t.booleanLiteral(value));70 //path.remove();71 //scope.removeBinding(node.left.name);72 }73 },74 IfStatement,75 ConditionalExpression: IfStatement,76 Identifier(path) {77 const { node } = path;78 var parent = path.parent;79 if (parent.type === 'AssignmentExpression' && parent.left === node) {80 return;81 }82 if (node.name in globals) {83 path.replaceWith(t.booleanLiteral(globals[node.name]));84 }85 },86 CallExpression,87 LogicalExpression(path) {88 const { node } = path;89 if (node.left.type === 'Identifier' && node.left.name in globals) {90 const value = globals[node.left.name];91 if (node.operator === '&&') {92 if (value) {93 path.replaceWith(node.right);94 } else {95 path.replaceWith(t.booleanLiteral(value));96 }97 } else if (node.operator === '||') {98 if (value) {99 path.replaceWith(t.booleanLiteral(value));100 } else {101 path.replaceWith(node.right);102 }103 }104 }105 }106 };107 var secondPass = {108 CallExpression,109 };110 return {111 visitor: {112 Program(path) {113 path.traverse(firstPass);114 while (hasDeadModules(requires)) {115 _requires = requires;116 requires = {};117 path.traverse(secondPass);118 }119 }120 }121 };...
normalize.js
Source:normalize.js
...14var parentDirectoryNixEndRegExp3 = /^\/+\.\.$/;15// RegExp magic :)16module.exports = function normalize(path) {17 while(currentDirectoryWinMiddleRegExp.test(path))18 path = path.replace(currentDirectoryWinMiddleRegExp, "\\");19 path = path.replace(currentDirectoryWinEndRegExp, "");20 while(parentDirectoryWinMiddleRegExp.test(path))21 path = path.replace(parentDirectoryWinMiddleRegExp, "\\");22 path = path.replace(parentDirectoryWinEndRegExp1, "$1");23 path = path.replace(parentDirectoryWinEndRegExp2, "");24 while(currentDirectoryNixMiddleRegExp.test(path))25 path = path.replace(currentDirectoryNixMiddleRegExp, "/");26 path = path.replace(currentDirectoryNixEndRegExp1, "/");27 path = path.replace(currentDirectoryNixEndRegExp2, "");28 while(parentDirectoryNixMiddleRegExp.test(path))29 path = path.replace(parentDirectoryNixMiddleRegExp, "/");30 path = path.replace(parentDirectoryNixEndRegExp1, "/");31 path = path.replace(parentDirectoryNixEndRegExp2, "");32 path = path.replace(parentDirectoryNixEndRegExp3, "/");33 return path.replace(doubleSlashWinRegExp, "\\").replace(doubleSlashNixRegExp, "/");...
Using AI Code Generation
1var path = require('path');2var websiteAbout = "Desktop/Bucky/thenewboston/about.html";3console.log(path.normalize(websiteHome));4console.log(path.normalize(websiteAbout));5console.log(path.dirname(websiteAbout));6console.log(path.basename(websiteAbout));7console.log(path.extname(websiteAbout));8console.log(path.join(__dirname, 'test', 'hello.html'));9var path = require('path');10var websiteAbout = "Desktop/Bucky/thenewboston/about.html";11console.log(path.resolve(websiteHome));12console.log(path.resolve(websiteAbout));13console.log(path.dirname(websiteAbout));14console.log(path.basename(websiteAbout));15console.log(path.extname(websiteAbout));16console.log(path.join(__dirname, 'test', 'hello.html'));17var path = require('path');18var websiteAbout = "Desktop/Bucky/thenewboston/about.html";19console.log(path.resolve(websiteHome));20console.log(path.resolve(websiteAbout));21console.log(path.dirname(websiteAbout));22console.log(path.basename(websiteAbout));23console.log(path.extname(websiteAbout));24console.log(path.join(__dirname, 'test', 'hello.html'));25var path = require('path');26var websiteAbout = "Desktop/Bucky/thenewboston/about.html";27console.log(path.resolve(websiteHome));28console.log(path.resolve(websiteAbout));29console.log(path.dirname(websiteAbout));30console.log(path.basename(websiteAbout));31console.log(path.extname(websiteAbout));32console.log(path.join(__dirname, 'test', 'hello.html'));33var path = require('path');34var websiteAbout = "Desktop/Bucky/thenewboston/about.html";35console.log(path.resolve(websiteHome));36console.log(path.resolve(websiteAbout));
Using AI Code Generation
1const path = require('path');2const pathObj = path.parse(__filename);3console.log(pathObj);4const path = require('path');5const pathObj = path.join(__dirname,'test','test.js');6console.log(pathObj);7const path = require('path');8const pathObj = path.resolve(__dirname,'test','test.js');9console.log(pathObj);10const path = require('path');11const pathObj = path.isAbsolute(__filename);12console.log(pathObj);13const path = require('path');14const pathObj = path.isAbsolute(__filename);15console.log(pathObj);16const path = require('path');17const pathObj = path.isAbsolute(__filename);18console.log(pathObj);19const path = require('path');20const pathObj = path.isAbsolute(__filename);21console.log(pathObj);22const path = require('path');23const pathObj = path.isAbsolute(__filename);24console.log(pathObj);25const path = require('path');26const pathObj = path.isAbsolute(__filename);27console.log(pathObj);28const path = require('path');29const pathObj = path.isAbsolute(__filename);30console.log(pathObj);31const path = require('path');32const pathObj = path.isAbsolute(__filename);33console.log(pathObj);34const path = require('path');35const pathObj = path.isAbsolute(__filename);36console.log(pathObj);37const path = require('path');38const pathObj = path.isAbsolute(__filename);39console.log(pathObj);
Using AI Code Generation
1var path = require('path');2var fs = require('fs');3var filePath = path.join(__dirname, 'test.js');4console.log(filePath);5var newPath = filePath.replace(path.extname(filePath), '.txt');6console.log(newPath);7fs.rename(filePath, newPath, function(err) {8 if (err) {9 console.log(err);10 }11 console.log('File renamed');12});
Using AI Code Generation
1const path = require('path');2const myPath = path.replace('test.js');3console.log(myPath);4const path = require('path');5const myPath = path.resolve('test.js');6console.log(myPath);7const path = require('path');8const myPath = path.join('test.js');9console.log(myPath);10const path = require('path');11const myPath = path.normalize('test.js');12console.log(myPath);13const path = require('path');14const myPath = path.isAbsolute('test.js');15console.log(myPath);16const path = require('path');17const myPath = path.parse('test.js');18console.log(myPath);19const path = require('path');20const myPath = path.format({21});22console.log(myPath);23const path = require('path');24const myPath = path.dirname('test.js');25console.log(myPath);26const path = require('path');27const myPath = path.basename('test.js');28console.log(myPath);29const path = require('path');30const myPath = path.extname('test.js');31console.log(myPath);32const path = require('path');33const myPath = path.sep;34console.log(myPath);35const path = require('path');36const myPath = path.delimiter;37console.log(myPath);
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!!