1
1
/**
2
- * Converts an array of strings to a comma-separated sentence
3
- * @param items {Array<string>}
4
- * @returns {string } Returns a string with the items joined by a comma and the last item joined by ", or"
2
+ * Convert words to a sentence.
3
+ *
4
+ * @param items - An array of words to be joined.
5
+ * @returns A string with the items joined by a comma and the last item joined by ", or".
5
6
*/
6
7
export const toSentence = ( items : string [ ] ) : string => {
7
8
// TODO: Once Safari supports it, use Intl.ListFormat
@@ -19,19 +20,41 @@ export const toSentence = (items: string[]): string => {
19
20
const IP_V4_ADDRESS_REGEX =
20
21
/ ^ ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) \. ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) \. ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) \. ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) $ / ;
21
22
23
+ /**
24
+ * Checks if a string is a valid IPv4 address.
25
+ *
26
+ * @returns True if the string is a valid IPv4 address, false otherwise.
27
+ */
22
28
export function isIPV4Address ( str : string | undefined | null ) : boolean {
23
29
return IP_V4_ADDRESS_REGEX . test ( str || '' ) ;
24
30
}
25
31
32
+ /**
33
+ * Converts the first character of a string to uppercase.
34
+ *
35
+ * @param str - The string to be converted.
36
+ * @returns The modified string with the rest of the string unchanged.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * titleize('hello world') // 'Hello world'
41
+ * ```
42
+ */
26
43
export function titleize ( str : string | undefined | null ) : string {
27
44
const s = str || '' ;
28
45
return s . charAt ( 0 ) . toUpperCase ( ) + s . slice ( 1 ) ;
29
46
}
30
47
48
+ /**
49
+ * Converts a string from snake_case to camelCase.
50
+ */
31
51
export function snakeToCamel ( str : string | undefined ) : string {
32
52
return str ? str . replace ( / ( [ - _ ] [ a - z ] ) / g, match => match . toUpperCase ( ) . replace ( / - | _ / , '' ) ) : '' ;
33
53
}
34
54
55
+ /**
56
+ * Converts a string from camelCase to snake_case.
57
+ */
35
58
export function camelToSnake ( str : string | undefined ) : string {
36
59
return str ? str . replace ( / [ A - Z ] / g, letter => `_${ letter . toLowerCase ( ) } ` ) : '' ;
37
60
}
@@ -73,6 +96,7 @@ const createDeepObjectTransformer = (transform: any) => {
73
96
* Transforms camelCased objects/ arrays to snake_cased.
74
97
* This function recursively traverses all objects and arrays of the passed value
75
98
* camelCased keys are removed.
99
+ *
76
100
* @function
77
101
*/
78
102
export const deepCamelToSnake = createDeepObjectTransformer ( camelToSnake ) ;
@@ -81,13 +105,15 @@ export const deepCamelToSnake = createDeepObjectTransformer(camelToSnake);
81
105
* Transforms snake_cased objects/ arrays to camelCased.
82
106
* This function recursively traverses all objects and arrays of the passed value
83
107
* camelCased keys are removed.
108
+ *
84
109
* @function
85
110
*/
86
111
export const deepSnakeToCamel = createDeepObjectTransformer ( snakeToCamel ) ;
87
112
88
113
/**
89
- * Returns true for `true`, true, positive numbers.
90
- * Returns false for `false`, false, 0, negative integers and anything else.
114
+ * A function to determine if a value is truthy.
115
+ *
116
+ * @returns True for `true`, true, positive numbers. False for `false`, false, 0, negative integers and anything else.
91
117
*/
92
118
export function isTruthy ( value : unknown ) : boolean {
93
119
// Return if Boolean
@@ -125,6 +151,9 @@ export function isTruthy(value: unknown): boolean {
125
151
return false ;
126
152
}
127
153
154
+ /**
155
+ * Get all non-undefined values from an object.
156
+ */
128
157
export function getNonUndefinedValues < T extends object > ( obj : T ) : Partial < T > {
129
158
return Object . entries ( obj ) . reduce ( ( acc , [ key , value ] ) => {
130
159
if ( value !== undefined ) {
0 commit comments