1var BasketItem = function(item_object) { 2 this.image = item_object.image; 3 this.order = item_object.order; 4 this.courses = item_object.courses; 5 this.regularPrice = parseFloat(item_object.regularPrice); 6 this.discountedPrice = parseFloat(item_object.discountedPrice); 7 this.color = item_object.color; 8 this.size = item_object.size; 9 this.item = item_object.item; 10 this.desc = item_object.desc; 11 this.isFinaleSale = item_object.isFinaleSale; 12 this.variant_3 = item_object.variant_3; 13 this.customizations = item_object.customizations; 14 this.price = parseFloat(item_object.price); 15 this.used = item_object.used; 16 this.qty = parseFloat(item_object.qty); 17 this.text = item_object.text; 18 // define constant offset ints 19 this.offset_int = '77'; 20}; 21 22BasketItem.prototype.getDetails = function() 23{ 24 return { 25 item: this.item, 26 desc: this.desc, 27 price: this.price, 28 used: this.used, 29 qty: this.qty, 30 text: this.text 31 }; 32}; 33 34BasketItem.prototype.generateSignature = function() 35{ 36 var string_builder_hash = [this.item, this.desc, this.qty].join(this.offset_int); 37 var hash = 0, i, chr; 38 for(i = 0; i < string_builder_hash.length; i++) { 39 chr = string_builder_hash.charCodeAt(i); 40 hash = ( ( hash << 5 ) - hash) + chr; 41 hash |= 0; 42 } 43 return hash; 44}; 45 46BasketItem.prototype.generateSubSignature = function(details) 47{ 48 var string_builder_hash = details.join(this.offset_int); 49 var hash = 0, i, chr; 50 for(i = 0; i < string_builder_hash.length; i++) { 51 chr = string_builder_hash.charCodeAt(i); 52 hash = ( ( hash << 5 ) - hash) + chr; 53 hash |= 0; 54 } 55 return hash; 56}; 57 58// This is mainly used in testing to ensure the signature calculation is firing and working properly 59BasketItem.prototype.compare = function (origin) { 60 var origin_info = origin.getDetails(); 61 var self = this.getDetails(); 62 if( origin.generateSubSignature([ origin_info.item, origin_info.desc ]) == this.generateSubSignature([ this.item, this.desc ]) ) 63 { 64 return { 65 state: 'UPDATE', 66 increment: true 67 }; 68 } 69 else 70 { 71 return { 72 state: 'DIFFERENT', 73 increment: false 74 }; 75 } 76}; 77 78BasketItem.test = function() { 79 // We test ourselves 80 var i1 = new BasketItem({ 81 item: 1234, 82 desc: "First Item", 83 price: 4.13, 84 used: "", 85 qty: 1, 86 text: null 87 }); 88 89 var i2 = new BasketItem({ 90 item: 1234, 91 desc: "First Item", 92 price: 4.13, 93 used: "", 94 qty: 1, 95 text: null 96 }); 97 98 /** 99 * Our first test, we expect the items to be identical 100 */ 101 if(i1.generateSignature() === i2.generateSignature()) 102 { 103 console.log('[Test] i1 === i2: Passed'); 104 } 105 106 /** 107 * Our next test, we'll operate on i2, change the QTY field, and expect a state of update 108 */ 109 i2.qty = 4; 110 if(i1.generateSignature() !== i2.generateSignature()) 111 { 112 var comparison = i1.compare(i2); 113 if(comparison.state === 'UPDATE' && comparison.increment) 114 { 115 console.log('[Test] i1 !== i2 by qty: Passed'); 116 } 117 else 118 { 119 throw new TypeError('Failed to run test two; the state was ' + comparison.state + ', however we expected UPDATE.'); 120 } 121 } 122 else 123 { 124 throw new TypeError('Failed to run test two; both signatures match.'); 125 } 126 127 /** 128 * Our final test, we'll change i1 entirely, and compare them two, we expect a DIFFERENT state with INCREMENT as false 129 */ 130 i1.item = Math.floor(Math.random() * 32) + 1; 131 i1.desc = "New Item Is Here"; 132 i1.price = 4.63; 133 i1.used = ""; 134 i1.qty = 46; 135 i1.text = null; 136 137 if(i1.generateSignature() !== i2.generateSignature()) 138 { 139 var comparison = i1.compare(i2); 140 if(comparison.state === 'DIFFERENT' && !comparison.increment) 141 { 142 console.log('[Test] i1 !== i2 due to different objects: Passed'); 143 } 144 else 145 { 146 throw new TypeError('Failed to run test two; the state was ' + comparison.state + ', however we expected DIFFERENT.'); 147 } 148 } 149 else 150 { 151 throw new TypeError('Failed to run test three; both signatures match.'); 152 } 153}; 154 155if(typeof module !== 'undefined') { 156 module.exports = BasketItem; 157}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.