was.js – Diff between revs 26 and 27

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 26 Rev 27
1 /*************************************************************************/ 1 /*************************************************************************/
2 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 2 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
3 /*************************************************************************/ 3 /*************************************************************************/
4 function wasProduct(b) { 4 function wasProduct(b) {
5 var a = this; 5 var a = this;
6 var m = Math.max(this.length, a.length); 6 var m = Math.max(this.length, a.length);
7 var o = {}; 7 var o = {};
8 for(var i = 0; i < m; ++i) { 8 for(var i = 0; i < m; ++i) {
9 o[a[i]] = b[i]; 9 o[a[i]] = b[i];
10 } 10 }
11 return o; 11 return o;
12 } 12 }
13 if (!Array.prototype.product) { 13 if (!Array.prototype.product) {
14 Array.prototype.product = wasProduct; 14 Array.prototype.product = wasProduct;
15 } 15 }
16 // jQuery 16 // jQuery
17 if(typeof jQuery === 'function') { 17 if(typeof jQuery === 'function') {
18 $.extend({ 18 $.extend({
19 product: wasProduct 19 product: wasProduct
20 }); 20 });
21 } 21 }
22   22  
23 /*************************************************************************/ 23 /*************************************************************************/
24 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 24 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
25 /*************************************************************************/ 25 /*************************************************************************/
26 function wasStride(s) { 26 function wasStride(s) {
27 return this.filter(function(e, i) { 27 return this.filter(function(e, i) {
28 return i % s === 0; 28 return i % s === 0;
29 }); 29 });
30 } 30 }
31 if (!Array.prototype.stride) { 31 if (!Array.prototype.stride) {
32 Array.prototype.stride = wasStride; 32 Array.prototype.stride = wasStride;
33 } 33 }
34 // jQuery 34 // jQuery
35 if(typeof jQuery === 'function') { 35 if(typeof jQuery === 'function') {
36 $.extend({ 36 $.extend({
37 stride: wasStride 37 stride: wasStride
38 }); 38 });
39 } 39 }
40   40  
41 /*************************************************************************/ 41 /*************************************************************************/
42 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 42 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
43 /*************************************************************************/ 43 /*************************************************************************/
44 // Vanilla JavaScript 44 // Vanilla JavaScript
45 function wasChunk(n) { 45 function wasChunk(n) {
46 if (!this.length) { 46 if (!this.length) {
47 return []; 47 return [];
48 } 48 }
49 return [this.slice(0, n)] 49 return [this.slice(0, n)]
50 .concat(this.slice(n).wasChunk(n)); 50 .concat(this.slice(n).wasChunk(n));
51 } 51 }
52 if (!Array.prototype.chunk) { 52 if (!Array.prototype.chunk) {
53 Array.prototype.chunk = wasChunk; 53 Array.prototype.chunk = wasChunk;
54 } 54 }
55 // jQuery 55 // jQuery
56 if(typeof jQuery === 'function') { 56 if(typeof jQuery === 'function') {
57 $.extend({ 57 $.extend({
58 chunk: wasChunk 58 chunk: wasChunk
59 }); 59 });
60 } 60 }
61   61  
62 /*************************************************************************/ 62 /*************************************************************************/
63 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 63 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
64 /*************************************************************************/ 64 /*************************************************************************/
65 /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/ 65 /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/
66 /*************************************************************************/ 66 /*************************************************************************/
67 // Vanilla JavaScript 67 // Vanilla JavaScript
68 function wasEquals(a) { 68 function wasEquals(a) {
69 // if the other array is a falsy value, return 69 // if the other array is a falsy value, return
70 if (!a) { 70 if (!a) {
71 return false; 71 return false;
72 } 72 }
73   73  
74 // compare lengths - can save a lot of time 74 // compare lengths - can save a lot of time
75 if (this.length !== a.length) { 75 if (this.length !== a.length) {
76 return false; 76 return false;
77 } 77 }
78   78  
79 for (var i = 0, l = this.length; i < l; i++) { 79 for (var i = 0, l = this.length; i < l; i++) {
80 // Check if we have nested arrays 80 // Check if we have nested arrays
81 if (this[i] instanceof Array && a[i] instanceof Array) { 81 if (this[i] instanceof Array && a[i] instanceof Array) {
82 // recurse into the nested arrays 82 // recurse into the nested arrays
83 if (!this[i].equals(a[i])) { 83 if (!this[i].equals(a[i])) {
84 return false; 84 return false;
85 } 85 }
86 } else if (this[i] !== a[i]) { 86 } else if (this[i] !== a[i]) {
87 // Warning - two different object instances will never be equal: {x:20} != {x:20} 87 // Warning - two different object instances will never be equal: {x:20} != {x:20}
88 return false; 88 return false;
89 } 89 }
90 } 90 }
91 return true; 91 return true;
92 } 92 }
93 if (!Array.prototype.equals) { 93 if (!Array.prototype.equals) {
94 // attach the .equals method to Array's prototype to call it on any array 94 // attach the .equals method to Array's prototype to call it on any array
95 Array.prototype.equals = wasEquals; 95 Array.prototype.equals = wasEquals;
96 } 96 }
97 // jQuery 97 // jQuery
98 if(typeof jQuery === 'function') { 98 if(typeof jQuery === 'function') {
99 $.extend({ 99 $.extend({
100 equals: wasEquals 100 equals: wasEquals
101 }); 101 });
102 } 102 }
103   103  
104 /*************************************************************************/ 104 /*************************************************************************/
105 /* Node.JS package export. */ 105 /* Node.JS package export. */
106 /*************************************************************************/ 106 /*************************************************************************/
107 module.exports.collections.arrays = { 107 module.exports.collections = {
108 product: wasProduct, 108 product: wasProduct,
109 stride: wasStride, 109 stride: wasStride,
110 chunk: wasChunk, 110 chunk: wasChunk,
111 equals: wasEquals 111 equals: wasEquals
112 }; 112 };
113   113