Zorbing

ByteConverter.ts

May 23rd, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // for Typescript 1.7+
  2. const si = false;
  3. const byteUnitSize = si ? 1000 : 1024;
  4. const decimalPoint = ',';
  5. const thousandsSeperator = '.';
  6. const filesizeRegex = /([\d\.,]+)\s*([KMGTPE]?i?B(?:ytes)?)\b/i;
  7. const typeFactor = {
  8.        'B': byteUnitSize ** 0
  9.     , 'KB': byteUnitSize ** 1
  10.     , 'MB': byteUnitSize ** 2
  11.     , 'GB': byteUnitSize ** 3
  12.     , 'TB': byteUnitSize ** 4
  13.     , 'PB': byteUnitSize ** 5
  14.     , 'EB': byteUnitSize ** 6
  15. };
  16. const byteUnits = si ? 'kMGTPE' : 'KMGTPE';
  17.  
  18.  
  19. export class ByteConverter
  20. {
  21.     /**
  22.      * Returns the size in bytes represented by the given string.
  23.      *
  24.      * @param  {!string} str
  25.      *                   The size as a string
  26.      * @return {number}  The size in bytes
  27.      */
  28.     public static fromString(str: string): number
  29.     {
  30.         const match = str.match(filesizeRegex);
  31.         if (!match)
  32.         {
  33.             return null;
  34.         }
  35.  
  36.         let size = parseFloat(match[1].replace(/,/g, ''));
  37.         const sizeType = match[2].replace(/i|ytes/g, '').toUpperCase();
  38.         size *= typeFactor[sizeType];
  39.         // always round to the next higher integer
  40.         size = Math.ceil(size);
  41.         return size;
  42.     }
  43.  
  44.     /**
  45.      * Returns the given size in bytes as a readable string.
  46.      *
  47.      * @param  {number} bytes
  48.      *                  The size in bytes
  49.      * @param  {number=0} padp
  50.      *                    How many decimal places should be displayed (padp = position after decimal point)
  51.      * @return {string}  The size as a readable string
  52.      */
  53.     public static toString(bytes: number, padp = 0): string
  54.     {
  55.         let exp = 0;
  56.         let type = 'B';
  57.         const sign = bytes >= 0 ? '' : '-';
  58.         bytes = Math.abs(bytes);
  59.         if (bytes >= byteUnitSize)
  60.         {
  61.             exp = Math.min(Math.floor(Math.log(bytes) / Math.log(byteUnitSize)), byteUnits.length);
  62.             type = byteUnits.charAt(exp-1) + (si ? '' : 'i') + 'B';
  63.         }
  64.         let num = bytes / (byteUnitSize ** exp);
  65.         if (exp > 0)
  66.         {
  67.             const roundFactor = (10 ** padp);
  68.             num = Math.round(num * roundFactor) / roundFactor;
  69.         }
  70.  
  71.         // transform the decimal point into a comma
  72.         let numStr = num.toString(10).replace('.', decimalPoint);
  73.         // insert a thousand seperator if needed
  74.         if (num >= 1000)
  75.         {
  76.             const parts = numStr.split(decimalPoint);
  77.             parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeperator);
  78.             numStr = parts.join(decimalPoint);
  79.         }
  80.         return sign + numStr + ' ' + type;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment