@zthun/helpful-fn 9.11.6 → 9.11.8

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.js CHANGED
@@ -40,7 +40,7 @@ import { startOfToday, parse, formatDate } from 'date-fns';
40
40
  return ZHorizontalAnchor;
41
41
  }({});
42
42
 
43
- function _define_property$6(obj, key, value) {
43
+ function _define_property$7(obj, key, value) {
44
44
  if (key in obj) {
45
45
  Object.defineProperty(obj, key, {
46
46
  value: value,
@@ -113,10 +113,45 @@ function _define_property$6(obj, key, value) {
113
113
  /**
114
114
  * Initializes a new instance of this object.
115
115
  */ constructor(){
116
- _define_property$6(this, "_messages", []);
116
+ _define_property$7(this, "_messages", []);
117
117
  }
118
118
  }
119
119
 
120
+ /**
121
+ * Gets whether the candidate can represent an enumeration object of type TEnum.
122
+ *
123
+ * This is mostly helpful for type guarding value options in a type.
124
+ *
125
+ * @param enumeration -
126
+ * The enumeration object that contains the values.
127
+ * @param candidate -
128
+ * The candidate to check.
129
+ *
130
+ * @returns
131
+ * True if candidate can represents one of the white listed
132
+ * object values in enumeration.
133
+ */ function isEnum(enumeration, candidate) {
134
+ return candidate != null && (typeof candidate === "string" || typeof candidate === "number") && Object.values(enumeration).includes(candidate);
135
+ }
136
+
137
+ /**
138
+ * Attempts to cast the candidate to an enumeration value.
139
+ *
140
+ * @param enumeration -
141
+ * The enumeration type.
142
+ * @param candidate -
143
+ * The candidate to cast to the enum type.
144
+ * @param fallback -
145
+ * The fallback to use in the case that candidate
146
+ * cannot represent the enumeration type.
147
+ *
148
+ * @returns
149
+ * The candidate if candidate represents the enumeration type,
150
+ * or fallback in the case that it does not.
151
+ */ function castEnum(enumeration, candidate, fallback) {
152
+ return isEnum(enumeration, candidate) ? candidate : fallback;
153
+ }
154
+
120
155
  /**
121
156
  * Puts a . in front of name if one is not already there.
122
157
  *
@@ -159,6 +194,44 @@ function _define_property$6(obj, key, value) {
159
194
  return `.${normalized}`;
160
195
  }
161
196
 
197
+ /**
198
+ * Attempts to cast candidate to a number.
199
+ *
200
+ * This method assumes you want an actual number and
201
+ * not NaN. In the case that candidate results in
202
+ * NaN, then the fallback condition applies.
203
+ *
204
+ * @param candidate -
205
+ * The candidate to cast.
206
+ *
207
+ * @returns
208
+ * The casted number or undefined in the cast
209
+ * that casting candidate would result in NaN.
210
+ */ /**
211
+ * Attempts to cast candidate to a number.
212
+ *
213
+ * This method assumes you want an actual number and
214
+ * not NaN. In the case that candidate results in
215
+ * NaN, then the fallback condition applies.
216
+ *
217
+ * @param candidate -
218
+ * The candidate to cast.
219
+ * @param fallback -
220
+ * The fallback value in the case that the result
221
+ * of casting candidate would result in NaN
222
+ *
223
+ * @returns
224
+ * The casted number or fallback in the cast
225
+ * that casting candidate would result in NaN.
226
+ */ function castNumber(candidate, fallback) {
227
+ try {
228
+ const casted = Number(candidate);
229
+ return Number.isNaN(casted) ? fallback : casted;
230
+ } catch {
231
+ return fallback;
232
+ }
233
+ }
234
+
162
235
  /**
163
236
  * Calculates the total number of buckets you need to
164
237
  * store a number of items where each bucket can hold a
@@ -527,6 +600,96 @@ function _define_property$6(obj, key, value) {
527
600
  return candidate == null || candidateType === "object" && Object.keys(candidate).length === 0;
528
601
  }
529
602
 
603
+ /**
604
+ * Information for an enum value.
605
+ *
606
+ * This is useful for front end development when
607
+ * you want to map an enumeration like value to
608
+ * things like a display name, description, and
609
+ * avatar. ECMAScript does not support decorators
610
+ * for literal fields so this is an alternative
611
+ * to describe that kind of information.
612
+ *
613
+ * This is similar to Metadata from helpful-query,
614
+ * but it's much more specific and specialized
615
+ * to enumerations.
616
+ */ function _define_property$6(obj, key, value) {
617
+ if (key in obj) {
618
+ Object.defineProperty(obj, key, {
619
+ value: value,
620
+ enumerable: true,
621
+ configurable: true,
622
+ writable: true
623
+ });
624
+ } else {
625
+ obj[key] = value;
626
+ }
627
+ return obj;
628
+ }
629
+ /**
630
+ * Builds information for an enum.
631
+ */ class ZEnumInfoBuilder {
632
+ /**
633
+ * The display name of the enum.
634
+ *
635
+ * @param name -
636
+ * The display name of the enum.
637
+ *
638
+ * @returns
639
+ * This object.
640
+ */ name(name) {
641
+ this._metadata.name = name;
642
+ return this;
643
+ }
644
+ /**
645
+ * Text description of the enum.
646
+ *
647
+ * @param description -
648
+ * Text description of what the value means.
649
+ *
650
+ * @returns
651
+ * This object.
652
+ */ description(description) {
653
+ this._metadata.description = description;
654
+ return this;
655
+ }
656
+ /**
657
+ * The avatar representation of the enum.
658
+ *
659
+ * @param avatar -
660
+ * The avatar representation of what the value
661
+ * is. This can be anything but should be related
662
+ * to the framework it is being developed for.
663
+ *
664
+ * @returns
665
+ * This object.
666
+ */ avatar(avatar) {
667
+ this._metadata.avatar = avatar;
668
+ return this;
669
+ }
670
+ /**
671
+ * Returns a shallow copy of the metadata.
672
+ *
673
+ * @returns
674
+ * A shallow copy of the metadata.
675
+ */ build() {
676
+ return {
677
+ ...this._metadata
678
+ };
679
+ }
680
+ /**
681
+ * Initializes a new instance of this object.
682
+ *
683
+ * @param value -
684
+ * The value to initialize with.
685
+ */ constructor(value){
686
+ _define_property$6(this, "_metadata", void 0);
687
+ this._metadata = {
688
+ value
689
+ };
690
+ }
691
+ }
692
+
530
693
  const BYTES_PER_KIBIBYTE = 1024;
531
694
  const BYTES_PER_MEBIBYTE = 1048576; // 1024 ^ 2
532
695
  const BYTES_PER_GIBIBYTE = 1073741824; // 1024 ^ 3
@@ -1979,5 +2142,5 @@ function _define_property(obj, key, value) {
1979
2142
  return tryFallback(()=>JSON.parse(buffer), fallback);
1980
2143
  }
1981
2144
 
1982
- export { $global, ZAssert, ZDateFormats, ZDeserializeJson, ZDeserializeTry, ZHorizontalAnchor, ZLazy, ZOrientation, ZQuadrilateralBuilder, ZQuadrilateralCornersBuilder, ZRectangle, ZSerializeJson, ZVerticalAnchor, bash, castExtension, commaJoinDefined, countBuckets, createError, createGuid, css, cssJoinDefined, culture, cultures, detokenize, firstDefined, firstTruthy, firstWhere, formatDateTime, gib, gibibytes, guessDateTime, html, isEmptyObject, javascript, joinDefined, js, kib, kibibytes, markdown, md, mebibytes, mib, optional, parseDateTime, pebibytes, peel, peelBetween, pib, pickBy, pickDataAttributes, pickDefined, pickEvents, pipeJoinDefined, purge, required, semiColonJoinDefined, setFirst, sh, sleep, spaceJoinDefined, tag, tebibytes, tib, timeZones, tryFallback, tryFallbackAsync, tryJsonParse, ts, typescript, userTimeZone, zsh };
2145
+ export { $global, ZAssert, ZDateFormats, ZDeserializeJson, ZDeserializeTry, ZEnumInfoBuilder, ZHorizontalAnchor, ZLazy, ZOrientation, ZQuadrilateralBuilder, ZQuadrilateralCornersBuilder, ZRectangle, ZSerializeJson, ZVerticalAnchor, bash, castEnum, castExtension, castNumber, commaJoinDefined, countBuckets, createError, createGuid, css, cssJoinDefined, culture, cultures, detokenize, firstDefined, firstTruthy, firstWhere, formatDateTime, gib, gibibytes, guessDateTime, html, isEmptyObject, isEnum, javascript, joinDefined, js, kib, kibibytes, markdown, md, mebibytes, mib, optional, parseDateTime, pebibytes, peel, peelBetween, pib, pickBy, pickDataAttributes, pickDefined, pickEvents, pipeJoinDefined, purge, required, semiColonJoinDefined, setFirst, sh, sleep, spaceJoinDefined, tag, tebibytes, tib, timeZones, tryFallback, tryFallbackAsync, tryJsonParse, ts, typescript, userTimeZone, zsh };
1983
2146
  //# sourceMappingURL=index.js.map