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