was.js – Diff between revs 8 and 12

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