inferred-types 0.32.0 → 0.33.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 +54 -5
- package/dist/index.js +25 -5
- package/dist/index.mjs +21 -5
- package/package.json +1 -1
- package/src/types/TypeInfo/IsUndefined.ts +3 -1
- package/src/utility/runtime/conditions/isBoolean.ts +15 -0
- package/src/utility/runtime/conditions/isNull.ts +24 -1
- package/src/utility/runtime/conditions/isString.ts +7 -1
- package/src/utility/runtime/conditions/isTrue.ts +17 -2
- package/src/utility/runtime/conditions/isUndefined.ts +23 -1
- package/tests/runtime/if-is.spec.ts +49 -1
- package/tests/runtime/type.spec.ts +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -365,7 +365,7 @@ declare type SimplifyObject<T extends {}> = ExpandRecursively<UnionToIntersectio
|
|
|
365
365
|
*
|
|
366
366
|
* Boolean type utility returns `true` if `T` is undefined; `false` otherwise
|
|
367
367
|
*/
|
|
368
|
-
declare type IsUndefined<T> = T extends undefined ? true : false;
|
|
368
|
+
declare type IsUndefined<T extends Narrowable> = T extends undefined ? true : false;
|
|
369
369
|
/**
|
|
370
370
|
* **IfUndefined**
|
|
371
371
|
*
|
|
@@ -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 extends Narrowable, ELSE extends Narrowable>(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;
|
|
@@ -652,7 +677,7 @@ declare function isString<T>(i: T): IsString<T>;
|
|
|
652
677
|
* @param ifVal the value (strongly typed) returned if val is _string_
|
|
653
678
|
* @param elseVal the value (strongly typed) returned if val is NOT a _string
|
|
654
679
|
*/
|
|
655
|
-
declare function ifString<T, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE): IsString<T> extends true ? IF : ELSE;
|
|
680
|
+
declare function ifString<T extends Narrowable, IF extends Narrowable, ELSE extends Narrowable>(val: T, ifVal: IF, elseVal: ELSE): IsString<T> extends true ? IF : ELSE;
|
|
656
681
|
|
|
657
682
|
declare function isSymbol<T>(i: T): T extends symbol ? true : false;
|
|
658
683
|
|
|
@@ -674,8 +699,32 @@ 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
|
-
declare function isUndefined<T>(i: T): undefined extends T ? true : false;
|
|
715
|
+
declare function isUndefined<T extends Narrowable>(i: T): undefined extends T ? true : false;
|
|
716
|
+
/**
|
|
717
|
+
* **ifUndefined**
|
|
718
|
+
*
|
|
719
|
+
* Strongly type-aware conditional statement which checks whether a value is
|
|
720
|
+
* _undefined_ and returns one of two values (strongly typed) based on the evaluation
|
|
721
|
+
* of this criteria.
|
|
722
|
+
*
|
|
723
|
+
* @param val the value being tested
|
|
724
|
+
* @param ifVal the value (strongly typed) returned if val is `undefined`
|
|
725
|
+
* @param elseVal the value (strongly typed) returned if val is NOT `undefined`
|
|
726
|
+
*/
|
|
727
|
+
declare function ifUndefined<T extends Narrowable, IF extends Narrowable, ELSE extends Narrowable>(val: T, ifVal: IF, elseVal: ELSE): IsUndefined<T> extends true ? IF : ELSE;
|
|
679
728
|
|
|
680
729
|
declare type Type<T extends any, V extends Function> = {
|
|
681
730
|
name: string;
|
|
@@ -2334,4 +2383,4 @@ interface IFluentConfigurator<C> {
|
|
|
2334
2383
|
*/
|
|
2335
2384
|
declare function FluentConfigurator<I>(initial?: I): IFluentConfigurator<{}>;
|
|
2336
2385
|
|
|
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 };
|
|
2386
|
+
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, ifUndefined, 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,9 +50,13 @@ __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,
|
|
59
|
+
ifUndefined: () => ifUndefined,
|
|
56
60
|
isArray: () => isArray,
|
|
57
61
|
isBoolean: () => isBoolean,
|
|
58
62
|
isFalse: () => isFalse,
|
|
@@ -279,6 +283,9 @@ function isArray(i) {
|
|
|
279
283
|
function isBoolean(i) {
|
|
280
284
|
return typeof i === "boolean";
|
|
281
285
|
}
|
|
286
|
+
function ifBoolean(val, ifVal, elseVal) {
|
|
287
|
+
return isBoolean(val) ? ifVal : elseVal;
|
|
288
|
+
}
|
|
282
289
|
|
|
283
290
|
// src/utility/runtime/conditions/isFalse.ts
|
|
284
291
|
function isFalse(i) {
|
|
@@ -294,6 +301,9 @@ function isFunction(input) {
|
|
|
294
301
|
function isNull(i) {
|
|
295
302
|
return i === null;
|
|
296
303
|
}
|
|
304
|
+
function ifNull(val, ifVal, elseVal) {
|
|
305
|
+
return isNull(val) ? ifVal : elseVal;
|
|
306
|
+
}
|
|
297
307
|
|
|
298
308
|
// src/utility/runtime/conditions/isNumber.ts
|
|
299
309
|
function isNumber(i) {
|
|
@@ -323,13 +333,19 @@ function isSymbol(i) {
|
|
|
323
333
|
|
|
324
334
|
// src/utility/runtime/conditions/isTrue.ts
|
|
325
335
|
function isTrue(i) {
|
|
326
|
-
return typeof i === "boolean" && i;
|
|
336
|
+
return typeof i === "boolean" && i === true;
|
|
337
|
+
}
|
|
338
|
+
function ifTrue(val, ifVal, elseVal) {
|
|
339
|
+
return isTrue(val) ? ifVal : elseVal;
|
|
327
340
|
}
|
|
328
341
|
|
|
329
342
|
// src/utility/runtime/conditions/isUndefined.ts
|
|
330
343
|
function isUndefined(i) {
|
|
331
344
|
return typeof i === "undefined";
|
|
332
345
|
}
|
|
346
|
+
function ifUndefined(val, ifVal, elseVal) {
|
|
347
|
+
return isUndefined(val) ? ifVal : elseVal;
|
|
348
|
+
}
|
|
333
349
|
|
|
334
350
|
// src/utility/runtime/type.ts
|
|
335
351
|
var typeApi = () => ({
|
|
@@ -496,10 +512,10 @@ var stringOps = (config, boolLogic) => {
|
|
|
496
512
|
const combined = boolLogic === "AND" ? (input) => conditions.every((c) => c(input)) : (input) => conditions.some((c) => c(input));
|
|
497
513
|
return combined;
|
|
498
514
|
};
|
|
499
|
-
var filterFn = (defn, logicCombinator,
|
|
515
|
+
var filterFn = (defn, logicCombinator, ifUndefined2 = "no-impact") => {
|
|
500
516
|
const config = isNotFilter(defn) ? defn["not"] : defn;
|
|
501
517
|
const filter2 = (input) => {
|
|
502
|
-
const undefValue =
|
|
518
|
+
const undefValue = ifUndefined2 === "no-impact" ? logicCombinator === "AND" ? true : false : ifUndefined2;
|
|
503
519
|
let flag;
|
|
504
520
|
if (typeof input === "undefined") {
|
|
505
521
|
flag = undefValue;
|
|
@@ -514,8 +530,8 @@ var filterFn = (defn, logicCombinator, ifUndefined = "no-impact") => {
|
|
|
514
530
|
};
|
|
515
531
|
return filter2;
|
|
516
532
|
};
|
|
517
|
-
var filter = (config, logicCombinator = "AND",
|
|
518
|
-
return filterFn(config, logicCombinator,
|
|
533
|
+
var filter = (config, logicCombinator = "AND", ifUndefined2 = "no-impact") => {
|
|
534
|
+
return filterFn(config, logicCombinator, ifUndefined2);
|
|
519
535
|
};
|
|
520
536
|
|
|
521
537
|
// src/utility/dictionary/arrayToKeyLookup.ts
|
|
@@ -886,9 +902,13 @@ function Model(name) {
|
|
|
886
902
|
idLiteral,
|
|
887
903
|
idTypeGuard,
|
|
888
904
|
identity,
|
|
905
|
+
ifBoolean,
|
|
906
|
+
ifNull,
|
|
889
907
|
ifNumber,
|
|
890
908
|
ifString,
|
|
909
|
+
ifTrue,
|
|
891
910
|
ifTypeOf,
|
|
911
|
+
ifUndefined,
|
|
892
912
|
isArray,
|
|
893
913
|
isBoolean,
|
|
894
914
|
isFalse,
|
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,13 +236,19 @@ 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
|
|
237
246
|
function isUndefined(i) {
|
|
238
247
|
return typeof i === "undefined";
|
|
239
248
|
}
|
|
249
|
+
function ifUndefined(val, ifVal, elseVal) {
|
|
250
|
+
return isUndefined(val) ? ifVal : elseVal;
|
|
251
|
+
}
|
|
240
252
|
|
|
241
253
|
// src/utility/runtime/type.ts
|
|
242
254
|
var typeApi = () => ({
|
|
@@ -403,10 +415,10 @@ var stringOps = (config, boolLogic) => {
|
|
|
403
415
|
const combined = boolLogic === "AND" ? (input) => conditions.every((c) => c(input)) : (input) => conditions.some((c) => c(input));
|
|
404
416
|
return combined;
|
|
405
417
|
};
|
|
406
|
-
var filterFn = (defn, logicCombinator,
|
|
418
|
+
var filterFn = (defn, logicCombinator, ifUndefined2 = "no-impact") => {
|
|
407
419
|
const config = isNotFilter(defn) ? defn["not"] : defn;
|
|
408
420
|
const filter2 = (input) => {
|
|
409
|
-
const undefValue =
|
|
421
|
+
const undefValue = ifUndefined2 === "no-impact" ? logicCombinator === "AND" ? true : false : ifUndefined2;
|
|
410
422
|
let flag;
|
|
411
423
|
if (typeof input === "undefined") {
|
|
412
424
|
flag = undefValue;
|
|
@@ -421,8 +433,8 @@ var filterFn = (defn, logicCombinator, ifUndefined = "no-impact") => {
|
|
|
421
433
|
};
|
|
422
434
|
return filter2;
|
|
423
435
|
};
|
|
424
|
-
var filter = (config, logicCombinator = "AND",
|
|
425
|
-
return filterFn(config, logicCombinator,
|
|
436
|
+
var filter = (config, logicCombinator = "AND", ifUndefined2 = "no-impact") => {
|
|
437
|
+
return filterFn(config, logicCombinator, ifUndefined2);
|
|
426
438
|
};
|
|
427
439
|
|
|
428
440
|
// src/utility/dictionary/arrayToKeyLookup.ts
|
|
@@ -792,9 +804,13 @@ export {
|
|
|
792
804
|
idLiteral,
|
|
793
805
|
idTypeGuard,
|
|
794
806
|
identity,
|
|
807
|
+
ifBoolean,
|
|
808
|
+
ifNull,
|
|
795
809
|
ifNumber,
|
|
796
810
|
ifString,
|
|
811
|
+
ifTrue,
|
|
797
812
|
ifTypeOf,
|
|
813
|
+
ifUndefined,
|
|
798
814
|
isArray,
|
|
799
815
|
isBoolean,
|
|
800
816
|
isFalse,
|
package/package.json
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { Narrowable } from "../Narrowable";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* **IsUndefined**
|
|
3
5
|
*
|
|
4
6
|
* Boolean type utility returns `true` if `T` is undefined; `false` otherwise
|
|
5
7
|
*/
|
|
6
|
-
export type IsUndefined<T> = T extends undefined ? true : false;
|
|
8
|
+
export type IsUndefined<T extends Narrowable> = T extends undefined ? true : false;
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* **IfUndefined**
|
|
@@ -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,26 @@
|
|
|
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 extends Narrowable, ELSE extends Narrowable>(
|
|
21
|
+
val: T,
|
|
22
|
+
ifVal: IF,
|
|
23
|
+
elseVal: ELSE
|
|
24
|
+
) {
|
|
25
|
+
return (isNull(val) ? ifVal : elseVal) as IsNull<T> extends true ? IF : ELSE;
|
|
26
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Narrowable } from "src/types";
|
|
2
|
+
|
|
1
3
|
export type IsString<T> = T extends string ? true : false;
|
|
2
4
|
|
|
3
5
|
export function isString<T>(i: T) {
|
|
@@ -15,6 +17,10 @@ export function isString<T>(i: T) {
|
|
|
15
17
|
* @param ifVal the value (strongly typed) returned if val is _string_
|
|
16
18
|
* @param elseVal the value (strongly typed) returned if val is NOT a _string
|
|
17
19
|
*/
|
|
18
|
-
export function ifString<T, IF
|
|
20
|
+
export function ifString<T extends Narrowable, IF extends Narrowable, ELSE extends Narrowable>(
|
|
21
|
+
val: T,
|
|
22
|
+
ifVal: IF,
|
|
23
|
+
elseVal: ELSE
|
|
24
|
+
) {
|
|
19
25
|
return (isString(val) ? ifVal : elseVal) as IsString<T> extends true ? IF : ELSE;
|
|
20
26
|
}
|
|
@@ -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,3 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
import { Narrowable } from "src/types";
|
|
2
|
+
import { IsUndefined } from "src/types/TypeInfo/IsUndefined";
|
|
3
|
+
|
|
4
|
+
export function isUndefined<T extends Narrowable>(i: T) {
|
|
2
5
|
return (typeof i === "undefined") as undefined extends T ? true : false;
|
|
3
6
|
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* **ifUndefined**
|
|
10
|
+
*
|
|
11
|
+
* Strongly type-aware conditional statement which checks whether a value is
|
|
12
|
+
* _undefined_ and returns one of two values (strongly typed) based on the evaluation
|
|
13
|
+
* of this criteria.
|
|
14
|
+
*
|
|
15
|
+
* @param val the value being tested
|
|
16
|
+
* @param ifVal the value (strongly typed) returned if val is `undefined`
|
|
17
|
+
* @param elseVal the value (strongly typed) returned if val is NOT `undefined`
|
|
18
|
+
*/
|
|
19
|
+
export function ifUndefined<T extends Narrowable, IF extends Narrowable, ELSE extends Narrowable>(
|
|
20
|
+
val: T,
|
|
21
|
+
ifVal: IF,
|
|
22
|
+
elseVal: ELSE
|
|
23
|
+
) {
|
|
24
|
+
return (isUndefined(val) ? ifVal : elseVal) as IsUndefined<T> extends true ? IF : ELSE;
|
|
25
|
+
}
|
|
@@ -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, ifUndefined } from "src/utility";
|
|
5
5
|
|
|
6
6
|
describe("runtime if/is", () => {
|
|
7
7
|
it("ifString(v,i,e)", () => {
|
|
@@ -14,4 +14,52 @@ 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
|
+
});
|
|
52
|
+
|
|
53
|
+
it("ifUndefined(v,i,e)", () => {
|
|
54
|
+
const t = ifUndefined(undefined, 42, false);
|
|
55
|
+
const f = ifUndefined(false, "yikes", 42);
|
|
56
|
+
const f2 = ifUndefined("", "yikes", 42);
|
|
57
|
+
|
|
58
|
+
type cases = [
|
|
59
|
+
Expect<Equal<typeof t, 42>>, //
|
|
60
|
+
Expect<Equal<typeof f, 42>>, //
|
|
61
|
+
Expect<Equal<typeof f2, 42>> //
|
|
62
|
+
];
|
|
63
|
+
const cases: cases = [true, true, true];
|
|
64
|
+
});
|
|
17
65
|
});
|
|
@@ -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);
|