was.js – Blame information for rev 12

Subversion Repositories:
Rev:
Rev Author Line No. Line
12 office 1 /*! was - v1.0.0 - 2017-05-29
3 office 2 * http://grimore.org
3 * Copyright (c) 2017 Wizardry and Steamworks <office@grimore.org>; Licensed GPL-3.0 */
4 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
5 /*************************************************************************/
6 if (!Array.prototype.product) {
7 Array.prototype.product = function(b) {
8 var a = this;
9 return $.map(
10 new Array(Math.max(this.length, a.length)),
11 function(e, i) {
12 var o = {};
13 o[a[i]] = b[i];
14 return o;
5 office 15 });
3 office 16 };
17 }
18 $.extend({
19 product: function(a, b) {
20 return $.map(
21 new Array(Math.max(this.length, a.length)),
22 function(e, i) {
23 var o = {};
24 o[a[i]] = b[i];
25 return o;
5 office 26 });
3 office 27 }
28 });
29  
30 /*************************************************************************/
31 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
32 /*************************************************************************/
33 if (!Array.prototype.stride) {
34 Array.prototype.stride = function(s) {
35 return this.filter(function(e, i) {
36 return i % s === 0;
37 });
38 };
39 }
40 $.extend({
41 stride: function(a, s) {
42 return a.filter(function(e, i) {
43 return i % s === 0;
44 });
45 }
46 });
47  
48 /*************************************************************************/
49 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
50 /*************************************************************************/
51 if (!Array.prototype.chunk) {
52 Array.prototype.chunk = function(n) {
53 if (!this.length) {
54 return [];
55 }
56 return [this.slice(0, n)]
57 .concat(this.slice(n).chunk(n));
58 };
59 }
60 $.extend({
61 chunk: function(a, n) {
62 if (!a.length) {
63 return [];
64 }
65 return [a.slice(0, n)]
66 .concat(a.slice(n).chunk(n));
67 }
68 });
69  
5 office 70 /*************************************************************************/
71 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
72 /*************************************************************************/
73 /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/
74 /*************************************************************************/
75 if (!Array.prototype.equals) {
76 // attach the .equals method to Array's prototype to call it on any array
77 Array.prototype.equals = function(a) {
78 // if the other array is a falsy value, return
79 if (!a) {
80 return false;
81 }
82  
83 // compare lengths - can save a lot of time
84 if (this.length !== a.length) {
85 return false;
86 }
87  
88 for (var i = 0, l = this.length; i < l; i++) {
89 // Check if we have nested arrays
90 if (this[i] instanceof Array && a[i] instanceof Array) {
91 // recurse into the nested arrays
92 if (!this[i].equals(a[i])) {
93 return false;
94 }
95 } else if (this[i] !== a[i]) {
96 // Warning - two different object instances will never be equal: {x:20} != {x:20}
97 return false;
98 }
99 }
100 return true;
101 };
102 }
103  
104 $.extend({
105 equals: function(a) {
106 // if the other array is a falsy value, return
107 if (!a) {
108 return false;
109 }
110  
111 // compare lengths - can save a lot of time
112 if (this.length !== a.length) {
113 return false;
114 }
115  
116 for (var i = 0, l = this.length; i < l; i++) {
117 // Check if we have nested arrays
118 if (this[i] instanceof Array && a[i] instanceof Array) {
119 // recurse into the nested arrays
120 if (!this[i].equals(a[i])) {
121 return false;
122 }
123 } else if (this[i] !== a[i]) {
124 // Warning - two different object instances will never be equal: {x:20} != {x:20}
125 return false;
126 }
127 }
128 return true;
129 }
130 });
131  
7 office 132 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
133 /*************************************************************************/
3 office 134 function wasCSVToArray(csv) {
135 var l = [];
136 var s = [];
137 var m = "";
138  
139 do {
140 var a = csv.charAt(0);
141 csv = csv.slice(1, csv.length);
142 if(a === ",") {
143 if(s[s.length-1] !== '"') {
144 l.push(m);
145 m = "";
146 continue;
147 }
148 m += a;
149 continue;
150 }
151 if(a === '"' && csv.charAt(0) === a) {
152 m += a;
153 csv = csv.slice(1, csv.length);
154 continue;
155 }
156 if(a === '"') {
157 if(s[s.length-1] !== a) {
158 s.push(a);
159 continue;
160 }
161 s.pop();
162 continue;
163 }
164 m += a;
165 } while(csv !== "");
166  
167 l.push(m);
168  
169 return l;
170 }
171  
7 office 172 /*************************************************************************/
173 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
174 /*************************************************************************/
3 office 175 function wasArrayToCSV(a) {
176 var csv = [];
177 for(var i=0; i<a.length; ++i) {
178 var cell = a[i].toString().replace('"', '""');
179 if(/"\s,\r\n/.test(cell)) {
180 csv[i] = '"' + cell + '"';
181 continue;
182 }
183 csv[i] = cell;
184 }
185 return csv.join();
186 }
7 office 187  
12 office 188 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
189 /*************************************************************************/
190 function wasKeyValueObjectify(a) {
191 var o = {};
192 a.reduce(function(a, c, i) {
193 i = Math.floor(i / 2);
194 if (!a[i]) {
195 a[i] = [];
196 }
197 a[i].push(c);
198 return a;
199 }, []).forEach(function(c, i, a) {
200 o[c[0]] = c[1];
201 }, o);
202 return o;
203 }
204  
7 office 205 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
206 /*************************************************************************/
207 function wasMapValueToRange(value, xMin, xMax, yMin, yMax) {
208 return yMin + (
209 ( yMax - yMin ) * ( value - xMin ) / ( xMax - xMin )
210 );
211 }
212  
213 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
214 /*************************************************************************/
8 office 215 function wasHexToRGB(hex) {
7 office 216 var shortRegEx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
217 hex = hex.replace(
8 office 218 shortRegEx,
7 office 219 function(m, r, g, b) {
220 return r + r + g + g + b + b;
221 }
222 );
8 office 223  
7 office 224 var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
225 return result ? {
226 r: parseInt(result[1], 16),
227 g: parseInt(result[2], 16),
228 b: parseInt(result[3], 16)
229 } : null;
8 office 230 }
231  
232 /*************************************************************************/
233 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
234 /*************************************************************************/
235 function wasRGBToHex(r, g, b) {
236 return "#" + (
237 (1 << 24) +
238 (r << 16) +
239 (g << 8) +
240 b
241 ).toString(16).slice(1);
242 }