TypeScript

by Terrance Smith

HTML

<div id="app">

</div>

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
  text-align: center;
}

TypeScript

function convertToLargestUnitOfSize(bytes: number) {
  const kiloByteSize = 1024;
  let result = '';
  if (bytes < kiloByteSize) {
    result = bytes.toString() + ' B';
  } else if ((bytes >= kiloByteSize) && (bytes < Math.pow(kiloByteSize, 2))) {
    result = (bytes / kiloByteSize).toString() + ' KB';
  } else if ((bytes >= Math.pow(kiloByteSize, 2)) && (bytes < Math.pow(kiloByteSize, 3))) {
    result = (bytes / Math.pow(kiloByteSize, 2)).toString() + ' MB';
  } else if ((bytes >= Math.pow(kiloByteSize, 3)) && (bytes < Math.pow(kiloByteSize, 4))) {
    result = (bytes / Math.pow(kiloByteSize, 3)).toString() + ' GB';
  } else {
    result = (bytes / Math.pow(kiloByteSize, 4)).toString() + ' TB';
  }
  return result;
}

function transform(value: any){
  if (!value|| isNaN(value) || (typeof value !== 'number' && typeof value !== 'string')) {
    return value;
  }
  return convertToLargestUnitOfSize(Number(value));
}


document.querySelector("#app").innerHTML += "Ello" + "<br />";
document.querySelector("#app").innerHTML += transform("Ello") + "<br />";
document.querySelector("#app").innerHTML += transform("1") + "<br />";
document.querySelector("#app").innerHTML += transform("-1") + "<br />";
document.querySelector("#app").innerHTML += transform("00001") + "<br />";
document.querySelector("#app").innerHTML += transform("Ello") + "<br />";
//document.querySelector("#app").innerHTML += pipe.tranform("1");
//document.querySelector("#app").innerHTML += pipe.tranform("0");