-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
69 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'type-plus': minor | ||
--- | ||
|
||
Add `MathPlus.ToNegative` |
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
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
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 { it } from '@jest/globals' | ||
import { MathPlus, testType } from '../index.js' | ||
|
||
it('converts positive bigint to negative', () => { | ||
testType.equal<MathPlus.ToNegative<1n>, -1n>(true) | ||
}) | ||
|
||
it('converts positive number to negative', () => { | ||
testType.equal<MathPlus.ToNegative<1>, -1>(true) | ||
}) | ||
|
||
it('converts 0 to 0', () => { | ||
testType.equal<MathPlus.ToNegative<0>, 0>(true) | ||
testType.equal<MathPlus.ToNegative<0n>, 0n>(true) | ||
|
||
testType.equal<MathPlus.ToNegative<-0>, 0>(true) | ||
testType.equal<MathPlus.ToNegative<-0n>, 0n>(true) | ||
}) | ||
|
||
it('returns N if N is negative', () => { | ||
testType.equal<MathPlus.ToNegative<-1>, -1>(true) | ||
testType.equal<MathPlus.ToNegative<-1n>, -1n>(true) | ||
}) |
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,22 @@ | ||
/** | ||
* Converts a number or bigint `N` to negative. | ||
* If `N` is already negative, it returns itself. | ||
* | ||
* @example | ||
* ```ts | ||
* ToNegative<5> // -5 | ||
* ToNegative<0> // 0 | ||
* ToNegative<-5> // -5 | ||
* ``` | ||
*/ | ||
export type ToNegative<N extends number | bigint> = N extends number | ||
? N extends 0 | ||
? 0 | ||
: `-${N}` extends `${infer W extends number}` | ||
? W | ||
: N | ||
: N extends 0n | ||
? 0n | ||
: `-${N}` extends `${infer W extends bigint}` | ||
? W | ||
: N |
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 @@ | ||
export type { ToNegative } from './math_plus.to_negative.js' |