CyrefJS
CCyrefJS
فا
Back to documentation
String/Since 1.38.0

constantCase

Converts a string into CONSTANT_CASE by tokenizing it, converting every word to uppercase, and joining the words with underscores.

constantCase(value):string

Behavior

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

valuestringrequired

The string to convert to CONSTANT_CASE.

Returns

string

The tokenized input converted to uppercase and joined with underscores.

Examples

01

Basic usage

Convert a space-separated phrase to CONSTANT_CASE.

TypeScript
import { constantCase } from "@cyref/js";

constantCase("hello world");
// "HELLO_WORLD"
02

Multiple words

Each token is converted to uppercase and separated with an underscore.

TypeScript
constantCase("hello beautiful world");
// "HELLO_BEAUTIFUL_WORLD"
03

Mixed case

The casing of every token is normalized to uppercase.

TypeScript
constantCase("hello WoRLD TeST");
// "HELLO_WORLD_TEST"
04

Single word

A single token is simply converted to uppercase.

TypeScript
constantCase("hello");
// "HELLO"
05

Empty input

If tokenization produces no words, an empty string is returned.

TypeScript
constantCase("");
// ""
06

Existing separators

The input is processed through tokenize before the resulting words are joined with underscores.

TypeScript
constantCase("hello-world");
// "HELLO_WORLD"
Built with@cyref/js·Official documentation