type-plus 5.2.0 → 5.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.
Files changed (117) hide show
  1. package/README.md +18 -4
  2. package/cjs/array/PadLeft.spec.js.map +1 -1
  3. package/cjs/array/UnionOfValues.d.ts +1 -1
  4. package/cjs/array/UnionOfValues.d.ts.map +1 -1
  5. package/cjs/array/UnionOfValues.spec.js +10 -0
  6. package/cjs/array/UnionOfValues.spec.js.map +1 -1
  7. package/cjs/array/index.d.ts +3 -3
  8. package/cjs/array/index.d.ts.map +1 -1
  9. package/cjs/array/index.js.map +1 -1
  10. package/cjs/assertion/assertType.spec.js +226 -131
  11. package/cjs/assertion/assertType.spec.js.map +1 -1
  12. package/cjs/function/ExtractFunction.d.ts +8 -2
  13. package/cjs/function/ExtractFunction.d.ts.map +1 -1
  14. package/cjs/function/ExtractFunction.js +5 -0
  15. package/cjs/function/ExtractFunction.js.map +1 -1
  16. package/cjs/function/ExtractFunction.spec.js +18 -6
  17. package/cjs/function/ExtractFunction.spec.js.map +1 -1
  18. package/cjs/functional/compose.d.ts +5 -1
  19. package/cjs/functional/compose.d.ts.map +1 -1
  20. package/cjs/functional/compose.js +5 -1
  21. package/cjs/functional/compose.js.map +1 -1
  22. package/cjs/functional/context.d.ts +36 -0
  23. package/cjs/functional/context.d.ts.map +1 -0
  24. package/cjs/functional/context.js +43 -0
  25. package/cjs/functional/context.js.map +1 -0
  26. package/cjs/functional/context.spec.d.ts +2 -0
  27. package/cjs/functional/context.spec.d.ts.map +1 -0
  28. package/cjs/functional/context.spec.js +39 -0
  29. package/cjs/functional/context.spec.js.map +1 -0
  30. package/cjs/functional/index.d.ts +1 -0
  31. package/cjs/functional/index.d.ts.map +1 -1
  32. package/cjs/functional/index.js +1 -0
  33. package/cjs/functional/index.js.map +1 -1
  34. package/cjs/predicates/CanAssign.spec.js +4 -3
  35. package/cjs/predicates/CanAssign.spec.js.map +1 -1
  36. package/cjs/predicates/Equal.d.ts +14 -18
  37. package/cjs/predicates/Equal.d.ts.map +1 -1
  38. package/cjs/predicates/Equal.js +0 -8
  39. package/cjs/predicates/Equal.js.map +1 -1
  40. package/cjs/predicates/Equal.spec.js +18 -0
  41. package/cjs/predicates/Equal.spec.js.map +1 -1
  42. package/cjs/predicates/IsAny.d.ts +3 -0
  43. package/cjs/predicates/IsAny.d.ts.map +1 -0
  44. package/cjs/predicates/IsAny.js +3 -0
  45. package/cjs/predicates/IsAny.js.map +1 -0
  46. package/cjs/predicates/index.d.ts +2 -3
  47. package/cjs/predicates/index.d.ts.map +1 -1
  48. package/cjs/predicates/index.js.map +1 -1
  49. package/cjs/predicates/isType.js +1 -1
  50. package/cjs/predicates/isType.spec.js +16 -12
  51. package/cjs/predicates/isType.spec.js.map +1 -1
  52. package/cjs/predicates/logical.d.ts +4 -4
  53. package/cjs/predicates/logical.d.ts.map +1 -1
  54. package/esm/array/PadLeft.spec.js.map +1 -1
  55. package/esm/array/UnionOfValues.d.ts +1 -1
  56. package/esm/array/UnionOfValues.d.ts.map +1 -1
  57. package/esm/array/UnionOfValues.spec.js +11 -1
  58. package/esm/array/UnionOfValues.spec.js.map +1 -1
  59. package/esm/array/index.d.ts +3 -3
  60. package/esm/array/index.d.ts.map +1 -1
  61. package/esm/array/index.js.map +1 -1
  62. package/esm/assertion/assertType.spec.js +226 -131
  63. package/esm/assertion/assertType.spec.js.map +1 -1
  64. package/esm/function/ExtractFunction.d.ts +8 -2
  65. package/esm/function/ExtractFunction.d.ts.map +1 -1
  66. package/esm/function/ExtractFunction.js +5 -0
  67. package/esm/function/ExtractFunction.js.map +1 -1
  68. package/esm/function/ExtractFunction.spec.js +18 -6
  69. package/esm/function/ExtractFunction.spec.js.map +1 -1
  70. package/esm/functional/compose.d.ts +5 -1
  71. package/esm/functional/compose.d.ts.map +1 -1
  72. package/esm/functional/compose.js +5 -1
  73. package/esm/functional/compose.js.map +1 -1
  74. package/esm/functional/context.d.ts +36 -0
  75. package/esm/functional/context.d.ts.map +1 -0
  76. package/esm/functional/context.js +28 -0
  77. package/esm/functional/context.js.map +1 -0
  78. package/esm/functional/context.spec.d.ts +2 -0
  79. package/esm/functional/context.spec.d.ts.map +1 -0
  80. package/esm/functional/context.spec.js +37 -0
  81. package/esm/functional/context.spec.js.map +1 -0
  82. package/esm/functional/index.d.ts +1 -0
  83. package/esm/functional/index.d.ts.map +1 -1
  84. package/esm/functional/index.js +1 -0
  85. package/esm/functional/index.js.map +1 -1
  86. package/esm/predicates/CanAssign.spec.js +4 -3
  87. package/esm/predicates/CanAssign.spec.js.map +1 -1
  88. package/esm/predicates/Equal.d.ts +14 -18
  89. package/esm/predicates/Equal.d.ts.map +1 -1
  90. package/esm/predicates/Equal.js +0 -8
  91. package/esm/predicates/Equal.js.map +1 -1
  92. package/esm/predicates/Equal.spec.js +18 -0
  93. package/esm/predicates/Equal.spec.js.map +1 -1
  94. package/esm/predicates/IsAny.d.ts +3 -0
  95. package/esm/predicates/IsAny.d.ts.map +1 -0
  96. package/esm/predicates/IsAny.js +2 -0
  97. package/esm/predicates/IsAny.js.map +1 -0
  98. package/esm/predicates/index.d.ts +2 -3
  99. package/esm/predicates/index.d.ts.map +1 -1
  100. package/esm/predicates/index.js.map +1 -1
  101. package/esm/predicates/isType.js +1 -1
  102. package/esm/predicates/isType.spec.js +16 -12
  103. package/esm/predicates/isType.spec.js.map +1 -1
  104. package/esm/predicates/logical.d.ts +4 -4
  105. package/esm/predicates/logical.d.ts.map +1 -1
  106. package/package.json +2 -2
  107. package/ts/array/UnionOfValues.ts +14 -10
  108. package/ts/array/index.ts +3 -4
  109. package/ts/function/ExtractFunction.ts +8 -2
  110. package/ts/functional/compose.ts +5 -1
  111. package/ts/functional/context.ts +69 -0
  112. package/ts/functional/index.ts +1 -0
  113. package/ts/predicates/Equal.ts +19 -23
  114. package/ts/predicates/IsAny.ts +3 -0
  115. package/ts/predicates/index.ts +2 -5
  116. package/ts/predicates/isType.ts +1 -1
  117. package/ts/predicates/logical.ts +26 -4
