filesize.d.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Type definitions for filesize 8.0.3
  2. // Project: https://github.com/avoidwork/filesize.js, https://filesizejs.com
  3. // Definitions by: Giedrius Grabauskas <https://github.com/GiedriusGrabauskas>
  4. // Renaud Chaput <https://github.com/renchap>
  5. // Roman Nuritdinov <https://github.com/Ky6uk>
  6. // Sam Hulick <https://github.com/ffxsam>
  7. // Tomoto S. Washio <https://github.com/tomoto>
  8. declare var fileSize: Filesize.Filesize;
  9. export = fileSize;
  10. export as namespace filesize;
  11. declare namespace Filesize {
  12. interface SiJedecBits {
  13. b?: string;
  14. Kb?: string;
  15. Mb?: string;
  16. Gb?: string;
  17. Tb?: string;
  18. Pb?: string;
  19. Eb?: string;
  20. Zb?: string;
  21. Yb?: string;
  22. }
  23. interface SiJedecBytes {
  24. B?: string;
  25. KB?: string;
  26. MB?: string;
  27. GB?: string;
  28. TB?: string;
  29. PB?: string;
  30. EB?: string;
  31. ZB?: string;
  32. YB?: string;
  33. }
  34. type SiJedec = SiJedecBits & SiJedecBytes & { [name: string]: string };
  35. interface Options {
  36. /**
  37. * Number base, default is 10
  38. */
  39. base?: number;
  40. /**
  41. * Enables bit sizes, default is false
  42. */
  43. bits?: boolean;
  44. /**
  45. * Specifies the SI suffix via exponent, e.g. 2 is MB for bytes, default is -1
  46. */
  47. exponent?: number;
  48. /**
  49. * Enables full form of unit of measure, default is false
  50. */
  51. fullform?: boolean;
  52. /**
  53. * Array of full form overrides, default is []
  54. */
  55. fullforms?: string[];
  56. /**
  57. * BCP 47 language tag to specify a locale, or true to use default locale, default is ""
  58. */
  59. locale?: string | boolean;
  60. /**
  61. * ECMA-402 number format option overrides, default is "{}"
  62. */
  63. localeOptions?: Intl.NumberFormatOptions;
  64. /**
  65. * Output of function (array, exponent, object, or string), default is string
  66. */
  67. output?: "array" | "exponent" | "object" | "string";
  68. /**
  69. * Decimal place end padding, default is false
  70. */
  71. pad?: boolean;
  72. /**
  73. * Sets precision of numerical output, default is 0
  74. */
  75. precision?: number;
  76. /**
  77. * Decimal place, default is 2
  78. */
  79. round?: number;
  80. /**
  81. * Rounding method, can be round, floor, or ceil, default is round
  82. */
  83. roundingMethod?: "round" | "floor" | "ceil";
  84. /**
  85. * Decimal separator character, default is `.`
  86. */
  87. separator?: string;
  88. /**
  89. * Character between the result and suffix, default is ` `
  90. */
  91. spacer?: string;
  92. /**
  93. * Standard unit of measure, can be iec or jedec, default is iec; can be overruled by base
  94. */
  95. standard?: "iec" | "jedec";
  96. /**
  97. * Dictionary of SI/JEDEC symbols to replace for localization, defaults to english if no match is found
  98. */
  99. symbols?: SiJedec;
  100. /**
  101. * Enables unix style human readable output, e.g ls -lh, default is false
  102. */
  103. unix?: boolean;
  104. }
  105. // Result type inference from the output option
  106. interface ResultTypeMap {
  107. array: [number, string];
  108. exponent: number;
  109. object: {
  110. value: number,
  111. symbol: string,
  112. exponent: number,
  113. unit: string,
  114. };
  115. string: string;
  116. }
  117. type DefaultOutput<O extends Options> = Exclude<O["output"], keyof ResultTypeMap> extends never ? never : "string"
  118. type CanonicalOutput<O extends Options> = Extract<O["output"], keyof ResultTypeMap> | DefaultOutput<O>
  119. interface Filesize {
  120. (bytes: number): string;
  121. <O extends Options>(bytes: number, options: O): ResultTypeMap[CanonicalOutput<O>];
  122. partial: <O extends Options>(options: O) => ((bytes: number) => ResultTypeMap[CanonicalOutput<O>]);
  123. }
  124. }