was.js – Diff between revs 34 and 41

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