Skip to content

Commit

Permalink
feat: udpate IsBigint IsNotBigint
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Sep 27, 2023
1 parent 215ca11 commit de546a2
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 198 deletions.
57 changes: 0 additions & 57 deletions type-plus/ts/bigint/bigint_type.spec.ts

This file was deleted.

21 changes: 0 additions & 21 deletions type-plus/ts/bigint/bigint_type.ts

This file was deleted.

42 changes: 32 additions & 10 deletions type-plus/ts/bigint/is_bigint.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { it } from '@jest/globals'
import { testType, type IsBigint } from '../index.js'
import { testType, type IsBigint, type $Then, type $Else } from '../index.js'

it('returns true for bigint', () => {
testType.true<IsBigint<bigint>>(true)
Expand Down Expand Up @@ -35,20 +35,42 @@ it('returns false for other types', () => {
testType.false<IsBigint<() => void>>(true)
})

it('returns false for union type', () => {
testType.false<IsBigint<bigint | 1>>(true)
it('distributes for union type', () => {
testType.equal<IsBigint<bigint | 1>, boolean>(true)
testType.equal<IsBigint<1n | 1>, boolean>(true)
})

it('returns true for interaction type', () => {
testType.true<IsBigint<bigint & { a: 1 }>>(true)
testType.true<IsBigint<1n & { a: 1 }>>(true)
})

it('can override Then/Else', () => {
testType.equal<IsBigint<bigint, 1, 2>, 1>(true)
testType.equal<IsBigint<0n, 1, 2>, 1>(true)
it('works as filter', () => {
testType.equal<IsBigint<1n, { selection: 'filter' }>, 1n>(true)

testType.equal<IsBigint<any, 1, 2>, 2>(true)
testType.equal<IsBigint<unknown, 1, 2>, 2>(true)
testType.equal<IsBigint<never, 1, 2>, 2>(true)
testType.equal<IsBigint<void, 1, 2>, 2>(true)
testType.equal<IsBigint<never, { selection: 'filter' }>, never>(true)
testType.equal<IsBigint<unknown, { selection: 'filter' }>, never>(true)
testType.equal<IsBigint<string | boolean, { selection: 'filter' }>, never>(true)

testType.equal<IsBigint<string | 1n, { selection: 'filter' }>, 1n>(true)

testType.equal<IsBigint<string | boolean, { selection: 'filter-unknown' }>, unknown>(true)
testType.equal<IsBigint<string | 1n, { selection: 'filter-unknown' }>, unknown>(true)
})

it('can disable union distribution', () => {
testType.equal<IsBigint<1n | 1>, boolean>(true)
testType.equal<IsBigint<1n | 1, { distributive: false }>, false>(true)
})

it('works with unique branches', () => {
testType.equal<IsBigint<bigint, IsBigint.$Branch>, $Then>(true)
testType.equal<IsBigint<1n, IsBigint.$Branch>, $Then>(true)

testType.equal<IsBigint<any, IsBigint.$Branch>, $Else>(true)
testType.equal<IsBigint<unknown, IsBigint.$Branch>, $Else>(true)
testType.equal<IsBigint<never, IsBigint.$Branch>, $Else>(true)
testType.equal<IsBigint<void, IsBigint.$Branch>, $Else>(true)

testType.equal<IsBigint<1n | 1, IsBigint.$Branch>, $Then | $Else>(true)
})
72 changes: 64 additions & 8 deletions type-plus/ts/bigint/is_bigint.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,71 @@
import type { BigintType } from './bigint_type.js'
import type { SelectWithDistribute } from '../type_plus/branch/select_with_distribute.js'

/**
* Is the type `T` exactly `bigint`.
* 🎭 *predicate*
*
* Validate if `T` is `bigint` or `bigint` literals.
*
* @example
* ```ts
* type R = IsBigint<bigint> // true
* type R = isBigint<bigint> // true
* type R = isBigint<1n> // true
*
* type R = isBigint<never> // false
* type R = isBigint<unknown> // false
* type R = isBigint<string | boolean> // false
*
* type R = IsBigint<1n> // false
* type R = IsBigint<number> // false
* type R = IsBigint<bigint | boolean> // false
* type R = IsBigint<unknown> // false
* type R = isBigint<string | bigint> // boolean
* ```
*
* 🔢 *customize*
*
* Filter to ensure `T` is `bigint` or `bigint` literals, otherwise returns `never`.
*
* @example
* ```ts
* type R = isBigint<bigint, { selection: 'filter' }> // bigint
* type R = isBigint<1n, { selection: 'filter' }> // bigint
*
* type R = isBigint<never, { selection: 'filter' }> // never
* type R = isBigint<unknown, { selection: 'filter' }> // never
* type R = isBigint<string | boolean, { selection: 'filter' }> // never
*
* type R = isBigint<string | bigint> // bigint
* ```
*
* 🔢 *customize*
*
* Filter to ensure `T` is `bigint` or `bigint` literals, otherwise returns `unknown`.
*
* @example
* ```ts
* type R = isBigint<string | boolean, { selection: 'filter-unknown' }> // unknown
* type R = isBigint<string | bigint, { selection: 'filter-unknown' }> // unknown
* ```
*
* 🔢 *customize*:
*
* Disable distribution of union types.
*
* ```ts
* type R = isBigint<bigint | 1> // boolean
* type R = isBigint<bigint | 1, { distributive: false }> // false
* ```
*
* 🔢 *customize*
*
* Use unique branch identifiers to allow precise processing of the result.
*
* @example
* ```ts
* type R = isBigint<bigint, $SelectionBranch> // $Then
* type R = isBigint<string, $SelectionBranch> // $Else
* ```
*/
export type IsBigint<T, Then = true, Else = false> = BigintType<T, Then, Else>
export type IsBigint<T, $O extends IsBigint.$Options = {}> = SelectWithDistribute<T, bigint, $O>

export namespace IsBigint {
export type $Options = SelectWithDistribute.$Options
export type $Default = SelectWithDistribute.$Default
export type $Branch = SelectWithDistribute.$Branch
}
40 changes: 30 additions & 10 deletions type-plus/ts/bigint/is_not_bigint.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { it } from '@jest/globals'
import { testType, type IsNotBigint } from '../index.js'
import { testType, type $Else, type $Then, type IsNotBigint } from '../index.js'

it('returns false for bigint', () => {
testType.false<IsNotBigint<bigint>>(true)
Expand Down Expand Up @@ -35,20 +35,40 @@ it('returns true for other types', () => {
testType.true<IsNotBigint<() => void>>(true)
})

it('returns true for union type', () => {
testType.true<IsNotBigint<bigint | 1>>(true)
it('distributes over union type', () => {
testType.equal<IsNotBigint<bigint | 1>, boolean>(true)
})

it('can disable union distribution', () => {
testType.equal<IsNotBigint<1n | 1>, boolean>(true)
testType.equal<IsNotBigint<1n | 1, { distributive: false }>, true>(true)
})

it('returns false for interaction type', () => {
testType.false<IsNotBigint<bigint & { a: 1 }>>(true)
})

it('can override Then/Else', () => {
testType.equal<IsNotBigint<bigint, 1, 2>, 2>(true)
testType.equal<IsNotBigint<0n, 1, 2>, 2>(true)
it('works as filter', () => {
testType.equal<IsNotBigint<1n, { selection: 'filter' }>, never>(true)

testType.equal<IsNotBigint<never, { selection: 'filter' }>, never>(true)
testType.equal<IsNotBigint<unknown, { selection: 'filter' }>, unknown>(true)
testType.equal<IsNotBigint<string | boolean, { selection: 'filter' }>, string | boolean>(true)

testType.equal<IsNotBigint<string | 1n, { selection: 'filter' }>, string>(true)

testType.equal<IsNotBigint<string | boolean, { selection: 'filter-unknown' }>, string | boolean>(true)
testType.equal<IsNotBigint<string | 1n, { selection: 'filter-unknown' }>, unknown>(true)
})

it('works with unique branches', () => {
testType.equal<IsNotBigint<bigint, IsNotBigint.$Branch>, $Else>(true)
testType.equal<IsNotBigint<1n, IsNotBigint.$Branch>, $Else>(true)

testType.equal<IsNotBigint<any, IsNotBigint.$Branch>, $Then>(true)
testType.equal<IsNotBigint<unknown, IsNotBigint.$Branch>, $Then>(true)
testType.equal<IsNotBigint<never, IsNotBigint.$Branch>, $Then>(true)
testType.equal<IsNotBigint<void, IsNotBigint.$Branch>, $Then>(true)

testType.equal<IsNotBigint<any, 1, 2>, 1>(true)
testType.equal<IsNotBigint<unknown, 1, 2>, 1>(true)
testType.equal<IsNotBigint<never, 1, 2>, 1>(true)
testType.equal<IsNotBigint<void, 1, 2>, 1>(true)
testType.equal<IsNotBigint<1n | 1, IsNotBigint.$Branch>, $Then | $Else>(true)
})
68 changes: 62 additions & 6 deletions type-plus/ts/bigint/is_not_bigint.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,71 @@
import type { BigintType } from './bigint_type.js'
import type { SelectInvertWithDistribute } from '../type_plus/branch/select_invert_with_distribute.js'

/**
* Is the type `T` not exactly `null`.
* 🎭 *predicate*
*
* Validate if `T` is not `bigint` nor `bigint` literals.
*
* @example
* ```ts
* type R = IsNotBigint<bigint> // false
* type R = IsNotBigint<1n> // false
*
* type R = IsNotBigint<1n> // true
* type R = IsNotBigint<number> // true
* type R = IsNotBigint<bigint | boolean> // true
* type R = IsNotBigint<never> // true
* type R = IsNotBigint<unknown> // true
* type R = IsNotBigint<string | boolean> // true
* ```
*
* 🔢 *customize*
*
* Filter to ensure `T` is not `bigint` nor `bigint` literals, otherwise returns `never`.
*
* @example
* ```ts
* type R = IsNotBigint<bigint, { selection: 'filter' }> // never
* type R = IsNotBigint<1n, { selection: 'filter' }> // never
*
* type R = IsNotBigint<never, { selection: 'filter' }> // never
* type R = IsNotBigint<unknown, { selection: 'filter' }> // unknown
* type R = IsNotBigint<string | boolean, { selection: 'filter' }> // string | boolean
* ```
*
* 🔢 *customize*
*
* Filter to ensure `T` is not `bigint` nor `bigint` literals, otherwise returns `unknown`.
*
* @example
* ```ts
* type R = IsNotBigint<bigint, { selection: 'filter-unknown' }> // unknown
* type R = IsNotBigint<1n, { selection: 'filter-unknown' }> // unknown
*
* type R = IsNotBigint<string | boolean, { selection: 'filter-unknown' }> // string | boolean
* type R = IsNotBigint<string | bigint, { selection: 'filter-unknown' }> // unknown
* ```
*
* 🔢 *customize*
*
* Disable distribution of union types.
*
* @example
* ```ts
* type R = IsNotBigint<bigint | 1> // boolean
* type R = IsNotBigint<bigint | 1, { distributive: false }> // true
* ```
*
* 🔢 *customize*
*
* Use unique branch identifiers to allow precise processing of the result.
*
* @example
* ```ts
* type R = IsNotBigint<string, $SelectionBranch> // $Then
* type R = IsNotBigint<bigint, $SelectionBranch> // $Else
* ```
*/
export type IsNotBigint<T, Then = true, Else = false> = BigintType<T, Else, Then>
export type IsNotBigint<T, $O extends IsNotBigint.$Options = {}> = SelectInvertWithDistribute<T, bigint, $O>

export namespace IsNotBigint {
export type $Options = SelectInvertWithDistribute.$Options
export type $Default = SelectInvertWithDistribute.$Default
export type $Branch = SelectInvertWithDistribute.$Branch
}
Loading

0 comments on commit de546a2

Please sign in to comment.