1.38.0constantCase
Converts a string into CONSTANT_CASE by tokenizing it, converting every word to uppercase, and joining the words with underscores.
constantCase(value):stringBehavior
constantCase first tokenizes the input into words. Each word is converted entirely to uppercase, and the resulting words are joined using an underscore.
Tokenization
Word extraction is delegated to the internal tokenize utility. Therefore, the exact word boundaries depend on tokenize.
Uppercase conversion
Every token is converted to uppercase before the tokens are joined.
Separator
The resulting tokens are joined with a single underscore character.
Parameters
valuestringrequiredThe string to convert to CONSTANT_CASE.
Returns
stringThe tokenized input converted to uppercase and joined with underscores.
Examples
Basic usage
Convert a space-separated phrase to CONSTANT_CASE.
import { constantCase } from "@cyref/js";
constantCase("hello world");
// "HELLO_WORLD"Multiple words
Each token is converted to uppercase and separated with an underscore.
constantCase("hello beautiful world");
// "HELLO_BEAUTIFUL_WORLD"Mixed case
The casing of every token is normalized to uppercase.
constantCase("hello WoRLD TeST");
// "HELLO_WORLD_TEST"Single word
A single token is simply converted to uppercase.
constantCase("hello");
// "HELLO"Empty input
If tokenization produces no words, an empty string is returned.
constantCase("");
// ""Existing separators
The input is processed through tokenize before the resulting words are joined with underscores.
constantCase("hello-world");
// "HELLO_WORLD"