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.mjs CHANGED
@@ -84,6 +84,333 @@ var api = (priv) => (pub) => {
84
84
  return surface;
85
85
  };
86
86
 
87
+ // src/utility/boolean-logic/and.ts
88
+ var and = (...ops) => {
89
+ const fn = (...args) => {
90
+ return [...ops].every((i) => i(...args));
91
+ };
92
+ return fn;
93
+ };
94
+
95
+ // src/utility/boolean-logic/or.ts
96
+ var or = (...ops) => {
97
+ const fn = (...args) => {
98
+ return [...ops].some((i) => i(...args));
99
+ };
100
+ return fn;
101
+ };
102
+
103
+ // src/utility/boolean-logic/not.ts
104
+ var not = (op) => {
105
+ const fn = (...args) => {
106
+ return !op(...args);
107
+ };
108
+ return fn;
109
+ };
110
+
111
+ // src/utility/runtime/condition.ts
112
+ var condition = (c, input) => {
113
+ return c(input);
114
+ };
115
+
116
+ // src/utility/runtime/ifTypeOf.ts
117
+ function runtimeExtendsCheck(val, base, narrow = false) {
118
+ if (typeof val !== typeof base) {
119
+ return false;
120
+ }
121
+ switch (typeof val) {
122
+ case "boolean":
123
+ case "string":
124
+ case "number":
125
+ case "symbol":
126
+ case "bigint":
127
+ return narrow ? val === base : true;
128
+ case "undefined":
129
+ return true;
130
+ case "function":
131
+ if (narrow) {
132
+ throw new Error(`Use of narrowlyExtends with a function is not possible!`);
133
+ }
134
+ return true;
135
+ case "object":
136
+ if (val === null && base === null) {
137
+ return true;
138
+ } else {
139
+ return keys(base).every(
140
+ (i) => runtimeExtendsCheck(val[i], base[i], narrow)
141
+ );
142
+ }
143
+ }
144
+ }
145
+ var ifTypeOf = (val) => ({
146
+ extends: (base) => {
147
+ const valid = runtimeExtendsCheck(val, base, false);
148
+ const trueFalse = valid ? true : false;
149
+ return {
150
+ then: (then) => ({
151
+ else: (elseVal) => {
152
+ return valid ? typeof then === "undefined" ? val : then : elseVal;
153
+ }
154
+ }),
155
+ else: (elseVal) => valid ? val : elseVal
156
+ } && trueFalse;
157
+ },
158
+ narrowlyExtends: (base) => {
159
+ const valid = runtimeExtendsCheck(val, base, true);
160
+ const trueFalse = valid ? true : false;
161
+ return {
162
+ then: (then) => ({
163
+ else: (elseVal) => {
164
+ return valid ? typeof then === "undefined" ? val : then : elseVal;
165
+ }
166
+ }),
167
+ else: (elseVal) => valid ? val : elseVal
168
+ } && trueFalse;
169
+ }
170
+ });
171
+
172
+ // src/utility/runtime/conditions/isArray.ts
173
+ function isArray(i) {
174
+ return Array.isArray(i) === true;
175
+ }
176
+
177
+ // src/utility/runtime/conditions/isBoolean.ts
178
+ function isBoolean(i) {
179
+ return typeof i === "boolean";
180
+ }
181
+
182
+ // src/utility/runtime/conditions/isFalse.ts
183
+ function isFalse(i) {
184
+ return typeof i === "boolean" && !i;
185
+ }
186
+
187
+ // src/utility/runtime/conditions/isFunction.ts
188
+ function isFunction(input) {
189
+ return typeof input === "function";
190
+ }
191
+
192
+ // src/utility/runtime/conditions/isNull.ts
193
+ function isNull(i) {
194
+ return i === null;
195
+ }
196
+
197
+ // src/utility/runtime/conditions/isNumber.ts
198
+ function isNumber(i) {
199
+ return typeof i === "number";
200
+ }
201
+
202
+ // src/utility/runtime/conditions/isObject.ts
203
+ function isObject(i) {
204
+ return typeof i === "object" && i !== null && Array.isArray(i) === false;
205
+ }
206
+
207
+ // src/utility/runtime/conditions/isString.ts
208
+ function isString(i) {
209
+ return typeof i === "string";
210
+ }
211
+
212
+ // src/utility/runtime/conditions/isSymbol.ts
213
+ function isSymbol(i) {
214
+ return typeof i === "symbol";
215
+ }
216
+
217
+ // src/utility/runtime/conditions/isTrue.ts
218
+ function isTrue(i) {
219
+ return typeof i === "boolean" && i;
220
+ }
221
+
222
+ // src/utility/runtime/conditions/isUndefined.ts
223
+ function isUndefined(i) {
224
+ return typeof i === "undefined";
225
+ }
226
+
227
+ // src/utility/runtime/type.ts
228
+ var typeApi = () => ({
229
+ string: {
230
+ name: "string",
231
+ type: "",
232
+ typeGuard: (v) => isString(v),
233
+ is: isString
234
+ },
235
+ boolean: {
236
+ name: "boolean",
237
+ type: true,
238
+ typeGuard: (v) => isBoolean(v),
239
+ is: isBoolean
240
+ },
241
+ number: {
242
+ name: "number",
243
+ type: 1,
244
+ typeGuard: (v) => isNumber(v),
245
+ is: isNumber
246
+ },
247
+ function: {
248
+ name: "function",
249
+ type: Function,
250
+ typeGuard: (v) => isFunction(v),
251
+ is: isFunction
252
+ },
253
+ null: {
254
+ name: "null",
255
+ type: null,
256
+ typeGuard: (v) => isNull(v),
257
+ is: isNull
258
+ },
259
+ symbol: {
260
+ name: "symbol",
261
+ type: Symbol(),
262
+ typeGuard: (v) => isSymbol(v),
263
+ is: isSymbol
264
+ },
265
+ undefined: {
266
+ name: "undefined",
267
+ type: void 0,
268
+ typeGuard: (v) => isUndefined(v),
269
+ is: isUndefined
270
+ },
271
+ true: {
272
+ name: "true",
273
+ type: true,
274
+ typeGuard: (v) => isTrue(v),
275
+ is: isTrue
276
+ },
277
+ false: {
278
+ name: "false",
279
+ type: false,
280
+ typeGuard: (v) => isFalse(v),
281
+ is: isFalse
282
+ },
283
+ object: {
284
+ name: "object",
285
+ type: {},
286
+ typeGuard: (v) => isObject(v),
287
+ is: isObject
288
+ },
289
+ array: {
290
+ name: "array",
291
+ type: {},
292
+ typeGuard: (v) => isArray(v),
293
+ is: isObject
294
+ }
295
+ });
296
+ function isType(t) {
297
+ return typeof t === "object" && ["name", "type", "is"].every((i) => Object.keys(t).includes(i));
298
+ }
299
+ function type(fn) {
300
+ const result = fn(typeApi());
301
+ if (!isType(result)) {
302
+ throw new Error(
303
+ `When using type(), the callback passed in returned an invalid type! Value returned was: ${result}`
304
+ );
305
+ }
306
+ return result;
307
+ }
308
+
309
+ // src/utility/dictionary/entries.ts
310
+ function entries(obj) {
311
+ const iterable = {
312
+ *[Symbol.iterator]() {
313
+ for (const k of keys(obj)) {
314
+ yield [k, obj[k]];
315
+ }
316
+ }
317
+ };
318
+ return iterable;
319
+ }
320
+
321
+ // src/utility/runtime/withValue.ts
322
+ function withValue(td) {
323
+ return (obj) => {
324
+ const t = type(td);
325
+ return Object.fromEntries(
326
+ [...entries(obj)].filter(([_key, value]) => {
327
+ return t.typeGuard(value);
328
+ })
329
+ );
330
+ };
331
+ }
332
+
333
+ // src/utility/lists/asArray.ts
334
+ var asArray = (thing, _widen) => {
335
+ return isArray(thing) ? thing : typeof thing === "undefined" ? [] : [thing];
336
+ };
337
+
338
+ // src/utility/boolean-logic/filter.ts
339
+ function isNotFilter(f) {
340
+ return typeof f === "object" && "not" in f;
341
+ }
342
+ function isNumericFilter(filter2) {
343
+ return "equals" in filter2 || "notEqual" in filter2 || "greaterThan" in filter2 || "lessThan" in filter2 ? true : false;
344
+ }
345
+ var numericOps = (config, boolLogic) => {
346
+ const equals = (n) => {
347
+ const f = asArray(n);
348
+ return f.length === 0 ? [] : [(input) => f.some((i) => i === input)];
349
+ };
350
+ const notEqual = (n) => {
351
+ const f = asArray(n);
352
+ return f.length === 0 ? [] : [(input) => f.every((i) => i !== input)];
353
+ };
354
+ const greaterThan = (n) => {
355
+ const val = [(input) => input !== void 0 && input > n];
356
+ return val;
357
+ };
358
+ const lessThan = (n) => {
359
+ const val = [(input) => input !== void 0 && input < n];
360
+ return val;
361
+ };
362
+ const conditions = [
363
+ ..."equals" in config ? equals(config.equals) : [],
364
+ ..."notEqual" in config ? notEqual(config.notEqual) : [],
365
+ ..."greaterThan" in config ? greaterThan(config.greaterThan) : [],
366
+ ..."lessThan" in config ? lessThan(config.lessThan) : []
367
+ ];
368
+ const combined = boolLogic === "AND" ? (input) => conditions.every((c) => c(input)) : (input) => conditions.some((c) => c(input));
369
+ return combined;
370
+ };
371
+ var stringOps = (config, boolLogic) => {
372
+ const startsWith = (n) => {
373
+ const f = asArray(n);
374
+ return f.length === 0 ? [] : [(input) => f.some((i) => input == null ? void 0 : input.startsWith(i))];
375
+ };
376
+ const endsWith = (n) => {
377
+ const f = asArray(n);
378
+ return f.length === 0 ? [] : [(input) => f.some((i) => input == null ? void 0 : input.endsWith(i))];
379
+ };
380
+ const contains = (n) => {
381
+ const f = asArray(n);
382
+ return f.length === 0 ? [] : [(input) => f.some((i) => input == null ? void 0 : input.includes(i))];
383
+ };
384
+ const conditions = [
385
+ ..."startsWith" in config ? startsWith(config.startsWith) : [],
386
+ ..."endsWith" in config ? endsWith(config.endsWith) : [],
387
+ ..."contains" in config ? contains(config.contains) : []
388
+ ];
389
+ const combined = boolLogic === "AND" ? (input) => conditions.every((c) => c(input)) : (input) => conditions.some((c) => c(input));
390
+ return combined;
391
+ };
392
+ var filterFn = (defn, logicCombinator, ifUndefined = "no-impact") => {
393
+ const config = isNotFilter(defn) ? defn["not"] : defn;
394
+ const filter2 = (input) => {
395
+ const undefValue = ifUndefined === "no-impact" ? logicCombinator === "AND" ? true : false : ifUndefined;
396
+ let flag;
397
+ if (typeof input === "undefined") {
398
+ flag = undefValue;
399
+ } else if (isNumericFilter(config)) {
400
+ const fn = numericOps(config, logicCombinator);
401
+ flag = isNotFilter(defn) ? !fn(input) : fn(input);
402
+ } else {
403
+ const fn = stringOps(config, logicCombinator);
404
+ flag = isNotFilter(defn) ? !fn(input) : fn(input);
405
+ }
406
+ return flag;
407
+ };
408
+ return filter2;
409
+ };
410
+ var filter = (config, logicCombinator = "AND", ifUndefined = "no-impact") => {
411
+ return filterFn(config, logicCombinator, ifUndefined);
412
+ };
413
+
87
414
  // src/utility/dictionary/arrayToKeyLookup.ts
