Skip to content

Commit

Permalink
feat: add MathPlus.ToNegative
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed May 22, 2023
1 parent b45c9d6 commit 72aca9d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-nails-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'type-plus': minor
---

Add `MathPlus.ToNegative`
1 change: 1 addition & 0 deletions ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type {
Max,
Subtract
} from './math/index.js'
export * as MathPlus from './math/math_plus.js'
export type { IsNever, IsNotNever, Is_Never, NeverType, NotNeverType, Not_Never } from './never/never_type.js'
export * from './nodejs/index.js'
export * from './nominal/index.js'
Expand Down
17 changes: 17 additions & 0 deletions ts/math/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Math

## MathPlus

`MathPlus` contains all types and type utilities related to math.

### [`MathPlus.ToNegative`](./math.to-negative.ts#L12)

> `MathPlus.ToNegative<N>`
Converts a number or bigint `N` to negative.
If `N` is already negative, it returns itself.

```ts
MathPlus.ToNegative<5> // -5
MathPlus.ToNegative<0> // 0
MathPlus.ToNegative<-5> // -5
```

## References

- [Implementing Arithmetic Within TypeScript’s Type System](https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f)
Expand Down
23 changes: 23 additions & 0 deletions ts/math/math_plus.to_negative.spec.ts
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)
})
22 changes: 22 additions & 0 deletions ts/math/math_plus.to_negative.ts
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
1 change: 1 addition & 0 deletions ts/math/math_plus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { ToNegative } from './math_plus.to_negative.js'

0 comments on commit 72aca9d

Please sign in to comment.