Skip to content

Commit

Permalink
Update Unit.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvianen committed Jan 12, 2025
1 parent eff8e96 commit 5465b45
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/ts/Unit.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
// ============================================================================
// Import
// ============================================================================


// ============================================================================
// Types
// ============================================================================

type UnitType = 'px' | 'em' | '%' | 'rem' | 'mm' | 'cm' | 'in';

class Unit {

// ============================================================================
// Classes
// ============================================================================

export default class Unit {

// Parameters
// ========================================================================

value: number;
unit: UnitType;

// Constructor
// ========================================================================

constructor(value: number, unit: UnitType) {
this.value = value;
this.unit = unit;
}

// Methods
// ========================================================================

add(other: Unit): Unit {
if (this.unit !== other.unit) {
throw new Error(`Cannot add units of different types: ${this.unit} and ${other.unit}`);
Expand Down Expand Up @@ -54,4 +78,4 @@ class Unit {
toString(): string {
return `${this.value}${this.unit}`;
}
}
}

0 comments on commit 5465b45

Please sign in to comment.