Best JavaScript code snippet using wpt
Subject.ts
Source:Subject.ts
...36 * Sets the value of this subject and notifies subscribers if the value changed.37 * @param value The new value.38 */39 public set(value: T): void {40 if (!this.equalityFunc(value, this.value)) {41 if (this.mutateFunc) {42 this.mutateFunc(this.value, value);43 } else {44 this.value = value;45 }46 this.notify();47 }48 }49 /**50 * Applies a partial set of properties to this subject's value and notifies subscribers if the value changed as a51 * result.52 * @param value The properties to apply.53 */54 public apply(value: Partial<T>): void {55 let changed = false;56 for (const prop in value) {57 changed = value[prop] !== this.value[prop];58 if (changed) {59 break;60 }61 }62 Object.assign(this.value, value);63 changed && this.notify();64 }65 /** @inheritdoc */66 public notify(): void {67 super.notify();68 }69 /**70 * Gets the value of this subject.71 * @returns The value of this subject.72 */73 public get(): T {74 return this.value;75 }76 /**77 * Maps this subject to a new subscribable.78 * @param fn The function to use to map to the new subscribable.79 * @param equalityFunc The function to use to check for equality between mapped values. Defaults to the strict80 * equality comparison (`===`).81 * @returns The mapped subscribable.82 */83 public map<M>(fn: (input: T, previousVal?: M) => M, equalityFunc?: ((a: M, b: M) => boolean)): MappedSubject<[T], M>;84 /**85 * Maps this subject to a new subscribable with a persistent, cached value which is mutated when it changes.86 * @param fn The function to use to map to the new subscribable.87 * @param equalityFunc The function to use to check for equality between mapped values.88 * @param mutateFunc The function to use to change the value of the mapped subscribable.89 * @param initialVal The initial value of the mapped subscribable.90 * @returns The mapped subscribable.91 */92 public map<M>(93 fn: (input: T, previousVal?: M) => M,94 equalityFunc: ((a: M, b: M) => boolean),95 mutateFunc: ((oldVal: M, newVal: M) => void),96 initialVal: M97 ): MappedSubject<[T], M>;98 // eslint-disable-next-line jsdoc/require-jsdoc99 public map<M>(100 fn: (input: T, previousVal?: M) => M,101 equalityFunc?: ((a: M, b: M) => boolean),102 mutateFunc?: ((oldVal: M, newVal: M) => void),103 initialVal?: M104 ): MappedSubject<[T], M> {105 const mapFunc = (inputs: readonly [T], previousVal?: M): M => fn(inputs[0], previousVal);106 return mutateFunc107 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion108 ? MappedSubject.create(mapFunc, equalityFunc!, mutateFunc, initialVal!, this)109 : MappedSubject.create(mapFunc, equalityFunc ?? AbstractSubscribable.DEFAULT_EQUALITY_FUNC, this);110 }111}112/**113 * A type which contains the `length` property of a tuple.114 */115// eslint-disable-next-line jsdoc/require-jsdoc116type TupleLength<T extends [...any[]]> = { length: T['length'] };117/**118 * A type which maps a tuple of input types to a tuple of subscribables that provide the input types.119 */120export type MappedSubscribableInputs<Types extends [...any[]]> = {121 [Index in keyof Types]: Subscribable<Types[Index]>122} & TupleLength<Types>;123/**124 * A subscribable subject that is a mapped stream from one or more input subscribables.125 */126export class MappedSubject<I extends [...any[]], T> extends AbstractSubscribable<T> implements MappedSubscribable<T> {127 protected readonly inputs: MappedSubscribableInputs<I>;128 protected readonly inputValues: I;129 protected value: T;130 protected readonly mutateFunc: (newVal: T) => void;131 protected readonly inputHandlers: (() => void)[];132 /**133 * Creates a new MappedSubject.134 * @param mapFunc The function which maps this subject's inputs to a value.135 * @param equalityFunc The function which this subject uses to check for equality between values.136 * @param mutateFunc The function which this subject uses to change its value.137 * @param initialVal The initial value of this subject.138 * @param inputs The subscribables which provide the inputs to this subject.139 */140 private constructor(141 protected readonly mapFunc: (inputs: Readonly<I>, previousVal?: T) => T,142 protected readonly equalityFunc: ((a: T, b: T) => boolean),143 mutateFunc?: ((oldVal: T, newVal: T) => void),144 initialVal?: T,145 ...inputs: MappedSubscribableInputs<I>146 ) {147 super();148 this.inputs = inputs;149 this.inputValues = inputs.map(input => input.get()) as I;150 if (initialVal && mutateFunc) {151 this.value = initialVal;152 mutateFunc(this.value, this.mapFunc(this.inputValues));153 this.mutateFunc = (newVal: T): void => { mutateFunc(this.value, newVal); };154 } else {155 this.value = this.mapFunc(this.inputValues);156 this.mutateFunc = (newVal: T): void => { this.value = newVal; };157 }158 this.inputHandlers = this.inputs.map((input, index) => this.updateValue.bind(this, index));159 for (let i = 0; i < inputs.length; i++) {160 inputs[i].sub(this.inputHandlers[i]);161 }162 }163 /**164 * Creates a new mapped subject. Values are compared for equality using the strict equality comparison (`===`).165 * @param mapFunc The function to use to map inputs to the new subject value.166 * @param inputs The subscribables which provide the inputs to the new subject.167 */168 public static create<I extends [...any[]], T>(169 mapFunc: (inputs: Readonly<I>, previousVal?: T) => T,170 ...inputs: MappedSubscribableInputs<I>171 ): MappedSubject<I, T>;172 /**173 * Creates a new mapped subject. Values are compared for equality using a custom function.174 * @param mapFunc The function to use to map inputs to the new subject value.175 * @param equalityFunc The function which the new subject uses to check for equality between values.176 * @param inputs The subscribables which provide the inputs to the new subject.177 */178 public static create<I extends [...any[]], T>(179 mapFunc: (inputs: Readonly<I>, previousVal?: T) => T,180 equalityFunc: (a: T, b: T) => boolean,181 ...inputs: MappedSubscribableInputs<I>182 ): MappedSubject<I, T>;183 /**184 * Creates a new mapped subject with a persistent, cached value which is mutated when it changes. Values are185 * compared for equality using a custom function.186 * @param mapFunc The function to use to map inputs to the new subject value.187 * @param equalityFunc The function which the new subject uses to check for equality between values.188 * @param mutateFunc The function to use to change the value of the new subject.189 * @param initialVal The initial value of the new subject.190 * @param inputs The subscribables which provide the inputs to the new subject.191 */192 public static create<I extends [...any[]], T>(193 mapFunc: (inputs: Readonly<I>, previousVal?: T) => T,194 equalityFunc: (a: T, b: T) => boolean,195 mutateFunc: (oldVal: T, newVal: T) => void,196 initialVal: T,197 ...inputs: MappedSubscribableInputs<I>198 ): MappedSubject<I, T>;199 // eslint-disable-next-line jsdoc/require-jsdoc200 public static create<I extends [...any[]], T>(201 mapFunc: (inputs: Readonly<I>, previousVal?: T) => T,202 ...args: any203 ): MappedSubject<I, T> {204 let equalityFunc, mutateFunc, initialVal;205 if (typeof args[0] === 'function') {206 equalityFunc = args.shift() as (a: T, b: T) => boolean;207 } else {208 equalityFunc = MappedSubject.DEFAULT_EQUALITY_FUNC;209 }210 if (typeof args[0] === 'function') {211 mutateFunc = args.shift() as ((oldVal: T, newVal: T) => void);212 initialVal = args.shift() as T;213 }214 return new MappedSubject<I, T>(mapFunc, equalityFunc, mutateFunc, initialVal, ...args as any);215 }216 /**217 * Updates an input value, then re-maps this subject's value, and notifies subscribers if this results in a change to218 * the mapped value according to this subject's equality function.219 * @param index The index of the input value to update.220 */221 protected updateValue(index: number): void {222 this.inputValues[index] = this.inputs[index].get();223 const value = this.mapFunc(this.inputValues, this.value);224 if (!this.equalityFunc(this.value, value)) {225 this.mutateFunc(value);226 this.notify();227 }228 }229 /**230 * Gets the current value of the subject.231 * @returns The current value.232 */233 public get(): T {234 return this.value;235 }236 /**237 * Destroys the subscription to the parent subscribable.238 */...
ConsumerSubject.ts
Source:ConsumerSubject.ts
...63 * Consumes an event.64 * @param value The value of the event.65 */66 protected onEventConsumed(value: T): void {67 if (!this.equalityFunc(this.value, value)) {68 if (this.mutateFunc) {69 this.mutateFunc(this.value, value);70 } else {71 this.value = value;72 }73 this.notify();74 }75 }76 /** @inheritdoc */77 public get(): T {78 return this.value;79 }80 /**81 * Maps this subscribable to a new subscribable....
arrayUnion.spec.ts
Source:arrayUnion.spec.ts
...14 });15 it("returns a concat with no duplicates", () => {16 expect(arrayUnion([{ a: 1 }], [{ a: 1 }], equalityFunc)).toEqual([{ a: 1 }]);17 });18 function equalityFunc(item1, item2) {19 return item1.a === item2.a;20 }...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.runTest(url, function(err, data) {4 if (err) return console.error(err);5 console.log('Test %s from %s', data.data.testId, data.data.from);6 wpt.getTestResults(data.data.testId, function(err, data) {7 if (err) return console.error(err);8 console.log('Test %s is %s', data.data.testId, data.data.statusText);9 if (data.data.statusCode === 200) {10 console.log('First View (ms):', data.data.median.firstView.loadTime);11 console.log('Repeat View (ms):', data.data.median.repeatView.loadTime);12 }13 });14});
Using AI Code Generation
1var wpt = new WebPageTest('www.webpagetest.org');2var options = {3 lighthouseConfig: {4 settings: {5 },6 },7 lighthouseThrottling: {8 },
Using AI Code Generation
1const { equal, deepEqual } = require('assert');2const { equal: wptEqual, deepEqual: wptDeepEqual } = require('wpt');3const { equal: wptEqualFunc, deepEqual: wptDeepEqualFunc } = wptEqual.equalityFunc((a, b) => a === b);4wptEqualFunc(1, 1);5wptDeepEqualFunc({ a: 1 }, { a: 1 });6wptEqualFunc(1, 2);7wptDeepEqualFunc({ a: 1 }, { a: 2 });8equal(1, 1);9deepEqual({ a: 1 }, { a: 1 });10equal(1, 2);11deepEqual({ a: 1 }, { a: 2 });12equal(1, 1);13deepEqual({ a: 1 }, { a: 1 });14equal(1, 2);15deepEqual({ a: 1 }, { a: 2 });16equal(1, 1);17deepEqual({ a: 1 }, { a: 1 });18equal(1, 2);19deepEqual({ a: 1 }, { a: 2 });20equal(1, 1);21deepEqual({ a: 1 }, { a: 1 });22equal(1, 2);23deepEqual({ a: 1 }, { a: 2 });24equal(1, 1);25deepEqual({ a: 1 }, { a: 1 });26equal(1, 2);27deepEqual({ a: 1 }, { a: 2 });28equal(1, 1);29deepEqual({ a: 1 }, { a: 1 });30equal(1, 2);31deepEqual({ a: 1 }, { a: 2 });32equal(1, 1);33deepEqual({ a: 1 }, { a: 1 });34equal(1, 2);35deepEqual({ a: 1 }, { a: 2 });
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Albert Einstein');3 .info(function(err, info) {4 console.log(info);5 })6 .catch(function(err) {7 console.log(err);8 });
Using AI Code Generation
1var wpt=require('./wpt');2var wptObj=new wpt('your wpt api key','your wpt server url');3var testScript='testScript';4var testScriptPath='./testScript.txt';5var testCallback=function(err,data){6 if(err){7 console.log(err);8 }9 else{10 console.log(JSON.stringify(data));11 }12};13var testOptions={
Using AI Code Generation
1var wpt = require('webpagetest');2var test = new wpt('A.5b8f0c1e2d1c3e7e0c0d5b7f5e1f3b3e');3}, function(err, data) {4 if (err) return console.error(err);5 console.log('Test status check interval (ms):', test.checkStatusInterval);6 console.log('Poll the test status until the test completes:');7 test.checkTestStatus(data.data.testId, function(err, data) {8 if (err) return console.error(err);9 console.log('Test completed:');10 console.log(data);11 });12});13### test.runTest(url, options, callback)14### test.checkTestStatus(testId, callback)15* `testId` - The test ID returned by `test.runTest()`16### test.setAPIKey(key)17### test.setAPIUrl(url)18### test.setAPIVersion(version)19### test.getAPIUrl()20### test.getAPIVersion()21### test.getAPIKey()22### test.getTestStatus(testId, callback
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!!