All files / utils functions.js

30% Statements 6/20
9.09% Branches 1/11
57.14% Functions 4/7
30% Lines 6/20
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            1x 1x                                                     15x   1x       1x                 1x    
/* @flow */
 
import type { DownloadData } from "../api/types";
 
export function isEmail(email: string): boolean {
  /*eslint no-useless-escape: "off"*/
  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;
}