all files / lib/ math.js

100% Statements 17/17
100% Branches 6/6
100% Functions 6/6
100% Lines 17/17
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   504×     15624×   15624×         507×         14×         11× 11× 10×          
import crypto from 'crypto';
export class Math {
  constructor() {}
  uuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
        .replace(/[xy]/g,
                 (c) => {
                   let r = global.Math.random() * 16 | 0,
                       v = c == 'x' ? r : (r & 0x3 | 0x8);
                   return v.toString(16);
                 })
        .toUpperCase();
  }
  checkUuid(uuid) {
    return uuid.search(
               /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i) !==
           -1
  }
  random(min, max) {
    return global.Math.floor(min + (global.Math.random() * (max + 1 - min)));
    // return min + offset;
  }
  uniqueRandomArray(amount, from, to) {
    if (amount > to - from) {
      return [];
    }
    let found = [];
    while (found.length < amount) {
      let r = this.random(from, to);
      if (found.indexOf(r) === -1) {
        found.push(r);
      }
    }
    return found;
  }
}
exports.default = Math;