inferred-types 0.32.0 → 0.32.1
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 +39 -2
- package/dist/index.js +16 -1
- package/dist/index.mjs +13 -1
- package/package.json +1 -1
- package/src/utility/runtime/conditions/isBoolean.ts +15 -0
- package/src/utility/runtime/conditions/isNull.ts +20 -1
- package/src/utility/runtime/conditions/isTrue.ts +17 -2
- package/tests/runtime/if-is.spec.ts +36 -1
- package/tests/runtime/type.spec.ts +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -594,6 +594,18 @@ declare type IsBoolean<T> = T extends boolean ? true : false;
|
|
|
594
594
|
* Runtime and type checks whether a variable is a boolean value.
|
|
595
595
|
*/
|
|
596
596
|
declare function isBoolean<T extends any>(i: T): IsBoolean<T>;
|
|
597
|
+
/**
|
|
598
|
+
* **ifBoolean**
|
|
599
|
+
*
|
|
600
|
+
* Strongly type-aware conditional statement which checks whether a value is
|
|
601
|
+
* a _boolean_ and returns one of two values (strongly typed) based on the evaluation
|
|
602
|
+
* of this criteria.
|
|
603
|
+
*
|
|
604
|
+
* @param val the value being tested
|
|
605
|
+
* @param ifVal the value (strongly typed) returned if val is _boolean_
|
|
606
|
+
* @param elseVal the value (strongly typed) returned if val is NOT a _boolean
|
|
607
|
+
*/
|
|
608
|
+
declare function ifBoolean<T, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE): IsBoolean<T> extends true ? IF : ELSE;
|
|
597
609
|
|
|
598
610
|
declare type IsFalse<T extends Narrowable> = IsBoolean<T> extends true ? T extends false ? true : true extends T ? false : unknown : false;
|
|
599
611
|
declare function isFalse<T>(i: T): IsFalse<T>;
|
|
@@ -613,7 +625,20 @@ declare type IsFunction<T> = T extends FunctionType ? true : false;
|
|
|
613
625
|
*/
|
|
614
626
|
declare function isFunction<T extends unknown>(input: T): IsFunction<T>;
|
|
615
627
|
|
|
616
|
-
declare
|
|
628
|
+
declare type IsNull<T> = T extends null ? true : false;
|
|
629
|
+
declare function isNull<T extends Narrowable>(i: T): T extends null ? true : false;
|
|
630
|
+
/**
|
|
631
|
+
* **ifNull**
|
|
632
|
+
*
|
|
633
|
+
* Strongly type-aware conditional statement which checks whether a value is
|
|
634
|
+
* Null and returns one of two values (strongly typed) based on the evaluation
|
|
635
|
+
* of this criteria.
|
|
636
|
+
*
|
|
637
|
+
* @param val the value being tested
|
|
638
|
+
* @param ifVal the value (strongly typed) returned if val is `null`
|
|
639
|
+
* @param elseVal the value (strongly typed) returned if val is NOT `null`
|
|
640
|
+
*/
|
|
641
|
+
declare function ifNull<T extends Narrowable, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE): IsNull<T> extends true ? IF : ELSE;
|
|
617
642
|
|
|
618
643
|
declare type IsNumber<T> = T extends number ? true : false;
|
|
619
644
|
declare function isNumber<T>(i: T): T extends number ? true : false;
|
|
@@ -674,6 +699,18 @@ declare type IsTrue<T> = IsBoolean<T> extends true ? T extends true ? true : T e
|
|
|
674
699
|
* Run-time and type checking of whether a variable is `true`.
|
|
675
700
|
*/
|
|
676
701
|
declare function isTrue<T extends Narrowable>(i: T): IsTrue<T>;
|
|
702
|
+
/**
|
|
703
|
+
* **ifTrue**
|
|
704
|
+
*
|
|
705
|
+
* Strongly type-aware conditional statement which checks whether a value is
|
|
706
|
+
* a _true_ and returns one of two values (strongly typed) based on the evaluation
|
|
707
|
+
* of this criteria.
|
|
708
|
+
*
|
|
709
|
+
* @param val the value being tested
|
|
710
|
+
* @param ifVal the value (strongly typed) returned if val is _true_ value
|
|
711
|
+
* @param elseVal the value (strongly typed) returned if val is NOT a _true_ value
|
|
712
|
+
*/
|
|
713
|
+
declare function ifTrue<T, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE): IsTrue<T> extends true ? IF : ELSE;
|
|
677
714
|
|
|
678
715
|
declare function isUndefined<T>(i: T): undefined extends T ? true : false;
|
|
679
716
|
|
|
@@ -2334,4 +2371,4 @@ interface IFluentConfigurator<C> {
|
|
|
2334
2371
|
*/
|
|
2335
2372
|
declare function FluentConfigurator<I>(initial?: I): IFluentConfigurator<{}>;
|
|
2336
2373
|
|
|
2337
|
-
export { AllCaps, Alpha, AlphaNumeric, Api, ApiFunction, ApiValue, AppendToDictionary, AppendToObject, ArrConcat, Array$1 as Array, ArrayConverter, AsArray, AsFinalizedConfig, AssertExtends, Bracket, Break, CamelCase, CapitalizeWords, Cardinality, Cardinality0, Cardinality1, CardinalityExplicit, CardinalityFilter0, CardinalityFilter1, CardinalityIn, CardinalityInput, CardinalityNode, CardinalityOut, CardinalityTuple, ClosingBracket, Condition, ConditionFilter, Configurator, ConfiguredMap, Constructor, DEFAULT_MANY_TO_ONE_MAPPING, DEFAULT_ONE_TO_MANY_MAPPING, DEFAULT_ONE_TO_ONE_MAPPING, DashToSnake, DashUppercase, Dasherize, DecomposeMapConfig, DefaultManyToOneMapping, DefaultOneToManyMapping, DefaultOneToOneMapping, DefinePropertiesApi, DictArrApi, DictArray, DictArrayFilterCallback, DictArrayKv, DictChangeValue, DictFromKv, DictKvTuple, DictPartialApplication, DictPrependWithFn, DictReturnValues, DynamicRule, DynamicRuleSet, Email, EnumValues, ExpandRecursively, ExpectExtends, ExplicitFunction, Extends, ExtendsClause, ExtendsNarrowlyClause, FilterContains, FilterDefn, FilterEnds, FilterEquals, FilterFn, FilterGreaterThan, FilterIs, FilterLessThan, FilterNotEqual, FilterStarts, FinalReturn, FinalizedMapConfig, First, FirstKey, FirstKeyValue, FirstOfEach, FluentConfigurator, FluentFunction, FromDictArray, FunctionType, GeneralDictionary, Get, HasUppercase, IConfigurator, IFluentConfigurator, If, IfBooleanLiteral, IfExtends, IfExtendsThen, IfLiteral, IfNumericLiteral, IfObject, IfOptionalLiteral, IfScalar, IfStringLiteral, Include, Includes, IntersectingKeys, IsArray, IsBoolean, IsBooleanLiteral, IsCapitalized, IsFalse, IsFunction, IsLiteral, IsNumber, IsNumericLiteral, IsObject, IsOptionalLiteral, IsScalar, IsString, IsStringLiteral, IsTrue, KebabCase, KeyValue, KeyedRecord, Keys, KeysWithValue, KvFrom, KvTuple, LastInUnion, LeftWhitespace, Length, LogicFunction, LogicalCombinator, LowerAllCaps, LowerAlpha, ManyToMany, ManyToOne, ManyToZero, MapCard, MapCardinality, MapCardinalityFrom, MapCardinalityIllustrated, MapConfig, MapFn, MapFnInput, MapFnOutput, MapIR, MapInput, MapInputFrom, MapOR, MapOutput, MapOutputFrom, MapTo, Mapper, MapperApi, MaybeFalse, MaybeTrue, Model, Mutable, MutableProps, MutationFunction, MutationIdentity, Narrowable, NonAlpha, NonNumericKeys, NonStringKeys, Not, NotFilter, Numeric, NumericFilter, NumericKeys, NumericString, ObjectType, OneToMany, OneToOne, OneToZero, Opaque, OpeningBracket, OptRequired, 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, TypeDefault, TypeDefinition, TypeGuard, TypeOptions, UndefinedArrayIsUnknown, UndefinedTreatment, UndefinedValue, UnionToIntersection, UnionToTuple, UniqueDictionary, UniqueForProp, Uniqueness, UnwrapNot, UpperAlpha, ValueTuple, ValueTypeFunc, ValueTypes, Where, WhereNot, Whitespace, Widen, WithNumericKeys, WithStringKeys, WithValue, ZeroToMany, ZeroToOne, ZeroToZero, ZipCode, and, api, arrayToKeyLookup, arrayToObject, asArray, condition, createFnWithProps, createMutationFunction, defineProperties, defineType, dictArr, dictToKv, dictionaryTransform, entries, filter, filterDictArray, fnWithProps, groupBy, idLiteral, idTypeGuard, identity, ifNumber, ifString, ifTypeOf, isArray, isBoolean, isFalse, isFunction, isNotFilter, isNull, isNumber, isNumericFilter, isObject, isString, isSymbol, isTrue, isType, isUndefined, keys, kindLiteral, kv, kvToDict, literal, mapTo, mapToDict, mapToFn, mapValues, nameLiteral, not, or, randomString, readonlyFnWithProps, ruleSet, strArrayToDict, type, typeApi, uuid, valueTypes, withValue };
|
|
2374
|
+
export { AllCaps, Alpha, AlphaNumeric, Api, ApiFunction, ApiValue, AppendToDictionary, AppendToObject, ArrConcat, Array$1 as Array, ArrayConverter, AsArray, AsFinalizedConfig, AssertExtends, Bracket, Break, CamelCase, CapitalizeWords, Cardinality, Cardinality0, Cardinality1, CardinalityExplicit, CardinalityFilter0, CardinalityFilter1, CardinalityIn, CardinalityInput, CardinalityNode, CardinalityOut, CardinalityTuple, ClosingBracket, Condition, ConditionFilter, Configurator, ConfiguredMap, Constructor, DEFAULT_MANY_TO_ONE_MAPPING, DEFAULT_ONE_TO_MANY_MAPPING, DEFAULT_ONE_TO_ONE_MAPPING, DashToSnake, DashUppercase, Dasherize, DecomposeMapConfig, DefaultManyToOneMapping, DefaultOneToManyMapping, DefaultOneToOneMapping, DefinePropertiesApi, DictArrApi, DictArray, DictArrayFilterCallback, DictArrayKv, DictChangeValue, DictFromKv, DictKvTuple, DictPartialApplication, DictPrependWithFn, DictReturnValues, DynamicRule, DynamicRuleSet, Email, EnumValues, ExpandRecursively, ExpectExtends, ExplicitFunction, Extends, ExtendsClause, ExtendsNarrowlyClause, FilterContains, FilterDefn, FilterEnds, FilterEquals, FilterFn, FilterGreaterThan, FilterIs, FilterLessThan, FilterNotEqual, FilterStarts, FinalReturn, FinalizedMapConfig, First, FirstKey, FirstKeyValue, FirstOfEach, FluentConfigurator, FluentFunction, FromDictArray, FunctionType, GeneralDictionary, Get, HasUppercase, IConfigurator, IFluentConfigurator, If, IfBooleanLiteral, IfExtends, IfExtendsThen, IfLiteral, IfNumericLiteral, IfObject, IfOptionalLiteral, IfScalar, IfStringLiteral, Include, Includes, IntersectingKeys, IsArray, IsBoolean, IsBooleanLiteral, IsCapitalized, IsFalse, IsFunction, IsLiteral, IsNull, IsNumber, IsNumericLiteral, IsObject, IsOptionalLiteral, IsScalar, IsString, IsStringLiteral, IsTrue, KebabCase, KeyValue, KeyedRecord, Keys, KeysWithValue, KvFrom, KvTuple, LastInUnion, LeftWhitespace, Length, LogicFunction, LogicalCombinator, LowerAllCaps, LowerAlpha, ManyToMany, ManyToOne, ManyToZero, MapCard, MapCardinality, MapCardinalityFrom, MapCardinalityIllustrated, MapConfig, MapFn, MapFnInput, MapFnOutput, MapIR, MapInput, MapInputFrom, MapOR, MapOutput, MapOutputFrom, MapTo, Mapper, MapperApi, MaybeFalse, MaybeTrue, Model, Mutable, MutableProps, MutationFunction, MutationIdentity, Narrowable, NonAlpha, NonNumericKeys, NonStringKeys, Not, NotFilter, Numeric, NumericFilter, NumericKeys, NumericString, ObjectType, OneToMany, OneToOne, OneToZero, Opaque, OpeningBracket, OptRequired, 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, TypeDefault, TypeDefinition, TypeGuard, TypeOptions, UndefinedArrayIsUnknown, UndefinedTreatment, UndefinedValue, UnionToIntersection, UnionToTuple, UniqueDictionary, UniqueForProp, Uniqueness, UnwrapNot, UpperAlpha, ValueTuple, ValueTypeFunc, ValueTypes, Where, WhereNot, Whitespace, Widen, WithNumericKeys, WithStringKeys, WithValue, ZeroToMany, ZeroToOne, ZeroToZero, ZipCode, and, api, arrayToKeyLookup, arrayToObject, asArray, condition, createFnWithProps, createMutationFunction, defineProperties, defineType, dictArr, dictToKv, dictionaryTransform, entries, filter, filterDictArray, fnWithProps, groupBy, idLiteral, idTypeGuard, identity, ifBoolean, ifNull, ifNumber, ifString, ifTrue, ifTypeOf, isArray, isBoolean, isFalse, isFunction, isNotFilter, isNull, isNumber, isNumericFilter, isObject, isString, isSymbol, isTrue, isType, isUndefined, keys, kindLiteral, kv, kvToDict, literal, mapTo, mapToDict, mapToFn, mapValues, nameLiteral, not, or, randomString, readonlyFnWithProps, ruleSet, strArrayToDict, type, typeApi, uuid, valueTypes, withValue };
|
package/dist/index.js
CHANGED
|
@@ -50,8 +50,11 @@ __export(src_exports, {
|
|
|
50
50
|
idLiteral: () => idLiteral,
|
|
51
51
|
idTypeGuard: () => idTypeGuard,
|
|
52
52
|
identity: () => identity,
|
|
53
|
+
ifBoolean: () => ifBoolean,
|
|
54
|
+
ifNull: () => ifNull,
|
|
53
55
|
ifNumber: () => ifNumber,
|
|
54
56
|
ifString: () => ifString,
|
|
57
|
+
ifTrue: () => ifTrue,
|
|
55
58
|
ifTypeOf: () => ifTypeOf,
|
|
56
59
|
isArray: () => isArray,
|
|
57
60
|
isBoolean: () => isBoolean,
|
|
@@ -279,6 +282,9 @@ function isArray(i) {
|
|
|
279
282
|
function isBoolean(i) {
|
|
280
283
|
return typeof i === "boolean";
|
|
281
284
|
}
|
|
285
|
+
function ifBoolean(val, ifVal, elseVal) {
|
|
286
|
+
return isBoolean(val) ? ifVal : elseVal;
|
|
287
|
+
}
|
|
282
288
|
|
|
283
289
|
// src/utility/runtime/conditions/isFalse.ts
|
|
284
290
|
function isFalse(i) {
|
|
@@ -294,6 +300,9 @@ function isFunction(input) {
|
|
|
294
300
|
function isNull(i) {
|
|
295
301
|
return i === null;
|
|
296
302
|
}
|
|
303
|
+
function ifNull(val, ifVal, elseVal) {
|
|
304
|
+
return isNull(val) ? ifVal : elseVal;
|
|
305
|
+
}
|
|
297
306
|
|
|
298
307
|
// src/utility/runtime/conditions/isNumber.ts
|
|
299
308
|
function isNumber(i) {
|
|
@@ -323,7 +332,10 @@ function isSymbol(i) {
|
|
|
323
332
|
|
|
324
333
|
// src/utility/runtime/conditions/isTrue.ts
|
|
325
334
|
function isTrue(i) {
|
|
326
|
-
return typeof i === "boolean" && i;
|
|
335
|
+
return typeof i === "boolean" && i === true;
|
|
336
|
+
}
|
|
337
|
+
function ifTrue(val, ifVal, elseVal) {
|
|
338
|
+
return isTrue(val) ? ifVal : elseVal;
|
|
327
339
|
}
|
|
328
340
|
|
|
329
341
|
// src/utility/runtime/conditions/isUndefined.ts
|
|
@@ -886,8 +898,11 @@ function Model(name) {
|
|
|
886
898
|
idLiteral,
|
|
887
899
|
idTypeGuard,
|
|
888
900
|
identity,
|
|
901
|
+
ifBoolean,
|
|
902
|
+
ifNull,
|
|
889
903
|
ifNumber,
|
|
890
904
|
ifString,
|
|
905
|
+
ifTrue,
|
|
891
906
|
ifTypeOf,
|
|
892
907
|
isArray,
|
|
893
908
|
isBoolean,
|
package/dist/index.mjs
CHANGED
|
@@ -186,6 +186,9 @@ function isArray(i) {
|
|
|
186
186
|
function isBoolean(i) {
|
|
187
187
|
return typeof i === "boolean";
|
|
188
188
|
}
|
|
189
|
+
function ifBoolean(val, ifVal, elseVal) {
|
|
190
|
+
return isBoolean(val) ? ifVal : elseVal;
|
|
191
|
+
}
|
|
189
192
|
|
|
190
193
|
// src/utility/runtime/conditions/isFalse.ts
|
|
191
194
|
function isFalse(i) {
|
|
@@ -201,6 +204,9 @@ function isFunction(input) {
|
|
|
201
204
|
function isNull(i) {
|
|
202
205
|
return i === null;
|
|
203
206
|
}
|
|
207
|
+
function ifNull(val, ifVal, elseVal) {
|
|
208
|
+
return isNull(val) ? ifVal : elseVal;
|
|
209
|
+
}
|
|
204
210
|
|
|
205
211
|
// src/utility/runtime/conditions/isNumber.ts
|
|
206
212
|
function isNumber(i) {
|
|
@@ -230,7 +236,10 @@ function isSymbol(i) {
|
|
|
230
236
|
|
|
231
237
|
// src/utility/runtime/conditions/isTrue.ts
|
|
232
238
|
function isTrue(i) {
|
|
233
|
-
return typeof i === "boolean" && i;
|
|
239
|
+
return typeof i === "boolean" && i === true;
|
|
240
|
+
}
|
|
241
|
+
function ifTrue(val, ifVal, elseVal) {
|
|
242
|
+
return isTrue(val) ? ifVal : elseVal;
|
|
234
243
|
}
|
|
235
244
|
|
|
236
245
|
// src/utility/runtime/conditions/isUndefined.ts
|
|
@@ -792,8 +801,11 @@ export {
|
|
|
792
801
|
idLiteral,
|
|
793
802
|
idTypeGuard,
|
|
794
803
|
identity,
|
|
804
|
+
ifBoolean,
|
|
805
|
+
ifNull,
|
|
795
806
|
ifNumber,
|
|
796
807
|
ifString,
|
|
808
|
+
ifTrue,
|
|
797
809
|
ifTypeOf,
|
|
798
810
|
isArray,
|
|
799
811
|
isBoolean,
|
package/package.json
CHANGED
|
@@ -6,3 +6,18 @@ export type IsBoolean<T> = T extends boolean ? true : false;
|
|
|
6
6
|
export function isBoolean<T extends any>(i: T) {
|
|
7
7
|
return (typeof i === "boolean") as IsBoolean<T>;
|
|
8
8
|
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* **ifBoolean**
|
|
12
|
+
*
|
|
13
|
+
* Strongly type-aware conditional statement which checks whether a value is
|
|
14
|
+
* a _boolean_ and returns one of two values (strongly typed) based on the evaluation
|
|
15
|
+
* of this criteria.
|
|
16
|
+
*
|
|
17
|
+
* @param val the value being tested
|
|
18
|
+
* @param ifVal the value (strongly typed) returned if val is _boolean_
|
|
19
|
+
* @param elseVal the value (strongly typed) returned if val is NOT a _boolean
|
|
20
|
+
*/
|
|
21
|
+
export function ifBoolean<T, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE) {
|
|
22
|
+
return (isBoolean(val) ? ifVal : elseVal) as IsBoolean<T> extends true ? IF : ELSE;
|
|
23
|
+
}
|
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
import { Narrowable } from "src/types";
|
|
2
|
+
|
|
3
|
+
export type IsNull<T> = T extends null ? true : false;
|
|
4
|
+
|
|
5
|
+
export function isNull<T extends Narrowable>(i: T) {
|
|
2
6
|
return (i === null) as T extends null ? true : false;
|
|
3
7
|
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* **ifNull**
|
|
11
|
+
*
|
|
12
|
+
* Strongly type-aware conditional statement which checks whether a value is
|
|
13
|
+
* Null and returns one of two values (strongly typed) based on the evaluation
|
|
14
|
+
* of this criteria.
|
|
15
|
+
*
|
|
16
|
+
* @param val the value being tested
|
|
17
|
+
* @param ifVal the value (strongly typed) returned if val is `null`
|
|
18
|
+
* @param elseVal the value (strongly typed) returned if val is NOT `null`
|
|
19
|
+
*/
|
|
20
|
+
export function ifNull<T extends Narrowable, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE) {
|
|
21
|
+
return (isNull(val) ? ifVal : elseVal) as IsNull<T> extends true ? IF : ELSE;
|
|
22
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Narrowable } from "src/types
|
|
1
|
+
import { Narrowable } from "src/types";
|
|
2
2
|
import type { IsBoolean } from "./isBoolean";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -28,5 +28,20 @@ export type IsTrue<T> = IsBoolean<T> extends true
|
|
|
28
28
|
* Run-time and type checking of whether a variable is `true`.
|
|
29
29
|
*/
|
|
30
30
|
export function isTrue<T extends Narrowable>(i: T) {
|
|
31
|
-
return (typeof i === "boolean" && i) as IsTrue<T>;
|
|
31
|
+
return (typeof i === "boolean" && i === true) as IsTrue<T>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* **ifTrue**
|
|
36
|
+
*
|
|
37
|
+
* Strongly type-aware conditional statement which checks whether a value is
|
|
38
|
+
* a _true_ and returns one of two values (strongly typed) based on the evaluation
|
|
39
|
+
* of this criteria.
|
|
40
|
+
*
|
|
41
|
+
* @param val the value being tested
|
|
42
|
+
* @param ifVal the value (strongly typed) returned if val is _true_ value
|
|
43
|
+
* @param elseVal the value (strongly typed) returned if val is NOT a _true_ value
|
|
44
|
+
*/
|
|
45
|
+
export function ifTrue<T, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE) {
|
|
46
|
+
return (isTrue(val) ? ifVal : elseVal) as IsTrue<T> extends true ? IF : ELSE;
|
|
32
47
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, it } from "vitest";
|
|
2
2
|
|
|
3
3
|
import type { Expect, Equal } from "@type-challenges/utils";
|
|
4
|
-
import { ifString } from "src/utility";
|
|
4
|
+
import { ifBoolean, ifNumber, ifString, ifTrue } from "src/utility";
|
|
5
5
|
|
|
6
6
|
describe("runtime if/is", () => {
|
|
7
7
|
it("ifString(v,i,e)", () => {
|
|
@@ -14,4 +14,39 @@ describe("runtime if/is", () => {
|
|
|
14
14
|
];
|
|
15
15
|
const cases: cases = [true, true];
|
|
16
16
|
});
|
|
17
|
+
|
|
18
|
+
it("ifNumber(v,i,e)", () => {
|
|
19
|
+
const t = ifNumber(42, 42, false);
|
|
20
|
+
const f = ifNumber("foo", "yikes", 42);
|
|
21
|
+
|
|
22
|
+
type cases = [
|
|
23
|
+
Expect<Equal<typeof t, 42>>, //
|
|
24
|
+
Expect<Equal<typeof f, 42>> //
|
|
25
|
+
];
|
|
26
|
+
const cases: cases = [true, true];
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("ifBoolean(v,i,e)", () => {
|
|
30
|
+
const t = ifBoolean(false, 42, false);
|
|
31
|
+
const f = ifBoolean(undefined, "yikes", 42);
|
|
32
|
+
|
|
33
|
+
type cases = [
|
|
34
|
+
Expect<Equal<typeof t, 42>>, //
|
|
35
|
+
Expect<Equal<typeof f, 42>> //
|
|
36
|
+
];
|
|
37
|
+
const cases: cases = [true, true];
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("ifTrue(v,i,e)", () => {
|
|
41
|
+
const t = ifTrue(true as true, 42, false);
|
|
42
|
+
const f = ifTrue(false, "yikes", 42);
|
|
43
|
+
const f2 = ifTrue(true as boolean, "yikes", 42);
|
|
44
|
+
|
|
45
|
+
type cases = [
|
|
46
|
+
Expect<Equal<typeof t, 42>>, //
|
|
47
|
+
Expect<Equal<typeof f, 42>>, //
|
|
48
|
+
Expect<Equal<typeof f2, 42>> //
|
|
49
|
+
];
|
|
50
|
+
const cases: cases = [true, true, true];
|
|
51
|
+
});
|
|
17
52
|
});
|
|
@@ -130,8 +130,8 @@ describe("testing type() utility and some pre-made conditions", () => {
|
|
|
130
130
|
|
|
131
131
|
expect(t.name).toBe("true");
|
|
132
132
|
|
|
133
|
-
const trueIsTrue = t.is(true);
|
|
134
|
-
const falseNotTrue = t.is(false);
|
|
133
|
+
const trueIsTrue = t.is(true as true);
|
|
134
|
+
const falseNotTrue = t.is(false as false);
|
|
135
135
|
const nadaNotTrue = t.is("nada");
|
|
136
136
|
const b = true as boolean;
|
|
137
137
|
const booleanUnknown = t.is(b);
|