was.js – Blame information for rev 48

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