Best JavaScript code snippet using wpt
kustomization.model.ts
Source:kustomization.model.ts
...15 config ? {config} : {config: {apiVersion: 'kustomize.config.k8s.io/v1beta1', kind: 'Kustomization'}}16 );17 }18 addResource(resource: string): Kustomization {19 if (!this.containsResource(resource)) {20 this.resources.push(resource);21 this.resources.sort()22 }23 return this;24 }25 removeResource(resource: string): Kustomization {26 if (this.containsResource(resource)) {27 const index = this.resources.indexOf(resource);28 this.resources.splice(index, 1);29 this.resources.sort();30 }31 return this;32 }33 containsResource(resource: string): boolean {34 return this.resources.includes(resource);35 }36 asJson() {37 const resource = Object.assign(38 {},39 this.config,40 {41 resources: this.resources42 }43 );44 return resource;45 }46 asJsonString(): string {47 return JSON.stringify(this.asJson());48 }49 asYamlString(): string {50 return YAML.dump(this.asJson());51 }52}53export const addKustomizeResource = async (kustomizeFile: string | File, path: string): Promise<boolean> => {54 const file: File = isFile(kustomizeFile) ? kustomizeFile : new File(kustomizeFile);55 const kustomize: Kustomization = await loadKustomize(kustomizeFile);56 if (kustomize.containsResource(path)) {57 return false;58 }59 kustomize.addResource(path);60 return file.write(kustomize.asYamlString()).then(() => true);61};62export const removeKustomizeResource = async (kustomizeFile: string | File, path: string): Promise<boolean> => {63 const logger: Logger = Container.get(Logger);64 const file: File = isFile(kustomizeFile) ? kustomizeFile : new File(kustomizeFile);65 const kustomize: Kustomization = await loadKustomize(kustomizeFile);66 if (kustomize.containsResource(path)) {67 kustomize.removeResource(path);68 console.log(`Updated kustomization.yaml file: ${kustomize.asYamlString()}`)69 const result: boolean = await file.write(kustomize.asYamlString()).then(() => true);70 console.log(` File changed: ${result}`)71 return result;72 } else {73 logger.debug(`Kustomize does not contain resource: ${path}`, {resources: kustomize.resources})74 }75 return false;76};77export const loadKustomize = async (kustomizeFile: File | string): Promise<Kustomization> => {78 const logger: Logger = Container.get(Logger)79 const file: File = isFile(kustomizeFile) ? kustomizeFile : new File(kustomizeFile);80 logger.debug(`Loading kustomize file: ${file.filename}`)...
mung-resources.js
Source:mung-resources.js
...10 * @param {Function} fn - fn(resource, req, res)11 */12mung.resources = function resources (fn) {13 function mungResources(body, req, res) {14 if (!containsResource(body)) {15 return body;16 }17 let isSingleton = isSingletonResource(body);18 // Munge the body.19 let resources = resourcesIn(body).map (r => {20 if (res.headersSent) return null;21 let original = r;22 r = fn(r, req, res);23 if (r === null) {24 return undefined;25 }26 if (r === undefined)27 r = original;28 return r;29 });30 if (res.headersSent) return null;31 // remove undefines and nulls32 resources = resources.filter(r => r);33 // If singleton and removed then 204 No Content34 if (isSingleton && resources.length === 0) {35 return null;36 }37 // Set the body to transform resource(s)38 if (isSingleton)39 body = resources[0];40 else41 body.data = resources;42 return body;43 }44 return mung.json(mungResources);45};46/**47 * Allow other's to mung the resource(s).48 *49 * @param {Function} fn - fn(resource, req, res)50 */51mung.resourcesAsync = function resourcesAsync (fn) {52 function mungResourcesAsync(body, req, res) {53 if (!containsResource(body)) {54 return Promise.resolve(body);55 }56 let isSingleton = isSingletonResource(body);57 return Promise58 // Munge the resource(s) in the body59 .map(resourcesIn(body), r => {60 if (res.headersSent) return null;61 let original = r;62 r = fn(r, req, res);63 if (r === null) {64 return undefined;65 }66 if (r === undefined)67 r = original;...
only.js
Source:only.js
...9/*10 * Middleware to only return a subset of the resource's properties.11 */12function only (body, req, res) {13 if (!req.query.only || !containsResource(body))14 return Promise.resolve(body);15 if (isSingletonResource(body) && typeof req.query.only === 'string') {16 let value = jpointer.get(body, req.query.only);17 if (!value)18 return Promise.resolve(null);19 // If scalar value, then decrypt. If not classified, then decrypt20 // does nothing.21 if (typeof value !== 'object' && !Array.isArray(value)) {22 return req.user23 .demandAsync('view', body._metadata.self, req.query.only)24 .then(() => security.decryptAsync(value));25 }26 return Promise.resolve(value);27 }...
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.containsResource(url, function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10var wpt = require('wpt');11var wpt = new WebPageTest('www.webpagetest.org');12wpt.getLocations(function(err, data) {13 if (err) {14 console.log(err);15 } else {16 console.log(data);17 }18});19var wpt = require('wpt');20var wpt = new WebPageTest('www.webpagetest.org');21wpt.getTesters(function(err, data) {22 if (err) {23 console.log(err);24 } else {25 console.log(data);26 }27});28var wpt = require('wpt');29var wpt = new WebPageTest('www.webpagetest.org');30var testId = '140317_4E_4';31wpt.getTestStatus(testId, function(err, data) {32 if (err) {33 console.log(err);34 } else {35 console.log(data);36 }37});38var wpt = require('wpt');39var wpt = new WebPageTest('www.webpagetest.org');40var testId = '140317_4E_4';41wpt.getTestResults(testId, function(err, data) {42 if (err) {43 console.log(err);44 } else {45 console.log(data);46 }47});
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4 videoParams: {5 }6};7wpt.runTest(url, options, function(err, data) {8 if (err) return console.error(err);9 var testId = data.data.testId;10 wpt.getTestResults(testId, function(err, data) {11 if (err) return console.error(err);12 console.log(data.data.median.firstView.SpeedIndex);13 console.log(data.data.median.firstView.videoFrames);14 console.log(data.data.median.firstView.videoFiles);15 console.log(data.data.median.firstView.videoCompleteURL);16 console.log(data.data.median.firstView.videoURL);17 });18});
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.getTestStatus('170304_9K_2a2e2c8f2f2b6a2a7a99a1f1d1a1a1a1', function(err, data) {4 if (err) return console.error(err);5 console.log(data);6});7wpt.getLocations(function(err, data) {8 if (err) return console.error(err);9 console.log(data);10});11wpt.getTesters(function(err, data) {12 if (err) return console.error(err);13 console.log(data);14});15wpt.getBrowsers(function(err, data) {16 if (err) return console.error(err);17 console.log(data);18});19wpt.getTesters(function(err, data) {20 if (err) return console.error(err);21 console.log(data);22});23wpt.getLocations(function(err, data) {24 if (err) return console.error(err);25 console.log(data);26});27wpt.getLocations(function(err, data) {28 if (err) return console.error(err);29 console.log(data);30});31wpt.getLocations(function(err, data) {32 if (err) return console.error(err);33 console.log(data);34});35wpt.getLocations(function(err, data) {36 if (err) return console.error(err);37 console.log(data);38});39wpt.getLocations(function(err, data) {40 if (err) return console.error(err);41 console.log(data);42});
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!!