std/text/to_constant_case.ts
GuyBorderless abdecb30b7
feat(text/unstable): add toConstantCase() (#5110)
* feat(text): added toConstantCase

* move to to_constant_case.ts

* mark as unstable API

* fix import in example

* unstable notice

---------

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
2024-07-22 00:49:57 +00:00

31 lines
839 B
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { splitToWords } from "./_util.ts";
/**
* Converts a string into CONSTANT_CASE (also known as SCREAMING_SNAKE_CASE).
*
* > [!WARNING]
* > **UNSTABLE**: New API, yet to be vetted.
*
* @experimental
*
* @example Usage
* ```ts
* import { toConstantCase } from "@std/text/to-constant-case";
* import { assertEquals } from "@std/assert/equals";
*
* assertEquals(toConstantCase("deno is awesome"), "DENO_IS_AWESOME");
* ```
*
* @param input The string that is going to be converted into CONSTANT_CASE
* @returns The string as CONSTANT_CASE
*
* @experimental
*/
export function toConstantCase(input: string): string {
input = input.trim();
return splitToWords(input).join("_").toLocaleUpperCase();
}