-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtoLowerCase.ts
More file actions
46 lines (41 loc) · 1.24 KB
/
toLowerCase.ts
File metadata and controls
46 lines (41 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { requireNonEmptyString } from "./requireNonEmptyString";
interface ToLowerCaseOptions {
trim?: boolean;
}
/**
* Returns a new string converted to lowercase.
*
* @example
* ```javascript
* const text = " Hello world! ";
* const result = toLowerCase(text);
*
* console.log(result); // hello world!
*
* const trimmedResult = toLowerCase(text, { trim: true });
* console.log(trimmedResult); // hello world!
* ```
*
* @param {string} value - The input string to convert.
* @param {ToLowerCaseOptions} [options] - Optional configuration options.
* @returns {string} The string converted to lowercase, with optional whitespace normalization.
* @throws {@link EmptyStringException}
* @see {@link toCamelCase}
* @see {@link toKebabCase}
* @see {@link toProperCase}
* @see {@link toSnakeCase}
* @see {@link toUpperCase}
* @since 1.0.0
* @version 1.0.0
*/
export function toLowerCase(value: string, options: ToLowerCaseOptions = {}): string {
const effectiveOptions: Required<ToLowerCaseOptions> = {
trim: false,
...options
};
let result = requireNonEmptyString(value).toLowerCase();
if (effectiveOptions.trim === true) {
result = result.trim().replace(/\s+/g, " ");
}
return result;
}