All files / utils functions.js

24% Statements 6/25
9.09% Branches 1/11
50% Functions 4/8
24% Lines 6/25
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          3x 3x                                                     18x   1x       1x               1x                        
/* @flow */
 
import type { DownloadData } from "../api/types";
 
export function isEmail(email: string): boolean {
  var re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(\	".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return re.test(email);
}
 
export function fileext(filename: string): string {
  return filename.split(".").pop();
}
 
export function formatSize(sizebytes: number, prec: number): string {
  if (prec === undefined) {
    prec = 1;
  }
 
  sizebytes = parseInt(sizebytes, 10);
 
  if (sizebytes >= 1099511627776) {
    return (sizebytes / 1099511627776).toFixed(prec) + " Tb";
  } else if (sizebytes >= 1073741824) {
    return (sizebytes / 1073741824).toFixed(prec) + " Gb";
  } else if (sizebytes >= 1048576) {
    return (sizebytes / 1048576).toFixed(prec) + " Mb";
  } else if (sizebytes >= 1024) {
    return (sizebytes / 1024).toFixed(prec) + " Kb";
  } else {
    return sizebytes.toFixed(prec) + " B";
  }
}
 
let start = 0;
export function uniqueNumber(): number {
  return ++start;
}
 
export function randomNumber(chars: number = 10) {
  return Math.random() * (10 << chars);
}
 
export function methodStringify(method: string, params: Object): string {
  return JSON.stringify({ method: method, params: params });
}
 
export function pCloudUrl(data: DownloadData) {
  return "https://" + data.hosts[0] + data.path;
}
 
export function generateRandomString(length: number) {
  const strArr = [];
  const base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
 
  for (let i = 0; i < length; i++) {
    strArr.push(base[Math.floor(Math.random() * 100)]);
  }
 
  return strArr.join("");
}