was.js – Blame information for rev 5
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
3 | office | 1 | /*! was - v1.0.0 - 2017-05-28 |
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 | |||
3 | office | 132 | /////////////////////////////////////////////////////////////////////////// |
133 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
134 | /////////////////////////////////////////////////////////////////////////// |
||
135 | function wasCSVToArray(csv) { |
||
136 | var l = []; |
||
137 | var s = []; |
||
138 | var m = ""; |
||
139 | |||
140 | do { |
||
141 | var a = csv.charAt(0); |
||
142 | csv = csv.slice(1, csv.length); |
||
143 | if(a === ",") { |
||
144 | if(s[s.length-1] !== '"') { |
||
145 | l.push(m); |
||
146 | m = ""; |
||
147 | continue; |
||
148 | } |
||
149 | m += a; |
||
150 | continue; |
||
151 | } |
||
152 | if(a === '"' && csv.charAt(0) === a) { |
||
153 | m += a; |
||
154 | csv = csv.slice(1, csv.length); |
||
155 | continue; |
||
156 | } |
||
157 | if(a === '"') { |
||
158 | if(s[s.length-1] !== a) { |
||
159 | s.push(a); |
||
160 | continue; |
||
161 | } |
||
162 | s.pop(); |
||
163 | continue; |
||
164 | } |
||
165 | m += a; |
||
166 | } while(csv !== ""); |
||
167 | |||
168 | l.push(m); |
||
169 | |||
170 | return l; |
||
171 | } |
||
172 | |||
173 | /////////////////////////////////////////////////////////////////////////// |
||
174 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
175 | /////////////////////////////////////////////////////////////////////////// |
||
176 | function wasArrayToCSV(a) { |
||
177 | var csv = []; |
||
178 | for(var i=0; i<a.length; ++i) { |
||
179 | var cell = a[i].toString().replace('"', '""'); |
||
180 | if(/"\s,\r\n/.test(cell)) { |
||
181 | csv[i] = '"' + cell + '"'; |
||
182 | continue; |
||
183 | } |
||
184 | csv[i] = cell; |
||
185 | } |
||
186 | return csv.join(); |
||
187 | } |