You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/utils.js

58 lines
1.6 KiB
JavaScript

5 years ago
const map = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8',
5 years ago
'9', '0',
5 years ago
];
5 years ago
String.random = function random(digit) {
let str = '';
5 years ago
for (let i = 1; i <= digit; i++) str += map[Math.floor(Math.random() * 62)];
return str;
};
5 years ago
/**
* @param {Array} a
* @param {Array} b
*/
Array.isDiff = function isDiff(a, b) {
5 years ago
if (a.length !== b.length) return true;
5 years ago
a.sort();
b.sort();
5 years ago
for (const i in a) { if (a[i] !== b[i]) return true; }
5 years ago
return false;
};
5 years ago
Date.prototype.format = function formatDate(fmt = '%Y-%m-%d %H:%M:%S') {
return fmt
.replace('%Y', this.getFullYear())
.replace('%m', this.getMonth() + 1)
.replace('%D', this.getDate())
.replace('%d', this.getDate())
.replace('%H', this.getHours())
.replace('%M', this.getMinutes())
.replace('%S', this.getSeconds());
};
Set.isSuperset = function isSuperset(set, subset) {
for (const elem of subset) {
if (!set.has(elem)) return false;
}
return true;
};
Set.union = function union(setA, setB) {
const _union = new Set(setA);
for (const elem of setB) _union.add(elem);
return _union;
};
Set.intersection = function intersection(setA, setB) {
const _intersection = new Set();
for (const elem of setB) {
if (setA.has(elem)) _intersection.add(elem);
}
return _intersection;
};