was.js – Diff between revs 5 and 25

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