diff --git a/docs/java.md b/docs/java.md index c85d8b0acd..531d0c75de 100644 --- a/docs/java.md +++ b/docs/java.md @@ -2330,7 +2330,7 @@ If true, this reference points to the managing controller. ### SizeConversionOptions -Options for how to convert time to a different unit. +Options for how to convert size to a different unit. #### Initializer @@ -4039,6 +4039,7 @@ When the amount is passed as a token, unit conversion is not possible. | **Name** | **Description** | | --- | --- | +| asString | Returns amount with abbreviated storage unit. | | toGibibytes | Return this storage as a total number of gibibytes. | | toKibibytes | Return this storage as a total number of kibibytes. | | toMebibytes | Return this storage as a total number of mebibytes. | @@ -4047,6 +4048,14 @@ When the amount is passed as a token, unit conversion is not possible. --- +##### `asString` + +```java +public java.lang.String asString() +``` + +Returns amount with abbreviated storage unit. + ##### `toGibibytes` ```java diff --git a/docs/python.md b/docs/python.md index f001797782..66e80146ee 100644 --- a/docs/python.md +++ b/docs/python.md @@ -2366,7 +2366,7 @@ If true, this reference points to the managing controller. ### SizeConversionOptions -Options for how to convert time to a different unit. +Options for how to convert size to a different unit. #### Initializer @@ -4334,6 +4334,7 @@ When the amount is passed as a token, unit conversion is not possible. | **Name** | **Description** | | --- | --- | +| as_string | Returns amount with abbreviated storage unit. | | to_gibibytes | Return this storage as a total number of gibibytes. | | to_kibibytes | Return this storage as a total number of kibibytes. | | to_mebibytes | Return this storage as a total number of mebibytes. | @@ -4342,6 +4343,14 @@ When the amount is passed as a token, unit conversion is not possible. --- +##### `as_string` + +```python +def as_string() -> str +``` + +Returns amount with abbreviated storage unit. + ##### `to_gibibytes` ```python diff --git a/docs/typescript.md b/docs/typescript.md index dfc75b226d..36aad1f26e 100644 --- a/docs/typescript.md +++ b/docs/typescript.md @@ -2035,7 +2035,7 @@ If true, this reference points to the managing controller. ### SizeConversionOptions -Options for how to convert time to a different unit. +Options for how to convert size to a different unit. #### Initializer @@ -3555,6 +3555,7 @@ When the amount is passed as a token, unit conversion is not possible. | **Name** | **Description** | | --- | --- | +| asString | Returns amount with abbreviated storage unit. | | toGibibytes | Return this storage as a total number of gibibytes. | | toKibibytes | Return this storage as a total number of kibibytes. | | toMebibytes | Return this storage as a total number of mebibytes. | @@ -3563,6 +3564,14 @@ When the amount is passed as a token, unit conversion is not possible. --- +##### `asString` + +```typescript +public asString(): string +``` + +Returns amount with abbreviated storage unit. + ##### `toGibibytes` ```typescript diff --git a/src/size.ts b/src/size.ts index 3fdd060b17..6608855508 100644 --- a/src/size.ts +++ b/src/size.ts @@ -58,6 +58,13 @@ export class Size { this.unit = unit; } + /** + * Returns amount with abbreviated storage unit + */ + public asString(): string { + return `${this.amount}${this.unit.abbr}`; + } + /** * Return this storage as a total number of kibibytes. */ @@ -104,11 +111,10 @@ export enum SizeRoundingBehavior { FLOOR, /** Don't round. Return even if the result is a fraction. */ NONE, - } /** - * Options for how to convert time to a different unit. + * Options for how to convert size to a different unit. */ export interface SizeConversionOptions { /** @@ -119,13 +125,13 @@ export interface SizeConversionOptions { } class StorageUnit { - public static readonly Kibibytes = new StorageUnit('kibibytes', 1); - public static readonly Mebibytes = new StorageUnit('mebibytes', 1024); - public static readonly Gibibytes = new StorageUnit('gibibytes', 1024 * 1024); - public static readonly Tebibytes = new StorageUnit('tebibytes', 1024 * 1024 * 1024); - public static readonly Pebibytes = new StorageUnit('pebibytes', 1024 * 1024 * 1024 * 1024); + public static readonly Kibibytes = new StorageUnit('kibibytes', 1, 'Ki'); + public static readonly Mebibytes = new StorageUnit('mebibytes', 1024, 'Mi'); + public static readonly Gibibytes = new StorageUnit('gibibytes', 1024 * 1024, 'Gi'); + public static readonly Tebibytes = new StorageUnit('tebibytes', 1024 * 1024 * 1024, 'Ti'); + public static readonly Pebibytes = new StorageUnit('pebibytes', 1024 * 1024 * 1024 * 1024, 'Pi'); - private constructor(public readonly label: string, public readonly inKibiBytes: number) { + private constructor(public readonly label: string, public readonly inKibiBytes: number, public readonly abbr: string) { // MAX_SAFE_INTEGER is 2^53, so by representing storage in kibibytes, // the highest storage we can represent is 8 exbibytes. } @@ -137,7 +143,9 @@ class StorageUnit { function convert(amount: number, fromUnit: StorageUnit, toUnit: StorageUnit, options: SizeConversionOptions = {}) { const rounding = options.rounding ?? SizeRoundingBehavior.FAIL; - if (fromUnit.inKibiBytes === toUnit.inKibiBytes) { return amount; } + if (fromUnit.inKibiBytes === toUnit.inKibiBytes) { + return amount; + } const multiplier = fromUnit.inKibiBytes / toUnit.inKibiBytes; const value = amount * multiplier; @@ -153,4 +161,4 @@ function convert(amount: number, fromUnit: StorageUnit, toUnit: StorageUnit, opt } return value; } -} \ No newline at end of file +} diff --git a/test/size.test.ts b/test/size.test.ts index 9d5368ea94..84273b8733 100644 --- a/test/size.test.ts +++ b/test/size.test.ts @@ -82,3 +82,10 @@ test('rounding behavior', () => { expect(size.toKibibytes({ rounding: SizeRoundingBehavior.NONE })).toEqual(5_324_800); }); +test('asString function gives abbreviated units', () => { + expect(Size.kibibytes(10).asString()).toEqual('10Ki'); + expect(Size.mebibytes(10).asString()).toEqual('10Mi'); + expect(Size.gibibytes(10).asString()).toEqual('10Gi'); + expect(Size.tebibytes(10).asString()).toEqual('10Ti'); + expect(Size.pebibyte(10).asString()).toEqual('10Pi'); +});