was.js – Diff between revs 5 and 25

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 5 Rev 25
Line 1... Line 1...
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 }
Line 27... Line 22...
27   22  
28 /*************************************************************************/ 23 /*************************************************************************/
29 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 24 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
-   25 /*************************************************************************/
-   26 function wasStride(s) {
-   27 return this.filter(function(e, i) {
-   28 return i % s === 0;
-   29 });
30 /*************************************************************************/ 30 }
31 if (!Array.prototype.stride) { 31 if (!Array.prototype.stride) {
32 Array.prototype.stride = function(s) { -  
33 return this.filter(function(e, i) { -  
34 return i % s === 0; -  
35 }); -  
36 }; 32 Array.prototype.stride = wasStride;
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; -  
42 }); 37 stride: wasStride
43 } 38 });
Line 44... Line 39...
44 }); 39 }
45   40  
46 /*************************************************************************/ 41 /*************************************************************************/
47 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ -  
48 /*************************************************************************/ -  
49 if (!Array.prototype.chunk) { -  
50 Array.prototype.chunk = function(n) { 42 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
51 if (!this.length) { -  
52 return []; -  
53 } -  
54 return [this.slice(0, n)] -  
55 .concat(this.slice(n).chunk(n)); -  
56 }; -  
57 } 43 /*************************************************************************/
58 $.extend({ 44 // Vanilla JavaScript
59 chunk: function(a, n) { 45 function wasChunk(n) {
60 if (!a.length) { -  
61 return []; -  
62 } -  
63 return [a.slice(0, n)] 46 if (!this.length) {
-   47 return [];
-   48 }
64 .concat(a.slice(n).chunk(n)); 49 return [this.slice(0, n)]
-   50 .concat(this.slice(n).wasChunk(n));
-   51 }
-   52 if (!Array.prototype.chunk) {
-   53 Array.prototype.chunk = wasChunk;
-   54 }
-   55 // jQuery
-   56 if(typeof jQuery === 'function') {
-   57 $.extend({
-   58 chunk: wasChunk
Line 65... Line 59...
65 } 59 });
66 }); 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*/ -  
72 /*************************************************************************/ 65 /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/
73 if (!Array.prototype.equals) { 66 /*************************************************************************/
74 // attach the .equals method to Array's prototype to call it on any array 67 // Vanilla JavaScript
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
Line 77... Line 70...
77 if (!a) { 70 if (!a) {
78 return false; 71 return false;
79 } 72 }
80   73  
Line 81... Line 74...
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++) { -  
87 // Check if we have nested arrays -  
88 if (this[i] instanceof Array && a[i] instanceof Array) { -  
89 // recurse into the nested arrays -  
90 if (!this[i].equals(a[i])) { 79 for (var i = 0, l = this.length; i < l; i++) {
91 return false; 80 // Check if we have nested arrays
92 } -  
93 } else if (this[i] !== a[i]) { -  
94 // Warning - two different object instances will never be equal: {x:20} != {x:20} -  
95 return false; -  
96 } -  
97 } -  
98 return true; 81 if (this[i] instanceof Array && a[i] instanceof Array) {
99 }; 82 // recurse into the nested arrays
100 } -  
101   83 if (!this[i].equals(a[i])) {
102 $.extend({ 84 return false;
103 equals: function(a) { -  
104 // if the other array is a falsy value, return -  
105 if (!a) { -  
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} 85 }
-   86 } else if (this[i] !== a[i]) {
-   87 // Warning - two different object instances will never be equal: {x:20} != {x:20}
-   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 /*************************************************************************/
123 return false; 107 module.exports.collections.arrays = {