diff --git a/docs/reference/file-size.md b/docs/reference/file-size.md index 1b77c4f..5e8dd12 100644 --- a/docs/reference/file-size.md +++ b/docs/reference/file-size.md @@ -31,16 +31,16 @@ Accepts integers greater than or equal to `0`. ## Methods -| Method | Description | -| --- | --- | -| `static fromBytes(bytes)` | Creates a size from bytes. | -| `static from(value, unit)` | Creates a size from a value in a `FileSizeUnit`, rounded to the nearest byte. | -| `static parse(text)` | Parses text such as `1.5 MiB` or `2MB`. | -| `toBytes()` | Returns the bytes as `PositiveInteger`. | -| `to(unit)` | Returns the size in `unit` as `NumberValueObject`, without rounding. | -| `toHumanReadable(units)` | Returns `{ value, unit }` using the largest unit of `units` that fits. | -| `add(other)` | Returns the sum as `FileSize`. | -| `subtract(other)` | Returns the difference as `FileSize`. | +| Method | Description | +| --------------------------- | ---------------------------------------------------------------------------------------------------- | +| `static fromBytes(bytes)` | Creates a size from bytes. | +| `static from(value, unit)` | Creates a size from a value in a `FileSizeUnit`, rounded to the nearest byte. | +| `static parse(text)` | Parses text such as `1.5 MiB` or `2MB`. | +| `toBytes()` | Returns the bytes as `PositiveInteger`. | +| `to(unit)` | Returns the size in `unit` as `NumberValueObject`, without rounding. | +| `toHumanReadable(units)` | Returns `{ value, unit }` using the largest unit of `units` that fits. | +| `add(other: FileSize)` | Returns the sum as `FileSize`. | +| `subtract(other: FileSize)` | Returns the difference as `FileSize`. Throws `InvalidPositiveNumberError` if the result is negative. | Comparison and the rest of the arithmetic are inherited from `NumberValueObject`. diff --git a/src/value-objects/file-size/FileSize.ts b/src/value-objects/file-size/FileSize.ts index 17f1247..4e8a246 100644 --- a/src/value-objects/file-size/FileSize.ts +++ b/src/value-objects/file-size/FileSize.ts @@ -41,6 +41,14 @@ export class FileSize extends PositiveInteger { return new this(Math.round(Number(value) * unit.getBytes().valueOf())); } + public add(other: FileSize): FileSize { + return this.clone(this.value + other.valueOf()); + } + + public subtract(other: FileSize): FileSize { + return this.clone(this.value - other.valueOf()); + } + public toBytes(): PositiveInteger { return new PositiveInteger(this.value); } diff --git a/tests/value-objects/FileSize.spec.ts b/tests/value-objects/FileSize.spec.ts index 52563a7..cf6ad02 100644 --- a/tests/value-objects/FileSize.spec.ts +++ b/tests/value-objects/FileSize.spec.ts @@ -60,6 +60,10 @@ describe('FileSize', () => { const sum = new FileSize(1000).add(new FileSize(24)); const diff = new FileSize(1000).subtract(new FileSize(400)); + // Compile-time check: only compiles if both return FileSize + expect(sum.to(FileSizeUnit.KIBIBYTE).valueOf()).toBeCloseTo(1); + expect(diff.to(FileSizeUnit.BYTE).valueOf()).toBe(600); + expect(sum).toBeInstanceOf(FileSize); expect(sum.valueOf()).toBe(1024); expect(diff).toBeInstanceOf(FileSize);