Files: fbad0279dd4439c8eb8da96d385e27acd5755c74 / lib / math.js
986 bytesRaw
1 | import crypto from 'crypto'; |
2 | export class Math { |
3 | constructor() {} |
4 | uuid() { |
5 | return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' |
6 | .replace(/[xy]/g, |
7 | (c) => { |
8 | let r = global.Math.random() * 16 | 0, |
9 | v = c == 'x' ? r : (r & 0x3 | 0x8); |
10 | return v.toString(16); |
11 | }) |
12 | .toUpperCase(); |
13 | } |
14 | checkUuid(uuid) { |
15 | return uuid.search( |
16 | /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i) !== |
17 | -1 |
18 | } |
19 | random(min, max) { |
20 | return global.Math.floor(min + (global.Math.random() * (max + 1 - min))); |
21 | // return min + offset; |
22 | } |
23 | uniqueRandomArray(amount, from, to) { |
24 | if (amount > to - from) { |
25 | return []; |
26 | } |
27 | let found = []; |
28 | while (found.length < amount) { |
29 | let r = this.random(from, to); |
30 | if (found.indexOf(r) === -1) { |
31 | found.push(r); |
32 | } |
33 | } |
34 | return found; |
35 | } |
36 | } |
37 | exports.default = Math; |
38 |
Built with git-ssb-web