Skip to content

Instantly share code, notes, and snippets.

@jackfarrington
Last active March 29, 2016 22:54
Show Gist options
  • Save jackfarrington/8ccda2b737a59d6131eb to your computer and use it in GitHub Desktop.
Save jackfarrington/8ccda2b737a59d6131eb to your computer and use it in GitHub Desktop.
Utilities to convert between various byte capacities
/**
* Represents a capacity in bytes.
* @class
* @param {Number} sizeInBytes - The size in bytes.
* @prop {Number} oneKB - One kilobyte.
* @prop {Number} oneMB - One megabyte.
* @prop {Number} oneGB - One gigabyte.
* @prop {Number} oneTB - One terabyte.
*/
function Capacity(sizeInBytes) {
if (!(this instanceof Capacity)) return new Capacity(sizeInBytes);
if (sizeInBytes === undefined || isNaN(sizeInBytes)) {
throw new Error("Required argument undefined: 'sizeInBytes'");
}
if (sizeInBytes < 0) {
throw new Error("Invalid size specified: 'sizeInBytes'");
}
Object.defineProperty(this, 'sizeInBytes', { value: sizeInBytes });
}
Object.defineProperty(Capacity, 'oneKB', { value: 1024 });
Object.defineProperty(Capacity, 'oneMB', { value: 1024 * 1024 });
Object.defineProperty(Capacity, 'oneGB', { value: 1024 * 1024 * 1024 });
Object.defineProperty(Capacity, 'oneTB', { value: 1024 * 1024 * 1024 * 1024 });
/** @func
* @name toBytes
* @returns {Number} - The size in bytes.
*/
Capacity.prototype.toBytes = function toBytes() {
return this.sizeInBytes;
}
/** @func
* @name toKB
* @returns {Number} - The size in kilobytes.
*/
Capacity.prototype.toKB = function toKB() {
return this.sizeInBytes / Capacity.oneKB;
}
/** @func
* @name toMB
* @returns {Number} - The size in megabytes.
*/
Capacity.prototype.toMB = function toMB() {
return this.sizeInBytes / Capacity.oneMB;
}
/** @func
* @name toGB
* @returns {Number} - The size in gigabytes.
*/
Capacity.prototype.toGB = function toGB() {
return this.sizeInBytes / Capacity.oneGB;
}
/** @func
* @name toTB
* @returns {Number} - The size in terabytes.
*/
Capacity.prototype.toTB = function toTB() {
return this.sizeInBytes / Capacity.oneTB;
}
/** @func toKBString */
Capacity.prototype.toKBString = function toKBString(precision) {
return this.toKB().toFixed(precision) + ' KB';
}
/** @func toMBString */
Capacity.prototype.toMBString = function toMBString(precision) {
return this.toMB().toFixed(precision) + ' MB';
}
/** @func toGBString */
Capacity.prototype.toGBString = function toGBString(precision) {
return this.toGB().toFixed(precision) + ' GB';
}
/** @func toTBString */
Capacity.prototype.toTBString = function toTBString(precision) {
return this.toTB().toFixed(precision) + ' TB';
}
/** @func toNearestByteString */
Capacity.prototype.toNearestByteString = function toNearestByteString(precision) {
if (this.sizeInBytes < Capacity.oneMB) {
return this.toKBString(precision);
}
if (this.sizeInBytes < Capacity.oneGB) {
return this.toMBString(precision);
}
if (this.sizeInBytes < Capacity.oneTB) {
return this.toGBString(precision);
}
return this.toTBString(precision);
}
Capacity.prototype.toString = function capacityToString() {
return this.sizeInBytes.toString();
}
if (module && module.exports) {
module.exports.Capacity = Capacity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment