-
Notifications
You must be signed in to change notification settings - Fork 1
Allow the + operator to work with more types. Throw error on incomptible types #18
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
Changes from all commits
900ea72
059a01d
c32b3ab
cedeafd
4584092
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,18 +8,45 @@ export function add(a: any, b: any): any { | |
| if (a === undefined || b === undefined) { | ||
| return undefined; | ||
| } | ||
| // If both values are strings and at least one of them in a non-number | ||
|
|
||
| // If both values are numbers then we want to add the numbers. | ||
| if (typeof a === 'number' && typeof b === 'number') { | ||
| return a + b; | ||
| } | ||
|
|
||
| // If both values are strings and at least one of them is a non-number | ||
| // then we want to concatenate the strings. | ||
| if (typeof a === 'string' && typeof b === 'string') { | ||
| const numA = Number(a); | ||
| const numB = Number(b); | ||
|
|
||
| if (isNaN(numA) || isNaN(numB)) { | ||
| return a + b; | ||
| return `${a}${b}`; | ||
| } | ||
| } | ||
|
|
||
| // Add the numeric values. | ||
| return Number(a) + Number(b); | ||
| // If both values are arrays then we want to concatenate the arrays. | ||
| if (Array.isArray(a) && Array.isArray(b)) { | ||
| return a.concat(b); | ||
| } | ||
|
|
||
| // If both values are objects then we want to merge the objects. | ||
| if ( | ||
| typeof a === 'object' && | ||
| typeof b === 'object' && | ||
| !Array.isArray(a) && | ||
| !Array.isArray(b) | ||
| ) { | ||
| return { ...a, ...b }; | ||
| } | ||
|
Comment on lines
+28
to
+41
|
||
|
|
||
| // If both values can be converted to numbers then we want to add the numbers. | ||
| if (!isNaN(Number(a)) && !isNaN(Number(b))) { | ||
| return Number(a) + Number(b); | ||
| } | ||
|
|
||
| // Otherwise return an error indicating that the values of mixed types cannot be added. | ||
| throw new Error(`Cannot add values of incompatible types: ${typeof a} and ${typeof b}`); | ||
| } | ||
|
|
||
| export function sub(a: number | undefined, b: number | undefined): number | undefined { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic may not handle null values correctly. When both a and b are null, typeof null === 'object' is true, so this condition will be met and return an object spread of null values, which results in an empty object {}. This behavior may be unexpected. Consider adding null checks before the object merge logic.