was.js – Diff between revs 28 and 30

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 28 Rev 30
Line 1... Line 1...
1 /*! was.js - v1.0.0 - 2017-12-13 1 /*! was.js - v1.0.0 - 2017-12-13
2 * http://grimore.org 2 * http://grimore.org
3 * Copyright (c) 2017 Wizardry and Steamworks <office@grimore.org>; Licensed GPL-3.0 */ 3 * Copyright (c) 2017 Wizardry and Steamworks <office@grimore.org>; Licensed GPL-3.0 */
4 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 4 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
5 /*************************************************************************/ 5 /*************************************************************************/
6 function wasProduct(b) { 6 function wasProduct(a, b) {
7 var a = this; -  
8 var m = Math.max(this.length, a.length); 7 var m = Math.max(a.length, b.length);
9 var o = {}; 8 var o = {};
10 for(var i = 0; i < m; ++i) { 9 for(var i = 0; i < m; ++i) {
11 o[a[i]] = b[i]; 10 o[a[i]] = b[i];
12 } 11 }
13 return o; 12 return o;
14 } 13 }
15 if (!Array.prototype.product) { 14 if (!Array.prototype.product) {
16 Array.prototype.product = wasProduct; 15 Array.prototype.product = function(b) {
-   16 return wasProduct(this, b);
-   17 };
17 } 18 }
18 // jQuery 19 // jQuery
19 if(typeof jQuery === 'function') { 20 if(typeof jQuery === 'function') {
20 $.extend({ 21 $.extend({
21 product: wasProduct 22 product: wasProduct
Line 23... Line 24...
23 } 24 }
Line 24... Line 25...
24   25  
25 /*************************************************************************/ 26 /*************************************************************************/
26 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 27 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
27 /*************************************************************************/ 28 /*************************************************************************/
28 function wasStride(s) { 29 function wasStride(a, s) {
29 return this.filter(function(e, i) { 30 return a.filter(function(e, i) {
30 return i % s === 0; 31 return i % s === 0;
31 }); 32 });
32 } 33 }
33 if (!Array.prototype.stride) { 34 if (!Array.prototype.stride) {
-   35 Array.prototype.stride = function(s) {
-   36 return wasStride(this, s);
34 Array.prototype.stride = wasStride; 37 };
35 } 38 }
36 // jQuery 39 // jQuery
37 if(typeof jQuery === 'function') { 40 if(typeof jQuery === 'function') {
38 $.extend({ 41 $.extend({
Line 42... Line 45...
42   45  
43 /*************************************************************************/ 46 /*************************************************************************/
44 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 47 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
45 /*************************************************************************/ 48 /*************************************************************************/
46 // Vanilla JavaScript 49 // Vanilla JavaScript
47 function wasChunk(n) { 50 function wasChunk(a, n) {
48 if (!this.length) { 51 if (!a.length) {
49 return []; 52 return [];
50 } 53 }
51 return [this.slice(0, n)] 54 return [a.slice(0, n)]
52 .concat(this.slice(n).wasChunk(n)); 55 .concat(a.slice(n).wasChunk(n));
53 } 56 }
54 if (!Array.prototype.chunk) { 57 if (!Array.prototype.chunk) {
-   58 Array.prototype.chunk = function(a, n) {
-   59 return wasChunk(this, n);
55 Array.prototype.chunk = wasChunk; 60 };
56 } 61 }
57 // jQuery 62 // jQuery
58 if(typeof jQuery === 'function') { 63 if(typeof jQuery === 'function') {
59 $.extend({ 64 $.extend({
Line 65... Line 70...
65 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 70 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
66 /*************************************************************************/ 71 /*************************************************************************/
67 /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/ 72 /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/
68 /*************************************************************************/ 73 /*************************************************************************/
69 // Vanilla JavaScript 74 // Vanilla JavaScript
70 function wasEquals(a) { 75 function wasEquals(a, b) {
71 // if the other array is a falsy value, return 76 // if the other array is a falsy value, return
72 if (!a) { 77 if (!b) {
73 return false; 78 return false;
74 } 79 }
Line 75... Line 80...
75   80  
76 // compare lengths - can save a lot of time 81 // compare lengths - can save a lot of time
77 if (this.length !== a.length) { 82 if (a.length !== b.length) {
78 return false; 83 return false;
Line 79... Line 84...
79 } 84 }
80   85  
81 for (var i = 0, l = this.length; i < l; i++) { 86 for (var i = 0, l = a.length; i < l; i++) {
82 // Check if we have nested arrays 87 // Check if we have nested arrays
83 if (this[i] instanceof Array && a[i] instanceof Array) { 88 if (a[i] instanceof Array && b[i] instanceof Array) {
84 // recurse into the nested arrays 89 // recurse into the nested arrays
85 if (!this[i].equals(a[i])) { 90 if (!a[i].equals(b[i])) {
86 return false; 91 return false;
87 } 92 }
88 } else if (this[i] !== a[i]) { 93 } else if (a[i] !== b[i]) {
89 // Warning - two different object instances will never be equal: {x:20} != {x:20} 94 // Warning - two different object instances will never be equal: {x:20} != {x:20}
90 return false; 95 return false;
91 } 96 }
92 } 97 }
93 return true; 98 return true;
94 } 99 }
95 if (!Array.prototype.equals) { 100 if (!Array.prototype.equals) {
-   101 // attach the .equals method to Array's prototype to call it on any array
-   102 Array.prototype.equals = function(a, b) {
96 // attach the .equals method to Array's prototype to call it on any array 103 return wasEquals(this, b);
97 Array.prototype.equals = wasEquals; 104 };
98 } 105 }
99 // jQuery 106 // jQuery
100 if(typeof jQuery === 'function') { 107 if(typeof jQuery === 'function') {
Line 207... Line 214...
207 function wasSwitch(q, d, ...c) { 214 function wasSwitch(q, d, ...c) {
208 if(c.length % 2 !== 0) { 215 if(c.length % 2 !== 0) {
209 throw "Pairs of predicates expected for cases"; 216 throw "Pairs of predicates expected for cases";
210 } 217 }
Line 211... Line 218...
211 218
212 (Array.isArray(this) ? this : [ this ]).forEach((s) => { 219 (Array.isArray(q) ? q : [ q ]).forEach((s) => {
213 var m = false; 220 var m = false;
214 for(var i = 0; i < c.length; i += 2) { 221 for(var i = 0; i < c.length; i += 2) {
215 if(!c[i](s)) { 222 if(!c[i](s)) {
216 continue; 223 continue;
Line 225... Line 232...
225 d(s); 232 d(s);
226 } 233 }
227 }); 234 });
228 } 235 }
229 if (!Array.prototype.switch) { 236 if (!Array.prototype.switch) {
230 Array.prototype.switch = wasSwitch; 237 Array.prototype.switch = function(d, ...c) {
-   238 wasSwitch(this, d, ...c);
-   239 };
231 } 240 }
232 // jQuery 241 // jQuery
233 if(typeof jQuery === 'function') { 242 if(typeof jQuery === 'function') {
234 $.extend({ 243 $.extend({
235 switch: wasSwitch 244 switch: wasSwitch