@rimbu/common 1.0.0 → 2.0.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.
@@ -21,7 +21,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var internal_exports = {};
22
22
  __export(internal_exports, {
23
23
  AsyncOptLazy: () => AsyncOptLazy,
24
- AsyncReducer: () => AsyncReducer,
25
24
  CollectFun: () => CollectFun,
26
25
  Comp: () => Comp,
27
26
  Eq: () => Eq,
@@ -31,7 +30,6 @@ __export(internal_exports, {
31
30
  OptLazy: () => OptLazy,
32
31
  OptLazyOr: () => OptLazyOr,
33
32
  Range: () => Range,
34
- Reducer: () => Reducer,
35
33
  TraverseState: () => TraverseState,
36
34
  Update: () => Update
37
35
  });
@@ -406,31 +404,31 @@ var Comp;
406
404
 
407
405
  // src/eq.mts
408
406
  var Eq;
409
- ((Eq4) => {
407
+ ((Eq2) => {
410
408
  function convertAnyToString(value) {
411
409
  if (typeof value !== "object" || null === value || !("toString" in value) || typeof value.toString !== "function" || value.toString !== Object.prototype.toString) {
412
410
  return String(value);
413
411
  }
414
412
  return JSON.stringify(value);
415
413
  }
416
- Eq4.convertAnyToString = convertAnyToString;
414
+ Eq2.convertAnyToString = convertAnyToString;
417
415
  const _anyFlatEq = createAnyEq("FLAT");
418
416
  const _anyShallowEq = createAnyEq("SHALLOW");
419
417
  const _anyDeepEq = createAnyEq("DEEP");
420
418
  function defaultEq() {
421
419
  return _anyDeepEq;
422
420
  }
423
- Eq4.defaultEq = defaultEq;
424
- Eq4.objectIs = Object.is;
421
+ Eq2.defaultEq = defaultEq;
422
+ Eq2.objectIs = Object.is;
425
423
  const _valueOfEq = (v1, v2) => Object.is(v1.valueOf(), v2.valueOf());
426
424
  function valueOfEq() {
427
425
  return _valueOfEq;
428
426
  }
429
- Eq4.valueOfEq = valueOfEq;
427
+ Eq2.valueOfEq = valueOfEq;
430
428
  function dateEq() {
431
429
  return _valueOfEq;
432
430
  }
433
- Eq4.dateEq = dateEq;
431
+ Eq2.dateEq = dateEq;
434
432
  function createIterableEq(itemEq) {
435
433
  return (v1, v2) => {
436
434
  if (Object.is(v1, v2))
@@ -453,7 +451,7 @@ var Eq;
453
451
  return _iterableAnyEq;
454
452
  return createIterableEq(itemEq);
455
453
  }
456
- Eq4.iterableEq = iterableEq;
454
+ Eq2.iterableEq = iterableEq;
457
455
  function createObjectEq(valueEq) {
458
456
  return (v1, v2) => {
459
457
  if (Object.is(v1, v2))
@@ -483,7 +481,7 @@ var Eq;
483
481
  return _objectEq;
484
482
  return createObjectEq(valueEq);
485
483
  }
486
- Eq4.objectEq = objectEq;
484
+ Eq2.objectEq = objectEq;
487
485
  function createAnyEq(mode) {
488
486
  const result = (v1, v2) => {
489
487
  if (Object.is(v1, v2))
@@ -531,15 +529,15 @@ var Eq;
531
529
  function anyFlatEq() {
532
530
  return _anyFlatEq;
533
531
  }
534
- Eq4.anyFlatEq = anyFlatEq;
532
+ Eq2.anyFlatEq = anyFlatEq;
535
533
  function anyShallowEq() {
536
534
  return _anyShallowEq;
537
535
  }
538
- Eq4.anyShallowEq = anyShallowEq;
536
+ Eq2.anyShallowEq = anyShallowEq;
539
537
  function anyDeepEq() {
540
538
  return _anyDeepEq;
541
539
  }
542
- Eq4.anyDeepEq = anyDeepEq;
540
+ Eq2.anyDeepEq = anyDeepEq;
543
541
  const _defaultCollator = Intl.Collator("und");
544
542
  const _defaultStringCollatorEq = (v1, v2) => _defaultCollator.compare(v1, v2) === 0;
545
543
  function createStringCollatorEq(...args) {
@@ -548,14 +546,14 @@ var Eq;
548
546
  const collator = Intl.Collator(...args);
549
547
  return (v1, v2) => collator.compare(v1, v2) === 0;
550
548
  }
551
- Eq4.createStringCollatorEq = createStringCollatorEq;
549
+ Eq2.createStringCollatorEq = createStringCollatorEq;
552
550
  const _stringCaseInsensitiveEq = createStringCollatorEq("und", {
553
551
  sensitivity: "accent"
554
552
  });
555
553
  function stringCaseInsentitiveEq() {
556
554
  return _stringCaseInsensitiveEq;
557
555
  }
558
- Eq4.stringCaseInsentitiveEq = stringCaseInsentitiveEq;
556
+ Eq2.stringCaseInsentitiveEq = stringCaseInsentitiveEq;
559
557
  const _stringCharCodeEq = (v1, v2) => {
560
558
  const len = v1.length;
561
559
  if (len !== v2.length)
@@ -570,21 +568,21 @@ var Eq;
570
568
  function stringCharCodeEq() {
571
569
  return _stringCharCodeEq;
572
570
  }
573
- Eq4.stringCharCodeEq = stringCharCodeEq;
571
+ Eq2.stringCharCodeEq = stringCharCodeEq;
574
572
  const _anyToStringEq = (v1, v2) => convertAnyToString(v1) === convertAnyToString(v2);
575
573
  function anyToStringEq() {
576
574
  return _anyToStringEq;
577
575
  }
578
- Eq4.anyToStringEq = anyToStringEq;
576
+ Eq2.anyToStringEq = anyToStringEq;
579
577
  const _anyJsonEq = (v1, v2) => JSON.stringify(v1) === JSON.stringify(v2);
580
578
  function anyJsonEq() {
581
579
  return _anyJsonEq;
582
580
  }
583
- Eq4.anyJsonEq = anyJsonEq;
581
+ Eq2.anyJsonEq = anyJsonEq;
584
582
  function tupleSymmetric(eq = defaultEq()) {
585
583
  return (tup1, tup2) => eq(tup1[0], tup2[0]) && eq(tup1[1], tup2[1]) || eq(tup1[0], tup2[1]) && eq(tup1[1], tup2[0]);
586
584
  }
587
- Eq4.tupleSymmetric = tupleSymmetric;
585
+ Eq2.tupleSymmetric = tupleSymmetric;
588
586
  })(Eq || (Eq = {}));
589
587
 
590
588
  // src/err.mts
@@ -701,9 +699,9 @@ var IndexRange;
701
699
  })(IndexRange || (IndexRange = {}));
702
700
 
703
701
  // src/optlazy.mts
704
- function OptLazy(optLazy) {
702
+ function OptLazy(optLazy, ...args) {
705
703
  if (optLazy instanceof Function)
706
- return optLazy();
704
+ return optLazy(...args);
707
705
  return optLazy;
708
706
  }
709
707
  function OptLazyOr(optLazyOr, otherValue) {
@@ -737,427 +735,6 @@ var Range;
737
735
  Range2.getNormalizedRange = getNormalizedRange;
738
736
  })(Range || (Range = {}));
739
737
 
740
- // src/reducer.mts
741
- function identity(value) {
742
- return value;
743
- }
744
- var Reducer;
745
- ((Reducer3) => {
746
- class Base {
747
- constructor(init, next, stateToResult) {
748
- this.init = init;
749
- this.next = next;
750
- this.stateToResult = stateToResult;
751
- }
752
- filterInput(pred) {
753
- return create(
754
- () => ({
755
- nextIndex: 0,
756
- state: OptLazy(this.init)
757
- }),
758
- (state, elem, index, halt) => {
759
- if (pred(elem, index, halt)) {
760
- state.state = this.next(state.state, elem, state.nextIndex++, halt);
761
- }
762
- return state;
763
- },
764
- (state) => this.stateToResult(state.state)
765
- );
766
- }
767
- mapInput(mapFun) {
768
- return create(
769
- this.init,
770
- (state, elem, index, halt) => this.next(state, mapFun(elem, index), index, halt),
771
- this.stateToResult
772
- );
773
- }
774
- collectInput(collectFun) {
775
- return create(
776
- () => ({
777
- nextIndex: 0,
778
- state: OptLazy(this.init)
779
- }),
780
- (state, elem, index, halt) => {
781
- const nextElem = collectFun(elem, index, CollectFun.Skip, halt);
782
- if (CollectFun.Skip !== nextElem) {
783
- state.state = this.next(
784
- state.state,
785
- nextElem,
786
- state.nextIndex++,
787
- halt
788
- );
789
- }
790
- return state;
791
- },
792
- (state) => this.stateToResult(state.state)
793
- );
794
- }
795
- mapOutput(mapFun) {
796
- return create(
797
- this.init,
798
- this.next,
799
- (state) => mapFun(this.stateToResult(state))
800
- );
801
- }
802
- takeInput(amount) {
803
- if (amount <= 0) {
804
- return create(this.init, identity, this.stateToResult);
805
- }
806
- return this.filterInput((_, i, halt) => {
807
- const more = i < amount;
808
- if (!more)
809
- halt();
810
- return more;
811
- });
812
- }
813
- dropInput(amount) {
814
- if (amount <= 0)
815
- return this;
816
- return this.filterInput((_, i) => i >= amount);
817
- }
818
- sliceInput(from = 0, amount) {
819
- if (void 0 === amount)
820
- return this.dropInput(from);
821
- if (amount <= 0)
822
- return create(this.init, identity, this.stateToResult);
823
- if (from <= 0)
824
- return this.takeInput(amount);
825
- return this.takeInput(amount).dropInput(from);
826
- }
827
- }
828
- Reducer3.Base = Base;
829
- function create(init, next, stateToResult) {
830
- return new Reducer3.Base(init, next, stateToResult);
831
- }
832
- Reducer3.create = create;
833
- function createMono(init, next, stateToResult) {
834
- return create(init, next, stateToResult ?? identity);
835
- }
836
- Reducer3.createMono = createMono;
837
- function createOutput(init, next, stateToResult) {
838
- return create(init, next, stateToResult ?? identity);
839
- }
840
- Reducer3.createOutput = createOutput;
841
- Reducer3.sum = createMono(0, (state, next) => state + next);
842
- Reducer3.product = createMono(1, (state, next, _, halt) => {
843
- if (0 === next)
844
- halt();
845
- return state * next;
846
- });
847
- Reducer3.average = createMono(
848
- 0,
849
- (avg, value, index) => avg + (value - avg) / (index + 1)
850
- );
851
- Reducer3.minBy = (compFun, otherwise) => {
852
- const token = Symbol();
853
- return create(
854
- token,
855
- (state, next) => {
856
- if (token === state)
857
- return next;
858
- return compFun(state, next) < 0 ? state : next;
859
- },
860
- (state) => token === state ? OptLazy(otherwise) : state
861
- );
862
- };
863
- Reducer3.min = (otherwise) => {
864
- return create(
865
- void 0,
866
- (state, next) => void 0 !== state && state < next ? state : next,
867
- (state) => state ?? OptLazy(otherwise)
868
- );
869
- };
870
- Reducer3.maxBy = (compFun, otherwise) => {
871
- const token = Symbol();
872
- return create(
873
- token,
874
- (state, next) => {
875
- if (token === state)
876
- return next;
877
- return compFun(state, next) > 0 ? state : next;
878
- },
879
- (state) => token === state ? OptLazy(otherwise) : state
880
- );
881
- };
882
- Reducer3.max = (otherwise) => {
883
- return create(
884
- void 0,
885
- (state, next) => void 0 !== state && state > next ? state : next,
886
- (state) => state ?? OptLazy(otherwise)
887
- );
888
- };
889
- function join({
890
- sep = "",
891
- start = "",
892
- end = "",
893
- valueToString = String
894
- } = {}) {
895
- let curSep = "";
896
- let curStart = start;
897
- return create(
898
- "",
899
- (state, next) => {
900
- const result = curStart.concat(state, curSep, valueToString(next));
901
- curSep = sep;
902
- curStart = "";
903
- return result;
904
- },
905
- (state) => state.concat(end)
906
- );
907
- }
908
- Reducer3.join = join;
909
- Reducer3.count = (pred) => {
910
- if (void 0 === pred)
911
- return createOutput(0, (_, __, i) => i + 1);
912
- return createOutput(0, (state, next, i) => {
913
- if (pred?.(next, i))
914
- return state + 1;
915
- return state;
916
- });
917
- };
918
- Reducer3.firstWhere = (pred, otherwise) => {
919
- const token = Symbol();
920
- return create(
921
- token,
922
- (state, next, i, halt) => {
923
- if (token === state && pred(next, i)) {
924
- halt();
925
- return next;
926
- }
927
- return state;
928
- },
929
- (state) => token === state ? OptLazy(otherwise) : state
930
- );
931
- };
932
- Reducer3.first = (otherwise) => {
933
- const token = Symbol();
934
- return create(
935
- token,
936
- (state, next, _, halt) => {
937
- halt();
938
- if (token === state)
939
- return next;
940
- return state;
941
- },
942
- (state) => token === state ? OptLazy(otherwise) : state
943
- );
944
- };
945
- Reducer3.lastWhere = (pred, otherwise) => {
946
- const token = Symbol();
947
- return create(
948
- token,
949
- (state, next, i) => {
950
- if (pred(next, i))
951
- return next;
952
- return state;
953
- },
954
- (state) => token === state ? OptLazy(otherwise) : state
955
- );
956
- };
957
- Reducer3.last = (otherwise) => {
958
- const token = Symbol();
959
- return create(
960
- () => token,
961
- (_, next) => next,
962
- (state) => token === state ? OptLazy(otherwise) : state
963
- );
964
- };
965
- function some(pred) {
966
- return createOutput(false, (state, next, i, halt) => {
967
- if (state)
968
- return state;
969
- const satisfies = pred(next, i);
970
- if (satisfies) {
971
- halt();
972
- }
973
- return satisfies;
974
- });
975
- }
976
- Reducer3.some = some;
977
- function every(pred) {
978
- return createOutput(true, (state, next, i, halt) => {
979
- if (!state)
980
- return state;
981
- const satisfies = pred(next, i);
982
- if (!satisfies) {
983
- halt();
984
- }
985
- return satisfies;
986
- });
987
- }
988
- Reducer3.every = every;
989
- function contains(elem, eq = Object.is) {
990
- return createOutput(false, (state, next, _, halt) => {
991
- if (state)
992
- return state;
993
- const satisfies = eq(next, elem);
994
- if (satisfies) {
995
- halt();
996
- }
997
- return satisfies;
998
- });
999
- }
1000
- Reducer3.contains = contains;
1001
- Reducer3.and = createMono(true, (state, next, _, halt) => {
1002
- if (!state)
1003
- return state;
1004
- if (!next)
1005
- halt();
1006
- return next;
1007
- });
1008
- Reducer3.or = createMono(false, (state, next, _, halt) => {
1009
- if (state)
1010
- return state;
1011
- if (next)
1012
- halt();
1013
- return next;
1014
- });
1015
- Reducer3.isEmpty = createOutput(
1016
- true,
1017
- (_, __, ___, halt) => {
1018
- halt();
1019
- return false;
1020
- }
1021
- );
1022
- Reducer3.nonEmpty = createOutput(
1023
- false,
1024
- (_, __, ___, halt) => {
1025
- halt();
1026
- return true;
1027
- }
1028
- );
1029
- function toArray() {
1030
- return create(
1031
- () => [],
1032
- (state, next) => {
1033
- state.push(next);
1034
- return state;
1035
- },
1036
- (state) => state.slice()
1037
- );
1038
- }
1039
- Reducer3.toArray = toArray;
1040
- function toJSMap() {
1041
- return create(
1042
- () => /* @__PURE__ */ new Map(),
1043
- (state, next) => {
1044
- state.set(next[0], next[1]);
1045
- return state;
1046
- },
1047
- (s) => new Map(s)
1048
- );
1049
- }
1050
- Reducer3.toJSMap = toJSMap;
1051
- function toJSSet() {
1052
- return create(
1053
- () => /* @__PURE__ */ new Set(),
1054
- (state, next) => {
1055
- state.add(next);
1056
- return state;
1057
- },
1058
- (s) => new Set(s)
1059
- );
1060
- }
1061
- Reducer3.toJSSet = toJSSet;
1062
- function toJSObject() {
1063
- return create(
1064
- () => ({}),
1065
- (state, entry) => {
1066
- state[entry[0]] = entry[1];
1067
- return state;
1068
- },
1069
- (s) => ({ ...s })
1070
- );
1071
- }
1072
- Reducer3.toJSObject = toJSObject;
1073
- function combineArr(...reducers) {
1074
- const createState = () => {
1075
- return reducers.map((reducer) => {
1076
- const result = {
1077
- reducer,
1078
- halted: false,
1079
- halt() {
1080
- result.halted = true;
1081
- },
1082
- state: OptLazy(reducer.init)
1083
- };
1084
- return result;
1085
- });
1086
- };
1087
- return create(
1088
- createState,
1089
- (allState, next, index, halt) => {
1090
- let anyNotHalted = false;
1091
- let i = -1;
1092
- const len = allState.length;
1093
- while (++i < len) {
1094
- const red = allState[i];
1095
- if (red.halted) {
1096
- continue;
1097
- }
1098
- red.state = red.reducer.next(red.state, next, index, red.halt);
1099
- if (!red.halted) {
1100
- anyNotHalted = true;
1101
- }
1102
- }
1103
- if (!anyNotHalted) {
1104
- halt();
1105
- }
1106
- return allState;
1107
- },
1108
- (allState) => allState.map((st) => st.reducer.stateToResult(st.state))
1109
- );
1110
- }
1111
- Reducer3.combineArr = combineArr;
1112
- function combineObj(reducerObj) {
1113
- const createState = () => {
1114
- const allState = {};
1115
- for (const key in reducerObj) {
1116
- const reducer = reducerObj[key];
1117
- const result = {
1118
- reducer,
1119
- halted: false,
1120
- halt() {
1121
- result.halted = true;
1122
- },
1123
- state: OptLazy(reducer.init)
1124
- };
1125
- allState[key] = result;
1126
- }
1127
- return allState;
1128
- };
1129
- return create(
1130
- createState,
1131
- (allState, next, index, halt) => {
1132
- let anyNotHalted = false;
1133
- for (const key in allState) {
1134
- const red = allState[key];
1135
- if (red.halted) {
1136
- continue;
1137
- }
1138
- red.state = red.reducer.next(red.state, next, index, red.halt);
1139
- if (!red.halted) {
1140
- anyNotHalted = true;
1141
- }
1142
- }
1143
- if (!anyNotHalted) {
1144
- halt();
1145
- }
1146
- return allState;
1147
- },
1148
- (allState) => {
1149
- const result = {};
1150
- for (const key in allState) {
1151
- const st = allState[key];
1152
- result[key] = st.reducer.stateToResult(st.state);
1153
- }
1154
- return result;
1155
- }
1156
- );
1157
- }
1158
- Reducer3.combineObj = combineObj;
1159
- })(Reducer || (Reducer = {}));
1160
-
1161
738
  // src/traverse-state.mts
1162
739
  var TraverseStateImpl = class {
1163
740
  constructor(startIndex) {
@@ -1191,485 +768,22 @@ function Update(value, update) {
1191
768
  // src/async-optlazy.mts
1192
769
  var AsyncOptLazy;
1193
770
  ((AsyncOptLazy2) => {
1194
- function toMaybePromise(optLazy) {
771
+ function toMaybePromise(optLazy, ...args) {
1195
772
  if (optLazy instanceof Function)
1196
- return optLazy();
773
+ return optLazy(...args);
1197
774
  return optLazy;
1198
775
  }
1199
776
  AsyncOptLazy2.toMaybePromise = toMaybePromise;
1200
- async function toPromise(optLazy) {
777
+ async function toPromise(optLazy, ...args) {
1201
778
  if (optLazy instanceof Function)
1202
- return optLazy();
779
+ return optLazy(...args);
1203
780
  return optLazy;
1204
781
  }
1205
782
  AsyncOptLazy2.toPromise = toPromise;
1206
783
  })(AsyncOptLazy || (AsyncOptLazy = {}));
1207
-
1208
- // src/async-reducer.mts
1209
- function identity2(value) {
1210
- return value;
1211
- }
1212
- var AsyncReducer;
1213
- ((AsyncReducer2) => {
1214
- class Base {
1215
- constructor(init, next, stateToResult, onClose) {
1216
- this.init = init;
1217
- this.next = next;
1218
- this.stateToResult = stateToResult;
1219
- this.onClose = onClose;
1220
- }
1221
- filterInput(pred) {
1222
- return create(
1223
- async () => ({
1224
- nextIndex: 0,
1225
- state: await AsyncOptLazy.toMaybePromise(this.init)
1226
- }),
1227
- async (state, elem, index, halt) => {
1228
- if (pred(elem, index, halt)) {
1229
- state.state = await this.next(
1230
- state.state,
1231
- elem,
1232
- state.nextIndex++,
1233
- halt
1234
- );
1235
- }
1236
- return state;
1237
- },
1238
- (state) => this.stateToResult(state.state),
1239
- (state, error) => this.onClose?.(state.state, error)
1240
- );
1241
- }
1242
- mapInput(mapFun) {
1243
- return create(
1244
- this.init,
1245
- async (state, elem, index, halt) => this.next(state, await mapFun(elem, index), index, halt),
1246
- this.stateToResult,
1247
- this.onClose
1248
- );
1249
- }
1250
- collectInput(collectFun) {
1251
- return create(
1252
- async () => ({
1253
- nextIndex: 0,
1254
- state: await AsyncOptLazy.toMaybePromise(this.init)
1255
- }),
1256
- async (state, elem, index, halt) => {
1257
- const nextElem = await collectFun(elem, index, CollectFun.Skip, halt);
1258
- if (CollectFun.Skip !== nextElem) {
1259
- state.state = await this.next(
1260
- state.state,
1261
- nextElem,
1262
- state.nextIndex++,
1263
- halt
1264
- );
1265
- }
1266
- return state;
1267
- },
1268
- (state) => this.stateToResult(state.state),
1269
- (state, error) => this.onClose?.(state.state, error)
1270
- );
1271
- }
1272
- mapOutput(mapFun) {
1273
- return create(
1274
- this.init,
1275
- this.next,
1276
- async (state) => mapFun(await this.stateToResult(state)),
1277
- this.onClose
1278
- );
1279
- }
1280
- takeInput(amount) {
1281
- if (amount <= 0) {
1282
- return create(this.init, identity2, this.stateToResult);
1283
- }
1284
- return this.filterInput((_, i, halt) => {
1285
- const more = i < amount;
1286
- if (!more)
1287
- halt();
1288
- return more;
1289
- });
1290
- }
1291
- dropInput(amount) {
1292
- if (amount <= 0) {
1293
- return this;
1294
- }
1295
- return this.filterInput((_, i) => i >= amount);
1296
- }
1297
- sliceInput(from2 = 0, amount) {
1298
- if (void 0 === amount)
1299
- return this.dropInput(from2);
1300
- if (amount <= 0)
1301
- return create(this.init, identity2, this.stateToResult);
1302
- if (from2 <= 0)
1303
- return this.takeInput(amount);
1304
- return this.takeInput(amount).dropInput(from2);
1305
- }
1306
- }
1307
- AsyncReducer2.Base = Base;
1308
- function create(init, next, stateToResult, onClose) {
1309
- return new AsyncReducer2.Base(
1310
- init,
1311
- next,
1312
- stateToResult,
1313
- onClose
1314
- );
1315
- }
1316
- AsyncReducer2.create = create;
1317
- function createMono(init, next, stateToResult, onClose) {
1318
- return create(init, next, stateToResult ?? identity2, onClose);
1319
- }
1320
- AsyncReducer2.createMono = createMono;
1321
- function createOutput(init, next, stateToResult, onClose) {
1322
- return create(init, next, stateToResult ?? identity2, onClose);
1323
- }
1324
- AsyncReducer2.createOutput = createOutput;
1325
- function from(reducer) {
1326
- return AsyncReducer2.create(
1327
- reducer.init,
1328
- reducer.next,
1329
- reducer.stateToResult
1330
- );
1331
- }
1332
- AsyncReducer2.from = from;
1333
- AsyncReducer2.sum = createMono(0, (state, next) => state + next);
1334
- AsyncReducer2.product = createMono(1, (state, next, _, halt) => {
1335
- if (0 === next)
1336
- halt();
1337
- return state * next;
1338
- });
1339
- AsyncReducer2.average = createMono(
1340
- 0,
1341
- (avg, value, index) => avg + (value - avg) / (index + 1)
1342
- );
1343
- AsyncReducer2.minBy = (compFun, otherwise) => {
1344
- const token = Symbol();
1345
- return create(
1346
- token,
1347
- async (state, next) => {
1348
- if (token === state)
1349
- return next;
1350
- return await compFun(state, next) < 0 ? state : next;
1351
- },
1352
- (state) => token === state ? AsyncOptLazy.toMaybePromise(otherwise) : state
1353
- );
1354
- };
1355
- AsyncReducer2.min = (otherwise) => {
1356
- return create(
1357
- void 0,
1358
- (state, next) => void 0 !== state && state < next ? state : next,
1359
- (state) => state ?? AsyncOptLazy.toMaybePromise(otherwise)
1360
- );
1361
- };
1362
- AsyncReducer2.maxBy = (compFun, otherwise) => {
1363
- const token = Symbol();
1364
- return create(
1365
- token,
1366
- async (state, next) => {
1367
- if (token === state)
1368
- return next;
1369
- return await compFun(state, next) > 0 ? state : next;
1370
- },
1371
- (state) => token === state ? AsyncOptLazy.toMaybePromise(otherwise) : state
1372
- );
1373
- };
1374
- AsyncReducer2.max = (otherwise) => {
1375
- return create(
1376
- void 0,
1377
- (state, next) => void 0 !== state && state > next ? state : next,
1378
- (state) => state ?? AsyncOptLazy.toMaybePromise(otherwise)
1379
- );
1380
- };
1381
- function join({
1382
- sep = "",
1383
- start = "",
1384
- end = "",
1385
- valueToString = String
1386
- } = {}) {
1387
- let curSep = "";
1388
- let curStart = start;
1389
- return create(
1390
- "",
1391
- (state, next) => {
1392
- const result = curStart.concat(state, curSep, valueToString(next));
1393
- curSep = sep;
1394
- curStart = "";
1395
- return result;
1396
- },
1397
- (state) => state.concat(end)
1398
- );
1399
- }
1400
- AsyncReducer2.join = join;
1401
- AsyncReducer2.count = (pred) => {
1402
- if (void 0 === pred)
1403
- return createOutput(0, (_, __, i) => i + 1);
1404
- return createOutput(0, async (state, next, i) => {
1405
- if (await pred?.(next, i))
1406
- return state + 1;
1407
- return state;
1408
- });
1409
- };
1410
- AsyncReducer2.firstWhere = (pred, otherwise) => {
1411
- const token = Symbol();
1412
- return create(
1413
- token,
1414
- async (state, next, i, halt) => {
1415
- if (token === state && await pred(next, i)) {
1416
- halt();
1417
- return next;
1418
- }
1419
- return state;
1420
- },
1421
- (state) => token === state ? AsyncOptLazy.toMaybePromise(otherwise) : state
1422
- );
1423
- };
1424
- AsyncReducer2.first = (otherwise) => {
1425
- const token = Symbol();
1426
- return create(
1427
- token,
1428
- (state, next, _, halt) => {
1429
- halt();
1430
- if (token === state)
1431
- return next;
1432
- return state;
1433
- },
1434
- (state) => token === state ? AsyncOptLazy.toMaybePromise(otherwise) : state
1435
- );
1436
- };
1437
- AsyncReducer2.lastWhere = (pred, otherwise) => {
1438
- const token = Symbol();
1439
- return create(
1440
- token,
1441
- async (state, next, i) => {
1442
- if (await pred(next, i))
1443
- return next;
1444
- return state;
1445
- },
1446
- (state) => token === state ? AsyncOptLazy.toMaybePromise(otherwise) : state
1447
- );
1448
- };
1449
- AsyncReducer2.last = (otherwise) => {
1450
- const token = Symbol();
1451
- return create(
1452
- () => token,
1453
- (_, next) => next,
1454
- (state) => token === state ? AsyncOptLazy.toMaybePromise(otherwise) : state
1455
- );
1456
- };
1457
- function some(pred) {
1458
- return createOutput(
1459
- false,
1460
- async (state, next, i, halt) => {
1461
- if (state)
1462
- return state;
1463
- const satisfies = await pred(next, i);
1464
- if (satisfies) {
1465
- halt();
1466
- }
1467
- return satisfies;
1468
- }
1469
- );
1470
- }
1471
- AsyncReducer2.some = some;
1472
- function every(pred) {
1473
- return createOutput(
1474
- true,
1475
- async (state, next, i, halt) => {
1476
- if (!state)
1477
- return state;
1478
- const satisfies = await pred(next, i);
1479
- if (!satisfies) {
1480
- halt();
1481
- }
1482
- return satisfies;
1483
- }
1484
- );
1485
- }
1486
- AsyncReducer2.every = every;
1487
- function contains(elem, eq = Object.is) {
1488
- return createOutput(false, (state, next, _, halt) => {
1489
- if (state)
1490
- return state;
1491
- const satisfies = eq(next, elem);
1492
- if (satisfies) {
1493
- halt();
1494
- }
1495
- return satisfies;
1496
- });
1497
- }
1498
- AsyncReducer2.contains = contains;
1499
- AsyncReducer2.and = createMono(true, (state, next, _, halt) => {
1500
- if (!state)
1501
- return state;
1502
- if (!next)
1503
- halt();
1504
- return next;
1505
- });
1506
- AsyncReducer2.or = createMono(false, (state, next, _, halt) => {
1507
- if (state)
1508
- return state;
1509
- if (next)
1510
- halt();
1511
- return next;
1512
- });
1513
- AsyncReducer2.isEmpty = createOutput(
1514
- true,
1515
- (_, __, ___, halt) => {
1516
- halt();
1517
- return false;
1518
- }
1519
- );
1520
- AsyncReducer2.nonEmpty = createOutput(
1521
- false,
1522
- (_, __, ___, halt) => {
1523
- halt();
1524
- return true;
1525
- }
1526
- );
1527
- function toArray() {
1528
- return create(
1529
- () => [],
1530
- (state, next) => {
1531
- state.push(next);
1532
- return state;
1533
- },
1534
- (state) => state.slice()
1535
- );
1536
- }
1537
- AsyncReducer2.toArray = toArray;
1538
- function toJSMap() {
1539
- return create(
1540
- () => /* @__PURE__ */ new Map(),
1541
- (state, next) => {
1542
- state.set(next[0], next[1]);
1543
- return state;
1544
- },
1545
- (s) => new Map(s)
1546
- );
1547
- }
1548
- AsyncReducer2.toJSMap = toJSMap;
1549
- function toJSSet() {
1550
- return create(
1551
- () => /* @__PURE__ */ new Set(),
1552
- (state, next) => {
1553
- state.add(next);
1554
- return state;
1555
- },
1556
- (s) => new Set(s)
1557
- );
1558
- }
1559
- AsyncReducer2.toJSSet = toJSSet;
1560
- function toJSObject() {
1561
- return create(
1562
- () => ({}),
1563
- (state, entry) => {
1564
- state[entry[0]] = entry[1];
1565
- return state;
1566
- },
1567
- (s) => ({ ...s })
1568
- );
1569
- }
1570
- AsyncReducer2.toJSObject = toJSObject;
1571
- function combineArr(...reducers) {
1572
- const createState = () => {
1573
- return Promise.all(
1574
- reducers.map(async (reducer) => {
1575
- const result = {
1576
- reducer,
1577
- halted: false,
1578
- halt() {
1579
- result.halted = true;
1580
- },
1581
- state: await AsyncOptLazy.toMaybePromise(reducer.init)
1582
- };
1583
- return result;
1584
- })
1585
- );
1586
- };
1587
- return create(
1588
- createState,
1589
- async (allState, next, index, halt) => {
1590
- let anyNotHalted = false;
1591
- await Promise.all(
1592
- allState.map(async (red) => {
1593
- if (red.halted)
1594
- return;
1595
- red.state = await red.reducer.next(
1596
- red.state,
1597
- next,
1598
- index,
1599
- red.halt
1600
- );
1601
- if (!red.halted)
1602
- anyNotHalted = true;
1603
- })
1604
- );
1605
- if (!anyNotHalted)
1606
- halt();
1607
- return allState;
1608
- },
1609
- (allState) => Promise.all(
1610
- allState.map((st) => st.reducer.stateToResult(st.state))
1611
- )
1612
- );
1613
- }
1614
- AsyncReducer2.combineArr = combineArr;
1615
- function combineObj(reducerObj) {
1616
- const createState = async () => {
1617
- const entries = await Promise.all(
1618
- Object.entries(reducerObj).map(async ([key, reducer]) => {
1619
- const result = {
1620
- reducer,
1621
- halted: false,
1622
- halt() {
1623
- result.halted = true;
1624
- },
1625
- state: await AsyncOptLazy.toMaybePromise(reducer.init)
1626
- };
1627
- return [key, result];
1628
- })
1629
- );
1630
- return Object.fromEntries(entries);
1631
- };
1632
- return create(
1633
- createState,
1634
- async (allState, next, index, halt) => {
1635
- let anyNotHalted = false;
1636
- await Promise.all(
1637
- Object.values(allState).map(async (red) => {
1638
- if (red.halted) {
1639
- return;
1640
- }
1641
- red.state = await red.reducer.next(
1642
- red.state,
1643
- next,
1644
- index,
1645
- red.halt
1646
- );
1647
- if (!red.halted) {
1648
- anyNotHalted = true;
1649
- }
1650
- })
1651
- );
1652
- if (!anyNotHalted) {
1653
- halt();
1654
- }
1655
- return allState;
1656
- },
1657
- async (allState) => {
1658
- const entries = await Promise.all(
1659
- Object.entries(allState).map(
1660
- async ([key, st]) => [key, await st.reducer.stateToResult(st.state)]
1661
- )
1662
- );
1663
- return Object.fromEntries(entries);
1664
- }
1665
- );
1666
- }
1667
- AsyncReducer2.combineObj = combineObj;
1668
- })(AsyncReducer || (AsyncReducer = {}));
1669
784
  // Annotate the CommonJS export names for ESM import in node:
1670
785
  0 && (module.exports = {
1671
786
  AsyncOptLazy,
1672
- AsyncReducer,
1673
787
  CollectFun,
1674
788
  Comp,
1675
789
  Eq,
@@ -1679,7 +793,6 @@ var AsyncReducer;
1679
793
  OptLazy,
1680
794
  OptLazyOr,
1681
795
  Range,
1682
- Reducer,
1683
796
  TraverseState,
1684
797
  Update
1685
798
  });