forked from pietervdvn/MapComplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.ts
205 lines (174 loc) · 6.55 KB
/
Utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import {UIElement} from "./UI/UIElement";
import * as $ from "jquery"
import {FixedUiElement} from "./UI/Base/FixedUiElement";
export class Utils {
static EncodeXmlValue(str) {
return str.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
}
/**
* Gives a clean float, or undefined if parsing fails
* @param str
*/
static asFloat(str): number {
if (str) {
const i = parseFloat(str);
if (isNaN(i)) {
return undefined;
}
return i;
}
return undefined;
}
public static Upper(str: string) {
return str.substr(0, 1).toUpperCase() + str.substr(1);
}
public static TwoDigits(i: number) {
if (i < 10) {
return "0" + i;
}
return "" + i;
}
public static Times(f: ((i: number) => string), count: number): string {
let res = "";
for (let i = 0; i < count; i++) {
res += f(i);
}
return res;
}
static DoEvery(millis: number, f: (() => void)) {
if (UIElement.runningFromConsole) {
return;
}
window.setTimeout(
function () {
f();
Utils.DoEvery(millis, f);
}
, millis)
}
public static NoNull<T>(array: T[]): T[] {
const ls: T[] = [];
for (const t of array) {
if (t === undefined || t === null) {
continue;
}
ls.push(t);
}
return ls;
}
public static NoEmpty(array: string[]): string[]{
const ls: string[] = [];
for (const t of array) {
if (t === "") {
continue;
}
ls.push(t);
}
return ls;
}
public static EllipsesAfter(str : string, l : number = 100){
if(str === undefined){
return undefined;
}
if(str.length <= l){
return str;
}
return str.substr(0, l - 3)+"...";
}
public static Dedup(arr: string[]):string[]{
if(arr === undefined){
return undefined;
}
const newArr = [];
for (const string of arr) {
if (newArr.indexOf(string) < 0) {
newArr.push(string);
}
}
return newArr;
}
public static MergeTags(a: any, b: any) {
const t = {};
for (const k in a) {
t[k] = a[k];
}
for (const k in b) {
t[k] = b[k];
}
return t;
}
public static SplitFirst(a: string, sep: string): string[] {
const index = a.indexOf(sep);
if (index < 0) {
return [a];
}
return [a.substr(0, index), a.substr(index + sep.length)];
}
public static isRetina(): boolean {
if (UIElement.runningFromConsole) {
return;
}
// The cause for this line of code: https://github.com/pietervdvn/MapComplete/issues/115
// See https://stackoverflow.com/questions/19689715/what-is-the-best-way-to-detect-retina-support-on-a-device-using-javascript
return ((window.matchMedia && (window.matchMedia('only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx), only screen and (min-resolution: 75.6dpcm)').matches || window.matchMedia('only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (min--moz-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)').matches)) || (window.devicePixelRatio && window.devicePixelRatio >= 2));
}
// Date will be undefined on failure
public static changesetDate(id: number, action: ((isFound: Date) => void)): void {
$.getJSON("https://www.openstreetmap.org/api/0.6/changeset/" + id,
function (data) {
console.log(data)
action(new Date(data.elements[0].created_at));
})
.fail(() => {
action(undefined);
});
}
public static generateStats(action:(stats:string) => void) {
// Binary searches the latest changeset
function search(lowerBound: number,
upperBound: number,
onCsFound: ((id: number, lastDate: Date) => void),
depth = 0) {
if (depth > 30) {
return;
}
const tested = Math.floor((lowerBound + upperBound) / 2);
console.log("Testing", tested)
Utils.changesetDate(tested, (createdAtDate: Date) => {
new FixedUiElement(`Searching, value between ${lowerBound} and ${upperBound}. Queries till now: ${depth}`).AttachTo('maindiv')
if (lowerBound + 1 >= upperBound) {
onCsFound(lowerBound, createdAtDate);
return;
}
if (createdAtDate !== undefined) {
search(tested, upperBound, onCsFound, depth + 1)
} else {
search(lowerBound, tested, onCsFound, depth + 1);
}
})
}
search(91000000, 100000000, (last, lastDate: Date) => {
const link = "http://osm.org/changeset/" + last;
const delta = 100000;
Utils.changesetDate(last - delta, (prevDate) => {
const diff = (lastDate.getTime() - prevDate.getTime()) / 1000;
// Diff: seconds needed/delta changesets
const secsPerCS = diff / delta;
const stillNeeded = 1000000 - (last % 1000000);
const timeNeededSeconds = Math.floor(secsPerCS * stillNeeded);
const secNeeded = timeNeededSeconds % 60;
const minNeeded = Math.floor(timeNeededSeconds / 60) % 60;
const hourNeeded = Math.floor(timeNeededSeconds / (60 * 60)) % 24;
const daysNeeded = Math.floor(timeNeededSeconds / (24 * 60 * 60));
const result = `Last changeset: <a href='${link}'>${link}</a><br/>We needed ${(Math.floor(diff / 60))} minutes for the last ${delta} changesets.<br/>
This is around ${secsPerCS} seconds/changeset.<br/> The next million (still ${stillNeeded} away) will be broken in around ${daysNeeded} days ${hourNeeded}:${minNeeded}:${secNeeded}`
action(result);
})
}
);
}
}