type-plus 4.3.0 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -71
- package/cjs/array/CreateTuple.d.ts +5 -4
- package/cjs/array/Some.d.ts +7 -6
- package/cjs/index.d.ts +1 -0
- package/cjs/index.js +1 -0
- package/cjs/index.js.map +1 -1
- package/cjs/math/Abs.d.ts +1 -1
- package/cjs/math/Add.d.ts +14 -10
- package/cjs/math/Digit.d.ts +7 -9
- package/cjs/math/GreaterThan.d.ts +11 -10
- package/cjs/math/Max.d.ts +10 -9
- package/cjs/math/Subtract.d.ts +11 -10
- package/cjs/math/index.d.ts +1 -0
- package/cjs/nodejs/isNodeError.d.ts +1 -2
- package/cjs/nodejs/isNodeError.js.map +1 -1
- package/cjs/object/split.d.ts +1 -1
- package/cjs/testing/index.d.ts +1 -0
- package/cjs/testing/index.js +18 -0
- package/cjs/testing/index.js.map +1 -0
- package/cjs/testing/stub.d.ts +4 -0
- package/cjs/testing/stub.js +8 -0
- package/cjs/testing/stub.js.map +1 -0
- package/cjs/utils/as.d.ts +2 -2
- package/cjs/utils/as.js +2 -2
- package/cjs/utils/as.js.map +1 -1
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/nodejs/isNodeError.js.map +1 -1
- package/esm/testing/index.js +2 -0
- package/esm/testing/index.js.map +1 -0
- package/esm/testing/stub.js +4 -0
- package/esm/testing/stub.js.map +1 -0
- package/esm/utils/as.js +2 -2
- package/esm/utils/as.js.map +1 -1
- package/package.json +8 -9
- package/ts/array/CreateTuple.ts +6 -4
- package/ts/array/Some.ts +26 -24
- package/ts/index.ts +1 -0
- package/ts/math/Abs.ts +4 -4
- package/ts/math/Add.ts +52 -49
- package/ts/math/Digit.ts +115 -115
- package/ts/math/GreaterThan.ts +38 -36
- package/ts/math/Max.ts +32 -31
- package/ts/math/Subtract.ts +43 -40
- package/ts/math/index.ts +1 -0
- package/ts/nodejs/isNodeError.ts +1 -2
- package/ts/object/run.ts +0 -0
- package/ts/object/split.ts +1 -1
- package/ts/testing/index.ts +1 -0
- package/ts/testing/stub.spec.ts +31 -0
- package/ts/testing/stub.ts +8 -0
- package/ts/utils/as.spec.ts +33 -24
- package/ts/utils/as.ts +7 -7
package/ts/math/Subtract.ts
CHANGED
|
@@ -1,40 +1,43 @@
|
|
|
1
|
-
import { PadLeft, Some, Tail } from '../array'
|
|
2
|
-
import { And } from '../predicates'
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
And<IsPositive<
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
PadLeft<
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
: Fail : Fail
|
|
23
|
-
: Fail : Fail
|
|
24
|
-
: Fail : Fail
|
|
25
|
-
: Fail : Fail
|
|
26
|
-
: Fail : Fail
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
1
|
+
import type { PadLeft, Some, Tail } from '../array'
|
|
2
|
+
import type { And } from '../predicates'
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
4
|
+
import type { Digit, DigitArray } from './Digit'
|
|
5
|
+
import type { IsPositive } from './IsPositive'
|
|
6
|
+
import type { IsWhole } from './IsWhole'
|
|
7
|
+
import type { Max } from './Max'
|
|
8
|
+
|
|
9
|
+
export type Subtract<A extends number, B extends number, Fail = never> =
|
|
10
|
+
And<
|
|
11
|
+
And<IsPositive<A>, IsWhole<A>>,
|
|
12
|
+
And<IsPositive<B>, IsWhole<B>>
|
|
13
|
+
> extends true
|
|
14
|
+
? (DigitArray.Shift10<DigitArray.FromNumber<A>> extends infer DA ? DA extends number[] ?
|
|
15
|
+
DigitArray.FromNumber<B> extends infer DB ? DB extends number[] ?
|
|
16
|
+
Max<DA['length'], DB['length']> extends infer M ? M extends number ?
|
|
17
|
+
PadLeft<DA, M, 0> extends infer PDA ? PDA extends number[] ?
|
|
18
|
+
PadLeft<DB, M, 0> extends infer PDB ? PDB extends number[] ?
|
|
19
|
+
Subtract.DigitArray<[], PDA, PDB> extends infer Result ? Result extends number[] ?
|
|
20
|
+
Some<Result, never, 'strict'> extends true ? Fail :
|
|
21
|
+
DigitArray.ToNumber<Result>
|
|
22
|
+
: Fail : Fail
|
|
23
|
+
: Fail : Fail
|
|
24
|
+
: Fail : Fail
|
|
25
|
+
: Fail : Fail
|
|
26
|
+
: Fail : Fail
|
|
27
|
+
: Fail : Fail)
|
|
28
|
+
: Fail
|
|
29
|
+
|
|
30
|
+
export namespace Subtract {
|
|
31
|
+
export type DigitArray<R extends number[], A extends number[], B extends number[]> =
|
|
32
|
+
A['length'] extends 0 ? R :
|
|
33
|
+
DigitArray<[...R, Digit<A[0], B[0]>], Tail<A>, Tail<B>>
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
export type Digit<A extends number, B extends number> =
|
|
37
|
+
Digit.ToTuple[A] extends [...(infer U), ...Digit.ToTuple[B]] ? U['length'] : never
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A - 1
|
|
42
|
+
*/
|
|
43
|
+
export type Decrement<A extends number, Fail = never> = Subtract<A, 1, Fail>
|
package/ts/math/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export type { Abs } from './Abs'
|
|
2
2
|
export type { Add, Increment } from './Add'
|
|
3
|
+
export type { Digit, DigitArray } from './Digit'
|
|
3
4
|
export type { GreaterThan } from './GreaterThan'
|
|
4
5
|
export type { IsPositive } from './IsPositive'
|
|
5
6
|
export type { IsWhole } from './IsWhole'
|
package/ts/nodejs/isNodeError.ts
CHANGED
package/ts/object/run.ts
ADDED
|
File without changes
|
package/ts/object/split.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AnyRecord, Omit, reduceByKey } from '..'
|
|
2
2
|
|
|
3
3
|
type Splitter<T extends AnyRecord> = Partial<{ [k in keyof T]: T[k] | undefined }>
|
|
4
|
-
type Split<T extends AnyRecord, S extends AnyRecord> = { [k in keyof S]: NonNullable<T[k]> | S[k] }
|
|
4
|
+
export type Split<T extends AnyRecord, S extends AnyRecord> = { [k in keyof S]: NonNullable<T[k]> | S[k] }
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Split an object into multiple objects.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './stub'
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { isType, stub } from '..'
|
|
2
|
+
|
|
3
|
+
test('stub is a partial of the actual object', () => {
|
|
4
|
+
const real = { a: 1, b: { c: 2 }, d: 3 }
|
|
5
|
+
|
|
6
|
+
const a = stub<typeof real>({ a: 2 })
|
|
7
|
+
expect(a.a).toBe(2)
|
|
8
|
+
isType.equal<true, typeof a, typeof real>()
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
test('stub function retains param types', () => {
|
|
12
|
+
function real(a: string, b: number): boolean { return a === String(b) }
|
|
13
|
+
|
|
14
|
+
const a = stub<typeof real>((_a, _b) => {
|
|
15
|
+
isType.equal<true, string, typeof _a>()
|
|
16
|
+
isType.equal<true, number, typeof _b>()
|
|
17
|
+
return false
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
expect(a('1', 1)).toBe(false)
|
|
21
|
+
isType.equal<true, typeof a, typeof real>()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('stub function params can be omitted', () => {
|
|
25
|
+
function real(a: string, b: number): boolean { return a === String(b) }
|
|
26
|
+
|
|
27
|
+
const a = stub<typeof real>(() => false)
|
|
28
|
+
|
|
29
|
+
expect(a('1', 1)).toBe(false)
|
|
30
|
+
isType.equal<true, typeof a, typeof real>()
|
|
31
|
+
})
|
package/ts/utils/as.spec.ts
CHANGED
|
@@ -1,24 +1,33 @@
|
|
|
1
|
-
import { as, asAny, isType } from '..'
|
|
2
|
-
|
|
3
|
-
describe('as<T>()', () => {
|
|
4
|
-
test('defaults subject type to unknown', () => {
|
|
5
|
-
const s: any = {}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
})
|
|
24
|
-
|
|
1
|
+
import { as, asAny, isType } from '..'
|
|
2
|
+
|
|
3
|
+
describe('as<T>()', () => {
|
|
4
|
+
test('defaults subject type to unknown', () => {
|
|
5
|
+
const s: any = {}
|
|
6
|
+
const a = as(s)
|
|
7
|
+
isType.equal<true, unknown, typeof a>()
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
test('cast type to T', () => {
|
|
11
|
+
const s: any = {}
|
|
12
|
+
const n = as<number>(s)
|
|
13
|
+
isType.equal<true, number, typeof n>()
|
|
14
|
+
|
|
15
|
+
const str = as<string>(s)
|
|
16
|
+
isType.equal<true, string, typeof str>()
|
|
17
|
+
|
|
18
|
+
const o = as<{ a: number }>(s)
|
|
19
|
+
isType.equal<true, { a: number }, typeof o>()
|
|
20
|
+
|
|
21
|
+
const any = as<any>(s)
|
|
22
|
+
isType.equal<true, any, typeof any>()
|
|
23
|
+
})
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
describe('asAny()', () => {
|
|
27
|
+
test('cast type to any', () => {
|
|
28
|
+
const s: unknown = {}
|
|
29
|
+
const a = asAny(s)
|
|
30
|
+
isType.equal<true, any, typeof a>()
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
|
package/ts/utils/as.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export function as<T>(subject: unknown):
|
|
2
|
-
return
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export function asAny(subject: unknown):
|
|
6
|
-
return
|
|
7
|
-
}
|
|
1
|
+
export function as<T>(subject: unknown): T {
|
|
2
|
+
return subject as T
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function asAny(subject: unknown): any {
|
|
6
|
+
return subject
|
|
7
|
+
}
|