inferred-types 0.25.0 → 0.27.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
@@ -41,6 +41,14 @@ var valueTypes = {
41
41
  literalArray: (arr) => [arr, true]
42
42
  };
43
43
 
44
+ // src/types/dictionary/MapTo.ts
45
+ var MapDirection = /* @__PURE__ */ ((MapDirection2) => {
46
+ MapDirection2["OneToMany"] = "I -> O[]";
47
+ MapDirection2["OneToOne"] = "I -> O";
48
+ MapDirection2["ManyToOne"] = "I[] -> O";
49
+ return MapDirection2;
50
+ })(MapDirection || {});
51
+
44
52
  // src/utility/keys.ts
45
53
  function keys(obj, ...without) {
46
54
  const v = without.length > 0 ? Object.keys(obj).filter((k) => !without.includes(k)) : Object.keys(obj);
@@ -84,6 +92,333 @@ var api = (priv) => (pub) => {
84
92
  return surface;
85
93
  };
86
94
 
95
+ // src/utility/boolean-logic/and.ts
96
+ var and = (...ops) => {
97
+ const fn = (...args) => {
98
+ return [...ops].every((i) => i(...args));
99
+ };
100
+ return fn;
101
+ };
102
+
103
+ // src/utility/boolean-logic/or.ts
104
+ var or = (...ops) => {
105
+ const fn = (...args) => {
106
+ return [...ops].some((i) => i(...args));
107
+ };
108
+ return fn;
109
+ };
110
+
111
+ // src/utility/boolean-logic/not.ts
112
+ var not = (op) => {
113
+ const fn = (...args) => {
114
+ return !op(...args);
115
+ };
116
+ return fn;
117
+ };
118
+
119
+ // src/utility/runtime/condition.ts
120
+ var condition = (c, input) => {
121
+ return c(input);
122
+ };
123
+
124
+ // src/utility/runtime/ifTypeOf.ts
125
+ function runtimeExtendsCheck(val, base, narrow = false) {
126
+ if (typeof val !== typeof base) {
127
+ return false;
128
+ }
129
+ switch (typeof val) {
130
+ case "boolean":
131
+ case "string":
132
+ case "number":
133
+ case "symbol":
134
+ case "bigint":
135
+ return narrow ? val === base : true;
136
+ case "undefined":
137
+ return true;
138
+ case "function":
139
+ if (narrow) {
140
+ throw new Error(`Use of narrowlyExtends with a function is not possible!`);
141
+ }
142
+ return true;
143
+ case "object":
144
+ if (val === null && base === null) {
145
+ return true;
146
+ } else {
147
+ return keys(base).every(
148
+ (i) => runtimeExtendsCheck(val[i], base[i], narrow)
149
+ );
150
+ }
151
+ }
152
+ }
153
+ var ifTypeOf = (val) => ({
154
+ extends: (base) => {
155
+ const valid = runtimeExtendsCheck(val, base, false);
156
+ const trueFalse = valid ? true : false;
157
+ return {
158
+ then: (then) => ({
159
+ else: (elseVal) => {
160
+ return valid ? typeof then === "undefined" ? val : then : elseVal;
161
+ }
162
+ }),
163
+ else: (elseVal) => valid ? val : elseVal
164
+ } && trueFalse;
165
+ },
166
+ narrowlyExtends: (base) => {
167
+ const valid = runtimeExtendsCheck(val, base, true);
168
+ const trueFalse = valid ? true : false;
169
+ return {
170
+ then: (then) => ({
171
+ else: (elseVal) => {
172
+ return valid ? typeof then === "undefined" ? val : then : elseVal;
173
+ }
174
+ }),
175
+ else: (elseVal) => valid ? val : elseVal
176
+ } && trueFalse;
177
+ }
178
+ });
179
+
180
+ // src/utility/runtime/conditions/isArray.ts
181
+ function isArray(i) {
182
+ return Array.isArray(i) === true;
183
+ }
184
+
185
+ // src/utility/runtime/conditions/isBoolean.ts
186
+ function isBoolean(i) {
187
+ return typeof i === "boolean";
188
+ }
189
+
190
+ // src/utility/runtime/conditions/isFalse.ts
191
+ function isFalse(i) {
192
+ return typeof i === "boolean" && !i;
193
+ }
194
+
195
+ // src/utility/runtime/conditions/isFunction.ts
196
+ function isFunction(input) {
197
+ return typeof input === "function";
198
+ }
199
+
200
+ // src/utility/runtime/conditions/isNull.ts
201
+ function isNull(i) {
202
+ return i === null;
203
+ }
204
+
205
+ // src/utility/runtime/conditions/isNumber.ts
206
+ function isNumber(i) {
207
+ return typeof i === "number";
208
+ }
209
+
210
+ // src/utility/runtime/conditions/isObject.ts
211
+ function isObject(i) {
212
+ return typeof i === "object" && i !== null && Array.isArray(i) === false;
213
+ }
214
+
215
+ // src/utility/runtime/conditions/isString.ts
216
+ function isString(i) {
217
+ return typeof i === "string";
218
+ }
219
+
220
+ // src/utility/runtime/conditions/isSymbol.ts
221
+ function isSymbol(i) {
222
+ return typeof i === "symbol";
223
+ }
224
+
225
+ // src/utility/runtime/conditions/isTrue.ts
226
+ function isTrue(i) {
227
+ return typeof i === "boolean" && i;
228
+ }
229
+
230
+ // src/utility/runtime/conditions/isUndefined.ts
231
+ function isUndefined(i) {
232
+ return typeof i === "undefined";
233
+ }
234
+
235
+ // src/utility/runtime/type.ts
236
+ var typeApi = () => ({
237
+ string: {
238
+ name: "string",
239
+ type: "",
240
+ typeGuard: (v) => isString(v),
241
+ is: isString
242
+ },
243
+ boolean: {
244
+ name: "boolean",
245
+ type: true,
246
+ typeGuard: (v) => isBoolean(v),
247
+ is: isBoolean
248
+ },
249
+ number: {
250
+ name: "number",
251
+ type: 1,
252
+ typeGuard: (v) => isNumber(v),
253
+ is: isNumber
254
+ },
255
+ function: {
256
+ name: "function",
257
+ type: Function,
258
+ typeGuard: (v) => isFunction(v),
259
+ is: isFunction
260
+ },
261
+ null: {
262
+ name: "null",
263
+ type: null,
264
+ typeGuard: (v) => isNull(v),
265
+ is: isNull
266
+ },
267
+ symbol: {
268
+ name: "symbol",
269
+ type: Symbol(),
270
+ typeGuard: (v) => isSymbol(v),
271
+ is: isSymbol
272
+ },
273
+ undefined: {
274
+ name: "undefined",
275
+ type: void 0,
276
+ typeGuard: (v) => isUndefined(v),
277
+ is: isUndefined
278
+ },
279
+ true: {
280
+ name: "true",
281
+ type: true,
282
+ typeGuard: (v) => isTrue(v),
283
+ is: isTrue
284
+ },
285
+ false: {
286
+ name: "false",
287
+ type: false,
288
+ typeGuard: (v) => isFalse(v),
289
+ is: isFalse
290
+ },
291
+ object: {
292
+ name: "object",
293
+ type: {},
294
+ typeGuard: (v) => isObject(v),
295
+ is: isObject
296
+ },
297
+ array: {
298
+ name: "array",
299
+ type: {},
300
+ typeGuard: (v) => isArray(v),
301
+ is: isObject
302
+ }
303
+ });
304
+ function isType(t) {
305
+ return typeof t === "object" && ["name", "type", "is"].every((i) => Object.keys(t).includes(i));
306
+ }
307
+ function type(fn) {
308
+ const result = fn(typeApi());
309
+ if (!isType(result)) {
310
+ throw new Error(
311
+ `When using type(), the callback passed in returned an invalid type! Value returned was: ${result}`
312
+ );
313
+ }
314
+ return result;
315
+ }
316
+
317
+ // src/utility/dictionary/entries.ts
318
+ function entries(obj) {
319
+ const iterable = {
320
+ *[Symbol.iterator]() {
321
+ for (const k of keys(obj)) {
322
+ yield [k, obj[k]];
323
+ }
324
+ }
325
+ };
326
+ return iterable;
327
+ }
328
+
329
+ // src/utility/runtime/withValue.ts
330
+ function withValue(td) {
331
+ return (obj) => {
332
+ const t = type(td);
333
+ return Object.fromEntries(
334
+ [...entries(obj)].filter(([_key, value]) => {
335
+ return t.typeGuard(value);
336
+ })
337
+ );
338
+ };
339
+ }
340
+
341
+ // src/utility/lists/asArray.ts
342
+ var asArray = (thing, _widen) => {
343
+ return isArray(thing) ? thing : typeof thing === "undefined" ? [] : [thing];
344
+ };
345
+
346
+ // src/utility/boolean-logic/filter.ts
347
+ function isNotFilter(f) {
348
+ return typeof f === "object" && "not" in f;
349
+ }
350
+ function isNumericFilter(filter2) {
351
+ return "equals" in filter2 || "notEqual" in filter2 || "greaterThan" in filter2 || "lessThan" in filter2 ? true : false;
352
+ }
353
+ var numericOps = (config, boolLogic) => {
354
+ const equals = (n) => {
355
+ const f = asArray(n);
356
+ return f.length === 0 ? [] : [(input) => f.some((i) => i === input)];
357
+ };
358
+ const notEqual = (n) => {
359
+ const f = asArray(n);
360
+ return f.length === 0 ? [] : [(input) => f.every((i) => i !== input)];
361
+ };
362
+ const greaterThan = (n) => {
363
+ const val = [(input) => input !== void 0 && input > n];
364
+ return val;
365
+ };
366
+ const lessThan = (n) => {
367
+ const val = [(input) => input !== void 0 && input < n];
368
+ return val;
369
+ };
370
+ const conditions = [
371
+ ..."equals" in config ? equals(config.equals) : [],
372
+ ..."notEqual" in config ? notEqual(config.notEqual) : [],
373
+ ..."greaterThan" in config ? greaterThan(config.greaterThan) : [],
374
+ ..."lessThan" in config ? lessThan(config.lessThan) : []
375
+ ];
376
+ const combined = boolLogic === "AND" ? (input) => conditions.every((c) => c(input)) : (input) => conditions.some((c) => c(input));
377
+ return combined;
378
+ };
379
+ var stringOps = (config, boolLogic) => {
380
+ const startsWith = (n) => {
381
+ const f = asArray(n);
382
+ return f.length === 0 ? [] : [(input) => f.some((i) => input == null ? void 0 : input.startsWith(i))];
383
+ };
384
+ const endsWith = (n) => {
385
+ const f = asArray(n);
386
+ return f.length === 0 ? [] : [(input) => f.some((i) => input == null ? void 0 : input.endsWith(i))];
387
+ };
388
+ const contains = (n) => {
389
+ const f = asArray(n);
390
+ return f.length === 0 ? [] : [(input) => f.some((i) => input == null ? void 0 : input.includes(i))];
391
+ };
392
+ const conditions = [
393
+ ..."startsWith" in config ? startsWith(config.startsWith) : [],
394
+ ..."endsWith" in config ? endsWith(config.endsWith) : [],
395
+ ..."contains" in config ? contains(config.contains) : []
396
+ ];
397
+ const combined = boolLogic === "AND" ? (input) => conditions.every((c) => c(input)) : (input) => conditions.some((c) => c(input));
398
+ return combined;
399
+ };
400
+ var filterFn = (defn, logicCombinator, ifUndefined = "no-impact") => {
401
+ const config = isNotFilter(defn) ? defn["not"] : defn;
402
+ const filter2 = (input) => {
403
+ const undefValue = ifUndefined === "no-impact" ? logicCombinator === "AND" ? true : false : ifUndefined;
404
+ let flag;
405
+ if (typeof input === "undefined") {
406
+ flag = undefValue;
407
+ } else if (isNumericFilter(config)) {
408
+ const fn = numericOps(config, logicCombinator);
409
+ flag = isNotFilter(defn) ? !fn(input) : fn(input);
410
+ } else {
411
+ const fn = stringOps(config, logicCombinator);
412
+ flag = isNotFilter(defn) ? !fn(input) : fn(input);
413
+ }
414
+ return flag;
415
+ };
416
+ return filter2;
417
+ };
418
+ var filter = (config, logicCombinator = "AND", ifUndefined = "no-impact") => {
419
+ return filterFn(config, logicCombinator, ifUndefined);
420
+ };
421
+
87
422
  // src/utility/dictionary/arrayToKeyLookup.ts
88
423
  function arrayToKeyLookup(...keys2) {
89
424
  const obj = {};
@@ -159,18 +494,6 @@ var dictArr = (...dicts) => {
159
494
  return api2;
160
495
  };
161
496
 
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
497
  // src/utility/dictionary/mapValues.ts
175
498
  function mapValues(obj, valueMapper) {
176
499
  return Object.fromEntries(
@@ -179,14 +502,34 @@ function mapValues(obj, valueMapper) {
179
502
  }
180
503
 
181
504
  // src/utility/dictionary/mapTo.ts
182
- var mapTo = (cb) => (source) => {
183
- if (Array.isArray(source)) {
184
- return source.flatMap((i) => cb(i)).filter((i) => i !== null);
185
- } else {
186
- const result = cb(source);
187
- return result ? result : [];
188
- }
505
+ var mapper = (config = {}) => (map) => {
506
+ config = {
507
+ input: "req",
508
+ output: "opt",
509
+ direction: "I -> O[]",
510
+ ...config
511
+ };
512
+ const fn = (source) => {
513
+ const isArray2 = config.direction === "I -> O[]" && Array.isArray(source) ? true : config.direction === "I[] -> O" && Array.isArray(source) && Array.isArray(source[0]) ? true : false;
514
+ if (isArray2) {
515
+ return source.flatMap(map);
516
+ } else {
517
+ return map(source);
518
+ }
519
+ };
520
+ return fn;
189
521
  };
522
+ var mapToFn = (map) => {
523
+ return mapper()(map);
524
+ };
525
+ var mapToDict = {
526
+ config: (c = { input: "req", output: "opt", direction: "I -> O[]" }) => ({
527
+ map(map) {
528
+ return mapper(c)(map);
529
+ }
530
+ })
531
+ };
532
+ var mapTo = createFnWithProps(mapToFn, mapToDict);
190
533
 
191
534
  // src/utility/dictionary/strArrayToDict.ts
192
535
  function strArrayToDict(...strings) {
@@ -332,33 +675,6 @@ function literal(obj) {
332
675
  return obj;
333
676
  }
334
677
 
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
678
  // src/utility/modelling/Model.ts
363
679
  function Model(name) {
364
680
  return {
@@ -376,226 +692,18 @@ function Model(name) {
376
692
  }
377
693
  };
378
694
  }
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
695
  export {
590
696
  Configurator,
591
697
  ExplicitFunction,
592
698
  FluentConfigurator,
699
+ MapDirection,
593
700
  Model,
594
701
  MutationIdentity,
595
702
  and,
596
703
  api,
597
704
  arrayToKeyLookup,
598
705
  arrayToObject,
706
+ asArray,
599
707
  condition,
600
708
  createFnWithProps,
601
709
  createMutationFunction,
@@ -605,10 +713,9 @@ export {
605
713
  dictToKv,
606
714
  dictionaryTransform,
607
715
  entries,
608
- equals,
716
+ filter,
609
717
  filterDictArray,
610
718
  fnWithProps,
611
- greater,
612
719
  groupBy,
613
720
  idLiteral,
614
721
  idTypeGuard,
@@ -618,8 +725,10 @@ export {
618
725
  isBoolean,
619
726
  isFalse,
620
727
  isFunction,
728
+ isNotFilter,
621
729
  isNull,
622
730
  isNumber,
731
+ isNumericFilter,
623
732
  isObject,
624
733
  isString,
625
734
  isSymbol,
@@ -630,11 +739,13 @@ export {
630
739
  kindLiteral,
631
740
  kv,
632
741
  kvToDict,
633
- less,
634
742
  literal,
635
743
  mapTo,
744
+ mapToDict,
745
+ mapToFn,
636
746
  mapValues,
637
747
  nameLiteral,
748
+ not,
638
749
  or,
639
750
  randomString,
640
751
  readonlyFnWithProps,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inferred-types",
3
- "version": "0.25.0",
3
+ "version": "0.27.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>",