88
415
  function arrayToKeyLookup(...keys2) {
89
416
  const obj = {};
@@ -159,18 +486,6 @@ var dictArr = (...dicts) => {
159
486
  return api2;
160
487
  };
161
488
 
162
- // src/utility/dictionary/entries.ts
163
- function entries(obj) {
164
- const iterable = {
165
- *[Symbol.iterator]() {
166
- for (const k of keys(obj)) {
167
- yield [k, obj[k]];
168
- }
169
- }
170
- };
171
- return iterable;
172
- }
173
-
174
489
  // src/utility/dictionary/mapValues.ts
175
490
  function mapValues(obj, valueMapper) {
176
491
  return Object.fromEntries(
@@ -332,33 +647,6 @@ function literal(obj) {
332
647
  return obj;
333
648
  }
334
649
 
335
- // src/utility/map-reduce/filter.ts
336
- var equals = (field, val) => ({
337
- kind: "Equals",
338
- field,
339
- val
340
- });
341
- var greater = (field, val) => ({
342
- kind: "Greater",
343
- field,
344
- val
345
- });
346
- var less = (field, val) => ({
347
- kind: "Less",
348
- field,
349
- val
350
- });
351
- var and = (a, b) => ({
352
- kind: "And",
353
- a,
354
- b
355
- });
356
- var or = (a, b) => ({
357
- kind: "Or",
358
- a,
359
- b
360
- });
361
-
362
650
  // src/utility/modelling/Model.ts
363
651
  function Model(name) {
364
652
  return {
@@ -376,216 +664,6 @@ function Model(name) {
376
664
  }
377
665
  };
378
666
  }
379
-
380
- // src/utility/runtime/condition.ts
381
- var condition = (c, input) => {
382
- return c(input);
383
- };
384
-
385
- // src/utility/runtime/ifTypeOf.ts
386
- function runtimeExtendsCheck(val, base, narrow = false) {
387
- if (typeof val !== typeof base) {
388
- return false;
389
- }
390
- switch (typeof val) {
391
- case "boolean":
392
- case "string":
393
- case "number":
394
- case "symbol":
395
- case "bigint":
396
- return narrow ? val === base : true;
397
- case "undefined":
398
- return true;
399
- case "function":
400
- if (narrow) {
401
- throw new Error(`Use of narrowlyExtends with a function is not possible!`);
402
- }
403
- return true;
404
- case "object":
405
- if (val === null && base === null) {
406
- return true;
407
- } else {
408
- return keys(base).every(
409
- (i) => runtimeExtendsCheck(val[i], base[i], narrow)
410
- );
411
- }
412
- }
413
- }
414
- var ifTypeOf = (val) => ({
415
- extends: (base) => {
416
- const valid = runtimeExtendsCheck(val, base, false);
417
- const trueFalse = valid ? true : false;
418
- return {
419
- then: (then) => ({
420
- else: (elseVal) => {
421
- return valid ? typeof then === "undefined" ? val : then : elseVal;
422
- }
423
- }),
424
- else: (elseVal) => valid ? val : elseVal
425
- } && trueFalse;
426
- },
427
- narrowlyExtends: (base) => {
428
- const valid = runtimeExtendsCheck(val, base, true);
429
- const trueFalse = valid ? true : false;
430
- return {
431
- then: (then) => ({
432
- else: (elseVal) => {
433
- return valid ? typeof then === "undefined" ? val : then : elseVal;
434
- }
435
- }),
436
- else: (elseVal) => valid ? val : elseVal
437
- } && trueFalse;
438
- }
439
- });
440
-
441
- // src/utility/runtime/conditions/isArray.ts
442
- function isArray(i) {
443
- return Array.isArray(i) === true;
444
- }
445
-
446
- // src/utility/runtime/conditions/isBoolean.ts
447
- function isBoolean(i) {
448
- return typeof i === "boolean";
449
- }
450
-
451
- // src/utility/runtime/conditions/isFalse.ts
452
- function isFalse(i) {
453
- return typeof i === "boolean" && !i;
454
- }
455
-
456
- // src/utility/runtime/conditions/isFunction.ts
457
- function isFunction(input) {
458
- return typeof input === "function";
459
- }
460
-
461
- // src/utility/runtime/conditions/isNull.ts
462
- function isNull(i) {
463
- return i === null;
464
- }
465
-
466
- // src/utility/runtime/conditions/isNumber.ts
467
- function isNumber(i) {
468
- return typeof i === "number";
469
- }
470
-
471
- // src/utility/runtime/conditions/isObject.ts
472
- function isObject(i) {
473
- return typeof i === "object" && i !== null && Array.isArray(i) === false;
474
- }
475
-
476
- // src/utility/runtime/conditions/isString.ts
477
- function isString(i) {
478
- return typeof i === "string";
479
- }
480
-
481
- // src/utility/runtime/conditions/isSymbol.ts
482
- function isSymbol(i) {
483
- return typeof i === "symbol";
484
- }
485
-
486
- // src/utility/runtime/conditions/isTrue.ts
487
- function isTrue(i) {
488
- return typeof i === "boolean" && i;
489
- }
490
-
491
- // src/utility/runtime/conditions/isUndefined.ts
492
- function isUndefined(i) {
493
- return typeof i === "undefined";
494
- }
495
-
496
- // src/utility/runtime/type.ts
497
- var typeApi = () => ({
498
- string: {
499
- name: "string",
500
- type: "",
501
- typeGuard: (v) => isString(v),
502
- is: isString
503
- },
504
- boolean: {
505
- name: "boolean",
506
- type: true,
507
- typeGuard: (v) => isBoolean(v),
508
- is: isBoolean
509
- },
510
- number: {
511
- name: "number",
512
- type: 1,
513
- typeGuard: (v) => isNumber(v),
514
- is: isNumber
515
- },
516
- function: {
517
- name: "function",
518
- type: Function,
519
- typeGuard: (v) => isFunction(v),
520
- is: isFunction
521
- },
522
- null: {
523
- name: "null",
524
- type: null,
525
- typeGuard: (v) => isNull(v),
526
- is: isNull
527
- },
528
- symbol: {
529
- name: "symbol",
530
- type: Symbol(),
531
- typeGuard: (v) => isSymbol(v),
532
- is: isSymbol
533
- },
534
- undefined: {
535
- name: "undefined",
536
- type: void 0,
537
- typeGuard: (v) => isUndefined(v),
538
- is: isUndefined
539
- },
540
- true: {
541
- name: "true",
542
- type: true,
543
- typeGuard: (v) => isTrue(v),
544
- is: isTrue
545
- },
546
- false: {
547
- name: "false",
548
- type: false,
549
- typeGuard: (v) => isFalse(v),
550
- is: isFalse
551
- },
552
- object: {
553
- name: "object",
554
- type: {},
555
- typeGuard: (v) => isObject(v),
556
- is: isObject
557
- },
558
- array: {
559
- name: "array",
560
- type: {},
561
- typeGuard: (v) => isArray(v),
562
- is: isObject
563
- }
564
- });
565
- function isType(t) {
566
- return typeof t === "object" && ["name", "type", "is"].every((i) => Object.keys(t).includes(i));
567
- }
568
- function type(fn) {
569
- const result = fn(typeApi());
570
- if (!isType(result)) {
571
- throw new Error(
572
- `When using type(), the callback passed in returned an invalid type! Value returned was: ${result}`
573
- );
574
- }
575
- return result;
576
- }
577
-
578
- // src/utility/runtime/withValue.ts
579
- function withValue(td) {
580
- return (obj) => {
581
- const t = type(td);
582
- return Object.fromEntries(
583
- [...entries(obj)].filter(([_key, value]) => {
584
- return t.typeGuard(value);
585
- })
586
- );
587
- };
588
- }
589
667
  export {
590
668
  Configurator,
591
669
  ExplicitFunction,
@@ -596,6 +674,7 @@ export {
596
674
  api,
597
675
  arrayToKeyLookup,
598
676
  arrayToObject,
677
+ asArray,
599
678
  condition,
600
679
  createFnWithProps,
601
680
  createMutationFunction,
@@ -605,10 +684,9 @@ export {
605
684
  dictToKv,
606
685
  dictionaryTransform,
607
686
  entries,
608
- equals,
687
+ filter,
609
688
  filterDictArray,
610
689
  fnWithProps,
611
- greater,
612
690
  groupBy,
613
691
  idLiteral,
614
692
  idTypeGuard,
@@ -618,8 +696,10 @@ export {
618
696
  isBoolean,
619
697
  isFalse,
620
698
  isFunction,
699
+ isNotFilter,
621
700
  isNull,
622
701
  isNumber,
702
+ isNumericFilter,
623
703
  isObject,
624
704
  isString,
625
705
  isSymbol,
@@ -630,11 +710,11 @@ export {
630
710
  kindLiteral,
631
711
  kv,
632
712
  kvToDict,
633
- less,
634
713
  literal,
635
714
  mapTo,
636
715
  mapValues,
637
716
  nameLiteral,
717
+ not,
638
718
  or,
639
719
  randomString,
640
720
  readonlyFnWithProps,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inferred-types",
3
- "version": "0.24.3",
3
+ "version": "0.26.0",
4
4
  "description": "Functions which provide useful type inference on TS projects",
5
5
  "license": "MIT",
6
6
  "author": "Ken Snyder<ken@ken.net>",
@@ -46,6 +46,7 @@
46
46
  "eslint-plugin-prettier": "^4.2.1",
47
47
  "eslint-plugin-promise": "^6.1.1",
48
48
  "npm-run-all": "~4.1.5",
49
+ "pathe": "^0.3.9",
49
50
  "prettier": "~2.7.1",
50
51
  "rimraf": "^3.0.2",
51
52
  "sharp": "^0.31.1",
@@ -0,0 +1,34 @@
1
+ import { TupleToUnion } from "../type-conversion";
2
+ import { IsLiteral } from "./IsLiteral";
3
+ import { IsStringLiteral } from "./IsStringLiteral";
4
+
5
+ /**
6
+ * **Includes<TSource, TValue>**
7
+ *
8
+ * Type utility which returns `true` or `false` based on whether `TValue` is found
9
+ * in `TSource`. Where `TSource` can be a string literal or an array of string literals.
10
+ *
11
+ * **Note:** if the source is a _wide_ type (aka, `string` or `string[]`) then there is
12
+ * no way to know at design-time whether the value includes `TValue` and so it will return
13
+ * a type of `boolean`.
14
+ */
15
+ export type Includes<
16
+ TSource extends string | string[],
17
+ TValue extends string
18
+ > = TSource extends string[]
19
+ ? IsStringLiteral<TupleToUnion<TSource>> extends true
20
+ ? IsLiteral<TValue> extends true
21
+ ? TValue extends TupleToUnion<TSource>
22
+ ? true
23
+ : false
24
+ : boolean
25
+ : boolean
26
+ : TSource extends string
27
+ ? IsLiteral<TSource> extends true
28
+ ? IsLiteral<TValue> extends true
29
+ ? TSource extends `${string}${TValue}${string}`
30
+ ? true
31
+ : false
32
+ : boolean
33
+ : boolean
34
+ : boolean;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * **IsBooleanLiteral**
3
+ *
4
+ * Type utility which returns true/false if the boolean value is a _boolean literal_ versus
5
+ * just the wider _boolean_ type.
6
+ */
7
+ export type IsBooleanLiteral<T extends boolean> = boolean extends T ? false : true;
@@ -0,0 +1,20 @@
1
+ import { IsBooleanLiteral } from "./IsBooleanLiteral";
2
+ import { IsStringLiteral } from "./IsStringLiteral";
3
+ import { IsNumericLiteral } from "./IsNumericLiteral";
4
+
5
+ // [note on handling of boolean](https://stackoverflow.com/questions/74213646/detecting-type-literals-works-in-isolation-but-not-when-combined-with-other-lite/74213713#74213713)
6
+
7
+ /**
8
+ * **IsLiteral**
9
+ *
10
+ * Type utility which returns true/false if the value passed -- a form of a
11
+ * string, number, or boolean -- is a _literal_ value of that type (true) or
12
+ * the more generic wide type (false).
13
+ */
14
+ export type IsLiteral<T extends string | number | boolean> = [T] extends [string]
15
+ ? IsStringLiteral<T>
16
+ : [T] extends [boolean]
17
+ ? IsBooleanLiteral<T>
18
+ : [T] extends [number]
19
+ ? IsNumericLiteral<T>
20
+ : never;