Skip to content

Commit

Permalink
feat(number): rename safeDivide to divide
Browse files Browse the repository at this point in the history
BREAKING CHANGE: safeDivide is removed
  • Loading branch information
ASafaeirad committed Jun 28, 2023
1 parent 4341c4a commit afbfb2c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
3 changes: 2 additions & 1 deletion docs/pages/number/_meta.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"safe-divide": "safeDivide",
"divide": "divide",
"clamp": "clamp",
"random-int": "randomInt"
}
23 changes: 23 additions & 0 deletions docs/pages/number/divide.mdx
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
```
3 changes: 0 additions & 3 deletions docs/pages/number/safe-divide.mdx

This file was deleted.

16 changes: 16 additions & 0 deletions src/number/divide.spec.ts
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();
});
});
23 changes: 23 additions & 0 deletions src/number/divide.ts
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);
}

0 comments on commit afbfb2c

Please sign in to comment.