inferred-types 0.20.0 → 0.22.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/.eslintrc +36 -20
- package/.github/workflows/main.yml +34 -0
- package/.github/workflows/other.yml +15 -0
- package/.vscode/settings.json +10 -2
- package/dist/index.d.ts +398 -8541
- package/dist/index.js +17 -60
- package/dist/index.mjs +8 -48
- package/package.json +32 -24
- package/src/shared/valueTypes.ts +1 -1
- package/src/types/ExpandRecursively.ts +10 -1
- package/src/types/SimplifyObject.ts +12 -0
- package/src/types/Transformer.ts +1 -1
- package/src/types/alphabetic/CamelCase.ts +1 -1
- package/src/types/alphabetic/CapFirstAlpha.ts +7 -3
- package/src/types/alphabetic/Dasherize.ts +8 -5
- package/src/types/alphabetic/KebabCase.ts +1 -1
- package/src/types/alphabetic/PascalCase.ts +3 -1
- package/src/types/alphabetic/SnakeCase.ts +8 -3
- package/src/types/dictionary/DictPartialApplication.ts +30 -0
- package/src/types/dictionary/DictPrependWithFn.ts +23 -0
- package/src/types/dictionary/DictReturnValues.ts +27 -0
- package/src/types/dictionary/RequireProps.ts +15 -0
- package/src/types/{type-conversion → dictionary}/SameKeys.ts +0 -0
- package/src/types/dictionary/index.ts +8 -3
- package/src/types/{props.ts → dictionary/props.ts} +1 -1
- package/src/types/functions/FinalReturn.ts +12 -0
- package/{on-hold/types → src/types/functions}/index.ts +4 -3
- package/src/types/index.ts +4 -3
- package/src/types/ruleset-types.ts +3 -3
- package/src/types/runtime.ts +18 -18
- package/src/types/string-literals/LeftWhitespace.ts +5 -2
- package/src/types/string-literals/RightWhitespace.ts +5 -2
- package/src/types/tuples/FirstKey.ts +3 -3
- package/src/types/tuples/FromDictArray.ts +5 -4
- package/src/types/type-conversion/index.ts +2 -5
- package/src/utility/api/api.ts +8 -7
- package/src/utility/dictionary/dictionaryTransform.ts +5 -4
- package/src/utility/dictionary/entries.ts +9 -5
- package/src/utility/dictionary/index.ts +2 -3
- package/src/utility/dictionary/kv/dictToKv.ts +5 -3
- package/src/utility/dictionary/kv/filterDictArray.ts +5 -7
- package/src/utility/dictionary/kv/kv.ts +8 -3
- package/src/utility/dictionary/kv/kvToDict.ts +3 -2
- package/src/utility/dictionary/mapValues.ts +8 -5
- package/src/utility/dictionary/strArrayToDict.ts +1 -2
- package/src/utility/keys.ts +8 -4
- package/src/utility/lists/groupBy.ts +2 -9
- package/src/utility/literals/arrayToObject.ts +29 -29
- package/src/utility/literals/defineType.ts +6 -6
- package/src/utility/literals/identity.ts +5 -2
- package/src/utility/literals/literal.ts +3 -3
- package/src/utility/ruleset.ts +11 -9
- package/src/utility/runtime/condition.ts +1 -1
- package/src/utility/runtime/conditions/isFalse.ts +2 -2
- package/src/utility/runtime/conditions/isFunction.ts +2 -2
- package/src/utility/runtime/conditions/isObject.ts +4 -1
- package/src/utility/runtime/conditions/isTrue.ts +1 -1
- package/src/utility/runtime/ifTypeOf.ts +1 -1
- package/src/utility/runtime/type.ts +3 -1
- package/src/utility/runtime/withValue.ts +5 -3
- package/src/utility/state/Configurator.ts +2 -2
- package/src/utility/state/FluentConfigurator.ts +2 -2
- package/src/utility/state/index.ts +2 -3
- package/tests/CamelCase-spec.ts +2 -0
- package/tests/ExplicitFunction-spec.ts +3 -2
- package/tests/IncludeAndRetain-spec.ts +7 -12
- package/tests/Includes-spec.ts +2 -0
- package/tests/KebabCase-spec.ts +2 -0
- package/tests/MutationIdentity-spec.ts +2 -1
- package/tests/PascalCase-spec.ts +2 -0
- package/tests/Pluralize-spec.ts +2 -0
- package/tests/SnakeCase-spec.ts +2 -0
- package/tests/Where.spec.ts +9 -10
- package/tests/arrayToKeyLookup-spec.ts +5 -5
- package/tests/arrayToObject-spec.ts +12 -8
- package/tests/constructor.spec.ts +2 -0
- package/tests/createFnWithProps.spec.ts +2 -2
- package/tests/defineType-spec.ts +17 -11
- package/tests/dictionary/DictPartialApplication.test.ts +43 -0
- package/tests/dictionary/DictReturnValues.test.ts +32 -0
- package/tests/dictionary/Get.spec.ts +5 -3
- package/tests/dictionary/PrependValuesWithFunction.ts +34 -0
- package/tests/dictionary/RequireProps.test.ts +21 -0
- package/tests/dictionaryTransform-spec.ts +3 -3
- package/tests/ifTypeOf-spec.ts +31 -20
- package/tests/kv/KeyValue-spec.ts +4 -4
- package/tests/kv/SameKeys-spec.ts +6 -9
- package/tests/kv/dict-to-kv-and-back.spec.ts +5 -4
- package/tests/kv/entries-spec.ts +8 -9
- package/tests/kv/keys.spec.ts +9 -19
- package/tests/kv/kv-spec.ts +4 -4
- package/tests/lists/UniqueForProp.spec.ts +3 -1
- package/tests/literal-spec.ts +5 -3
- package/tests/mapValues-spec.ts +5 -3
- package/tests/props-spec.ts +3 -2
- package/tests/runtime/condition.spec.ts +2 -1
- package/tests/runtime/type.spec.ts +4 -3
- package/tests/strArrayToDict-spec.ts +6 -6
- package/tests/string-literals/AllCaps.spec.ts +6 -15
- package/tests/string-literals/CamelCase.spec.ts +13 -32
- package/tests/string-literals/Dasherize.spec.ts +13 -31
- package/tests/string-literals/HasUppercase.spec.ts +6 -15
- package/tests/string-literals/PascalCase.spec.ts +13 -32
- package/tests/string-literals/SnakeCase.spec.ts +13 -32
- package/tests/string-literals/whitespace-capture.spec.ts +10 -28
- package/tests/tuples/TupleToUnion.spec.ts +3 -1
- package/tests/withValue.spec.ts +4 -3
- package/tsconfig.json +8 -19
- package/vitest.config.ts +13 -0
- package/jest.config.ts +0 -20
- package/on-hold/Builder/Builder.ts +0 -66
- package/on-hold/Builder/BuilderApi.ts +0 -21
- package/on-hold/Builder/IdentityToMutationApi.ts +0 -44
- package/on-hold/Builder/MutationToFluentApi.ts +0 -60
- package/on-hold/Builder/index.ts +0 -35
- package/on-hold/tests/Builder-spec.ts +0 -310
- package/on-hold/tests/RuleSet-spec.ts +0 -59
- package/on-hold/tests/ToFluent-spec.ts +0 -60
- package/on-hold/tests/api-spec.ts +0 -73
- package/on-hold/tests/data/builder-data.ts +0 -53
- package/on-hold/tests/filterDictArray-spec.ts +0 -50
- package/on-hold/tests/run-time-spec.ts +0 -44
- package/on-hold/type.ts +0 -20
- package/on-hold/types/object.ts +0 -19
- package/on-hold/types/prop.ts +0 -28
- package/on-hold/types/scalar.ts +0 -37
- package/src/types/dictionary/NotEmptyObject.ts +0 -9
- package/src/types/type-conversion/UnwrapValue.ts +0 -13
- package/src/types/type-conversion/WrapValue.ts +0 -15
- package/src/utility/dictionary/valuesOfProp.ts +0 -13
- package/src/utility/state/KeyStorage.ts +0 -23
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
// #autoindex
|
|
2
2
|
// #region autoindexed files
|
|
3
|
-
// index last changed at:
|
|
4
|
-
// hash-code:
|
|
3
|
+
// index last changed at: 7th Jul, 2022, 02:28 PM ( GMT-7 )
|
|
4
|
+
// hash-code: 6e90bc78
|
|
5
5
|
|
|
6
6
|
// file exports
|
|
7
|
+
export * from "./DictPartialApplication";
|
|
8
|
+
export * from "./DictPrependWithFn";
|
|
9
|
+
export * from "./DictReturnValues";
|
|
7
10
|
export * from "./Get";
|
|
8
|
-
export * from "./
|
|
11
|
+
export * from "./RequireProps";
|
|
12
|
+
export * from "./SameKeys";
|
|
13
|
+
export * from "./props";
|
|
9
14
|
|
|
10
15
|
// #endregion
|
|
11
16
|
|
|
@@ -18,7 +18,7 @@ export type OptionalKeys<T extends object> = {
|
|
|
18
18
|
* The _keys_ on a given object `T` which have a literal value of `W`.
|
|
19
19
|
*
|
|
20
20
|
* Optionally, you may provide a generic `E` to exclude certain keys in
|
|
21
|
-
*
|
|
21
|
+
* result set.
|
|
22
22
|
* ```ts
|
|
23
23
|
* // "foo"
|
|
24
24
|
* type Str = KeysWithValue<{ foo: "hi"; bar: 5 }>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A type utility which looks at a chain for functions and reduces the type
|
|
3
|
+
* to the final `ReturnType` of the chain.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* // number
|
|
7
|
+
* type T = FinalReturn<() => (foo: string) => (bar: string) => () => number>;
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export type FinalReturn<T extends any> = T extends (...args: any[]) => any
|
|
11
|
+
? FinalReturn<ReturnType<T>>
|
|
12
|
+
: T;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// #autoindex
|
|
2
2
|
// #region autoindexed files
|
|
3
|
-
// index last changed at:
|
|
4
|
-
// hash-code:
|
|
5
|
-
|
|
3
|
+
// index last changed at: 6th Jun, 2022, 10:19 AM ( GMT-7 )
|
|
4
|
+
// hash-code: 7fa9afdf
|
|
6
5
|
|
|
6
|
+
// file exports
|
|
7
|
+
export * from "./FinalReturn";
|
|
7
8
|
|
|
8
9
|
// #endregion
|
|
9
10
|
|
package/src/types/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// #autoindex
|
|
2
2
|
// #region autoindexed files
|
|
3
|
-
// index last changed at:
|
|
4
|
-
// hash-code:
|
|
3
|
+
// index last changed at: 7th Jul, 2022, 01:15 PM ( GMT-7 )
|
|
4
|
+
// hash-code: f976e182
|
|
5
5
|
|
|
6
6
|
// file exports
|
|
7
7
|
export * from "./Api";
|
|
@@ -22,12 +22,12 @@ export * from "./Not";
|
|
|
22
22
|
export * from "./Numeric";
|
|
23
23
|
export * from "./Opaque";
|
|
24
24
|
export * from "./Retain";
|
|
25
|
+
export * from "./SimplifyObject";
|
|
25
26
|
export * from "./Transformer";
|
|
26
27
|
export * from "./TypeGuard";
|
|
27
28
|
export * from "./Where";
|
|
28
29
|
export * from "./append-types";
|
|
29
30
|
export * from "./maybe";
|
|
30
|
-
export * from "./props";
|
|
31
31
|
export * from "./ruleset-types";
|
|
32
32
|
export * from "./runtime";
|
|
33
33
|
export * from "./type-testing";
|
|
@@ -35,6 +35,7 @@ export * from "./type-testing";
|
|
|
35
35
|
export * from "./alphabetic/index";
|
|
36
36
|
export * from "./dictionary/index";
|
|
37
37
|
export * from "./fluent/index";
|
|
38
|
+
export * from "./functions/index";
|
|
38
39
|
export * from "./kv/index";
|
|
39
40
|
export * from "./lists/index";
|
|
40
41
|
export * from "./string-literals/index";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { OptionalKeys } from "./props";
|
|
3
|
-
import { SameKeys } from "./
|
|
1
|
+
import { TypeCondition } from "src/utility/runtime";
|
|
2
|
+
import { OptionalKeys } from "./dictionary/props";
|
|
3
|
+
import { SameKeys } from "./dictionary/SameKeys";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* **RuleDefinition**
|
package/src/types/runtime.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { TypeGuard } from "~/types";
|
|
2
|
-
|
|
3
1
|
// runtime types relate to the types coming out of the runtime utilities
|
|
4
2
|
|
|
3
|
+
import { TypeGuard } from "./TypeGuard";
|
|
4
|
+
|
|
5
5
|
export type RuntimeType<T> = {
|
|
6
6
|
__kind: "type";
|
|
7
7
|
|
|
@@ -14,21 +14,21 @@ export type RuntimeProp<P extends Readonly<PropertyKey>, T extends RuntimeType<a
|
|
|
14
14
|
key: Readonly<P>;
|
|
15
15
|
valueType: Readonly<T["type"]>;
|
|
16
16
|
/**
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
17
|
+
* Provides the _type_ to the type system when used with `typeof`.
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* const t = number();
|
|
21
|
+
* // number
|
|
22
|
+
* type T = typeof t.type;
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* **Note:** _the runtime system will get a string equivalent name:_
|
|
26
|
+
* ```ts
|
|
27
|
+
* const t = number();
|
|
28
|
+
* // "number"
|
|
29
|
+
* console.log(t.type);
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
32
|
type: Record<P, T["type"]>;
|
|
33
33
|
is: TypeGuard<Record<P, T["type"]>>;
|
|
34
34
|
};
|
|
@@ -36,4 +36,4 @@ export type RuntimeProp<P extends Readonly<PropertyKey>, T extends RuntimeType<a
|
|
|
36
36
|
export type TypeOptions<T extends Partial<object> = {}> = {
|
|
37
37
|
/** each type has a default type guard but you can override if you need to be more specific */
|
|
38
38
|
typeGuard?: TypeGuard<T>;
|
|
39
|
-
} & T;
|
|
39
|
+
} & T;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Replace } from "./Replace";
|
|
2
|
+
import { TrimLeft } from "./TrimLeft";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Provides the _left_ whitespace of a string
|
|
@@ -7,4 +8,6 @@ import type { Replace, TrimLeft } from "~/types";
|
|
|
7
8
|
* type T = LeftWhitespace<"\n\t foobar">;
|
|
8
9
|
* ```
|
|
9
10
|
*/
|
|
10
|
-
export type LeftWhitespace<S extends string> = string extends S
|
|
11
|
+
export type LeftWhitespace<S extends string> = string extends S
|
|
12
|
+
? string
|
|
13
|
+
: Replace<S, TrimLeft<S>, "">;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Replace } from "./Replace";
|
|
2
|
+
import { TrimRight } from "./TrimRight";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Provides the _left_ whitespace of a string
|
|
@@ -7,4 +8,6 @@ import type { Replace, TrimRight } from "~/types";
|
|
|
7
8
|
* type T = LeftWhitespace<"\n\t foobar">;
|
|
8
9
|
* ```
|
|
9
10
|
*/
|
|
10
|
-
export type RightWhitespace<S extends string> = string extends S
|
|
11
|
+
export type RightWhitespace<S extends string> = string extends S
|
|
12
|
+
? string
|
|
13
|
+
: Replace<S, TrimRight<S>, "">;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Keys } from "../Keys";
|
|
2
|
+
import { UnionToTuple } from "../type-conversion/UnionToTuple";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Returns the _first_ key in an object.
|
|
5
|
-
*
|
|
6
|
+
*
|
|
6
7
|
* **Note:** key order is not guarenteed so typically this is used
|
|
7
8
|
* for a key/value pair where only one key is expected
|
|
8
9
|
*/
|
|
9
10
|
export type FirstKey<T extends object> = UnionToTuple<Keys<T>>[0];
|
|
10
|
-
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ExpandRecursively
|
|
2
|
-
|
|
1
|
+
import { ExpandRecursively } from "../ExpandRecursively";
|
|
2
|
+
import { UnionToIntersection } from "../type-conversion";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Typescript utility which receives `T` as shape which resembles `DictArray<D>`
|
|
@@ -9,5 +9,6 @@ import { ExpandRecursively, UnionToIntersection } from "~/types";
|
|
|
9
9
|
* type Dict = FromDictArray<[["foo", { foo: 1 }], ["bar", { bar: "hi" }]]>;
|
|
10
10
|
* ```
|
|
11
11
|
*/
|
|
12
|
-
export type FromDictArray<T extends [string, Record<string, unknown>][]> =
|
|
13
|
-
|
|
12
|
+
export type FromDictArray<T extends [string, Record<string, unknown>][]> = ExpandRecursively<
|
|
13
|
+
UnionToIntersection<T[number][1]>
|
|
14
|
+
>;
|
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
// #autoindex
|
|
2
2
|
|
|
3
3
|
// #region autoindexed files
|
|
4
|
-
// index last changed at: 6th Jun, 2022,
|
|
5
|
-
// hash-code:
|
|
4
|
+
// index last changed at: 6th Jun, 2022, 10:18 AM ( GMT-7 )
|
|
5
|
+
// hash-code: b346a5f3
|
|
6
6
|
|
|
7
7
|
// file exports
|
|
8
|
-
export * from "./SameKeys";
|
|
9
8
|
export * from "./TupleToUnion";
|
|
10
9
|
export * from "./UnionToIntersection";
|
|
11
10
|
export * from "./UnionToTuple";
|
|
12
|
-
export * from "./UnwrapValue";
|
|
13
|
-
export * from "./WrapValue";
|
|
14
11
|
|
|
15
12
|
// #endregion
|
|
16
13
|
|
package/src/utility/api/api.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { Narrowable } from "
|
|
1
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
2
2
|
|
|
3
|
-
export const api =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
};
|
|
3
|
+
export const api =
|
|
4
|
+
<N extends Narrowable, TPrivate extends Readonly<Record<any, N>>>(priv: TPrivate) =>
|
|
5
|
+
<TPublic extends object>(pub: TPublic) => {
|
|
6
|
+
const surface = () => pub as TPublic;
|
|
7
|
+
surface.prototype.priv = () => priv as TPrivate;
|
|
9
8
|
|
|
9
|
+
return surface;
|
|
10
|
+
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/* eslint-disable no-use-before-define */
|
|
2
|
-
|
|
3
|
-
import { SameKeys
|
|
2
|
+
|
|
3
|
+
import { SameKeys } from "src/types/dictionary";
|
|
4
|
+
import { Transformer } from "src/types/Transformer";
|
|
5
|
+
import { keys } from "../keys";
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* Takes a dictionary of type `I` and converts it to a dictionary of type `O` where
|
|
@@ -13,9 +15,8 @@ export function dictionaryTransform<I extends object, O extends SameKeys<I>>(
|
|
|
13
15
|
input: I,
|
|
14
16
|
transform: Transformer<I, O>
|
|
15
17
|
) {
|
|
16
|
-
|
|
17
18
|
return keys(input).reduce((acc, i) => {
|
|
18
19
|
const key = i as string & keyof I;
|
|
19
|
-
return {...acc, [key]: transform(input, key)};
|
|
20
|
+
return { ...acc, [key]: transform(input, key) };
|
|
20
21
|
}, {} as O);
|
|
21
22
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { KeyValue
|
|
1
|
+
import { KeyValue } from "src/types/kv";
|
|
2
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
2
3
|
import { keys } from "../keys";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* **entries**
|
|
6
7
|
*
|
|
7
|
-
* Provides an
|
|
8
|
+
* Provides an _iterable_ over the passed in dictionary object where each iteration
|
|
8
9
|
* provides a tuple of `[ key, value ]` which preserve type literals.
|
|
9
10
|
*
|
|
10
11
|
* For example:
|
|
@@ -14,8 +15,11 @@ import { keys } from "../keys";
|
|
|
14
15
|
* for (const [k, v] of entries(obj)) { ... }
|
|
15
16
|
* ```
|
|
16
17
|
*/
|
|
17
|
-
export function entries<
|
|
18
|
-
|
|
18
|
+
export function entries<
|
|
19
|
+
N extends Narrowable,
|
|
20
|
+
T extends Record<string, N>,
|
|
21
|
+
I extends KeyValue<T, keyof T>
|
|
22
|
+
>(obj: T) {
|
|
19
23
|
const iterable = {
|
|
20
24
|
*[Symbol.iterator]() {
|
|
21
25
|
for (const k of keys(obj)) {
|
|
@@ -26,4 +30,4 @@ export function entries<N extends Narrowable, T extends Record<string, N>, I ext
|
|
|
26
30
|
};
|
|
27
31
|
|
|
28
32
|
return iterable;
|
|
29
|
-
}
|
|
33
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// #autoindex
|
|
2
2
|
// #region autoindexed files
|
|
3
|
-
// index last changed at:
|
|
4
|
-
// hash-code:
|
|
3
|
+
// index last changed at: 7th Jul, 2022, 01:47 PM ( GMT-7 )
|
|
4
|
+
// hash-code: dd2bf50a
|
|
5
5
|
|
|
6
6
|
// file exports
|
|
7
7
|
export * from "./arrayToKeyLookup";
|
|
@@ -9,7 +9,6 @@ export * from "./dictionaryTransform";
|
|
|
9
9
|
export * from "./entries";
|
|
10
10
|
export * from "./mapValues";
|
|
11
11
|
export * from "./strArrayToDict";
|
|
12
|
-
export * from "./valuesOfProp";
|
|
13
12
|
// directory exports
|
|
14
13
|
export * from "./kv/index";
|
|
15
14
|
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { KvFrom } from "src/types/kv";
|
|
2
|
+
import { Mutable } from "src/types/Mutable";
|
|
3
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
4
|
+
import { UnionToTuple } from "src/types/type-conversion";
|
|
5
|
+
import { keys } from "src/utility/keys";
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* Converts a dictionary object into an array of dictionaries with `key` and `value` properties
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Configurator, IConfigurator } from "
|
|
1
|
+
import { DictArray, DictArrayFilterCallback } from "src/types/tuples";
|
|
2
|
+
import { Configurator, IConfigurator } from "src/utility/state";
|
|
3
3
|
|
|
4
4
|
export interface Array<T> {
|
|
5
5
|
filter<U extends T>(pred: (a: T) => a is U): U[];
|
|
@@ -11,12 +11,11 @@ export interface Array<T> {
|
|
|
11
11
|
*/
|
|
12
12
|
export function filterDictArray<
|
|
13
13
|
T extends object,
|
|
14
|
-
C extends DictArrayFilterCallback<keyof T, T, true | false
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
C extends DictArrayFilterCallback<keyof T, T, true | false>
|
|
15
|
+
>(dictArr: DictArray<T>, cb: C) {
|
|
17
16
|
const state: IConfigurator = Configurator();
|
|
18
17
|
|
|
19
|
-
const updated = dictArr.filter(i => {
|
|
18
|
+
const updated = dictArr.filter((i) => {
|
|
20
19
|
const [k, v] = i;
|
|
21
20
|
const keep = cb(k, v);
|
|
22
21
|
if (!keep) {
|
|
@@ -30,4 +29,3 @@ export function filterDictArray<
|
|
|
30
29
|
type ExcludedKeys = "";
|
|
31
30
|
return updated as unknown as DictArray<Omit<T, ExcludedKeys>>;
|
|
32
31
|
}
|
|
33
|
-
|
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
import { ExpandRecursively
|
|
1
|
+
import { ExpandRecursively } from "src/types/ExpandRecursively";
|
|
2
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Build a key-value pair where both _key_ and _value_ are inferred. This
|
|
5
6
|
* includes ensuring that the _key_ is a type literal not just a "string".
|
|
6
7
|
*
|
|
7
8
|
* > note: the value will be inferred but if you need to constrain it
|
|
8
|
-
* > to a narrower type then both
|
|
9
|
+
* > to a narrower type then both inferences will break and you should
|
|
9
10
|
* > instead use `KV2` to get this capability.
|
|
10
11
|
*/
|
|
11
|
-
export function kv<
|
|
12
|
+
export function kv<
|
|
13
|
+
K extends string,
|
|
14
|
+
N extends Narrowable,
|
|
15
|
+
V extends Record<any, N> | boolean | number | string | null | undefined
|
|
16
|
+
>(key: K, value: V) {
|
|
12
17
|
return { [key]: value } as ExpandRecursively<Record<K, V>>;
|
|
13
18
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { DictFromKv } from "src/types/kv";
|
|
2
|
+
import { Mutable } from "src/types/Mutable";
|
|
3
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Converts an array of dictionaries with `key` and `value` properties to a singular dictionary.
|
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
import { Narrowable } from "
|
|
1
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
2
2
|
import { entries } from "./entries";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* **mapValues**
|
|
6
|
-
*
|
|
6
|
+
*
|
|
7
7
|
* Maps over a dictionary, preserving the keys but allowing the values to be mutated.
|
|
8
|
-
*
|
|
8
|
+
*
|
|
9
9
|
* ```ts
|
|
10
10
|
* const colors = { red: 4, blue: 2, green: 3 };
|
|
11
11
|
* // { red: 8, blue: 4, green: 6 }
|
|
12
12
|
* const twoX = mapValues(colors, v => v * 2);
|
|
13
13
|
* ```
|
|
14
14
|
*/
|
|
15
|
-
export function mapValues<N extends Narrowable, T extends Record<string, N>, V>(
|
|
15
|
+
export function mapValues<N extends Narrowable, T extends Record<string, N>, V>(
|
|
16
|
+
obj: T,
|
|
17
|
+
valueMapper: (k: T[keyof T]) => V
|
|
18
|
+
) {
|
|
16
19
|
return Object.fromEntries(
|
|
17
20
|
// TODO: fix the type error with v and valueMapper
|
|
18
21
|
[...entries(obj)].map(([k, v]) => [k, valueMapper(v as any)])
|
|
19
22
|
) as { [K in keyof T]: V };
|
|
20
|
-
}
|
|
23
|
+
}
|
package/src/utility/keys.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import { Keys
|
|
1
|
+
import { Keys } from "src/types/Keys";
|
|
2
|
+
import { Length } from "src/types/Length";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Provides the _keys_ of an object with the `keyof T` made explicit.
|
|
5
6
|
*/
|
|
6
7
|
export function keys<T extends {}, W extends readonly string[]>(obj: T, ...without: W) {
|
|
7
|
-
const v =
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
const v =
|
|
9
|
+
without.length > 0
|
|
10
|
+
? (Object.keys(obj).filter((k) => !without.includes(k)) as unknown as Array<
|
|
11
|
+
Exclude<keyof T, Keys<W>>
|
|
12
|
+
>)
|
|
13
|
+
: (Object.keys(obj) as unknown as Array<keyof T>);
|
|
10
14
|
|
|
11
15
|
return v as unknown as Length<W> extends 0 ? Array<keyof T> : Array<Exclude<keyof T, Keys<W>>>;
|
|
12
16
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Narrowable } from "
|
|
2
|
-
// import { UniqueForProp } from "~/types/lists/UniqueForProp";
|
|
1
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Groups an array of data based on the value of a property
|
|
@@ -11,12 +10,6 @@ import { Narrowable } from "~/types";
|
|
|
11
10
|
*
|
|
12
11
|
* @ignore not implemented
|
|
13
12
|
*/
|
|
14
|
-
export function groupBy<T extends Record<string, Narrowable
|
|
15
|
-
_data: Readonly<T[]>
|
|
16
|
-
) {
|
|
13
|
+
export function groupBy<T extends Record<string, Narrowable>>(_data: Readonly<T[]>) {
|
|
17
14
|
throw new Error("not implemented");
|
|
18
|
-
return (_groupBy: K) => {
|
|
19
|
-
// type _GroupedBy = UniqueForProp<typeof data, K>;
|
|
20
|
-
// const _output = {} as Record<GroupedBy, Narrowable>;
|
|
21
|
-
};
|
|
22
15
|
}
|
|
@@ -1,22 +1,28 @@
|
|
|
1
|
-
import { Narrowable } from "
|
|
2
|
-
|
|
1
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* **UniqueDictionary**
|
|
6
|
-
*
|
|
5
|
+
*
|
|
7
6
|
* A dictionary converted by `arrayToObject()` which expects each key `S` to have a only a
|
|
8
7
|
* single/unique value.
|
|
9
8
|
*/
|
|
10
|
-
export type UniqueDictionary<
|
|
9
|
+
export type UniqueDictionary<
|
|
10
|
+
S extends PropertyKey,
|
|
11
|
+
N extends Narrowable,
|
|
12
|
+
T extends Record<keyof T, N> & Record<S, any>
|
|
13
|
+
> = { [V in T as V[S]]: V };
|
|
11
14
|
|
|
12
15
|
/**
|
|
13
16
|
* **GeneralDictionary**
|
|
14
|
-
*
|
|
17
|
+
*
|
|
15
18
|
* A dictionary converted by `arrayToObject()` which expects each key `S` to have an
|
|
16
19
|
* array of values.
|
|
17
20
|
*/
|
|
18
|
-
export type GeneralDictionary<
|
|
19
|
-
|
|
21
|
+
export type GeneralDictionary<
|
|
22
|
+
S extends PropertyKey,
|
|
23
|
+
N extends Narrowable,
|
|
24
|
+
T extends Record<keyof T, N> & Record<S, any>
|
|
25
|
+
> = { [V in T as V[S]]: V[] };
|
|
20
26
|
|
|
21
27
|
export type ArrayConverter<S extends PropertyKey, U extends boolean> =
|
|
22
28
|
/**
|
|
@@ -28,45 +34,40 @@ export type ArrayConverter<S extends PropertyKey, U extends boolean> =
|
|
|
28
34
|
arr: readonly T[]
|
|
29
35
|
) => true extends U ? UniqueDictionary<S, N, T> : GeneralDictionary<S, N, T>;
|
|
30
36
|
|
|
31
|
-
|
|
32
37
|
/**
|
|
33
38
|
* Converts an array of objects into a dictionary by picking a property name contained
|
|
34
39
|
* by all objects and using that as the key to the dictionary.
|
|
35
|
-
*
|
|
40
|
+
*
|
|
36
41
|
* ```ts
|
|
37
|
-
* const arr = [
|
|
42
|
+
* const arr = [
|
|
38
43
|
* { kind: "color", favorite: "blue", likes: 100 },
|
|
39
44
|
* { kind: "song", favorite: "some song", likes: 25 }
|
|
40
45
|
* ];
|
|
41
46
|
* const dict = arrayToObject("kind")(arr);
|
|
42
47
|
* ```
|
|
43
|
-
*
|
|
48
|
+
*
|
|
44
49
|
* This will produce a dictionary with keys of `color` and `song`.
|
|
45
50
|
*/
|
|
46
|
-
export function arrayToObject<
|
|
47
|
-
S extends PropertyKey,
|
|
48
|
-
U extends boolean
|
|
49
|
-
>(prop: S, unique?: U) {
|
|
51
|
+
export function arrayToObject<S extends PropertyKey, U extends boolean>(prop: S, unique?: U) {
|
|
50
52
|
type X = true extends U ? true : false;
|
|
51
53
|
/**
|
|
52
54
|
* **arrayToObject** - partially applied
|
|
53
|
-
*
|
|
55
|
+
*
|
|
54
56
|
* pass in an array of objects to complete application of arrayToObject()
|
|
55
57
|
*/
|
|
56
|
-
const transform: ArrayConverter<S, X> = <
|
|
58
|
+
const transform: ArrayConverter<S, X> = <
|
|
59
|
+
N extends Narrowable,
|
|
60
|
+
T extends Record<keyof T, N> & Record<S, any>
|
|
61
|
+
>(
|
|
57
62
|
arr: readonly T[]
|
|
58
63
|
): true extends X ? UniqueDictionary<S, N, T> : GeneralDictionary<S, N, T> => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
(acc, v) =>
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const existing = acc[v[prop] as T[S]] || [];
|
|
67
|
-
return { ...acc, [v[prop]]: [...existing, v] };
|
|
68
|
-
}, {} as GeneralDictionary<S, N, T>
|
|
69
|
-
);
|
|
64
|
+
const result =
|
|
65
|
+
unique !== false
|
|
66
|
+
? arr.reduce((acc, v) => ({ ...acc, [v[prop]]: v }), {} as UniqueDictionary<S, N, T>)
|
|
67
|
+
: arr.reduce((acc, v) => {
|
|
68
|
+
const existing = acc[v[prop] as T[S]] || [];
|
|
69
|
+
return { ...acc, [v[prop]]: [...existing, v] };
|
|
70
|
+
}, {} as GeneralDictionary<S, N, T>);
|
|
70
71
|
|
|
71
72
|
// type cast based on `U`
|
|
72
73
|
return result as true extends X ? UniqueDictionary<S, N, T> : GeneralDictionary<S, N, T>;
|
|
@@ -75,4 +76,3 @@ export function arrayToObject<
|
|
|
75
76
|
return transform;
|
|
76
77
|
// return converter(prop, unique);
|
|
77
78
|
}
|
|
78
|
-
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { ExpandRecursively
|
|
1
|
+
import { ExpandRecursively } from "src/types/ExpandRecursively";
|
|
2
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Build a _type_ from two run-time dictionaries.
|
|
5
|
-
*
|
|
6
|
+
*
|
|
6
7
|
* 1. The _first_ -- which is optional -- is interpreted as a _literal_ type definition
|
|
7
8
|
* 2. The _second_ dictionary is interpreted as a "wide" definition of prop types
|
|
8
9
|
*/
|
|
9
|
-
export function defineType<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
>(literal: TLiteral = {} as TLiteral) {
|
|
10
|
+
export function defineType<N extends Narrowable, TLiteral extends Record<string, N>>(
|
|
11
|
+
literal: TLiteral = {} as TLiteral
|
|
12
|
+
) {
|
|
13
13
|
/**
|
|
14
14
|
* Add any key/value pairs which you want to have _wide_ types associated;
|
|
15
15
|
* literal types are defined already and stated above.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Narrowable } from "
|
|
1
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* An identity function for any type, with the goal of preserving literal type information
|
|
4
5
|
* whereever possible.
|
|
@@ -6,4 +7,6 @@ import { Narrowable } from "~/types/Narrowable";
|
|
|
6
7
|
export const identity = <
|
|
7
8
|
N extends Narrowable,
|
|
8
9
|
T extends Record<any, N> | number | string | boolean | symbol | undefined | null
|
|
9
|
-
>(
|
|
10
|
+
>(
|
|
11
|
+
v: T
|
|
12
|
+
) => v;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-use-before-define */
|
|
2
2
|
|
|
3
|
-
import { Narrowable } from "
|
|
3
|
+
import { Narrowable } from "src/types/Narrowable";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Takes an object as input --which has an `id` property and returns it as the same
|
|
@@ -39,8 +39,8 @@ export function idTypeGuard<T extends { id: I }, I extends PropertyKey>(
|
|
|
39
39
|
/**
|
|
40
40
|
* Takes an object as input and infers the narrow literal types of the property
|
|
41
41
|
* values on the object.
|
|
42
|
-
*
|
|
43
|
-
* > Note: this addresses this [a known TS gap](https://github.com/microsoft/TypeScript/issues/30680).
|
|
42
|
+
*
|
|
43
|
+
* > Note: this addresses this [a known TS gap](https://github.com/microsoft/TypeScript/issues/30680).
|
|
44
44
|
* > Hopefully at some point this will be addressed in the language.
|
|
45
45
|
*/
|
|
46
46
|
export function literal<N extends Narrowable, T extends Record<keyof T, N>>(obj: T) {
|