-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(number): rename safeDivide to divide
BREAKING CHANGE: safeDivide is removed
- Loading branch information
1 parent
4341c4a
commit afbfb2c
Showing
5 changed files
with
64 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"safe-divide": "safeDivide", | ||
"divide": "divide", | ||
"clamp": "clamp", | ||
"random-int": "randomInt" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# divide | ||
|
||
Divides two numbers returns fallback if result is not a finite number | ||
|
||
### Import | ||
|
||
```typescript copy | ||
import { divide } from '@fullstacksjs/toolbox'; | ||
``` | ||
|
||
### Signature | ||
|
||
```typescript copy | ||
function divide(dividend: number, divisor: number, fallback: number = NaN): number {} | ||
``` | ||
|
||
### Examples | ||
|
||
```typescript copy | ||
divide(4, 2) // 2 | ||
divide(4, 0, 42) // 42 | ||
divide(4, 0) // NaN | ||
``` |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { divide } from './divide'; | ||
|
||
describe('divide', () => { | ||
it('should return the division of two number', () => { | ||
expect(divide(4, 2)).toBe(2); | ||
}); | ||
|
||
it('should return fallback when the divisor is zero', () => { | ||
expect(divide(4, 0, 42)).toBe(42); | ||
}); | ||
|
||
it('the default fallback should be nan', () => { | ||
expect(divide(4, 0)).toBeNaN(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { fallbackNumber } from '../values'; | ||
|
||
/** | ||
* Divides two numbers returns fallback if result is not a finite number | ||
* | ||
* @param {number} dividend dividend | ||
* @param {number} divisor divisor | ||
* @param {number} [fallback=NaN] fallback when the result is not finite | ||
* @returns {number} division result or fallback | ||
* | ||
* @example | ||
* | ||
* divide(4, 2) // 2 | ||
* divide(4, 0, 42) // 42 | ||
* divide(4, 0) // NaN | ||
*/ | ||
export function divide( | ||
dividend: number, | ||
divisor: number, | ||
fallback: number = NaN, | ||
): number { | ||
return fallbackNumber(dividend / divisor, fallback); | ||
} |