Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(text): cases #4082

Merged
merged 21 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions text/_util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

export function split(input: string) {
input = input.trim();
if (input.includes(" ")) return input.split(/ +/).filter(Boolean);
if (input.includes("-")) return input.split(/-+/).filter(Boolean);
if (input.includes("_")) return input.split(/_+/).filter(Boolean);
timreichen marked this conversation as resolved.
Show resolved Hide resolved
return input.split(/(?=[A-Z])+/).filter(Boolean);
}
export function capitalizeWord(word: string): string {
if (!word) return word;
const firstChar = word[0];
const firstCharAsUpperCase = firstChar.toLocaleUpperCase();
const restOfTheString = word.slice(1).toLocaleLowerCase();
return `${firstCharAsUpperCase}${restOfTheString}`;
timreichen marked this conversation as resolved.
Show resolved Hide resolved
}
138 changes: 138 additions & 0 deletions text/_util_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assertEquals } from "../assert/assert_equals.ts";
import { split } from "./_util.ts";

Deno.test({
name: "split() handles whitespace",
fn() {
const result = split("deno Is AWESOME");
const expected = ["deno", "Is", "AWESOME"];
assertEquals(result, expected);
},
});

Deno.test({
name: "split() trims input",
async fn(t) {
await t.step({
name: "whitespace",
fn() {
const result = split(" deno Is AWESOME ");
const expected = ["deno", "Is", "AWESOME"];
assertEquals(result, expected);
},
});
await t.step({
name: "camel case",
fn() {
const result = split(" denoIsAwesome ");
const expected = ["deno", "Is", "Awesome"];
assertEquals(result, expected);
},
});
await t.step({
name: "kebab case",
fn() {
const result = split("--deno--is-awesome-");
const expected = ["deno", "is", "awesome"];
assertEquals(result, expected);
},
});
await t.step({
name: "screaming snake case",
fn() {
const result = split(" __DENO__IS_AWESOME_ ");
const expected = ["DENO", "IS", "AWESOME"];
assertEquals(result, expected);
},
});
await t.step({
name: "sentence case",
fn() {
const result = split(" Deno is awesome ");
const expected = ["Deno", "is", "awesome"];
assertEquals(result, expected);
},
});
await t.step({
name: "snake case",
fn() {
const result = split(" __deno__is_awesome_ ");
const expected = ["deno", "is", "awesome"];
assertEquals(result, expected);
},
});
await t.step({
name: "title case",
fn() {
const result = split(" Deno Is Awesome ");
const expected = ["Deno", "Is", "Awesome"];
assertEquals(result, expected);
},
});
},
});

Deno.test({
name: "split() handles camel case",
fn() {
const result = split("denoIsAwesome");
const expected = ["deno", "Is", "Awesome"];
assertEquals(result, expected);
},
});

Deno.test({
name: "split() handles kebab case",
fn() {
const result = split("deno-is-awesome");
const expected = ["deno", "is", "awesome"];
assertEquals(result, expected);
},
});

Deno.test({
name: "split() handles pascal case",
fn() {
const result = split("DenoIsAwesome");
const expected = ["Deno", "Is", "Awesome"];
assertEquals(result, expected);
},
});

Deno.test({
name: "split() handles screaming snake case",
fn() {
const result = split("DENO_IS_AWESOME");
const expected = ["DENO", "IS", "AWESOME"];
assertEquals(result, expected);
},
});

Deno.test({
name: "split() handles sentence case",
fn() {
const result = split("Deno is awesome");
const expected = ["Deno", "is", "awesome"];
assertEquals(result, expected);
},
});

Deno.test({
name: "split() handles snake case",
fn() {
const result = split("deno_is_awesome");
const expected = ["deno", "is", "awesome"];
assertEquals(result, expected);
},
});

Deno.test({
name: "split() handles title case",
fn() {
const result = split("Deno Is Awesome");
const expected = ["Deno", "Is", "Awesome"];
assertEquals(result, expected);
},
});
132 changes: 132 additions & 0 deletions text/case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { capitalizeWord, split } from "./_util.ts";

/**
* Converts a input into camelCase
timreichen marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* ```ts
* import { toCamelCase } from "https://deno.land/std@$STD_VERSION/text/case.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
*
* assertEquals(toCamelCase("deno is awesome"), "denoIsAwesome");
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
* ```
*
* @param input The string that is going to be converted into camelCase
* @returns The string as camelCase
*/
export function toCamelCase(input: string): string {
const [first = "", ...rest] = split(input);
return [first.toLocaleLowerCase(), ...rest.map(capitalizeWord)].join("");
}

/**
* Converts a input into kebab-case
*
* @example
* ```ts
* import { toKebabCase } from "https://deno.land/std@$STD_VERSION/text/case.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
*
* assertEquals(toKebabCase("deno is awesome"), "deno-is-awesome");
* ```
*
* @param input is the string that is going to be converted into kebab-case
* @returns The string as kebab-case
*/
export function toKebabCase(input: string): string {
return split(input).join("-").toLocaleLowerCase();
}

/**
* Converts a input into PascalCase
*
* @example
* ```ts
* import { toPascalCase } from "https://deno.land/std@$STD_VERSION/text/case.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
*
* assertEquals(toPascalCase("deno is awesome"), "DenoIsAwesome");
* ```
*
* @param input The string that is going to be converted into PascalCase
* @returns The string as PascalCase
*/
export function toPascalCase(input: string): string {
return split(input).map(capitalizeWord).join("");
}

/**
* Converts a input into SCREAMING_SNAKE_CASE
*
* @example
* ```ts
* import { toScreamingSnakeCase } from "https://deno.land/std@$STD_VERSION/text/case.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
*
* assertEquals(toScreamingSnakeCase("deno is awesome"), "DENO_IS_AWESOME");
* ```
*
* @param input is the string that is going to be converted into SCREAMING_SNAKE_CASE
* @returns The string as SCREAMING_SNAKE_CASE
*/
export function toScreamingSnakeCase(input: string): string {
return toSnakeCase(input).toLocaleUpperCase();
}
timreichen marked this conversation as resolved.
Show resolved Hide resolved

/**
* Converts a input into Sentence case
*
* @example
* ```ts
* import { toSentenceCase } from "https://deno.land/std@$STD_VERSION/text/case.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
* assertEquals(toSentenceCase("deno is awesome"), "Deno is awesome");
* ```
*
* @param input is the string that is going to be converted into Sentence case
* @returns The string as Sentence case
*/
export function toSentenceCase(input: string): string {
const [first = "", ...rest] = split(input);
return [
capitalizeWord(first),
...rest.map((word) => word.toLocaleLowerCase()),
].join(" ");
}

/**
* Converts a input into snake_case
*
* @example
* ```ts
* import { toSnakeCase } from "https://deno.land/std@$STD_VERSION/text/case.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
* assertEquals(toSnakeCase("deno is awesome"), "deno_is_awesome");
* ```
*
* @param input is the string that is going to be converted into snake_case
* @returns The string as snake_case
*/
export function toSnakeCase(input: string): string {
return split(input).join("_").toLocaleLowerCase();
}

/**
* Converts a input into Title Case
*
* @example
* ```ts
* import { toTitleCase } from "https://deno.land/std@$STD_VERSION/text/case.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
*
* assertEquals(toTitleCase("deno is awesome"), "Deno Is Awesome");
* ```
*
* @param input is the string that is going to be converted into Title Case
* @returns The string as Title Case
*/
export function toTitleCase(input: string): string {
return split(input).map(capitalizeWord).join(" ");
}
Loading