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
package/dist/index.d.ts
CHANGED
|
@@ -165,12 +165,6 @@ declare type If<C extends boolean, T, F> = C extends T ? C | F : never;
|
|
|
165
165
|
*/
|
|
166
166
|
declare type Include<T, U, L extends boolean = false> = L extends true ? T extends U ? U extends T ? T : never : never : T extends U ? T : never;
|
|
167
167
|
|
|
168
|
-
/**
|
|
169
|
-
* A type takes the two arguments. The first is an array of string values, the second is the value
|
|
170
|
-
* which is being tested as it whether or not it's _included_ in the array. Result is true or false.
|
|
171
|
-
*/
|
|
172
|
-
declare type Includes<T extends readonly any[], U> = U extends T[number] ? true : false;
|
|
173
|
-
|
|
174
168
|
/**
|
|
175
169
|
* A "KeyedRecord" is intended to store a value without
|
|
176
170
|
* losing any ability to infer type information later.
|
|
@@ -1198,6 +1192,11 @@ declare type PureFluentApi<TApi extends Record<string, (...args: any[]) => PureF
|
|
|
1198
1192
|
*/
|
|
1199
1193
|
declare type FinalReturn<T extends any> = T extends (...args: any[]) => any ? FinalReturn<ReturnType<T>> : T;
|
|
1200
1194
|
|
|
1195
|
+
/**
|
|
1196
|
+
* A function which returns a boolean value
|
|
1197
|
+
*/
|
|
1198
|
+
declare type LogicFunction<T extends any[]> = (...args: T) => boolean;
|
|
1199
|
+
|
|
1201
1200
|
declare type DictFromKv<T extends readonly Readonly<{
|
|
1202
1201
|
key: string;
|
|
1203
1202
|
value: unknown;
|
|
@@ -1348,6 +1347,51 @@ declare type FromDictArray<T extends [string, Record<string, unknown>][]> = Expa
|
|
|
1348
1347
|
*/
|
|
1349
1348
|
declare type SecondOfEach<T extends any[][]> = T[number][1] extends T[number][number] ? T[number][1] : never;
|
|
1350
1349
|
|
|
1350
|
+
/**
|
|
1351
|
+
* **IsBooleanLiteral**
|
|
1352
|
+
*
|
|
1353
|
+
* Type utility which returns true/false if the boolean value is a _boolean literal_ versus
|
|
1354
|
+
* just the wider _boolean_ type.
|
|
1355
|
+
*/
|
|
1356
|
+
declare type IsBooleanLiteral<T extends boolean> = boolean extends T ? false : true;
|
|
1357
|
+
|
|
1358
|
+
/**
|
|
1359
|
+
* **IsStringLiteral**
|
|
1360
|
+
*
|
|
1361
|
+
* Type utility which returns true/false if the string a _string literal_ versus
|
|
1362
|
+
* just the _string_ type.
|
|
1363
|
+
*/
|
|
1364
|
+
declare type IsStringLiteral<T extends string> = string extends T ? false : true;
|
|
1365
|
+
|
|
1366
|
+
/**
|
|
1367
|
+
* **IsNumericLiteral**
|
|
1368
|
+
*
|
|
1369
|
+
* Type utility which returns true/false if the numeric value a _numeric literal_ versus
|
|
1370
|
+
* just the _number_ type.
|
|
1371
|
+
*/
|
|
1372
|
+
declare type IsNumericLiteral<T extends number> = number extends T ? false : true;
|
|
1373
|
+
|
|
1374
|
+
/**
|
|
1375
|
+
* **IsLiteral**
|
|
1376
|
+
*
|
|
1377
|
+
* Type utility which returns true/false if the value passed -- a form of a
|
|
1378
|
+
* string, number, or boolean -- is a _literal_ value of that type (true) or
|
|
1379
|
+
* the more generic wide type (false).
|
|
1380
|
+
*/
|
|
1381
|
+
declare type IsLiteral<T extends string | number | boolean> = [T] extends [string] ? IsStringLiteral<T> : [T] extends [boolean] ? IsBooleanLiteral<T> : [T] extends [number] ? IsNumericLiteral<T> : never;
|
|
1382
|
+
|
|
1383
|
+
/**
|
|
1384
|
+
* **Includes<TSource, TValue>**
|
|
1385
|
+
*
|
|
1386
|
+
* Type utility which returns `true` or `false` based on whether `TValue` is found
|
|
1387
|
+
* in `TSource`. Where `TSource` can be a string literal or an array of string literals.
|
|
1388
|
+
*
|
|
1389
|
+
* **Note:** if the source is a _wide_ type (aka, `string` or `string[]`) then there is
|
|
1390
|
+
* no way to know at design-time whether the value includes `TValue` and so it will return
|
|
1391
|
+
* a type of `boolean`.
|
|
1392
|
+
*/
|
|
1393
|
+
declare type Includes<TSource extends string | string[], TValue extends string> = TSource extends string[] ? IsStringLiteral<TupleToUnion<TSource>> extends true ? IsLiteral<TValue> extends true ? TValue extends TupleToUnion<TSource> ? true : false : boolean : boolean : TSource extends string ? IsLiteral<TSource> extends true ? IsLiteral<TValue> extends true ? TSource extends `${string}${TValue}${string}` ? true : false : boolean : boolean : boolean;
|
|
1394
|
+
|
|
1351
1395
|
declare function createFnWithProps<F extends Function, P extends {}>(fn: F, props: P): F & P;
|
|
1352
1396
|
/**
|
|
1353
1397
|
* Adds a dictionary of key/value pairs to a function.
|
|
@@ -1383,6 +1427,116 @@ declare function ruleSet<N extends Narrowable, TState extends Record<keyof TStat
|
|
|
1383
1427
|
|
|
1384
1428
|
declare const api: <N extends Narrowable, TPrivate extends Readonly<Record<any, N>>>(priv: TPrivate) => <TPublic extends object>(pub: TPublic) => () => TPublic;
|
|
1385
1429
|
|
|
1430
|
+
/**
|
|
1431
|
+
* Groups a number of "logic functions" together by combining their results using
|
|
1432
|
+
* the logical **AND** operator.
|
|
1433
|
+
*
|
|
1434
|
+
* **Note:** a "logic function" is any function which returns a boolean
|
|
1435
|
+
*/
|
|
1436
|
+
declare const and: <T extends any[]>(...ops: readonly LogicFunction<T>[]) => LogicFunction<T>;
|
|
1437
|
+
|
|
1438
|
+
/**
|
|
1439
|
+
* Groups a number of "logic functions" together by combining their results using
|
|
1440
|
+
* the logical **OR** operator.
|
|
1441
|
+
*
|
|
1442
|
+
* **Note:** a "logic function" is any function which returns a boolean
|
|
1443
|
+
*/
|
|
1444
|
+
declare const or: <T extends any[]>(...ops: LogicFunction<T>[]) => LogicFunction<T>;
|
|
1445
|
+
|
|
1446
|
+
/**
|
|
1447
|
+
* Groups a number of "logic functions" together by combining their results using
|
|
1448
|
+
* the logical **NOT** operator.
|
|
1449
|
+
*
|
|
1450
|
+
* **Note:** a "logic function" is any function which returns a boolean
|
|
1451
|
+
*/
|
|
1452
|
+
declare const not: <T extends any[]>(op: LogicFunction<T>) => LogicFunction<T>;
|
|
1453
|
+
|
|
1454
|
+
declare type FilterStarts = {
|
|
1455
|
+
/** one or more string which the value is allowed to start with */
|
|
1456
|
+
startsWith: string | string[];
|
|
1457
|
+
};
|
|
1458
|
+
declare type FilterIs = {
|
|
1459
|
+
/** whether a string _**is**_ of a particular value */
|
|
1460
|
+
is: string | string[];
|
|
1461
|
+
};
|
|
1462
|
+
declare type FilterEnds = {
|
|
1463
|
+
endsWith: string | string[];
|
|
1464
|
+
};
|
|
1465
|
+
declare type FilterContains = {
|
|
1466
|
+
/** whether any of the strings specified are _contained_ in the value */
|
|
1467
|
+
contains: string | string[];
|
|
1468
|
+
};
|
|
1469
|
+
declare type FilterEquals = {
|
|
1470
|
+
/** one or more values which _equal_ the value passed in */
|
|
1471
|
+
equals: number | number[];
|
|
1472
|
+
};
|
|
1473
|
+
declare type FilterNotEqual = {
|
|
1474
|
+
/** one or more values which ALL _do not equal_ the value passed in */
|
|
1475
|
+
notEqual: number | number[];
|
|
1476
|
+
};
|
|
1477
|
+
declare type FilterGreaterThan = {
|
|
1478
|
+
/** the incoming value is greater than this value */
|
|
1479
|
+
greaterThan: number;
|
|
1480
|
+
};
|
|
1481
|
+
declare type FilterLessThan = {
|
|
1482
|
+
/** the incoming value is less than this value */
|
|
1483
|
+
lessThan: number;
|
|
1484
|
+
};
|
|
1485
|
+
declare type StringFilter = FilterIs | FilterStarts | FilterEnds | FilterContains | (FilterStarts & FilterEnds) | (FilterStarts & FilterContains) | (FilterEnds & FilterContains) | (FilterStarts & FilterEnds & FilterContains);
|
|
1486
|
+
declare type NumericFilter = FilterEquals | FilterNotEqual | FilterGreaterThan | FilterLessThan | (FilterEquals & FilterNotEqual) | (FilterEquals & FilterGreaterThan) | (FilterEquals & FilterLessThan) | (FilterNotEqual & FilterGreaterThan) | (FilterNotEqual & FilterLessThan) | (FilterGreaterThan & FilterLessThan) | (FilterEquals & FilterGreaterThan & FilterLessThan) | (FilterNotEqual & FilterGreaterThan & FilterLessThan) | (FilterEquals & FilterNotEqual & FilterGreaterThan & FilterLessThan);
|
|
1487
|
+
declare type FilterDefn = StringFilter | NumericFilter;
|
|
1488
|
+
declare type NotFilter = {
|
|
1489
|
+
/**
|
|
1490
|
+
* **not**
|
|
1491
|
+
*
|
|
1492
|
+
* If you want to build a filter who's conditions being met results in _filtering_
|
|
1493
|
+
* the value rather than accepting it then choose this.
|
|
1494
|
+
*/
|
|
1495
|
+
not: FilterDefn;
|
|
1496
|
+
};
|
|
1497
|
+
declare function isNotFilter(f: FilterDefn | NotFilter): f is NotFilter;
|
|
1498
|
+
declare type UnwrapNot<T extends FilterDefn | NotFilter> = T extends NotFilter ? T["not"] extends StringFilter ? StringFilter : NumericFilter : T;
|
|
1499
|
+
declare function isNumericFilter(filter: FilterDefn): filter is NumericFilter;
|
|
1500
|
+
declare type UndefinedValue<U extends boolean> = true extends U ? "undefined treated as 'true'" : U extends "no-impact" ? "undefined treated in no-impact fashion" : "undefined treated as 'false'";
|
|
1501
|
+
declare type UndefinedTreatment = "undefined treated as 'true'" | "undefined treated as 'false'";
|
|
1502
|
+
declare type LogicalCombinator = "AND" | "OR";
|
|
1503
|
+
/**
|
|
1504
|
+
* **FilterFn**
|
|
1505
|
+
*
|
|
1506
|
+
* A filter function derived from the `filter()` configurator. This function is intended to provide a type-strong _filter_ function which can be used like so:
|
|
1507
|
+
* be used like:
|
|
1508
|
+
* ```ts
|
|
1509
|
+
* const onlyPrivate = filter({ startsWith: "_" });
|
|
1510
|
+
* const privateFiles = files.filter(onlyPrivate);
|
|
1511
|
+
* ```
|
|
1512
|
+
*
|
|
1513
|
+
*/
|
|
1514
|
+
declare type FilterFn<T extends StringFilter | NumericFilter> = T extends StringFilter ? <V extends string | undefined>(input: V) => boolean : <V extends number | undefined>(input: V) => boolean;
|
|
1515
|
+
/**
|
|
1516
|
+
* Defines a logical function for each condition type
|
|
1517
|
+
*/
|
|
1518
|
+
declare type ConditionFilter<T extends StringFilter | NumericFilter> = T extends StringFilter ? (input: string) => boolean : (input: number) => boolean;
|
|
1519
|
+
/**
|
|
1520
|
+
* **filter**
|
|
1521
|
+
*
|
|
1522
|
+
* A higher order helper utility which builds a boolean _filter_ function based on a simple
|
|
1523
|
+
* configuration object. Support either _string_ or _numeric_ filters.
|
|
1524
|
+
*
|
|
1525
|
+
* ```ts
|
|
1526
|
+
* const str = filter({startsWith: ["_", "."], endsWith: ".md"});
|
|
1527
|
+
* const num = filter({ greaterThan: 55, notEqual: [66, 77]});
|
|
1528
|
+
* ```
|
|
1529
|
+
*
|
|
1530
|
+
* All conditions (e.g., `startsWith`, `contains`, `notEqual`, etc.) that a particular filter
|
|
1531
|
+
* is defined as having -- if there is more than one -- will be logically combined using AND
|
|
1532
|
+
* unless specified otherwise in the third parameter.
|
|
1533
|
+
*
|
|
1534
|
+
* How a value of _undefined_ will be treated is stated in the second parameter but defaults
|
|
1535
|
+
* to "no-impact" which means it's `false` when the logicCombinator is OR but defaults
|
|
1536
|
+
* to `true` when the logicCombinator is AND.
|
|
1537
|
+
*/
|
|
1538
|
+
declare const filter: <F extends FilterDefn | NotFilter, U extends boolean | "no-impact", C extends LogicalCombinator>(config: F, logicCombinator?: C, ifUndefined?: U) => FilterFn<UnwrapNot<F>>;
|
|
1539
|
+
|
|
1386
1540
|
/**
|
|
1387
1541
|
* Passing in an array of strings, you are passed back a dictionary with
|
|
1388
1542
|
* all the keys being the strings and values set to `true`.
|
|
@@ -1553,6 +1707,24 @@ declare function kvToDict<K extends string, V extends Narrowable, T extends read
|
|
|
1553
1707
|
value: V;
|
|
1554
1708
|
}>[]>(kvArr: T): DictFromKv<T>;
|
|
1555
1709
|
|
|
1710
|
+
/**
|
|
1711
|
+
* Type utility which converts `undefined[]` to `unknown[]`
|
|
1712
|
+
*/
|
|
1713
|
+
declare type UndefinedArrayIsUnknown<T extends any[]> = undefined[] extends T ? unknown[] : T;
|
|
1714
|
+
declare type AsArray<T, W extends boolean = false> = T extends any[] ? W extends true ? Widen<T> : T : W extends true ? UndefinedArrayIsUnknown<Widen<T>[]> : UndefinedArrayIsUnknown<T[]>;
|
|
1715
|
+
/**
|
|
1716
|
+
* Ensures that any input passed in is passed back as an array:
|
|
1717
|
+
*
|
|
1718
|
+
* - if it was already an array than this just serves as an _identity_ function
|
|
1719
|
+
* - if it was not then it wraps the element into a one element array of the
|
|
1720
|
+
* given type
|
|
1721
|
+
*
|
|
1722
|
+
* Note: by default the _type_ of values will be intentionally widened so that the value "abc"
|
|
1723
|
+
* is of type `string` not the literal `abc`. If you want to keep literal types then
|
|
1724
|
+
* change the optional _widen_ parameter to _false_.
|
|
1725
|
+
*/
|
|
1726
|
+
declare const asArray: <T extends Narrowable, W extends boolean = true>(thing: T, _widen?: W | undefined) => AsArray<T, W>;
|
|
1727
|
+
|
|
1556
1728
|
/**
|
|
1557
1729
|
* Groups an array of data based on the value of a property
|
|
1558
1730
|
* in the objects within the array.
|
|
@@ -1668,33 +1840,6 @@ declare function idTypeGuard<T extends {
|
|
|
1668
1840
|
*/
|
|
1669
1841
|
declare function literal<N extends Narrowable, T extends Record<keyof T, N>>(obj: T): T;
|
|
1670
1842
|
|
|
1671
|
-
declare type Filter<A> = {
|
|
1672
|
-
kind: "Equals";
|
|
1673
|
-
field: keyof A;
|
|
1674
|
-
val: A[keyof A];
|
|
1675
|
-
} | {
|
|
1676
|
-
kind: "Greater";
|
|
1677
|
-
field: keyof A;
|
|
1678
|
-
val: A[keyof A];
|
|
1679
|
-
} | {
|
|
1680
|
-
kind: "Less";
|
|
1681
|
-
field: keyof A;
|
|
1682
|
-
val: A[keyof A];
|
|
1683
|
-
} | {
|
|
1684
|
-
kind: "And";
|
|
1685
|
-
a: Filter<A>;
|
|
1686
|
-
b: Filter<A>;
|
|
1687
|
-
} | {
|
|
1688
|
-
kind: "Or";
|
|
1689
|
-
a: Filter<A>;
|
|
1690
|
-
b: Filter<A>;
|
|
1691
|
-
};
|
|
1692
|
-
declare const equals: <A, K extends keyof A>(field: K, val: A[K]) => Filter<A>;
|
|
1693
|
-
declare const greater: <A, K extends keyof A>(field: K, val: A[K]) => Filter<A>;
|
|
1694
|
-
declare const less: <A, K extends keyof A>(field: K, val: A[K]) => Filter<A>;
|
|
1695
|
-
declare const and: <A>(a: Filter<A>, b: Filter<A>) => Filter<A>;
|
|
1696
|
-
declare const or: <A>(a: Filter<A>, b: Filter<A>) => Filter<A>;
|
|
1697
|
-
|
|
1698
1843
|
declare function Model<N extends string, _R extends {} = {}, _O extends {} = {}>(name: N): {
|
|
1699
1844
|
required<P extends string>(_prop: P): {
|
|
1700
1845
|
required<P_1 extends string>(_prop: P_1): any;
|
|
@@ -1782,4 +1927,4 @@ interface IFluentConfigurator<C> {
|
|
|
1782
1927
|
*/
|
|
1783
1928
|
declare function FluentConfigurator<I>(initial?: I): IFluentConfigurator<{}>;
|
|
1784
1929
|
|
|
1785
|
-
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,
|
|
1930
|
+
export { AllCaps, Alpha, AlphaNumeric, Api, ApiFunction, ApiValue, AppendToDictionary, AppendToObject, ArrConcat, Array$1 as Array, ArrayConverter, AsArray, AssertExtends, Bracket, Break, CamelCase, CapitalizeWords, ClosingBracket, Condition, ConditionFilter, 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, FilterContains, FilterDefn, FilterEnds, FilterEquals, FilterFn, FilterGreaterThan, FilterIs, FilterLessThan, FilterNotEqual, FilterStarts, FinalReturn, First, FirstKey, FirstKeyValue, FirstOfEach, FluentConfigurator, FluentFunction, FromDictArray, FunctionType, GeneralDictionary, Get, HasUppercase, IConfigurator, IFluentConfigurator, If, IfExtends, IfExtendsThen, Include, Includes, IsArray, IsBoolean, IsBooleanLiteral, IsCapitalized, IsFalse, IsFunction, IsLiteral, IsNumericLiteral, IsObject, IsString, IsStringLiteral, IsTrue, KebabCase, KeyValue, KeyedRecord, Keys, KeysWithValue, KvFrom, KvTuple, LastInUnion, LeftWhitespace, Length, LogicFunction, LogicalCombinator, LowerAllCaps, LowerAlpha, MapTo, MapToWithFiltering, MaybeFalse, MaybeTrue, Model, Mutable, MutableProps, MutationFunction, MutationIdentity, Narrowable, NonAlpha, NonNumericKeys, NonStringKeys, Not, NotFilter, Numeric, NumericFilter, 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, StringFilter, StringKeys, StringLength, ToFluent, Transformer, Trim, TrimLeft, TrimRight, TupleToUnion, Type, TypeApi, TypeCondition, TypeDefinition, TypeGuard, TypeOptions, UndefinedArrayIsUnknown, UndefinedTreatment, UndefinedValue, UnionToIntersection, UnionToTuple, UniqueDictionary, UniqueForProp, Uniqueness, UnwrapNot, UpperAlpha, ValueTuple, ValueTypeFunc, ValueTypes, Where, WhereNot, Whitespace, Widen, WithNumericKeys, WithStringKeys, WithValue, ZipCode, and, api, arrayToKeyLookup, arrayToObject, asArray, condition, createFnWithProps, createMutationFunction, defineProperties, defineType, dictArr, dictToKv, dictionaryTransform, entries, filter, filterDictArray, fnWithProps, groupBy, idLiteral, idTypeGuard, identity, ifTypeOf, isArray, isBoolean, isFalse, isFunction, isNotFilter, isNull, isNumber, isNumericFilter, isObject, isString, isSymbol, isTrue, isType, isUndefined, keys, kindLiteral, kv, kvToDict, literal, mapTo, mapValues, nameLiteral, not, or, randomString, readonlyFnWithProps, ruleSet, strArrayToDict, type, typeApi, uuid, valueTypes, withValue };
|