@bytebury/toolkit 1.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.
- package/LICENSE +21 -0
- package/README.md +49 -0
- package/esm/_dnt.test_polyfills.d.ts.map +1 -0
- package/esm/_dnt.test_shims.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/almost_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/array_includes.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assert.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equal.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/exists.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/fail.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/false.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/instance_of.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/is_error.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less_or_equal.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/match.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_instance_of.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_match.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/object_match.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/rejects.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/strict_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/string_includes.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/throws.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unimplemented.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unreachable.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/format.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/styles.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/types.d.ts.map +1 -0
- package/esm/mod.d.ts +7 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +6 -0
- package/esm/package.json +3 -0
- package/esm/src/core.d.ts +297 -0
- package/esm/src/core.d.ts.map +1 -0
- package/esm/src/core.js +256 -0
- package/esm/src/core_test.d.ts.map +1 -0
- package/esm/src/dates.d.ts +113 -0
- package/esm/src/dates.d.ts.map +1 -0
- package/esm/src/dates.js +197 -0
- package/esm/src/dates_test.d.ts.map +1 -0
- package/esm/src/duration.d.ts +102 -0
- package/esm/src/duration.d.ts.map +1 -0
- package/esm/src/duration.js +110 -0
- package/esm/src/duration_test.d.ts.map +1 -0
- package/esm/src/numbers.d.ts +58 -0
- package/esm/src/numbers.d.ts.map +1 -0
- package/esm/src/numbers.js +76 -0
- package/esm/src/numbers_test.d.ts.map +1 -0
- package/esm/src/strings.d.ts +164 -0
- package/esm/src/strings.d.ts.map +1 -0
- package/esm/src/strings.js +196 -0
- package/esm/src/strings_test.d.ts.map +1 -0
- package/esm/src/utility_types.d.ts +117 -0
- package/esm/src/utility_types.d.ts.map +1 -0
- package/esm/src/utility_types.js +1 -0
- package/package.json +28 -0
- package/script/_dnt.test_polyfills.d.ts.map +1 -0
- package/script/_dnt.test_shims.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/almost_equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/array_includes.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/assert.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/equal.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/exists.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/fail.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/false.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/greater.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/instance_of.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/is_error.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/less.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/less_or_equal.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/match.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/mod.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_instance_of.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_match.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/object_match.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/rejects.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/strict_equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/string_includes.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/throws.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/unimplemented.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/unreachable.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/build_message.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/diff.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/diff_str.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/format.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/styles.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/types.d.ts.map +1 -0
- package/script/mod.d.ts +7 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +22 -0
- package/script/package.json +3 -0
- package/script/src/core.d.ts +297 -0
- package/script/src/core.d.ts.map +1 -0
- package/script/src/core.js +279 -0
- package/script/src/core_test.d.ts.map +1 -0
- package/script/src/dates.d.ts +113 -0
- package/script/src/dates.d.ts.map +1 -0
- package/script/src/dates.js +224 -0
- package/script/src/dates_test.d.ts.map +1 -0
- package/script/src/duration.d.ts +102 -0
- package/script/src/duration.d.ts.map +1 -0
- package/script/src/duration.js +114 -0
- package/script/src/duration_test.d.ts.map +1 -0
- package/script/src/numbers.d.ts +58 -0
- package/script/src/numbers.d.ts.map +1 -0
- package/script/src/numbers.js +81 -0
- package/script/src/numbers_test.d.ts.map +1 -0
- package/script/src/strings.d.ts +164 -0
- package/script/src/strings.d.ts.map +1 -0
- package/script/src/strings.js +209 -0
- package/script/src/strings_test.d.ts.map +1 -0
- package/script/src/utility_types.d.ts +117 -0
- package/script/src/utility_types.d.ts.map +1 -0
- package/script/src/utility_types.js +2 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isWhitespace = isWhitespace;
|
|
4
|
+
exports.isNotWhitespace = isNotWhitespace;
|
|
5
|
+
exports.trim = trim;
|
|
6
|
+
exports.title = title;
|
|
7
|
+
exports.lower = lower;
|
|
8
|
+
exports.upper = upper;
|
|
9
|
+
exports.kebab = kebab;
|
|
10
|
+
exports.snake = snake;
|
|
11
|
+
exports.keepAlphabetical = keepAlphabetical;
|
|
12
|
+
exports.keepAlphanumeric = keepAlphanumeric;
|
|
13
|
+
exports.keepNumeric = keepNumeric;
|
|
14
|
+
/**
|
|
15
|
+
* Determines if the given text is only comprised of whitespace.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* This is `null | undefined` safe. If you pass `null | undefined` then this will return `true`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* isWhitespace(null); // true
|
|
23
|
+
* isWhitespace(" "); // true
|
|
24
|
+
* isWhitespace("\n\t"); // true
|
|
25
|
+
* isWhitespace("Hello"); // false
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
function isWhitespace(text) {
|
|
29
|
+
return trim(text).length === 0;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Determines if the given text contains any non-whitespace characters.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* This is `null | undefined` safe. If you pass `null | undefined` then this will return `false`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* isNotWhitespace(null); // false
|
|
40
|
+
* isNotWhitespace(" "); // false
|
|
41
|
+
* isNotWhitespace("Hello"); // true
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function isNotWhitespace(text) {
|
|
45
|
+
return !isWhitespace(text);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Trims the whitespace from the beginning and the end. This is an
|
|
49
|
+
* alias for `.trim()`. Useful for when you're mapping over lists.
|
|
50
|
+
*
|
|
51
|
+
* @remarks
|
|
52
|
+
* This is `null | undefined` safe. If you pass `null | undefined` then this will return `""`.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* trim(null); // ""
|
|
57
|
+
* trim(" HELLO WORLD \n"); // "HELLO WORLD"
|
|
58
|
+
* [' hello ', ' world '].map(trim); // ['hello', 'world'];
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
function trim(text) {
|
|
62
|
+
return (text || "").trim();
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Converts the string to Title Case. This will capitalize
|
|
66
|
+
* the letter of each word that is separated by a space. Underscores
|
|
67
|
+
* are considered spaces. Hyphens are respected.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* This is `null | undefined` safe. If you pass `null | undefined` then this will return `""`.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* title(null); // ""
|
|
75
|
+
* title("hello world"); // Hello World
|
|
76
|
+
* title("hello-world"); // Hello-world
|
|
77
|
+
* title("hello_world"); // Hello World
|
|
78
|
+
* title("HELLO wORLD"); // Hello World
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
function title(text) {
|
|
82
|
+
return lower(text)
|
|
83
|
+
.replace(/_/g, " ")
|
|
84
|
+
.split(" ")
|
|
85
|
+
.map((word) => upper(word.charAt(0)) + word.slice(1))
|
|
86
|
+
.join(" ");
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Converts the string to lowercase.
|
|
90
|
+
* An alias for `toLowerCase()`.
|
|
91
|
+
*
|
|
92
|
+
* @remarks
|
|
93
|
+
* This is `null | undefined` safe. If you pass `null | undefined` then this will return `""`.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* lower(null); // ""
|
|
98
|
+
* lower("Hello WORLD"); // "hello world"
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
function lower(text) {
|
|
102
|
+
return (text || "").toLowerCase();
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Converts the string to uppercase.
|
|
106
|
+
* An alias for `toUpperCase()`.
|
|
107
|
+
*
|
|
108
|
+
* @remarks
|
|
109
|
+
* This is `null | undefined` safe. If you pass `null | undefined` then this will return `""`.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* upper(null); // ""
|
|
114
|
+
* upper("Hello world"); // "HELLO WORLD"
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
function upper(text) {
|
|
118
|
+
return (text || "").toUpperCase();
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Converts the string to `kebab-case` by removing punctuation,
|
|
122
|
+
* trimming extra spaces, converting to lowercase, and joining words with hyphens.
|
|
123
|
+
*
|
|
124
|
+
* @remarks
|
|
125
|
+
* This is `null | undefined` safe. If you pass `null | undefined` then this will return `""`.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* kebab(null); // ""
|
|
130
|
+
* kebab("Hello World!"); // "hello-world"
|
|
131
|
+
* kebab(" Clean THIS_up!! "); // "clean-this-up"
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
function kebab(text) {
|
|
135
|
+
return trim(removePunctuation(lower(text))).replace(/\s+/g, "-");
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Converts a string to `snake_case` by replacing spaces and punctuation with underscores.
|
|
139
|
+
* Uses `kebab()` internally, replacing hyphens with underscores.
|
|
140
|
+
*
|
|
141
|
+
* @remarks
|
|
142
|
+
* This is `null | undefined` safe. If you pass `null | undefined` then this will return `""`.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* snake(null); // ""
|
|
147
|
+
* snake("Hello World"); // "hello_world"
|
|
148
|
+
* snake("User-Profile Page"); // "user_profile_page"
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
function snake(text) {
|
|
152
|
+
return kebab(text).replace(/-/g, "_");
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Returns a copy of the given string with all non-alpha characters removed,
|
|
156
|
+
* except for spaces. Letters are preserved regardless of case.
|
|
157
|
+
*
|
|
158
|
+
* @remarks
|
|
159
|
+
* This is `null | undefined` safe. If you pass `null | undefined` then this will return `""`.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* keepAlphabetical(null); // ""
|
|
164
|
+
* keepAlphabetical("Hello, World!"); // "Hello World"
|
|
165
|
+
* keepAlphabetical("123@#Test"); // "Test"
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
function keepAlphabetical(text) {
|
|
169
|
+
return (text || "").replace(/[^a-z ]/gi, "");
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Returns a copy of the given string with all non-alphanumeric characters removed,
|
|
173
|
+
* except for spaces. Letters and digits are preserved regardless of case.
|
|
174
|
+
*
|
|
175
|
+
* @remarks
|
|
176
|
+
* This is `null | undefined` safe. If you pass `null | undefined` then this will return `""`.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* keepAlphanumeric(null); // ""
|
|
181
|
+
* keepAlphanumeric("Hello, World!"); // "Hello World"
|
|
182
|
+
* keepAlphanumeric("123@#Test"); // "123Test"
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
function keepAlphanumeric(text) {
|
|
186
|
+
return (text || "").replace(/[^a-z0-9 ]/gi, "");
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Removes any non-numeric characters. This includes spaces.
|
|
190
|
+
*
|
|
191
|
+
* @remarks
|
|
192
|
+
* This is `null | undefined` safe. If you pass `null | undefined` then this will return `""`.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* keepNumeric(null); // ""
|
|
197
|
+
* keepNumeric('(555) 555-5555'); // 5555555555
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
function keepNumeric(text) {
|
|
201
|
+
return (text || "").replace(/[^\d]/g, "");
|
|
202
|
+
}
|
|
203
|
+
function removePunctuation(text) {
|
|
204
|
+
return text
|
|
205
|
+
.normalize("NFKD")
|
|
206
|
+
.replace(/[-_]/g, " ")
|
|
207
|
+
.replace(/\s+/g, " ")
|
|
208
|
+
.replace(/[^a-zA-Z0-9\s]/g, "");
|
|
209
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strings_test.d.ts","sourceRoot":"","sources":["../../src/src/strings_test.ts"],"names":[],"mappings":"AAAA,OAAO,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is a utility type that allows you to Autocomplete a string.
|
|
3
|
+
*
|
|
4
|
+
* For example, let's say you have a situation where you would like an input
|
|
5
|
+
* autocompleted with suggestion, but you also want people to be able to type
|
|
6
|
+
* whatever they want. This would come in handy.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* // You will get autocomplete suggestions for 'free', 'paid', and 'admin'.
|
|
11
|
+
* // but, you can also type anything you want.
|
|
12
|
+
* function setUserRole(role: AutoComplete<'free' | 'paid' | 'admin'>): void {
|
|
13
|
+
* // omitted
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export type AutoComplete<T extends string> = T | (string & Record<PropertyKey, unknown>);
|
|
18
|
+
/**
|
|
19
|
+
* This is a utility type that allows you to get the keys from an object or list.
|
|
20
|
+
*
|
|
21
|
+
* If `T` is a type of list, then this behaves the same as `ValuesOf`.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const MyEnum = { foo: 'bar', another: 'baz' } as const;
|
|
26
|
+
* const MyList = ['apple', 'banana', 'orange'] as const;
|
|
27
|
+
*
|
|
28
|
+
* function example(param: KeyOf<typeof MyEnum>): void {
|
|
29
|
+
* // param would be: "foo" | "another"
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* function example2(param: KeyOf<typeof MyList): void {
|
|
33
|
+
* // param would be: "apple" | "banana" | "orange"
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export type KeyOf<T> = T extends Readonly<unknown[]> ? ValueOf<T> : keyof T;
|
|
38
|
+
/**
|
|
39
|
+
* This is a utility type that allows you to get the keys from a list.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const MyList = ['apple', 'banana', 'orange'] as const;
|
|
44
|
+
*
|
|
45
|
+
* function example2(param: OneOf<typeof MyList>): void {
|
|
46
|
+
* // param would be: "apple" | "banana" | "orange"
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export type OneOf<T extends Readonly<unknown[]>> = T[number];
|
|
51
|
+
/**
|
|
52
|
+
* This is a utility type that allows you to get the values from an object
|
|
53
|
+
* or a list.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* const MyEnum = { foo: 'bar', another: 'baz' } as const;
|
|
58
|
+
* const MyList = ['apple', 'banana', 'orange'] as const;
|
|
59
|
+
*
|
|
60
|
+
* function example(param: ValueOf<typeof MyEnum>): void {
|
|
61
|
+
* // param would be: "bar" | "baz"
|
|
62
|
+
* }
|
|
63
|
+
*
|
|
64
|
+
* function example2(param: ValueOf<typeof MyList): void {
|
|
65
|
+
* // param would be: "apple" | "banana" | "orange"
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export type ValueOf<T> = T extends Readonly<unknown[]> ? T[number] : T[keyof T];
|
|
70
|
+
/** Represents any primitive ECMAScript value. */
|
|
71
|
+
export type Primitive = string | number | boolean | bigint | symbol | null | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Represents when there can be one or many of someting
|
|
74
|
+
*/
|
|
75
|
+
export type OneOrMany<T> = T | T[];
|
|
76
|
+
/**
|
|
77
|
+
* Represents a non-empty list.
|
|
78
|
+
*/
|
|
79
|
+
export type NonEmptyList<T> = [T, ...T[]];
|
|
80
|
+
/**
|
|
81
|
+
* Represents a thing that might be `None` (null or undefined).
|
|
82
|
+
*/
|
|
83
|
+
export type Option<T> = T | None;
|
|
84
|
+
/**
|
|
85
|
+
* A utility type that takes an object type and makes the hover overlay
|
|
86
|
+
* more readable for the developer.
|
|
87
|
+
*/
|
|
88
|
+
export type Prettify<T> = {
|
|
89
|
+
[K in keyof T]: T[K];
|
|
90
|
+
} & {};
|
|
91
|
+
/**
|
|
92
|
+
* Syntactic sugar for `null | undefined`.
|
|
93
|
+
*/
|
|
94
|
+
export type None = null | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* Represents a type that is branded with a unique symbol.
|
|
97
|
+
*
|
|
98
|
+
* Useful for creating types that are unique to your application.
|
|
99
|
+
*
|
|
100
|
+
* @remarks
|
|
101
|
+
* We use Branded types in our `Duration` class to remove the guess-work
|
|
102
|
+
* and make the code more readable in regards to time units.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* type Seconds = Brand<number, 'Seconds'>;
|
|
107
|
+
* type Milliseconds = Brand<number, 'Milliseconds'>;
|
|
108
|
+
*
|
|
109
|
+
* function secondsToMilliseconds(seconds: Seconds): Milliseconds {
|
|
110
|
+
* return seconds * 1000;
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export type Brand<T, B> = T & {
|
|
115
|
+
__brand: B;
|
|
116
|
+
};
|
|
117
|
+
//# sourceMappingURL=utility_types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utility_types.d.ts","sourceRoot":"","sources":["../../src/src/utility_types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,IACrC,CAAC,GACD,CAAC,MAAM,GAAG,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC;AAE5C;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,SAAS,QAAQ,CAAC,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC;AAE5E;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,QAAQ,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC;AAE7D;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,IAAI,CAAC,SAAS,QAAQ,CAAC,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAEhF,iDAAiD;AACjD,MAAM,MAAM,SAAS,GACjB,MAAM,GACN,MAAM,GACN,OAAO,GACP,MAAM,GACN,MAAM,GACN,IAAI,GACJ,SAAS,CAAC;AAEd;;GAEG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;AAEnC;;GAEG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC;AAE1C;;GAEG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;AAEjC;;;GAGG;AAEH,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,GAAG,EAAE,CAAC;AAExD;;GAEG;AACH,MAAM,MAAM,IAAI,GAAG,IAAI,GAAG,SAAS,CAAC;AAEpC;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG;IAAE,OAAO,EAAE,CAAC,CAAA;CAAE,CAAC"}
|