Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of NumberType and string as input for unit and quantity functions #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Lukinoh
Copy link
Contributor

@Lukinoh Lukinoh commented Apr 24, 2023

Now we can do meters('5') or meters(5) or meters(new Decimal(5)) if DecimalArithmetic was implemented.

Concerning the function isUnit and isQuantity:
As we are doing composition using ``withValueType` for some units, we cannot do "instanceof UnitImpl" or "instanceof QuantityImpl". Because, they will not have the same reference.

As a workaround, I decided to use Arithmetic object. I am not sure it is the best solution, but it is working.

Another idea would be to create a random class G outside of makeUnitFactory, and then make UnitImpl extends it.
I didn't dig to much in that solution.

Another idea would be to instead check for "isUnit" or "isQuantity" instead verify if the input is "number | string | NumberType".
We would need to add a new function to the arithmetic "isNumber(string | number | NumberType): boolean".
It should be doable, but the "isNumber" function and "from" function would be a bit trickier to write I think.

Let me know about your opinion.

@Lukinoh
Copy link
Contributor Author

Lukinoh commented Apr 24, 2023

Here you have a diff version using the solution based on "isNumber" attach to the arithmetic interface.
I think the biggest difference is that in this case, the user is responsible for the implementation, whereas with the solution in the pull request, it is the library that is doing the check with the information it has.

diff --git a/src/arithmetic.ts b/src/arithmetic.ts
index d0a610d..6bf6a17 100644
--- a/src/arithmetic.ts
+++ b/src/arithmetic.ts
@@ -20,6 +20,9 @@ export interface Arithmetic<NumberType> {
    * @param value The value to convert to NumberType
    */
   from(value: number | string | NumberType): NumberType;
+  isNumber<NotANumber>(
+    value: number | string | NumberType | NotANumber
+  ): value is number | string | NumberType;
   toNative(value: NumberType): number;
   add(left: NumberType, right: NumberType): NumberType;
   sub(left: NumberType, right: NumberType): NumberType;
@@ -44,8 +47,22 @@ export const NativeArithmetic: Arithmetic<number> = {
       !isNaN(convertedValue),
       `Input '${value}' cannot be converted to a number`
     );
+
     return convertedValue;
   },
+  isNumber: function (value): value is number | string {
+    if (typeof value === 'number') {
+      return true;
+    }
+
+    if (typeof value === 'string') {
+      // Source: https://stackoverflow.com/questions/12643009/regular-expression-for-floating-point-numbers
+      const isNumber = /^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$/;
+      return isNumber.test(value);
+    }
+
+    return false;
+  },
   toNative: function (value: number): number {
     return value;
   },
diff --git a/src/unit.ts b/src/unit.ts
index 3867a8e..724b3b6 100644
--- a/src/unit.ts
+++ b/src/unit.ts
@@ -434,20 +434,7 @@ const DEFAULT_LOCALE = 'en-us';
 export const makeUnitFactory = <NumberType>(
   arithmetic: Arithmetic<NumberType>
 ) => {
-  const {from, toNative, add, sub, mul, div, pow, abs, compare} = arithmetic;
-
-  function isUnit<NumberType, D extends Dimensions>(
-    val: string | number | NumberType | Unit<NumberType, D>
-  ): val is Unit<NumberType, D> {
-    const castedVal = val as Unit<NumberType, D>;
-    return castedVal.arithmetic && Object.is(castedVal.arithmetic, arithmetic);
-  }
-  function isQuantity<NumberType, D extends Dimensions>(
-    val: string | number | NumberType | Quantity<NumberType, D>
-  ): val is Quantity<NumberType, D> {
-    const castedVal = val as Quantity<NumberType, D>;
-    return castedVal.unit && isUnit(castedVal.unit);
-  }
+  const {from, isNumber, toNative, add, sub, mul, div, pow, abs, compare} = arithmetic;
 
   /**
    * Creates a new unit.
@@ -554,7 +541,7 @@ export const makeUnitFactory = <NumberType>(
     times<D2 extends Multiplicand<D>>(
       amountOrUnit: string | number | NumberType | Unit<NumberType, D2>
     ): Unit<NumberType, D> | Unit<NumberType, Times<D, D2>> {
-      if (!isUnit(amountOrUnit)) {
+      if (isNumber(amountOrUnit)) {
         return makeUnit(
           this.symbol,
           this.dimension,
@@ -587,7 +574,7 @@ export const makeUnitFactory = <NumberType>(
     per<D2 extends Divisor<D>>(
       amountOrUnit: string | number | NumberType | Unit<NumberType, D2>
     ): Unit<NumberType, D> | Unit<NumberType, Over<D, D2>> {
-      if (!isUnit(amountOrUnit)) {
+      if (isNumber(amountOrUnit)) {
         return makeUnit(
           this.symbol,
           this.dimension,
@@ -716,7 +703,7 @@ export const makeUnitFactory = <NumberType>(
     plus(quantity: NumberType): Quantity<NumberType, D>;
     plus(quantity: Quantity<NumberType, D>): Quantity<NumberType, D>;
     plus(quantity: string | number | NumberType | Quantity<NumberType, D>) {
-      if (!isQuantity(quantity)) {
+      if (isNumber(quantity)) {
         return new QuantityImpl(add(this.amount, from(quantity)), this.unit);
       }
 
@@ -731,7 +718,7 @@ export const makeUnitFactory = <NumberType>(
     minus(quantity: NumberType): Quantity<NumberType, D>;
     minus(quantity: Quantity<NumberType, D>): Quantity<NumberType, D>;
     minus(quantity: string | number | NumberType | Quantity<NumberType, D>) {
-      if (!isQuantity(quantity)) {
+      if (isNumber(quantity)) {
         return new QuantityImpl(sub(this.amount, from(quantity)), this.unit);
       }
 
@@ -751,7 +738,7 @@ export const makeUnitFactory = <NumberType>(
       this: Quantity<NumberType, D>,
       other: string | number | NumberType | Quantity<NumberType, D2>
     ): Quantity<NumberType, D> | Quantity<NumberType, Times<D, D2>> {
-      if (!isQuantity(other)) {
+      if (isNumber(other)) {
         return new QuantityImpl(mul(this.amount, from(other)), this.unit);
       }
 
@@ -784,7 +771,7 @@ export const makeUnitFactory = <NumberType>(
     per<D2 extends Divisor<D>>(
       other: string | number | NumberType | Quantity<NumberType, D2>
     ): Quantity<NumberType, D> | Quantity<NumberType, Over<D, D2>> {
-      if (!isQuantity(other)) {
+      if (isNumber(other)) {
         return new QuantityImpl(div(this.amount, from(other)), this.unit);
       }
 
diff --git a/test/unit-factory_test.ts b/test/unit-factory_test.ts
index 7111a5d..e983d3b 100644
--- a/test/unit-factory_test.ts
+++ b/test/unit-factory_test.ts
@@ -17,6 +17,23 @@ export const CustomArithmetic: Arithmetic<CustomNumber> = {
     }
     return new CustomNumber(NativeArithmetic.from(value));
   },
+  isNumber: function (value): value is number | string | CustomNumber {
+    if (typeof value === 'number') {
+      return true;
+    }
+
+    if (typeof value === 'string') {
+      // Source: https://stackoverflow.com/questions/12643009/regular-expression-for-floating-point-numbers
+      const isNumber = /^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$/;
+      return isNumber.test(value);
+    }
+
+    if (value instanceof CustomNumber) {
+      return true;
+    }
+
+    return false;
+  },
   toNative: function (value: CustomNumber): number {
     return value.value;
   },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant