-
Notifications
You must be signed in to change notification settings - Fork 7
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
Lukinoh
wants to merge
1
commit into
buge:main
Choose a base branch
from
Lukinoh:input-flexibility
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here you have a diff version using the solution based on "isNumber" attach to the arithmetic interface. 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.