inferred-types 0.24.0 → 0.24.2
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 +19 -16
- package/dist/index.js +8 -1
- package/dist/index.mjs +8 -1
- package/package.json +6 -6
- package/src/types/dictionary/MapTo.ts +18 -13
- package/src/types/dictionary/props.ts +1 -1
- package/src/utility/dictionary/mapTo.ts +10 -4
- package/tests/dictionary/mapTo.test.ts +18 -0
package/dist/index.d.ts
CHANGED
|
@@ -585,23 +585,26 @@ O extends (...args: any[]) => any = (...args: any[]) => any> = SimplifyObject<{
|
|
|
585
585
|
declare type Get<T, K> = K extends `${infer FK}.${infer L}` ? FK extends keyof T ? Get<T[FK], L> : never : K extends keyof T ? T[K] : never;
|
|
586
586
|
|
|
587
587
|
/**
|
|
588
|
-
*
|
|
588
|
+
* **MapTo**
|
|
589
589
|
*
|
|
590
|
-
*
|
|
591
|
-
* - you can either return a 1:1 or 1:M output as `O` or `O[]` when there is a mapping
|
|
590
|
+
* Maps from one type `I` to another `O[]`
|
|
592
591
|
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
*
|
|
597
|
-
* ```ts
|
|
598
|
-
* const mapper: ModelMapper<{title: string}, {title: string, kind: string}> = i => {
|
|
599
|
-
* title: i.title,
|
|
600
|
-
* kind: "markdown"
|
|
601
|
-
* }
|
|
602
|
-
* ```
|
|
592
|
+
* **Note:** because the output is an array you can easily support `1:1` and `1:M` mappings
|
|
593
|
+
* but not a filtering operation (e.g., `1:0`); if you need this then use `MapToWithFiltering`
|
|
594
|
+
* instead.
|
|
603
595
|
*/
|
|
604
596
|
declare type MapTo<I extends {}, O extends {}> = (i: I) => O[];
|
|
597
|
+
/**
|
|
598
|
+
* **MapToWithFiltering**
|
|
599
|
+
*
|
|
600
|
+
* Maps from one type `I` to another `(O | null)[]`
|
|
601
|
+
*
|
|
602
|
+
* **Note:** because the output is an array you can easily support `1:1` and `1:M` mappings
|
|
603
|
+
* and the allowance of the conversion to result in a `null` value also means that filter
|
|
604
|
+
* out an input value entirely is possible. If you don't need _filtering_ then use the
|
|
605
|
+
* `MapTo` type instead.
|
|
606
|
+
*/
|
|
607
|
+
declare type MapToWithFiltering<I extends {}, O extends {}> = (i: I) => O[] | null;
|
|
605
608
|
|
|
606
609
|
/**
|
|
607
610
|
* Given a dictionary of type `<T>`, this utility function will
|
|
@@ -745,7 +748,7 @@ declare type RequiredProps<T extends object> = Pick<T, RequiredKeys<T>>;
|
|
|
745
748
|
*
|
|
746
749
|
* Reduces an object to only key/value pairs where the key is optional
|
|
747
750
|
*/
|
|
748
|
-
declare type OptionalProps<T extends object> = Pick<T,
|
|
751
|
+
declare type OptionalProps<T extends object> = Pick<T, OptionalKeys<T>>;
|
|
749
752
|
/**
|
|
750
753
|
* **WithValue**
|
|
751
754
|
*
|
|
@@ -1483,7 +1486,7 @@ declare function mapValues<N extends Narrowable, T extends Record<string, N>, V>
|
|
|
1483
1486
|
* .map();
|
|
1484
1487
|
* ```
|
|
1485
1488
|
*/
|
|
1486
|
-
declare const mapTo: <I extends {}, O extends {}>(cb:
|
|
1489
|
+
declare const mapTo: <I extends {}, O extends {}>(cb: MapToWithFiltering<I, O>) => (source: I | I[]) => O[];
|
|
1487
1490
|
|
|
1488
1491
|
/**
|
|
1489
1492
|
* converts an array of strings `["a", "b", "c"]` into a more type friendly
|
|
@@ -1770,4 +1773,4 @@ interface IFluentConfigurator<C> {
|
|
|
1770
1773
|
*/
|
|
1771
1774
|
declare function FluentConfigurator<I>(initial?: I): IFluentConfigurator<{}>;
|
|
1772
1775
|
|
|
1773
|
-
export { AllCaps, Alpha, AlphaNumeric, Api, ApiFunction, ApiValue, AppendToDictionary, AppendToObject, ArrConcat, Array$1 as Array, ArrayConverter, AssertExtends, Bracket, Break, CamelCase, CapitalizeWords, ClosingBracket, Condition, Configurator, Constructor, DashToSnake, DashUppercase, Dasherize, DefinePropertiesApi, DictArrApi, DictArray, DictArrayFilterCallback, DictArrayKv, DictChangeValue, DictFromKv, DictKvTuple, DictPartialApplication, DictPrependWithFn, DictReturnValues, DynamicRule, DynamicRuleSet, Email, EnumValues, ExpandRecursively, ExpectExtends, ExplicitFunction, ExtendsClause, ExtendsNarrowlyClause, Filter, FinalReturn, First, FirstKey, FirstKeyValue, FirstOfEach, FluentConfigurator, FluentFunction, FromDictArray, FunctionType, GeneralDictionary, Get, HasUppercase, IConfigurator, IFluentConfigurator, If, IfExtends, IfExtendsThen, Include, Includes, IsArray, IsBoolean, IsCapitalized, IsFalse, IsFunction, IsObject, IsString, IsTrue, KebabCase, KeyValue, KeyedRecord, Keys, KeysWithValue, KvFrom, KvTuple, LastInUnion, LeftWhitespace, Length, LowerAllCaps, LowerAlpha, MapTo, MaybeFalse, MaybeTrue, Model, Mutable, MutableProps, MutationFunction, MutationIdentity, Narrowable, NonAlpha, NonNumericKeys, NonStringKeys, Not, Numeric, NumericKeys, NumericString, ObjectType, Opaque, OpeningBracket, OptionalKeys, OptionalProps, Parenthesis, PascalCase, Pluralize, PrivateKey, PrivateKeys, PublicKeys, Punctuation, PureFluentApi, Replace, RequireProps, RequiredKeys, RequiredProps, Retain, RightWhitespace, RuleDefinition, RuntimeProp, RuntimeType, SameKeys, SecondOfEach, SimplifyObject, SnakeCase, SpecialCharacters, StringDelimiter, StringKeys, StringLength, ToFluent, Transformer, Trim, TrimLeft, TrimRight, TupleToUnion, Type, TypeApi, TypeCondition, TypeDefinition, TypeGuard, TypeOptions, UnionToIntersection, UnionToTuple, UniqueDictionary, UniqueForProp, Uniqueness, UpperAlpha, ValueTuple, ValueTypeFunc, ValueTypes, Where, WhereNot, Whitespace, Widen, WithNumericKeys, WithStringKeys, WithValue, ZipCode, and, api, arrayToKeyLookup, arrayToObject, condition, createFnWithProps, createMutationFunction, defineProperties, defineType, dictArr, dictToKv, dictionaryTransform, entries, equals, filterDictArray, fnWithProps, greater, groupBy, idLiteral, idTypeGuard, identity, ifTypeOf, isArray, isBoolean, isFalse, isFunction, isNull, isNumber, isObject, isString, isSymbol, isTrue, isType, isUndefined, keys, kindLiteral, kv, kvToDict, less, literal, mapTo, mapValues, nameLiteral, or, randomString, readonlyFnWithProps, ruleSet, strArrayToDict, type, typeApi, uuid, valueTypes, withValue };
|
|
1776
|
+
export { AllCaps, Alpha, AlphaNumeric, Api, ApiFunction, ApiValue, AppendToDictionary, AppendToObject, ArrConcat, Array$1 as Array, ArrayConverter, AssertExtends, Bracket, Break, CamelCase, CapitalizeWords, ClosingBracket, Condition, Configurator, Constructor, DashToSnake, DashUppercase, Dasherize, DefinePropertiesApi, DictArrApi, DictArray, DictArrayFilterCallback, DictArrayKv, DictChangeValue, DictFromKv, DictKvTuple, DictPartialApplication, DictPrependWithFn, DictReturnValues, DynamicRule, DynamicRuleSet, Email, EnumValues, ExpandRecursively, ExpectExtends, ExplicitFunction, ExtendsClause, ExtendsNarrowlyClause, Filter, FinalReturn, First, FirstKey, FirstKeyValue, FirstOfEach, FluentConfigurator, FluentFunction, FromDictArray, FunctionType, GeneralDictionary, Get, HasUppercase, IConfigurator, IFluentConfigurator, If, IfExtends, IfExtendsThen, Include, Includes, IsArray, IsBoolean, IsCapitalized, IsFalse, IsFunction, IsObject, IsString, IsTrue, KebabCase, KeyValue, KeyedRecord, Keys, KeysWithValue, KvFrom, KvTuple, LastInUnion, LeftWhitespace, Length, LowerAllCaps, LowerAlpha, MapTo, MapToWithFiltering, MaybeFalse, MaybeTrue, Model, Mutable, MutableProps, MutationFunction, MutationIdentity, Narrowable, NonAlpha, NonNumericKeys, NonStringKeys, Not, Numeric, NumericKeys, NumericString, ObjectType, Opaque, OpeningBracket, OptionalKeys, OptionalProps, Parenthesis, PascalCase, Pluralize, PrivateKey, PrivateKeys, PublicKeys, Punctuation, PureFluentApi, Replace, RequireProps, RequiredKeys, RequiredProps, Retain, RightWhitespace, RuleDefinition, RuntimeProp, RuntimeType, SameKeys, SecondOfEach, SimplifyObject, SnakeCase, SpecialCharacters, StringDelimiter, StringKeys, StringLength, ToFluent, Transformer, Trim, TrimLeft, TrimRight, TupleToUnion, Type, TypeApi, TypeCondition, TypeDefinition, TypeGuard, TypeOptions, UnionToIntersection, UnionToTuple, UniqueDictionary, UniqueForProp, Uniqueness, UpperAlpha, ValueTuple, ValueTypeFunc, ValueTypes, Where, WhereNot, Whitespace, Widen, WithNumericKeys, WithStringKeys, WithValue, ZipCode, and, api, arrayToKeyLookup, arrayToObject, condition, createFnWithProps, createMutationFunction, defineProperties, defineType, dictArr, dictToKv, dictionaryTransform, entries, equals, filterDictArray, fnWithProps, greater, groupBy, idLiteral, idTypeGuard, identity, ifTypeOf, isArray, isBoolean, isFalse, isFunction, isNull, isNumber, isObject, isString, isSymbol, isTrue, isType, isUndefined, keys, kindLiteral, kv, kvToDict, less, literal, mapTo, mapValues, nameLiteral, or, randomString, readonlyFnWithProps, ruleSet, strArrayToDict, type, typeApi, uuid, valueTypes, withValue };
|
package/dist/index.js
CHANGED
|
@@ -262,7 +262,14 @@ function mapValues(obj, valueMapper) {
|
|
|
262
262
|
}
|
|
263
263
|
|
|
264
264
|
// src/utility/dictionary/mapTo.ts
|
|
265
|
-
var mapTo = (cb) => (source) =>
|
|
265
|
+
var mapTo = (cb) => (source) => {
|
|
266
|
+
if (Array.isArray(source)) {
|
|
267
|
+
return source.flatMap((i) => cb(i)).filter((i) => i !== null);
|
|
268
|
+
} else {
|
|
269
|
+
const result = cb(source);
|
|
270
|
+
return result ? result : [];
|
|
271
|
+
}
|
|
272
|
+
};
|
|
266
273
|
|
|
267
274
|
// src/utility/dictionary/strArrayToDict.ts
|
|
268
275
|
function strArrayToDict(...strings) {
|
package/dist/index.mjs
CHANGED
|
@@ -179,7 +179,14 @@ function mapValues(obj, valueMapper) {
|
|
|
179
179
|
}
|
|
180
180
|
|
|
181
181
|
// src/utility/dictionary/mapTo.ts
|
|
182
|
-
var mapTo = (cb) => (source) =>
|
|
182
|
+
var mapTo = (cb) => (source) => {
|
|
183
|
+
if (Array.isArray(source)) {
|
|
184
|
+
return source.flatMap((i) => cb(i)).filter((i) => i !== null);
|
|
185
|
+
} else {
|
|
186
|
+
const result = cb(source);
|
|
187
|
+
return result ? result : [];
|
|
188
|
+
}
|
|
189
|
+
};
|
|
183
190
|
|
|
184
191
|
// src/utility/dictionary/strArrayToDict.ts
|
|
185
192
|
function strArrayToDict(...strings) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inferred-types",
|
|
3
|
-
"version": "0.24.
|
|
3
|
+
"version": "0.24.2",
|
|
4
4
|
"description": "Functions which provide useful type inference on TS projects",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Ken Snyder<ken@ken.net>",
|
|
@@ -32,19 +32,19 @@
|
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@type-challenges/utils": "~0.1.1",
|
|
35
|
-
"@types/node": "16",
|
|
36
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
37
|
-
"@typescript-eslint/parser": "^5.
|
|
35
|
+
"@types/node": "^16.18.0",
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^5.41.0",
|
|
37
|
+
"@typescript-eslint/parser": "^5.41.0",
|
|
38
38
|
"bumpp": "^8.2.1",
|
|
39
39
|
"common-types": "^1.31.1",
|
|
40
40
|
"cross-env": "^7.0.3",
|
|
41
41
|
"dd": "^0.24.0",
|
|
42
42
|
"dotenv": "^16.0.3",
|
|
43
|
-
"eslint": "^8.
|
|
43
|
+
"eslint": "^8.26.0",
|
|
44
44
|
"eslint-config-prettier": "^8.5.0",
|
|
45
45
|
"eslint-plugin-import": "^2.26.0",
|
|
46
46
|
"eslint-plugin-prettier": "^4.2.1",
|
|
47
|
-
"eslint-plugin-promise": "^6.1.
|
|
47
|
+
"eslint-plugin-promise": "^6.1.1",
|
|
48
48
|
"npm-run-all": "~4.1.5",
|
|
49
49
|
"prettier": "~2.7.1",
|
|
50
50
|
"rimraf": "^3.0.2",
|
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* **MapTo**
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* - you can either return a 1:1 or 1:M output as `O` or `O[]` when there is a mapping
|
|
4
|
+
* Maps from one type `I` to another `O[]`
|
|
6
5
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* ```ts
|
|
12
|
-
* const mapper: ModelMapper<{title: string}, {title: string, kind: string}> = i => {
|
|
13
|
-
* title: i.title,
|
|
14
|
-
* kind: "markdown"
|
|
15
|
-
* }
|
|
16
|
-
* ```
|
|
6
|
+
* **Note:** because the output is an array you can easily support `1:1` and `1:M` mappings
|
|
7
|
+
* but not a filtering operation (e.g., `1:0`); if you need this then use `MapToWithFiltering`
|
|
8
|
+
* instead.
|
|
17
9
|
*/
|
|
18
10
|
export type MapTo<I extends {}, O extends {}> = (i: I) => O[];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* **MapToWithFiltering**
|
|
14
|
+
*
|
|
15
|
+
* Maps from one type `I` to another `(O | null)[]`
|
|
16
|
+
*
|
|
17
|
+
* **Note:** because the output is an array you can easily support `1:1` and `1:M` mappings
|
|
18
|
+
* and the allowance of the conversion to result in a `null` value also means that filter
|
|
19
|
+
* out an input value entirely is possible. If you don't need _filtering_ then use the
|
|
20
|
+
* `MapTo` type instead.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export type MapToWithFiltering<I extends {}, O extends {}> = (i: I) => O[] | null;
|
|
@@ -94,7 +94,7 @@ export type RequiredProps<T extends object> = Pick<T, RequiredKeys<T>>;
|
|
|
94
94
|
*
|
|
95
95
|
* Reduces an object to only key/value pairs where the key is optional
|
|
96
96
|
*/
|
|
97
|
-
export type OptionalProps<T extends object> = Pick<T,
|
|
97
|
+
export type OptionalProps<T extends object> = Pick<T, OptionalKeys<T>>;
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
100
|
* **WithValue**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MapToWithFiltering } from "src/types/dictionary";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* **mapTo**
|
|
@@ -17,6 +17,12 @@ import { MapTo } from "src/types/dictionary";
|
|
|
17
17
|
* ```
|
|
18
18
|
*/
|
|
19
19
|
export const mapTo =
|
|
20
|
-
<I extends {}, O extends {}>(cb:
|
|
21
|
-
(source: I) =>
|
|
22
|
-
|
|
20
|
+
<I extends {}, O extends {}>(cb: MapToWithFiltering<I, O>) =>
|
|
21
|
+
(source: I | I[]) => {
|
|
22
|
+
if (Array.isArray(source)) {
|
|
23
|
+
return source.flatMap((i) => cb(i)).filter((i) => i !== null) as O[];
|
|
24
|
+
} else {
|
|
25
|
+
const result = cb(source);
|
|
26
|
+
return result ? result : ([] as O[]);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
@@ -6,6 +6,7 @@ import { mapTo } from "src/utility/dictionary/mapTo";
|
|
|
6
6
|
|
|
7
7
|
type I = { title: string; color: string; products: string[] };
|
|
8
8
|
const i: I = { title: "Test", color: "green", products: ["foo", "bar", "baz"] };
|
|
9
|
+
const i2: I = { title: "i2", color: "green", products: ["foo", "bar", "baz"] };
|
|
9
10
|
|
|
10
11
|
type O = { title: string; count: number; unnecessary?: number };
|
|
11
12
|
|
|
@@ -103,4 +104,21 @@ describe("mapTo() utility function", () => {
|
|
|
103
104
|
const cases: cases = [true];
|
|
104
105
|
expect(cases).toBe(cases);
|
|
105
106
|
});
|
|
107
|
+
|
|
108
|
+
it("test input filtering", () => {
|
|
109
|
+
const m = mapTo<I, O>((x) => {
|
|
110
|
+
return x.title === "i2"
|
|
111
|
+
? null
|
|
112
|
+
: [
|
|
113
|
+
{
|
|
114
|
+
title: i.title,
|
|
115
|
+
count: i.products.length,
|
|
116
|
+
},
|
|
117
|
+
];
|
|
118
|
+
});
|
|
119
|
+
const results = m([i, i2]);
|
|
120
|
+
|
|
121
|
+
expect(results).toHaveLength(1);
|
|
122
|
+
expect(results[0].title).toBe("Test");
|
|
123
|
+
});
|
|
106
124
|
});
|