inferred-types 0.31.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 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,9 +625,35 @@ 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 function isNull<T>(i: T): T extends null ? true : false;
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
 
643
+ declare type IsNumber<T> = T extends number ? true : false;
618
644
  declare function isNumber<T>(i: T): T extends number ? true : false;
645
+ /**
646
+ * **ifNumber**
647
+ *
648
+ * Strongly type-aware conditional statement which checks whether a value is
649
+ * a _number_ and returns one of two values (strongly typed) based on the evaluation
650
+ * of this criteria.
651
+ *
652
+ * @param val the value being tested
653
+ * @param ifVal the value (strongly typed) returned if val is number
654
+ * @param elseVal the value (strongly typed) returned if val is NOT a number
655
+ */
656
+ declare function ifNumber<T, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE): IsNumber<T> extends true ? IF : ELSE;
619
657
 
620
658
  declare type ObjectType = Not<Record<string, Narrowable>, FunctionType>;
621
659
  /**
@@ -628,6 +666,18 @@ declare function isObject<T extends unknown>(i: T): IsObject<T>;
628
666
 
629
667
  declare type IsString<T> = T extends string ? true : false;
630
668
  declare function isString<T>(i: T): IsString<T>;
669
+ /**
670
+ * **ifString**
671
+ *
672
+ * Strongly type-aware conditional statement which checks whether a value is
673
+ * a _string_ and returns one of two values (strongly typed) based on the evaluation
674
+ * of this criteria.
675
+ *
676
+ * @param val the value being tested
677
+ * @param ifVal the value (strongly typed) returned if val is _string_
678
+ * @param elseVal the value (strongly typed) returned if val is NOT a _string
679
+ */
680
+ declare function ifString<T, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE): IsString<T> extends true ? IF : ELSE;
631
681
 
632
682
  declare function isSymbol<T>(i: T): T extends symbol ? true : false;
633
683
 
@@ -649,6 +699,18 @@ declare type IsTrue<T> = IsBoolean<T> extends true ? T extends true ? true : T e
649
699
  * Run-time and type checking of whether a variable is `true`.
650
700
  */
651
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;
652
714
 
653
715
  declare function isUndefined<T>(i: T): undefined extends T ? true : false;
654
716
 
@@ -980,6 +1042,11 @@ declare type MapFn<I, O, C extends FinalizedMapConfig<OptRequired, MapCardinalit
980
1042
  * const mapped = m(inputs);
981
1043
  * const mappedOver = inputs.map(m);
982
1044
  * ```
1045
+ *
1046
+ * Note: the root of a `Mapper` is the mapper function but
1047
+ * this is combined with a dictionary of settings and types
1048
+ * which you can use. For instance, look at the `fnSignature`
1049
+ * property to get the _type_ signature of the map function.
983
1050
  */
984
1051
  declare type Mapper<I = unknown, O = unknown, C extends FinalizedMapConfig<OptRequired, MapCardinalityIllustrated, OptRequired> = FinalizedMapConfig<OptRequired, MapCardinalityIllustrated, OptRequired>> = {
985
1052
  input: MapIR<C>;
@@ -2304,4 +2371,4 @@ interface IFluentConfigurator<C> {
2304
2371
  */
2305
2372
  declare function FluentConfigurator<I>(initial?: I): IFluentConfigurator<{}>;
2306
2373
 
2307
- 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, 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, 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,6 +50,11 @@ __export(src_exports, {
50
50
  idLiteral: () => idLiteral,
51
51
  idTypeGuard: () => idTypeGuard,
52
52
  identity: () => identity,
53
+ ifBoolean: () => ifBoolean,
54
+ ifNull: () => ifNull,
55
+ ifNumber: () => ifNumber,
56
+ ifString: () => ifString,
57
+ ifTrue: () => ifTrue,
53
58
  ifTypeOf: () => ifTypeOf,
54
59
  isArray: () => isArray,
55
60
  isBoolean: () => isBoolean,
@@ -277,6 +282,9 @@ function isArray(i) {
277
282
  function isBoolean(i) {
278
283
  return typeof i === "boolean";
279
284
  }
285
+ function ifBoolean(val, ifVal, elseVal) {
286
+ return isBoolean(val) ? ifVal : elseVal;
287
+ }
280
288
 
281
289
  // src/utility/runtime/conditions/isFalse.ts
282
290
  function isFalse(i) {
@@ -292,11 +300,17 @@ function isFunction(input) {
292
300
  function isNull(i) {
293
301
  return i === null;
294
302
  }
303
+ function ifNull(val, ifVal, elseVal) {
304
+ return isNull(val) ? ifVal : elseVal;
305
+ }
295
306
 
296
307
  // src/utility/runtime/conditions/isNumber.ts
297
308
  function isNumber(i) {
298
309
  return typeof i === "number";
299
310
  }
311
+ function ifNumber(val, ifVal, elseVal) {
312
+ return isNumber(val) ? ifVal : elseVal;
313
+ }
300
314
 
301
315
  // src/utility/runtime/conditions/isObject.ts
302
316
  function isObject(i) {
@@ -307,6 +321,9 @@ function isObject(i) {
307
321
  function isString(i) {
308
322
  return typeof i === "string";
309
323
  }
324
+ function ifString(val, ifVal, elseVal) {
325
+ return isString(val) ? ifVal : elseVal;
326
+ }
310
327
 
311
328
  // src/utility/runtime/conditions/isSymbol.ts
312
329
  function isSymbol(i) {
@@ -315,7 +332,10 @@ function isSymbol(i) {
315
332
 
316
333
  // src/utility/runtime/conditions/isTrue.ts
317
334
  function isTrue(i) {
318
- 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;
319
339
  }
320
340
 
321
341
  // src/utility/runtime/conditions/isUndefined.ts
@@ -878,6 +898,11 @@ function Model(name) {
878
898
  idLiteral,
879
899
  idTypeGuard,
880
900
  identity,
901
+ ifBoolean,
902
+ ifNull,
903
+ ifNumber,
904
+ ifString,
905
+ ifTrue,
881
906
  ifTypeOf,
882
907
  isArray,
883
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,11 +204,17 @@ 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) {
207
213
  return typeof i === "number";
208
214
  }
215
+ function ifNumber(val, ifVal, elseVal) {
216
+ return isNumber(val) ? ifVal : elseVal;
217
+ }
209
218
 
210
219
  // src/utility/runtime/conditions/isObject.ts
211
220
  function isObject(i) {
@@ -216,6 +225,9 @@ function isObject(i) {
216
225
  function isString(i) {
217
226
  return typeof i === "string";
218
227
  }
228
+ function ifString(val, ifVal, elseVal) {
229
+ return isString(val) ? ifVal : elseVal;
230
+ }
219
231
 
220
232
  // src/utility/runtime/conditions/isSymbol.ts
221
233
  function isSymbol(i) {
@@ -224,7 +236,10 @@ function isSymbol(i) {
224
236
 
225
237
  // src/utility/runtime/conditions/isTrue.ts
226
238
  function isTrue(i) {
227
- 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;
228
243
  }
229
244
 
230
245
  // src/utility/runtime/conditions/isUndefined.ts
@@ -786,6 +801,11 @@ export {
786
801
  idLiteral,
787
802
  idTypeGuard,
788
803
  identity,
804
+ ifBoolean,
805
+ ifNull,
806
+ ifNumber,
807
+ ifString,
808
+ ifTrue,
789
809
  ifTypeOf,
790
810
  isArray,
791
811
  isBoolean,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inferred-types",
3
- "version": "0.31.0",
3
+ "version": "0.32.1",
4
4
  "description": "Functions which provide useful type inference on TS projects",
5
5
  "license": "MIT",
6
6
  "author": "Ken Snyder<ken@ken.net>",
@@ -372,6 +372,11 @@ export type MapFn<
372
372
  * const mapped = m(inputs);
373
373
  * const mappedOver = inputs.map(m);
374
374
  * ```
