@zelgadis87/utils-core 5.0.3 → 5.1.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/esbuild/index.mjs CHANGED
@@ -644,8 +644,24 @@ function mapFirstTruthy(arr, mapFn) {
644
644
  }
645
645
  return null;
646
646
  }
647
- function makeArrayEmpty(arr) {
648
- arr.splice(0, arr.length);
647
+ function listToDict(arr, mapFn) {
648
+ return arr.reduce((dict, cur) => {
649
+ const [key, value] = mapFn(cur);
650
+ return { ...dict, [key]: value };
651
+ }, {});
652
+ }
653
+ function countArrayBy(arr, mapFn) {
654
+ return arr.reduce((dict, cur) => {
655
+ const value = mapFn(cur);
656
+ const count = (dict[value] ?? 0) + 1;
657
+ return { ...dict, [value]: count };
658
+ }, {});
659
+ }
660
+ function sumArrayBy(arr, mapFn) {
661
+ return arr.reduce((sum2, cur) => {
662
+ const value = mapFn(cur);
663
+ return sum2 + value;
664
+ }, 0);
649
665
  }
650
666
 
651
667
  // src/utils/booleans.ts
@@ -2077,6 +2093,12 @@ var Optional = class _Optional {
2077
2093
  this._value = t;
2078
2094
  this._present = true;
2079
2095
  }
2096
+ setNullable(t) {
2097
+ if (isDefined(t)) {
2098
+ return this.set(t);
2099
+ }
2100
+ return this;
2101
+ }
2080
2102
  clear() {
2081
2103
  this._value = void 0;
2082
2104
  this._present = false;
@@ -2102,31 +2124,69 @@ var Optional = class _Optional {
2102
2124
  callbackIfPresent(this.get());
2103
2125
  }
2104
2126
  }
2105
- orElse(newValue) {
2127
+ orElseReturn(newValue) {
2106
2128
  if (this.isPresent()) {
2107
- return this._value;
2129
+ return this.get();
2108
2130
  } else {
2109
2131
  return newValue;
2110
2132
  }
2111
2133
  }
2112
- orElseGet(newValueProducer) {
2134
+ orElse = this.orElseReturn.bind(this);
2135
+ orElseProduce(newValueProducer) {
2113
2136
  if (this.isPresent()) {
2114
- return this._value;
2137
+ return this.get();
2115
2138
  } else {
2116
2139
  return newValueProducer();
2117
2140
  }
2118
2141
  }
2119
- orElseNullable(newValue) {
2142
+ orElseGet = this.orElseProduce.bind(this);
2143
+ orElseReturnAndApply(newValue) {
2144
+ if (this.isPresent()) {
2145
+ return this.get();
2146
+ } else {
2147
+ this.set(newValue);
2148
+ return newValue;
2149
+ }
2150
+ }
2151
+ orElseProduceAndApply(newValueProducer) {
2152
+ if (this.isPresent()) {
2153
+ return this.get();
2154
+ } else {
2155
+ const newValue = newValueProducer();
2156
+ this.set(newValue);
2157
+ return newValue;
2158
+ }
2159
+ }
2160
+ orElseReturnNullableAndApply(newValue) {
2161
+ if (this.isPresent()) {
2162
+ return this;
2163
+ } else {
2164
+ this.setNullable(newValue);
2165
+ return this;
2166
+ }
2167
+ }
2168
+ orElseProduceNullableAndApply(newValueProducer) {
2169
+ if (this.isPresent()) {
2170
+ return this;
2171
+ } else {
2172
+ const newValue = newValueProducer();
2173
+ this.setNullable(newValue);
2174
+ return this;
2175
+ }
2176
+ }
2177
+ orElseReturnNullable(newValue) {
2120
2178
  if (this.isEmpty()) return _Optional.ofNullable(newValue);
2121
2179
  return this;
2122
2180
  }
2123
- orElseGetNullable(newValueProducer) {
2181
+ orElseNullable = this.orElseReturnNullable.bind(this);
2182
+ orElseProduceNullable(newValueProducer) {
2124
2183
  if (this.isEmpty()) {
2125
2184
  const newValue = newValueProducer();
2126
2185
  return _Optional.ofNullable(newValue);
2127
2186
  }
2128
2187
  return this;
2129
2188
  }
2189
+ orElseGetNullable = this.orElseProduceNullable.bind(this);
2130
2190
  orElseThrow(errorProducer) {
2131
2191
  if (this.isEmpty())
2132
2192
  throw errorProducer();
@@ -2670,6 +2730,7 @@ export {
2670
2730
  constantTrue,
2671
2731
  constantUndefined,
2672
2732
  constantZero,
2733
+ countArrayBy,
2673
2734
  cssDeclarationRulesDictionaryToCss,
2674
2735
  decrement,
2675
2736
  decrementBy,
@@ -2735,7 +2796,7 @@ export {
2735
2796
  isZero,
2736
2797
  jsonCloneDeep,
2737
2798
  last,
2738
- makeArrayEmpty,
2799
+ listToDict,
2739
2800
  mapDefined,
2740
2801
  mapEntries,
2741
2802
  mapFirstTruthy,
@@ -2773,6 +2834,7 @@ export {
2773
2834
  splitWords,
2774
2835
  stringToNumber,
2775
2836
  sum,
2837
+ sumArrayBy,
2776
2838
  sumBy,
2777
2839
  tail,
2778
2840
  throttle,