inferred-types 0.24.3 → 0.26.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/dist/index.d.ts +179 -34
- package/dist/index.js +337 -255
- package/dist/index.mjs +332 -252
- package/package.json +2 -1
- package/src/types/TypeInfo/Includes.ts +34 -0
- package/src/types/TypeInfo/IsBooleanLiteral.ts +7 -0
- package/src/types/TypeInfo/IsLiteral.ts +20 -0
- package/src/types/TypeInfo/IsNumericLiteral.ts +7 -0
- package/src/types/TypeInfo/IsStringLiteral.ts +7 -0
- package/src/types/TypeInfo/index.ts +5 -0
- package/src/types/functions/LogicFunction.ts +4 -0
- package/src/types/functions/index.ts +1 -0
- package/src/types/index.ts +1 -1
- package/src/utility/boolean-logic/and.ts +15 -0
- package/src/utility/boolean-logic/filter.ts +268 -0
- package/src/utility/boolean-logic/index.ts +4 -0
- package/src/utility/boolean-logic/not.ts +15 -0
- package/src/utility/boolean-logic/or.ts +15 -0
- package/src/utility/index.ts +1 -1
- package/src/utility/lists/asArray.ts +34 -0
- package/src/utility/lists/index.ts +1 -0
- package/tests/Includes-spec.ts +26 -5
- package/tests/TypeInfo/IsLiteral.spec.ts +49 -0
- package/tests/boolean-logic/boolean.spec.ts +21 -0
- package/tests/boolean-logic/filter.spec.ts +52 -0
- package/tests/lists/asArray.test.ts +91 -0
- package/tsconfig.json +8 -0
- package/vitest.config.ts +7 -0
- package/src/types/Includes.ts +0 -5
- package/src/utility/map-reduce/filter.ts +0 -35
- package/src/utility/map-reduce/index.ts +0 -12
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { asArray } from "src/utility";
|
|
3
|
+
import { Equal, Expect } from "@type-challenges/utils";
|
|
4
|
+
|
|
5
|
+
describe("asArray() function", () => {
|
|
6
|
+
it("non-array is returned as an array", () => {
|
|
7
|
+
const i = "a";
|
|
8
|
+
const o = asArray(i);
|
|
9
|
+
type O = typeof o;
|
|
10
|
+
|
|
11
|
+
// run-time
|
|
12
|
+
expect(o).toEqual(["a"]);
|
|
13
|
+
// design-time
|
|
14
|
+
type cases = [Expect<Equal<O, string[]>>];
|
|
15
|
+
const cases: cases = [true];
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("array is returned as an array", () => {
|
|
19
|
+
const i = ["a"];
|
|
20
|
+
const o = asArray(i);
|
|
21
|
+
type O = typeof o;
|
|
22
|
+
|
|
23
|
+
// run-time
|
|
24
|
+
expect(o).toEqual(["a"]);
|
|
25
|
+
// design-time
|
|
26
|
+
type cases = [Expect<Equal<O, string[]>>];
|
|
27
|
+
const cases: cases = [true];
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("non-array literal is returned as an array", () => {
|
|
31
|
+
const i = "a" as const;
|
|
32
|
+
const o = asArray(i, false);
|
|
33
|
+
const o2 = asArray(i, true);
|
|
34
|
+
|
|
35
|
+
type O = typeof o;
|
|
36
|
+
type O2 = typeof o2;
|
|
37
|
+
|
|
38
|
+
// run-time
|
|
39
|
+
expect(o).toEqual(["a"]);
|
|
40
|
+
// design-time
|
|
41
|
+
type cases = [
|
|
42
|
+
Expect<Equal<O, "a"[]>>, //
|
|
43
|
+
Expect<Equal<O2, string[]>>
|
|
44
|
+
];
|
|
45
|
+
const cases: cases = [true, true];
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("handling non-array element which presents as undefined", () => {
|
|
49
|
+
type T = string | undefined;
|
|
50
|
+
const i = undefined;
|
|
51
|
+
const i2: T = undefined;
|
|
52
|
+
const o = asArray(i);
|
|
53
|
+
const o2 = asArray(i2 as T);
|
|
54
|
+
const o3 = asArray(i2 as T, false);
|
|
55
|
+
type O = typeof o;
|
|
56
|
+
type O2 = typeof o2;
|
|
57
|
+
type O3 = typeof o3;
|
|
58
|
+
|
|
59
|
+
// run-time
|
|
60
|
+
expect(o).toEqual([]);
|
|
61
|
+
expect(o2).toEqual([]);
|
|
62
|
+
// design-time
|
|
63
|
+
type cases = [
|
|
64
|
+
Expect<Equal<O, unknown[]>>, //
|
|
65
|
+
// TODO: would be nice to extract the unknown[] part of the union
|
|
66
|
+
Expect<Equal<O2, unknown[] | string[]>>,
|
|
67
|
+
Expect<Equal<O3, unknown[] | string[]>>
|
|
68
|
+
];
|
|
69
|
+
const cases: cases = [true, true, true];
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("handling array element which contains undefined is unaffected", () => {
|
|
73
|
+
type T = string | undefined;
|
|
74
|
+
const i = [undefined, "foobar"];
|
|
75
|
+
const i2: T[] = [undefined, "foobar"];
|
|
76
|
+
const o = asArray(i);
|
|
77
|
+
const o2 = asArray(i2 as T[]);
|
|
78
|
+
type O = typeof o;
|
|
79
|
+
type O2 = typeof o2;
|
|
80
|
+
|
|
81
|
+
// run-time
|
|
82
|
+
expect(o).toEqual([undefined, "foobar"]);
|
|
83
|
+
expect(o2).toEqual([undefined, "foobar"]);
|
|
84
|
+
// design-time
|
|
85
|
+
type cases = [
|
|
86
|
+
Expect<Equal<O, (string | undefined)[]>>, //
|
|
87
|
+
Expect<Equal<O2, T[]>>
|
|
88
|
+
];
|
|
89
|
+
const cases: cases = [true, true];
|
|
90
|
+
});
|
|
91
|
+
});
|
package/tsconfig.json
CHANGED
package/vitest.config.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
/// <reference types="vitest" />
|
|
2
2
|
import { defineConfig } from "vite";
|
|
3
|
+
import { resolve } from "pathe";
|
|
3
4
|
|
|
4
5
|
// used for testing, library code uses TSUP to build exports
|
|
5
6
|
export default defineConfig({
|
|
7
|
+
resolve: {
|
|
8
|
+
alias: {
|
|
9
|
+
src: resolve(__dirname, "./src"),
|
|
10
|
+
test: resolve(__dirname, "./test"),
|
|
11
|
+
},
|
|
12
|
+
},
|
|
6
13
|
test: {
|
|
7
14
|
dir: "tests",
|
|
8
15
|
api: {
|
package/src/types/Includes.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A type takes the two arguments. The first is an array of string values, the second is the value
|
|
3
|
-
* which is being tested as it whether or not it's _included_ in the array. Result is true or false.
|
|
4
|
-
*/
|
|
5
|
-
export type Includes<T extends readonly any[], U> = U extends T[number] ? true : false;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
// inspired by [article](https://medium.com/@reidev275/creating-a-type-safe-dsl-for-filtering-in-typescript-53fe68a7942e)]
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export type Filter<A> =
|
|
5
|
-
| { kind: "Equals"; field: keyof A; val: A[keyof A] }
|
|
6
|
-
| { kind: "Greater"; field: keyof A; val: A[keyof A] }
|
|
7
|
-
| { kind: "Less"; field: keyof A; val: A[keyof A] }
|
|
8
|
-
| { kind: "And"; a: Filter<A>; b: Filter<A> }
|
|
9
|
-
| { kind: "Or"; a: Filter<A>; b: Filter<A> };
|
|
10
|
-
|
|
11
|
-
export const equals = <A, K extends keyof A>(field: K, val: A[K]): Filter<A> => ({
|
|
12
|
-
kind: "Equals",
|
|
13
|
-
field,
|
|
14
|
-
val
|
|
15
|
-
});
|
|
16
|
-
export const greater = <A, K extends keyof A>(field: K, val: A[K]): Filter<A> => ({
|
|
17
|
-
kind: "Greater",
|
|
18
|
-
field,
|
|
19
|
-
val
|
|
20
|
-
});
|
|
21
|
-
export const less = <A, K extends keyof A>(field: K, val: A[K]): Filter<A> => ({
|
|
22
|
-
kind: "Less",
|
|
23
|
-
field,
|
|
24
|
-
val
|
|
25
|
-
});
|
|
26
|
-
export const and = <A>(a: Filter<A>, b: Filter<A>): Filter<A> => ({
|
|
27
|
-
kind: "And",
|
|
28
|
-
a,
|
|
29
|
-
b
|
|
30
|
-
});
|
|
31
|
-
export const or = <A>(a: Filter<A>, b: Filter<A>): Filter<A> => ({
|
|
32
|
-
kind: "Or",
|
|
33
|
-
a,
|
|
34
|
-
b
|
|
35
|
-
});
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// #autoindex
|
|
2
|
-
// #region auto-indexed files
|
|
3
|
-
// index last changed at: 8th Aug, 2022, 09:51 AM ( GMT-7 )
|
|
4
|
-
// hash-code: 5ffc538b
|
|
5
|
-
|
|
6
|
-
// file exports
|
|
7
|
-
export * from "./filter";
|
|
8
|
-
|
|
9
|
-
// #endregion auto-indexed files
|
|
10
|
-
|
|
11
|
-
// see https://github.com/inocan-group/do-devops/docs/autoindex.md
|
|
12
|
-
// for more info
|