@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/cast/cast-enum.d.mts +29 -0
- package/dist/cast/cast-number.d.mts +33 -0
- package/dist/date/calendar-month.d.mts +14 -14
- package/dist/enum/enum-info.d.mts +87 -0
- package/dist/enum/is-enum.d.mts +15 -0
- package/dist/index.cjs +169 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +4 -0
- package/dist/index.js +166 -3
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attempts to cast the candidate to an enumeration value.
|
|
3
|
+
*
|
|
4
|
+
* @param enumeration -
|
|
5
|
+
* The enumeration type.
|
|
6
|
+
* @param candidate -
|
|
7
|
+
* The candidate to cast to the enum type.
|
|
8
|
+
*
|
|
9
|
+
* @returns
|
|
10
|
+
* The candidate if candidate represents the enumeration type,
|
|
11
|
+
* or undefined in the case that it does not.
|
|
12
|
+
*/
|
|
13
|
+
export declare function castEnum<TEnum extends Record<string, string | number>>(enumeration: TEnum, candidate: unknown): TEnum[keyof TEnum] | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Attempts to cast the candidate to an enumeration value.
|
|
16
|
+
*
|
|
17
|
+
* @param enumeration -
|
|
18
|
+
* The enumeration type.
|
|
19
|
+
* @param candidate -
|
|
20
|
+
* The candidate to cast to the enum type.
|
|
21
|
+
* @param fallback -
|
|
22
|
+
* The fallback to use in the case that candidate
|
|
23
|
+
* cannot represent the enumeration type.
|
|
24
|
+
*
|
|
25
|
+
* @returns
|
|
26
|
+
* The candidate if candidate represents the enumeration type,
|
|
27
|
+
* or fallback in the case that it does not.
|
|
28
|
+
*/
|
|
29
|
+
export declare function castEnum<TEnum extends Record<string, string | number>>(enumeration: TEnum, candidate: unknown, fallback: TEnum[keyof TEnum]): TEnum[keyof TEnum];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attempts to cast candidate to a number.
|
|
3
|
+
*
|
|
4
|
+
* This method assumes you want an actual number and
|
|
5
|
+
* not NaN. In the case that candidate results in
|
|
6
|
+
* NaN, then the fallback condition applies.
|
|
7
|
+
*
|
|
8
|
+
* @param candidate -
|
|
9
|
+
* The candidate to cast.
|
|
10
|
+
*
|
|
11
|
+
* @returns
|
|
12
|
+
* The casted number or undefined in the cast
|
|
13
|
+
* that casting candidate would result in NaN.
|
|
14
|
+
*/
|
|
15
|
+
export declare function castNumber(candidate: unknown): number | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Attempts to cast candidate to a number.
|
|
18
|
+
*
|
|
19
|
+
* This method assumes you want an actual number and
|
|
20
|
+
* not NaN. In the case that candidate results in
|
|
21
|
+
* NaN, then the fallback condition applies.
|
|
22
|
+
*
|
|
23
|
+
* @param candidate -
|
|
24
|
+
* The candidate to cast.
|
|
25
|
+
* @param fallback -
|
|
26
|
+
* The fallback value in the case that the result
|
|
27
|
+
* of casting candidate would result in NaN
|
|
28
|
+
*
|
|
29
|
+
* @returns
|
|
30
|
+
* The casted number or fallback in the cast
|
|
31
|
+
* that casting candidate would result in NaN.
|
|
32
|
+
*/
|
|
33
|
+
export declare function castNumber(candidate: unknown, fallback: number): number;
|
|
@@ -5,17 +5,17 @@
|
|
|
5
5
|
* since it's easy to be off by 1 when specifying
|
|
6
6
|
* a month index.
|
|
7
7
|
*/
|
|
8
|
-
export declare
|
|
9
|
-
January
|
|
10
|
-
February
|
|
11
|
-
March
|
|
12
|
-
April
|
|
13
|
-
May
|
|
14
|
-
June
|
|
15
|
-
July
|
|
16
|
-
August
|
|
17
|
-
September
|
|
18
|
-
October
|
|
19
|
-
November
|
|
20
|
-
December
|
|
21
|
-
}
|
|
8
|
+
export declare enum ZCalendarMonth {
|
|
9
|
+
January = 0,
|
|
10
|
+
February = 1,
|
|
11
|
+
March = 2,
|
|
12
|
+
April = 3,
|
|
13
|
+
May = 4,
|
|
14
|
+
June = 5,
|
|
15
|
+
July = 6,
|
|
16
|
+
August = 7,
|
|
17
|
+
September = 8,
|
|
18
|
+
October = 9,
|
|
19
|
+
November = 10,
|
|
20
|
+
December = 11
|
|
21
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Information for an enum value.
|
|
3
|
+
*
|
|
4
|
+
* This is useful for front end development when
|
|
5
|
+
* you want to map an enumeration like value to
|
|
6
|
+
* things like a display name, description, and
|
|
7
|
+
* avatar. ECMAScript does not support decorators
|
|
8
|
+
* for literal fields so this is an alternative
|
|
9
|
+
* to describe that kind of information.
|
|
10
|
+
*
|
|
11
|
+
* This is similar to Metadata from helpful-query,
|
|
12
|
+
* but it's much more specific and specialized
|
|
13
|
+
* to enumerations.
|
|
14
|
+
*/
|
|
15
|
+
export interface IZEnumInformation<TEnum> {
|
|
16
|
+
/**
|
|
17
|
+
* The display name of the enum value.
|
|
18
|
+
*/
|
|
19
|
+
name?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The avatar representation of the enum.
|
|
22
|
+
*
|
|
23
|
+
* This can be anything and is contextual of how
|
|
24
|
+
* this object is used.
|
|
25
|
+
*/
|
|
26
|
+
avatar?: any;
|
|
27
|
+
/**
|
|
28
|
+
* The description of the enum value.
|
|
29
|
+
*/
|
|
30
|
+
description?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The value being described.
|
|
33
|
+
*/
|
|
34
|
+
value: TEnum;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Builds information for an enum.
|
|
38
|
+
*/
|
|
39
|
+
export declare class ZEnumInfoBuilder<TEnum> {
|
|
40
|
+
private _metadata;
|
|
41
|
+
/**
|
|
42
|
+
* Initializes a new instance of this object.
|
|
43
|
+
*
|
|
44
|
+
* @param value -
|
|
45
|
+
* The value to initialize with.
|
|
46
|
+
*/
|
|
47
|
+
constructor(value: TEnum);
|
|
48
|
+
/**
|
|
49
|
+
* The display name of the enum.
|
|
50
|
+
*
|
|
51
|
+
* @param name -
|
|
52
|
+
* The display name of the enum.
|
|
53
|
+
*
|
|
54
|
+
* @returns
|
|
55
|
+
* This object.
|
|
56
|
+
*/
|
|
57
|
+
name(name: string): this;
|
|
58
|
+
/**
|
|
59
|
+
* Text description of the enum.
|
|
60
|
+
*
|
|
61
|
+
* @param description -
|
|
62
|
+
* Text description of what the value means.
|
|
63
|
+
*
|
|
64
|
+
* @returns
|
|
65
|
+
* This object.
|
|
66
|
+
*/
|
|
67
|
+
description(description: string): this;
|
|
68
|
+
/**
|
|
69
|
+
* The avatar representation of the enum.
|
|
70
|
+
*
|
|
71
|
+
* @param avatar -
|
|
72
|
+
* The avatar representation of what the value
|
|
73
|
+
* is. This can be anything but should be related
|
|
74
|
+
* to the framework it is being developed for.
|
|
75
|
+
*
|
|
76
|
+
* @returns
|
|
77
|
+
* This object.
|
|
78
|
+
*/
|
|
79
|
+
avatar(avatar: any): this;
|
|
80
|
+
/**
|
|
81
|
+
* Returns a shallow copy of the metadata.
|
|
82
|
+
*
|
|
83
|
+
* @returns
|
|
84
|
+
* A shallow copy of the metadata.
|
|
85
|
+
*/
|
|
86
|
+
build(): IZEnumInformation<TEnum>;
|
|
87
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets whether the candidate can represent an enumeration object of type TEnum.
|
|
3
|
+
*
|
|
4
|
+
* This is mostly helpful for type guarding value options in a type.
|
|
5
|
+
*
|
|
6
|
+
* @param enumeration -
|
|
7
|
+
* The enumeration object that contains the values.
|
|
8
|
+
* @param candidate -
|
|
9
|
+
* The candidate to check.
|
|
10
|
+
*
|
|
11
|
+
* @returns
|
|
12
|
+
* True if candidate can represents one of the white listed
|
|
13
|
+
* object values in enumeration.
|
|
14
|
+
*/
|
|
15
|
+
export declare function isEnum<TEnum extends Record<string, string | number>>(enumeration: TEnum, candidate: unknown): candidate is TEnum[keyof TEnum];
|
package/dist/index.cjs
CHANGED
|
@@ -44,7 +44,7 @@ const dateFns = require('date-fns');
|
|
|
44
44
|
return ZHorizontalAnchor;
|
|
45
45
|
}({});
|
|
46
46
|
|
|
47
|
-
function _define_property$
|
|
47
|
+
function _define_property$7(obj, key, value) {
|
|
48
48
|
if (key in obj) {
|
|
49
49
|
Object.defineProperty(obj, key, {
|
|
50
50
|
value: value,
|
|
@@ -117,10 +117,45 @@ function _define_property$6(obj, key, value) {
|
|
|
117
117
|
/**
|
|
118
118
|
* Initializes a new instance of this object.
|
|
119
119
|
*/ constructor(){
|
|
120
|
-
_define_property$
|
|
120
|
+
_define_property$7(this, "_messages", []);
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Gets whether the candidate can represent an enumeration object of type TEnum.
|
|
126
|
+
*
|
|
127
|
+
* This is mostly helpful for type guarding value options in a type.
|
|
128
|
+
*
|
|
129
|
+
* @param enumeration -
|
|
130
|
+
* The enumeration object that contains the values.
|
|
131
|
+
* @param candidate -
|
|
132
|
+
* The candidate to check.
|
|
133
|
+
*
|
|
134
|
+
* @returns
|
|
135
|
+
* True if candidate can represents one of the white listed
|
|
136
|
+
* object values in enumeration.
|
|
137
|
+
*/ function isEnum(enumeration, candidate) {
|
|
138
|
+
return candidate != null && (typeof candidate === "string" || typeof candidate === "number") && Object.values(enumeration).includes(candidate);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Attempts to cast the candidate to an enumeration value.
|
|
143
|
+
*
|
|
144
|
+
* @param enumeration -
|
|
145
|
+
* The enumeration type.
|
|
146
|
+
* @param candidate -
|
|
147
|
+
* The candidate to cast to the enum type.
|
|
148
|
+
* @param fallback -
|
|
149
|
+
* The fallback to use in the case that candidate
|
|
150
|
+
* cannot represent the enumeration type.
|
|
151
|
+
*
|
|
152
|
+
* @returns
|
|
153
|
+
* The candidate if candidate represents the enumeration type,
|
|
154
|
+
* or fallback in the case that it does not.
|
|
155
|
+
*/ function castEnum(enumeration, candidate, fallback) {
|
|
156
|
+
return isEnum(enumeration, candidate) ? candidate : fallback;
|
|
157
|
+
}
|
|
158
|
+
|
|
124
159
|
/**
|
|
125
160
|
* Puts a . in front of name if one is not already there.
|
|
126
161
|
*
|
|
@@ -163,6 +198,44 @@ function _define_property$6(obj, key, value) {
|
|
|
163
198
|
return `.${normalized}`;
|
|
164
199
|
}
|
|
165
200
|
|
|
201
|
+
/**
|
|
202
|
+
* Attempts to cast candidate to a number.
|
|
203
|
+
*
|
|
204
|
+
* This method assumes you want an actual number and
|
|
205
|
+
* not NaN. In the case that candidate results in
|
|
206
|
+
* NaN, then the fallback condition applies.
|
|
207
|
+
*
|
|
208
|
+
* @param candidate -
|
|
209
|
+
* The candidate to cast.
|
|
210
|
+
*
|
|
211
|
+
* @returns
|
|
212
|
+
* The casted number or undefined in the cast
|
|
213
|
+
* that casting candidate would result in NaN.
|
|
214
|
+
*/ /**
|
|
215
|
+
* Attempts to cast candidate to a number.
|
|
216
|
+
*
|
|
217
|
+
* This method assumes you want an actual number and
|
|
218
|
+
* not NaN. In the case that candidate results in
|
|
219
|
+
* NaN, then the fallback condition applies.
|
|
220
|
+
*
|
|
221
|
+
* @param candidate -
|
|
222
|
+
* The candidate to cast.
|
|
223
|
+
* @param fallback -
|
|
224
|
+
* The fallback value in the case that the result
|
|
225
|
+
* of casting candidate would result in NaN
|
|
226
|
+
*
|
|
227
|
+
* @returns
|
|
228
|
+
* The casted number or fallback in the cast
|
|
229
|
+
* that casting candidate would result in NaN.
|
|
230
|
+
*/ function castNumber(candidate, fallback) {
|
|
231
|
+
try {
|
|
232
|
+
const casted = Number(candidate);
|
|
233
|
+
return Number.isNaN(casted) ? fallback : casted;
|
|
234
|
+
} catch {
|
|
235
|
+
return fallback;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
166
239
|
/**
|
|
167
240
|
* Calculates the total number of buckets you need to
|
|
168
241
|
* store a number of items where each bucket can hold a
|
|
@@ -531,6 +604,96 @@ function _define_property$6(obj, key, value) {
|
|
|
531
604
|
return candidate == null || candidateType === "object" && Object.keys(candidate).length === 0;
|
|
532
605
|
}
|
|
533
606
|
|
|
607
|
+
/**
|
|
608
|
+
* Information for an enum value.
|
|
609
|
+
*
|
|
610
|
+
* This is useful for front end development when
|
|
611
|
+
* you want to map an enumeration like value to
|
|
612
|
+
* things like a display name, description, and
|
|
613
|
+
* avatar. ECMAScript does not support decorators
|
|
614
|
+
* for literal fields so this is an alternative
|
|
615
|
+
* to describe that kind of information.
|
|
616
|
+
*
|
|
617
|
+
* This is similar to Metadata from helpful-query,
|
|
618
|
+
* but it's much more specific and specialized
|
|
619
|
+
* to enumerations.
|
|
620
|
+
*/ function _define_property$6(obj, key, value) {
|
|
621
|
+
if (key in obj) {
|
|
622
|
+
Object.defineProperty(obj, key, {
|
|
623
|
+
value: value,
|
|
624
|
+
enumerable: true,
|
|
625
|
+
configurable: true,
|
|
626
|
+
writable: true
|
|
627
|
+
});
|
|
628
|
+
} else {
|
|
629
|
+
obj[key] = value;
|
|
630
|
+
}
|
|
631
|
+
return obj;
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Builds information for an enum.
|
|
635
|
+
*/ class ZEnumInfoBuilder {
|
|
636
|
+
/**
|
|
637
|
+
* The display name of the enum.
|
|
638
|
+
*
|
|
639
|
+
* @param name -
|
|
640
|
+
* The display name of the enum.
|
|
641
|
+
*
|
|
642
|
+
* @returns
|
|
643
|
+
* This object.
|
|
644
|
+
*/ name(name) {
|
|
645
|
+
this._metadata.name = name;
|
|
646
|
+
return this;
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Text description of the enum.
|
|
650
|
+
*
|
|
651
|
+
* @param description -
|
|
652
|
+
* Text description of what the value means.
|
|
653
|
+
*
|
|
654
|
+
* @returns
|
|
655
|
+
* This object.
|
|
656
|
+
*/ description(description) {
|
|
657
|
+
this._metadata.description = description;
|
|
658
|
+
return this;
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* The avatar representation of the enum.
|
|
662
|
+
*
|
|
663
|
+
* @param avatar -
|
|
664
|
+
* The avatar representation of what the value
|
|
665
|
+
* is. This can be anything but should be related
|
|
666
|
+
* to the framework it is being developed for.
|
|
667
|
+
*
|
|
668
|
+
* @returns
|
|
669
|
+
* This object.
|
|
670
|
+
*/ avatar(avatar) {
|
|
671
|
+
this._metadata.avatar = avatar;
|
|
672
|
+
return this;
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Returns a shallow copy of the metadata.
|
|
676
|
+
*
|
|
677
|
+
* @returns
|
|
678
|
+
* A shallow copy of the metadata.
|
|
679
|
+
*/ build() {
|
|
680
|
+
return {
|
|
681
|
+
...this._metadata
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Initializes a new instance of this object.
|
|
686
|
+
*
|
|
687
|
+
* @param value -
|
|
688
|
+
* The value to initialize with.
|
|
689
|
+
*/ constructor(value){
|
|
690
|
+
_define_property$6(this, "_metadata", void 0);
|
|
691
|
+
this._metadata = {
|
|
692
|
+
value
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
534
697
|
const BYTES_PER_KIBIBYTE = 1024;
|
|
535
698
|
const BYTES_PER_MEBIBYTE = 1048576; // 1024 ^ 2
|
|
536
699
|
const BYTES_PER_GIBIBYTE = 1073741824; // 1024 ^ 3
|
|
@@ -1988,6 +2151,7 @@ exports.ZAssert = ZAssert;
|
|
|
1988
2151
|
exports.ZDateFormats = ZDateFormats;
|
|
1989
2152
|
exports.ZDeserializeJson = ZDeserializeJson;
|
|
1990
2153
|
exports.ZDeserializeTry = ZDeserializeTry;
|
|
2154
|
+
exports.ZEnumInfoBuilder = ZEnumInfoBuilder;
|
|
1991
2155
|
exports.ZHorizontalAnchor = ZHorizontalAnchor;
|
|
1992
2156
|
exports.ZLazy = ZLazy;
|
|
1993
2157
|
exports.ZOrientation = ZOrientation;
|
|
@@ -1997,7 +2161,9 @@ exports.ZRectangle = ZRectangle;
|
|
|
1997
2161
|
exports.ZSerializeJson = ZSerializeJson;
|
|
1998
2162
|
exports.ZVerticalAnchor = ZVerticalAnchor;
|
|
1999
2163
|
exports.bash = bash;
|
|
2164
|
+
exports.castEnum = castEnum;
|
|
2000
2165
|
exports.castExtension = castExtension;
|
|
2166
|
+
exports.castNumber = castNumber;
|
|
2001
2167
|
exports.commaJoinDefined = commaJoinDefined;
|
|
2002
2168
|
exports.countBuckets = countBuckets;
|
|
2003
2169
|
exports.createError = createError;
|
|
@@ -2016,6 +2182,7 @@ exports.gibibytes = gibibytes;
|
|
|
2016
2182
|
exports.guessDateTime = guessDateTime;
|
|
2017
2183
|
exports.html = html;
|
|
2018
2184
|
exports.isEmptyObject = isEmptyObject;
|
|
2185
|
+
exports.isEnum = isEnum;
|
|
2019
2186
|
exports.javascript = javascript;
|
|
2020
2187
|
exports.joinDefined = joinDefined;
|
|
2021
2188
|
exports.js = js;
|