375
+ *
376
+ * Note: the root of a `Mapper` is the mapper function but
377
+ * this is combined with a dictionary of settings and types
378
+ * which you can use. For instance, look at the `fnSignature`
379
+ * property to get the _type_ signature of the map function.
375
380
  */
376
381
  export type Mapper<
377
382
  I = unknown,
@@ -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
- export function isNull<T>(i: T) {
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,3 +1,20 @@
1
+ export type IsNumber<T> = T extends number ? true : false;
2
+
1
3
  export function isNumber<T>(i: T) {
2
4
  return (typeof i === "number") as T extends number ? true : false;
3
5
  }
6
+
7
+ /**
8
+ * **ifNumber**
9
+ *
10
+ * Strongly type-aware conditional statement which checks whether a value is
11
+ * a _number_ and returns one of two values (strongly typed) based on the evaluation
12
+ * of this criteria.
13
+ *
14
+ * @param val the value being tested
15
+ * @param ifVal the value (strongly typed) returned if val is number
16
+ * @param elseVal the value (strongly typed) returned if val is NOT a number
17
+ */
18
+ export function ifNumber<T, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE) {
19
+ return (isNumber(val) ? ifVal : elseVal) as IsNumber<T> extends true ? IF : ELSE;
20
+ }
@@ -3,3 +3,18 @@ export type IsString<T> = T extends string ? true : false;
3
3
  export function isString<T>(i: T) {
4
4
  return (typeof i === "string") as IsString<T>;
5
5
  }
6
+
7
+ /**
8
+ * **ifString**
9
+ *
10
+ * Strongly type-aware conditional statement which checks whether a value is
11
+ * a _string_ and returns one of two values (strongly typed) based on the evaluation
12
+ * of this criteria.
13
+ *
14
+ * @param val the value being tested
15
+ * @param ifVal the value (strongly typed) returned if val is _string_
16
+ * @param elseVal the value (strongly typed) returned if val is NOT a _string
17
+ */
18
+ export function ifString<T, IF, ELSE>(val: T, ifVal: IF, elseVal: ELSE) {
19
+ return (isString(val) ? ifVal : elseVal) as IsString<T> extends true ? IF : ELSE;
20
+ }
@@ -1,4 +1,4 @@
1
- import { Narrowable } from "src/types/Narrowable";
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
  }
@@ -0,0 +1,52 @@
1
+ import { describe, it } from "vitest";
2
+
3
+ import type { Expect, Equal } from "@type-challenges/utils";
4
+ import { ifBoolean, ifNumber, ifString, ifTrue } from "src/utility";
5
+
6
+ describe("runtime if/is", () => {
7
+ it("ifString(v,i,e)", () => {
8
+ const t = ifString("foo", 42, false);
9
+ const f = ifString(-1, "yikes", 42);
10
+
11
+ type cases = [
12
+ Expect<Equal<typeof t, 42>>, //
13
+ Expect<Equal<typeof f, 42>> //
14
+ ];
15
+ const cases: cases = [true, true];
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
+ });
@@ -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);