@@ -2,7 +2,11 @@ import type { Head, Last } from '../array/index.js'
2
2
  import type { AnyFunction } from '../function/AnyFunction.js'
3
3
 
4
4
  /**
5
- * compose functions
5
+ * Compose functions to produce a new function.
6
+ * @params args functions to be composed.
7
+ * Each function will receive the return value of the previous function as its parameters.
8
+ * @return The composed function will expect the parameters of the first function,
9
+ * and return the result of the last function.
6
10
  */
7
11
  export function compose<FS extends AnyFunction[]>(...fns: FS): (...args: Parameters<Head<FS>>) => ReturnType<Last<FS>> {
8
12
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
@@ -0,0 +1,69 @@
1
+ import type { LeftJoin } from '../object/index.js'
2
+
3
+ export type ContextBaseShape = Record<string | symbol, any>
4
+
5
+ /**
6
+ * Extends the context with new props.
7
+ * @param context the current context.
8
+ * @return an additional context with new properties.
9
+ */
10
+ export type ContextExtender<
11
+ Current,
12
+ Additional
13
+ > = (context: Current) => Additional
14
+
15
+ export type ContextBuilder<
16
+ Init extends ContextBaseShape,
17
+ Ctx extends ContextBaseShape
18
+ > = {
19
+ /**
20
+ * Extends the context using an extender.
21
+ * @param extender function that add new props to the context.
22
+ *
23
+ * The extender only need to return a new object with new properties.
24
+ *
25
+ * Do not need to merge the existing context with the new props.
26
+ * The builder will do that for you.
27
+ */
28
+ extend<
29
+ Current extends ContextBaseShape = Ctx,
30
+ Additional extends ContextBaseShape = ContextBaseShape
31
+ >(extender: ContextExtender<Current, Additional>)
32
+ : ContextBuilder<Init, LeftJoin<Current, Additional>>,
33
+ /**
34
+ * Build and return the context.
35
+ */
36
+ build(): Ctx
37
+ }
38
+
39
+ /**
40
+ * A fluent context builder.
41
+ *
42
+ * The initializer and transformer
43
+ *
44
+ * @param init The initial context or an context initializer.
45
+ * @return the context builder where you can
46
+ * use `extend()` to add context, and
47
+ * use `build()` to build the context.
48
+ */
49
+ export function context<
50
+ Init extends ContextBaseShape,
51
+ Ctx extends ContextBaseShape = Init
52
+ >(init?: Init | (() => Init)): ContextBuilder<Init, Ctx> {
53
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
54
+ return typeof init === 'function'
55
+ ? contextBuilder({}, [init])
56
+ : contextBuilder(init ?? {}, []) as any
57
+ }
58
+
59
+ function contextBuilder(init: ContextBaseShape, transformers: ContextExtender<any, any>[]) {
60
+ return {
61
+ extend(transformer: ContextExtender<any, any>) {
62
+ return contextBuilder(init, transformers.concat(transformer))
63
+ },
64
+ build() {
65
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
66
+ return transformers.reduce((p, t) => ({ ...p, ...t(p) }), init)
67
+ }
68
+ }
69
+ }
@@ -1,3 +1,4 @@
1
1
  // export * from './Maybe.js'
2
2
  export type { ChainFn, EndoFn } from './ChainFn.js'
3
3
  export * from './compose.js'
4
+ export * from './context.js'
@@ -1,30 +1,26 @@
1
+ import type { IsAny } from './IsAny.js'
2
+ import type { And, Or } from './logical.js'
3
+
1
4
  /**
2
- * `<T>() => T extends A ? 1 : 2` idea originate from `typepark`
3
- * But it does not work with union types.
4
- *
5
- * `<T>() => T extends A` is a trick to create an inferred type `T`.
6
- * This is needed for `boolean`, `string`, and `number`,
7
- * as they supports literal types.
5
+ * Checks if the two types are equal.
8
6
  */
9
-
10
- import { IsNever } from '../PrimitiveTypes.js'
7
+ export type Equal<A, B, Then = true, Else = false> = [A, B] extends [B, A]
8
+ ? And<IsAny<A>, IsAny<B>,
9
+ Then,
10
+ Or<IsAny<A>, IsAny<B>, Else, Then>>
11
+ : Else
11
12
 
12
13
  /**
13
- * Checks if two types are equal.
14
+ * Checks if the two types are equal.
14
15
  */
15
- export type Equal<A, B, Then = true, Else = false> =
16
- [A, B] extends [object, object]
17
- ? (IsNever<A> extends true
18
- ? (IsNever<B> extends true ? Then : Else)
19
- : (A extends B ? B extends A ? Then : Else : Else))
20
- : (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? Then : Else
21
- export type IsEqual<A, B> = Equal<A, B>
16
+ export type IsEqual<A, B, Then = true, Else = false> = Equal<A, B, Then, Else>
22
17
 
23
- export type NotEqual<A, B, Then = true, Else = false> =
24
- [A, B] extends [object, object]
25
- ? (IsNever<A> extends true
26
- ? (IsNever<B> extends true ? Else : Then)
27
- : (A extends B ? B extends A ? Else : Then : Then))
28
- : (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? Else : Then
18
+ /**
19
+ * Check if the two types are not equal
20
+ */
21
+ export type NotEqual<A, B, Then = true, Else = false> = Equal<A, B, Else, Then>
29
22
 
30
- export type IsNotEqual<A, B> = NotEqual<A, B>
23
+ /**
24
+ * Check if the two types are not equal
25
+ */
26
+ export type IsNotEqual<A, B, Then = true, Else = false> = NotEqual<A, B, Then, Else>
@@ -0,0 +1,3 @@
1
+ import type { IsExtend } from './Extends.js'
2
+
3
+ export type IsAny<T, Then = true, Else = false> = boolean extends IsExtend<T, string> ? Then : Else
@@ -2,16 +2,13 @@ export * from './CanAssign.js'
2
2
  export type { Equal, IsEqual, IsNotEqual, NotEqual } from './Equal.js'
3
3
  export type { Extendable, IsExtend, IsNotExtend, NotExtendable } from './Extends.js'
4
4
  export type { If } from './If.js'
5
+ export type { IsAny } from './IsAny.js'
6
+ export type { IsEmptyObject } from './IsEmptyObject.js'
5
7
  export * from './isType.js'
6
8
  export type { And, Not, Or, Xor } from './logical.js'
7
- export type { IsEmptyObject } from './IsEmptyObject.js'
8
-
9
- import type { IsExtend } from './Extends.js'
10
9
 
11
10
  export type IsLiteral<T extends number | string, Then = true, Else = false> = number extends T ?
12
11
  Else :
13
12
  string extends T ? Else : Then
14
13
 
15
14
  export type IsBoolean<T, Then = true, Else = false> = boolean extends T ? Then : Else
16
-
17
- export type IsAny<T, Then = true, Else = false> = boolean extends IsExtend<T, string> ? Then : Else
@@ -21,7 +21,7 @@ isType.f = function <T extends false>(subject?: T) {
21
21
 
22
22
  /**
23
23
  * Are types A and B equals/not equals.
24
- * Easier to use then `isType.t<>()` and `isType.f<>()`,
24
+ * Easier to use than `isType.t<>()` and `isType.f<>()`,
25
25
  * when doing type level only equality comparison as you don't have to import `Equal<>`.
26
26
  */
27
27
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -1,4 +1,26 @@
1
- export type And<A extends boolean, B extends boolean> = A extends true ? B extends true ? true : false : false
2
- export type Or<A extends boolean, B extends boolean> = A extends true ? true : B extends true ? true : false
3
- export type Not<X extends boolean> = X extends true ? false : true
4
- export type Xor<A extends boolean, B extends boolean> = A extends true ? Not<B> : B extends true ? true : false
1
+ export type And<
2
+ A extends boolean,
3
+ B extends boolean,
4
+ Then = true,
5
+ Else = false
6
+ > = A extends true ? B extends true ? Then : Else : Else
7
+
8
+ export type Or<
9
+ A extends boolean,
10
+ B extends boolean,
11
+ Then = true,
12
+ Else = false
13
+ > = A extends true ? Then : B extends true ? Then : Else
14
+
15
+ export type Not<
16
+ X extends boolean,
17
+ Then = true,
18
+ Else = false
19
+ > = X extends true ? Else : Then
20
+
21
+ export type Xor<
22
+ A extends boolean,
23
+ B extends boolean,
24
+ Then = true,
25
+ Else = false
26
+ > = A extends Then ? Not<B> : B extends true ? Then : Else