Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions docs/reference/file-size.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
8 changes: 8 additions & 0 deletions src/value-objects/file-size/FileSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/value-objects/FileSize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading