Skip to content

Commit

Permalink
Implement Decimal128 for web
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Jun 6, 2024
1 parent e84d2bd commit c1145f5
Showing 1 changed file with 50 additions and 7 deletions.
57 changes: 50 additions & 7 deletions packages/realm_dart/lib/src/handles/web/decimal128.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Copyright 2024 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0

import 'package:decimal/decimal.dart';
import 'package:realm_dart/src/handles/native/convert.dart';
import 'package:realm_dart/src/handles/web/web_not_supported.dart';

import '../decimal128.dart' as intf;
import 'web_not_supported.dart';

class Decimal128 implements intf.Decimal128 {
static final zero = Decimal128.fromInt(0);
Expand All @@ -14,7 +17,7 @@ class Decimal128 implements intf.Decimal128 {
static final ten = Decimal128.fromInt(10);

/// The value NaN.
static get nan => throw UnimplementedError('web not supported');
static get nan => webNotSupported();

/// The value +Inf.
static final infinity = one / zero; // +Inf
Expand All @@ -23,17 +26,57 @@ class Decimal128 implements intf.Decimal128 {
static final negativeInfinity = -infinity;

/// Parses a string into a [Decimal128]. Returns `null` if the string is not a valid [Decimal128].
static Decimal128? tryParse(String source) => throw UnimplementedError('web not supported');
static Decimal128? tryParse(String source) => Decimal.tryParse(source).convert(Decimal128._);

/// Parses a string into a [Decimal128]. Throws a [FormatException] if the string is not a valid [Decimal128].
factory Decimal128.parse(String source) => throw UnimplementedError('web not supported');
factory Decimal128.parse(String source) => Decimal128._(Decimal.parse(source));

/// Converts a `int` into a [Decimal128].
factory Decimal128.fromInt(int value) => throw UnimplementedError('web not supported');
factory Decimal128.fromInt(int value) => Decimal128._(Decimal.fromInt(value));

/// Converts a `double` into a [Decimal128].
factory Decimal128.fromDouble(double value) => throw UnimplementedError('web not supported');
factory Decimal128.fromDouble(double value) => webNotSupported();

final Decimal _value;
Decimal128._(this._value); // TODO: truncate to 128 bits and handle NaN correctly

@override
Decimal128 operator *(covariant Decimal128 other) => Decimal128._(_value * other._value);

@override
Decimal128 operator +(covariant Decimal128 other) => Decimal128._(_value + other._value);

@override
Decimal128 operator -() => Decimal128._(-_value);

@override
Decimal128 operator -(covariant Decimal128 other) => Decimal128._(_value - other._value);

@override
Decimal128 operator /(covariant Decimal128 other) => Decimal128._((_value / other._value).toDecimal());

@override
bool operator <(covariant Decimal128 other) => _value < other._value;

@override
bool operator <=(covariant Decimal128 other) => _value <= other._value;

@override
bool operator >(covariant Decimal128 other) => _value > other._value;

@override
bool operator >=(covariant Decimal128 other) => _value >= other._value;

@override
Decimal128 abs() => Decimal128._(_value.abs());

@override
int compareTo(covariant Decimal128 other) => _value.compareTo(other._value);

@override
// TODO: implement isNaN
bool get isNaN => webNotSupported();

@override
noSuchMethod(Invocation invocation) => webNotSupported();
int toInt() => _value.toBigInt().toInt();
}

0 comments on commit c1145f5

Please sign in to comment.