How to use cloneValue method in wpt

Best JavaScript code snippet using wpt

clone.js

Source:clone.js Github

copy

Full Screen

1( function ( $, rwmb ) {2 'use strict';3 // Object holds all methods related to fields' index when clone4 var cloneIndex = {5 /**6 * Set index for fields in a .rwmb-clone7 * @param $inputs .rwmb-clone element8 * @param index Index value9 */10 set: function ( $inputs, index ) {11 $inputs.each( function () {12 var $field = $( this );13 // Name attribute14 var name = this.name;15 if ( name && ! $field.closest( '.rwmb-group-clone' ).length ) {16 $field.attr( 'name', cloneIndex.replace( index, name, '[', ']', false ) );17 }18 // ID attribute19 var id = this.id;20 if ( id ) {21 $field.attr( 'id', cloneIndex.replace( index, id, '_', '', true, true ) );22 }23 $field.trigger( 'update_index', index );24 } );25 // Address button's value attribute26 var $address = $inputs.filter( '.rwmb-map-goto-address-button' );27 if ( $address.length ) {28 var value = $address.attr( 'value' );29 $address.attr( 'value', cloneIndex.replace( index, value, '_' ) );30 }31 },32 /**33 * Replace an attribute of a field with updated index34 * @param index New index value35 * @param value Attribute value36 * @param before String before returned value37 * @param after String after returned value38 * @param alternative Check if attribute does not contain any integer, will reset the attribute?39 * @param isEnd Check if we find string at the end?40 * @return string41 */42 replace: function ( index, value, before, after, alternative, isEnd ) {43 before = before || '';44 after = after || '';45 if ( typeof alternative === 'undefined' ) {46 alternative = true;47 }48 var end = isEnd ? '$' : '';49 var regex = new RegExp( cloneIndex.escapeRegex( before ) + '(\\d+)' + cloneIndex.escapeRegex( after ) + end ),50 newValue = before + index + after;51 return regex.test( value ) ? value.replace( regex, newValue ) : (alternative ? value + newValue : value );52 },53 /**54 * Helper function to escape string in regular expression55 * @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions56 * @param string57 * @return string58 */59 escapeRegex: function ( string ) {60 return string.replace( /[.*+?^${}()|[\]\\]/g, "\\$&" );61 },62 /**63 * Helper function to create next index for clones64 * @param $container .rwmb-input container65 * @return integer66 */67 nextIndex: function ( $container ) {68 var nextIndex = $container.data( 'next-index' );69 $container.data( 'next-index', nextIndex + 1 );70 return nextIndex;71 }72 };73 // Object holds all method related to fields' value when clone.74 var cloneValue = {75 /**76 * Reset field value when clone. Expect this = current input.77 */78 reset: function() {79 cloneValue.$field = $( this );80 cloneValue.type = cloneValue.$field.attr( 'type' );81 cloneValue.isHiddenField = cloneValue.$field.hasClass( 'rwmb-hidden' );82 if ( true === cloneValue.$field.data( 'clone-default' ) ) {83 cloneValue.resetToDefault();84 } else {85 cloneValue.clear();86 }87 },88 /**89 * Reset field value to its default.90 */91 resetToDefault: function() {92 var defaultValue = cloneValue.$field.data( 'default' );93 if ( 'radio' === cloneValue.type ) {94 cloneValue.$field.prop( 'checked', cloneValue.$field.val() === defaultValue );95 } else if ( 'checkbox' === cloneValue.type ) {96 cloneValue.$field.prop( 'checked', !!defaultValue );97 } else if ( 'select' === cloneValue.type ) {98 cloneValue.$field.find( 'option[value="' + defaultValue + '"]' ).prop( 'selected', true );99 } else if ( ! cloneValue.isHiddenField ) {100 cloneValue.$field.val( defaultValue );101 }102 },103 /**104 * Clear field value.105 */106 clear: function() {107 if ( 'radio' === cloneValue.type || 'checkbox' === cloneValue.type ) {108 cloneValue.$field.prop( 'checked', false );109 } else if ( 'select' === cloneValue.type ) {110 cloneValue.$field.prop( 'selectedIndex', - 1 );111 } else if ( ! cloneValue.isHiddenField ) {112 cloneValue.$field.val( '' );113 }114 }115 };116 /**117 * Clone fields118 * @param $container A div container which has all fields119 */120 function clone( $container ) {121 var $last = $container.children( '.rwmb-clone' ).last(),122 $clone = $last.clone(),123 nextIndex = cloneIndex.nextIndex( $container );124 // Reset value for fields125 var $inputs = $clone.find( rwmb.inputSelectors );126 $inputs.each( cloneValue.reset );127 // Insert Clone128 $clone.insertAfter( $last );129 // Trigger custom event for the clone instance. Required for Group extension to update sub fields.130 $clone.trigger( 'clone_instance', nextIndex );131 // Set fields index. Must run before trigger clone event.132 cloneIndex.set( $inputs, nextIndex );133 // Trigger custom clone event.134 $inputs.trigger( 'clone', nextIndex );135 // After cloning fields.136 $inputs.trigger( 'after_clone', nextIndex );137 // Trigger custom change event for MB Blocks to update block attributes.138 $inputs.first().trigger( 'mb_change' );139 }140 /**141 * Hide remove buttons when there's only 1 of them142 *143 * @param $container .rwmb-input container144 */145 function toggleRemoveButtons( $container ) {146 var $clones = $container.children( '.rwmb-clone' );147 $clones.children( '.remove-clone' ).toggle( $clones.length > 1 );148 // Recursive for nested groups.149 $container.find( '.rwmb-input' ).each( function () {150 toggleRemoveButtons( $( this ) );151 } );152 }153 /**154 * Toggle add button155 * Used with [data-max-clone] attribute. When max clone is reached, the add button is hid and vice versa156 *157 * @param $container .rwmb-input container158 */159 function toggleAddButton( $container ) {160 var $button = $container.children( '.add-clone' ),161 maxClone = parseInt( $container.data( 'max-clone' ) ),162 numClone = $container.children( '.rwmb-clone' ).length;163 $button.toggle( isNaN( maxClone ) || ( maxClone && numClone < maxClone ) );164 }165 function addClone( e ) {166 e.preventDefault();167 var $container = $( this ).closest( '.rwmb-input' );168 clone( $container );169 toggleRemoveButtons( $container );170 toggleAddButton( $container );171 sortClones.apply( $container[0] );172 }173 function removeClone( e ) {174 e.preventDefault();175 var $this = $( this ),176 $container = $this.closest( '.rwmb-input' );177 // Remove clone only if there are 2 or more of them178 if ( $container.children( '.rwmb-clone' ).length < 2 ) {179 return;180 }181 $this.parent().trigger( 'remove' ).remove();182 toggleRemoveButtons( $container );183 toggleAddButton( $container );184 // Trigger custom change event for MB Blocks to update block attributes.185 $container.find( rwmb.inputSelectors ).first().trigger( 'mb_change' );186 }187 /**188 * Sort clones.189 * Expect this = .rwmb-input element.190 */191 function sortClones() {192 var $container = $( this );193 if ( undefined !== $container.sortable( 'instance' ) ) {194 return;195 }196 if ( 0 === $container.children( '.rwmb-clone' ).length ) {197 return;198 }199 $container.sortable( {200 handle: '.rwmb-clone-icon',201 placeholder: ' rwmb-clone rwmb-sortable-placeholder',202 items: '> .rwmb-clone',203 start: function ( event, ui ) {204 // Make the placeholder has the same height as dragged item205 ui.placeholder.height( ui.item.outerHeight() );206 // Fixed WYSIWYG field blank when inside a sortable, cloneable group.207 // https://stackoverflow.com/a/25667486/371240208 ui.item.find( '.rwmb-wysiwyg' ).each( function () {209 tinymce.execCommand( 'mceRemoveEditor', false, this.id );210 } );211 },212 update: function( event, ui ) {213 ui.item.find( '.rwmb-wysiwyg' ).each( function () {214 tinymce.execCommand( 'mceAddEditor', true, this.id );215 } );216 ui.item.find( rwmb.inputSelectors ).first().trigger( 'mb_change' );217 }218 } );219 }220 function start() {221 var $container = $( this );222 toggleRemoveButtons( $container );223 toggleAddButton( $container );224 $container.data( 'next-index', $container.children( '.rwmb-clone' ).length );225 sortClones.apply( this );226 }227 function init( e ) {228 $( e.target ).find( '.rwmb-input' ).each( start );229 }230 rwmb.$document231 .on( 'mb_ready', init )232 .on( 'click', '.add-clone', addClone )233 .on( 'click', '.remove-clone', removeClone );...

Full Screen

Full Screen

ngx-deep-clone.spec.ts

Source:ngx-deep-clone.spec.ts Github

copy

Full Screen

1import {ngxDeepClone} from './ngx-deep-clone';2describe('NgxDeepClone', () => {3 beforeEach(() => {4 });5 it('should light copy', () => {6 const source = {field: 1};7 const cloneValue = source;8 expect(cloneValue).toEqual(source);9 expect(cloneValue.field).toEqual(source.field);10 source.field = 1;11 expect(cloneValue.field).toEqual(1);12 });13 it('should deep copy', () => {14 const source = {field: 1};15 const cloneValue = ngxDeepClone(source);16 expect(cloneValue === source).toBeFalsy();17 expect(cloneValue.field === source.field).toBeTruthy();18 });19 it('should deep copy with regexp', () => {20 const source = {reg: new RegExp('/"/g')};21 const cloneValue = ngxDeepClone(source);22 expect(cloneValue === source).toBeFalsy();23 expect(cloneValue.reg === source.reg).toBeTruthy();24 });25 it('should deep copy with 5level', () => {26 const source = {level1: {level2: {level3: {level4: {level5: 'dest'}}}}};27 const cloneValue = ngxDeepClone(source);28 expect(cloneValue === source).toBeFalsy();29 expect(cloneValue === source).toBeFalsy();30 expect(cloneValue.level1 === source.level1).toBeFalsy();31 expect(cloneValue.level1.level2 === source.level1.level2).toBeFalsy();32 expect(cloneValue.level1.level2.level3 === source.level1.level2.level3).toBeFalsy();33 expect(cloneValue.level1.level2.level3.level4 === source.level1.level2.level3.level4).toBeFalsy();34 expect(cloneValue.level1.level2.level3.level4.level5 === source.level1.level2.level3.level4.level5).toBeTruthy();35 });36 it('should deep copy with same variable1', () => {37 const originVariable = {name: 'originVariable'};38 const source = {fieldA: originVariable, fieldB: originVariable};39 const cloneValue = ngxDeepClone(source);40 expect(source.fieldA === source.fieldB).toBeTruthy();41 expect(cloneValue.fieldA === source.fieldB).toBeFalsy();42 expect(source.fieldA === cloneValue.fieldB).toBeFalsy();43 expect(cloneValue.fieldA === cloneValue.fieldB).toBeTruthy();44 expect(JSON.stringify(cloneValue.fieldA) === JSON.stringify(cloneValue.fieldB)).toBeTruthy();45 });46 it('should deep copy with same variable2', () => {47 const originVariable = [1, 2, 3, 3, 3, 3, 3, 3];48 const source = {array1: originVariable, array2: originVariable};49 const cloneValue = ngxDeepClone(source);50 expect(source.array1 === source.array2).toBeTruthy();51 expect(cloneValue.array1 === cloneValue.array2).toBeTruthy();52 expect(JSON.stringify(cloneValue.fieldA) === JSON.stringify(cloneValue.fieldB)).toBeTruthy();53 });54 it('should deep copy with big data', () => {55 const source = {};56 let last = source;57 for (let i = 0; i < 1000; i++) {58 last[i] = {};59 last = last[i];60 }61 const cloneValue = ngxDeepClone(source);62 expect(source === cloneValue).toBeFalsy();63 expect(JSON.stringify(cloneValue.fieldA) === JSON.stringify(cloneValue.fieldB)).toBeTruthy();64 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptoolkit = require('wptoolkit');2var cloneValue = wptoolkit.cloneValue;3var obj = {4 c: {5 }6};7var clone = cloneValue(obj);8obj.c.e = 5;9var wptoolkit = require('wptoolkit');10var cloneObject = wptoolkit.cloneObject;11var obj = {12 c: {13 }14};15var clone = cloneObject(obj);16obj.c.e = 5;17var wptoolkit = require('wptoolkit');18var cloneArray = wptoolkit.cloneArray;19var arr = [1, 2, [3, 4]];20var clone = cloneArray(arr);21arr[2][1] = 5;22var wptoolkit = require('wptoolkit');23var cloneDate = wptoolkit.cloneDate;24var date = new Date();25var clone = cloneDate(date);26date.setFullYear(2018);

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var cloneValue = wpt.cloneValue;3var original = {a: 1, b: 2, c: 3};4var clone = cloneValue(original);5original.a = 4;6var wpt = require('wpt');7var cloneValue = wpt.cloneValue;8var original = {a: 1, b: 2, c: 3};9var clone = cloneValue(original);10original.a = 4;11var wpt = require('wpt');12var cloneValue = wpt.cloneValue;13var original = {a: 1, b: 2, c: 3};14var clone = cloneValue(original);15original.a = 4;16var wpt = require('wpt');17var cloneValue = wpt.cloneValue;18var original = {a: 1, b: 2, c: 3};19var clone = cloneValue(original);20original.a = 4;

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var cloneValue = wpt.cloneValue;3var obj = {a:1, b:2, c:3};4var cloneObj = cloneValue(obj);5var arr = [1,2,3];6var cloneArr = cloneValue(arr);7var str = "hello";8var cloneStr = cloneValue(str);9var num = 123;10var cloneNum = cloneValue(num);11var bool = true;12var cloneBool = cloneValue(bool);13var nul = null;14var cloneNul = cloneValue(nul);15var undf = undefined;16var cloneUndf = cloneValue(undf);17var date = new Date();18var cloneDate = cloneValue(date);19var regexp = new RegExp("test");20var cloneRegexp = cloneValue(regexp);21var func = function(){};22var cloneFunc = cloneValue(func);23var wpt = require('wpt');24var cloneValue = wpt.cloneValue;25var obj = {a:1, b:2, c:3};26var cloneObj = cloneValue(obj);27var arr = [1,2,3];28var cloneArr = cloneValue(arr);29var str = "hello";30var cloneStr = cloneValue(str);31var num = 123;

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var cloneValue = wpt.cloneValue;3var obj = {a:1, b:2, c:3};4var obj2 = cloneValue(obj);5obj2.a = 10;6obj2.b = 20;7var wpt = require('webpagetest');8var cloneValue = wpt.cloneValue;9var obj = {a:1, b:2, c:3};10var obj2 = cloneValue(obj);11obj2.a = 10;12obj2.b = 20;13var wpt = require('webpagetest');14var cloneValue = wpt.cloneValue;15var obj = {a:1, b:2, c:3};16var obj2 = cloneValue(obj);17obj2.a = 10;18obj2.b = 20;19var wpt = require('webpagetest');20var cloneValue = wpt.cloneValue;21var obj = {a:1, b:2, c:3};22var obj2 = cloneValue(obj);23obj2.a = 10;24obj2.b = 20;25var wpt = require('webpagetest');26var cloneValue = wpt.cloneValue;27var obj = {a:1, b:2, c:3};28var obj2 = cloneValue(obj);29obj2.a = 10;30obj2.b = 20;31var wpt = require('webpagetest');32var cloneValue = wpt.cloneValue;33var obj = {a:1, b:2, c:3};34var obj2 = cloneValue(obj);35obj2.a = 10;36obj2.b = 20;

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2wpt.cloneValue('test', function (err, data) {3 if (err) { throw err; }4 console.log(data);5});6module.exports = {7 cloneValue: function (test, callback) {8 var options = {9 };10 request(options, function (err, res, body) {11 if (err) { return callback(err); }12 if (res.statusCode !== 200) { return callback(new Error('Unexpected status code: ' + res.statusCode)); }13 callback(null, body);14 });15 }16};17var wpt = require('wpt');18var options = {19};20request(options, function (err, res, body) {21 if (err) { return callback(err); }22 if (res.statusCode !== 200) { return callback(new Error('Unexpected status code: ' + res.statusCode)); }23 callback(null, body);24});25var wpt = require('wpt');26var options = {

Full Screen

Using AI Code Generation

copy

Full Screen

1var obj = {name: 'test'};2console.log(obj);3var obj2 = cloneValue(obj);4console.log(obj2);5var obj = {name: 'test'};6console.log(obj);7var obj2 = cloneValue(obj);8console.log(obj2);9var obj = {name: 'test'};10console.log(obj);11var obj2 = cloneValue(obj);12console.log(obj2);13var obj = {name: 'test'};14console.log(obj);15var obj2 = cloneValue(obj);16console.log(obj2);

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var wptConfig = require('./wptConfig.json');3var wptApi = new wpt(wptConfig.apiKey, wptConfig.apiSecret);4var data = { "a": 1, "b": 2, "c": 3 };5var clonedData = wptApi.cloneValue(data);6data.a = 10;7var clonedData = wptApi.cloneValue(data);8wptApi.getLocations(function(err, locations) {9 if(err) {10 console.log("Error: " + err);11 } else {12 console.log("Locations: " + JSON.stringify(locations));13 }14});15wptApi.getOptions(function(err, options) {16 if(err) {17 console.log("Error: " + err);18 } else {19 console.log("Options: " + JSON.stringify(options));20 }21});22wptApi.getTesters(function(err, testers) {23 if(err) {24 console.log("Error: " + err);25 } else {26 console.log("Testers: " + JSON.stringify(testers));27 }28});29wptApi.getTest(testId, function(err, test) {30 if(err) {31 console.log("Error: " + err);32 } else {33 console.log("Test: " + JSON.stringify(test));34 }35});36wptApi.getTestResults(testId, function(err, testResults) {37 if(err) {38 console.log("Error: " + err);39 } else {40 console.log("Test Results: " + JSON.stringify(testResults));41 }42});

Full Screen

Using AI Code Generation

copy

Full Screen

1function wptbElementSetup( element, ...classes ) {2 classes.forEach( function( className ) {3 let el = element.getElementsByClassName( className );4 if( el.length > 0 ) {5 el = el[0];6 let elClone = wptbCloneValue( el );7 el.parentNode.insertBefore( elClone, el );8 }9 } );10}

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful