forked from K-Sakanoshita/community_mapmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basiclib.js
executable file
·95 lines (85 loc) · 3.6 KB
/
basiclib.js
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
// Basic Closure
class Basic {
getDate() { // Overpass Queryに付ける日付指定
let seldate = $("#Select_Date").val();
return seldate ? '[date:"' + (new Date(seldate)).toISOString() + '"]' : "";
}
formatDate(date, format) {
// date format
try {
format = format.replace(/YYYY/g, date.getFullYear());
format = format.replace(/YY/g, date.getFullYear().toString().slice(-2));
format = format.replace(/MM/g, ('0' + (date.getMonth() + 1)).slice(-2));
format = format.replace(/DD/g, ('0' + date.getDate()).slice(-2));
format = format.replace(/hh/g, ('0' + date.getHours()).slice(-2));
format = format.replace(/mm/g, ('0' + date.getMinutes()).slice(-2));
format = format.replace(/ss/g, ('0' + date.getSeconds()).slice(-2));
} catch {
format = "";
};
return format;
}
dataURItoBlob(dataURI) { // DataURIからBlobへ変換(ファイルサイズ2MB超過対応)
const b64 = atob(dataURI.split(',')[1]);
const u8 = Uint8Array.from(b64.split(""), function (e) { return e.charCodeAt() });
return new Blob([u8], { type: "image/png" });
}
concatTwoDimensionalArray(array1, array2, axis) { // 2次元配列の合成
if (axis != 1) axis = 0;
var array3 = [];
if (axis == 0) { // 縦方向の結合
array3 = array1.slice();
for (var i = 0; i < array2.length; i++) {
array3.push(array2[i]);
}
} else { // 横方向の結合
for (var i = 0; i < array1.length; i++) {
array3[i] = array1[i].concat(array2[i]);
};
};
return array3;
}
unicodeUnescape(str) { // \uxxx形式→文字列変換
let result = "", strs = str.match(/\\u.{4}/ig);
if (!strs) return '';
for (var i = 0, len = strs.length; i < len; i++) {
result += String.fromCharCode(strs[i].replace('\\u', '0x'));
};
return result;
}
uniq(array) {
let elems = new Map();
for (let elem of array) {
elems.set(elem, true); // 同じキーに何度も値を設定しても問題ない
};
return Array.from(elems.keys());
}
getWikipedia(lang, url) { // get wikipedia contents
return new Promise((resolve, reject) => {
let encurl = encodeURI(url);
encurl = "https://" + lang + "." + Conf.osm.wikipedia.api + encurl;
$.get({ url: encurl, dataType: "jsonp" }, function (data) {
let key = Object.keys(data.query.pages);
let text = data.query.pages[key].extract;
console.log(text);
resolve(text);
});
});
}
isSmartPhone() {
if (window.matchMedia && window.matchMedia('(max-device-width: 640px)').matches) {
return true;
} else {
return false;
};
}
convLinkTag(url) {
return (/^(ftp|http|https):\/\/[^ "]+$/.test(url)) ? `<a href="${url}" target="_blank" rel="noopener noreferrer">${url}</a>` : "";
}
async makeSHA256(text) {
const uint8 = new TextEncoder().encode(text);
const digest = await crypto.subtle.digest('SHA-256', uint8);
return Array.from(new Uint8Array(digest)).map(v => v.toString(16).padStart(2, '0')).join('');
}
};
var basic = new Basic();