Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions README.md

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions docs/feature-backlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ Notes:

- `strTruncate` (with optional suffix)
- `strCount` (substring occurrences)
- `strReplaceAll` wrapper/polyfill path **(ECMAScript 2021 / ES12 language feature wrapper)**
- `strCapitalizeWords`

### D. Iterator and Collection Helpers (Medium Value)

Expand Down
8 changes: 7 additions & 1 deletion docs/usage-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ String manipulation with built-in polyfill support:
import {
strTrim, strStartsWith, strEndsWith, strIncludes,
strLeft, strRight, strSubstring, strIsNullOrEmpty,
strCamelCase, strKebabCase, strSnakeCase
strCamelCase, strCapitalizeWords, strKebabCase, strSnakeCase
} from "@nevware21/ts-utils";

const text = " Hello World! ";
Expand All @@ -166,9 +166,15 @@ const rightPart = strRight(trimmed, 7); // "World!"

// Case transformations
const camelCase = strCamelCase("hello-world"); // "helloWorld"
const titleCase = strCapitalizeWords("hELLo-world from_ts-utils"); // "Hello-World From_Ts-Utils"
const kebabCase = strKebabCase("helloWorld"); // "hello-world"
const snakeCase = strSnakeCase("helloWorld"); // "hello_world"

// Conversion behavior differences
// strCapitalizeWords: keeps separators and lowercases the rest of each word
// strCamelCase: removes separators and joins words
// strKebabCase / strSnakeCase: normalize into a single delimiter style

// Check for empty strings
if (!strIsNullOrEmpty(text)) {
// Safe to work with text
Expand Down
2 changes: 1 addition & 1 deletion lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export { objPreventExtensions, objIsExtensible } from "./object/prevent_extensio
export { objPropertyIsEnumerable } from "./object/property_is_enumerable";
export { objSetPrototypeOf } from "./object/set_proto";
export { objIsFrozen, objIsSealed } from "./object/object_state";
export { strCamelCase, strKebabCase, strLetterCase, strSnakeCase } from "./string/conversion";
export { strCamelCase, strCapitalizeWords, strKebabCase, strLetterCase, strSnakeCase } from "./string/conversion";
export { strEndsWith } from "./string/ends_with";
export { strContains, strIncludes, polyStrIncludes } from "./string/includes";
export { strIndexOf, strLastIndexOf } from "./string/index_of";
Expand Down
32 changes: 30 additions & 2 deletions lib/src/string/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,36 @@ export function strLetterCase<T>(value: T): string {
}

/**
* Convert the provided value to `camelCased` string, you can optionally specifify whether the
* first caracter is upper cased (lowercase by default)from kebab `-` or snake `_` case.
* Convert the provided value to `Capitalized Words`, where each detected word starts with an
* uppercase character and the remaining characters of that word are lowercased.
*
* Word boundaries are detected using standard `\b` boundaries and `_` separators, so existing
* separators (spaces, punctuation, `_`, and `-`) are retained.
* If the value is not a string it will be converted.
* @since 0.14.0
* @group String
* @group Conversion
* @param value - The value to be converted to capitalized words
* @returns The Capitalized Words version of the provided value
* @example
* ```ts
* strCapitalizeWords(null); // "Null"
* strCapitalizeWords(undefined); // "Undefined"
* strCapitalizeWords("hello darkness"); // "Hello Darkness"
* strCapitalizeWords("hELLo dARKness"); // "Hello Darkness"
* strCapitalizeWords("hello_darkness"); // "Hello_Darkness"
* strCapitalizeWords("hello-darkness"); // "Hello-Darkness"
* strCapitalizeWords("hello darkness, my old friend."); // "Hello Darkness, My Old Friend."
* ```
*/
/*#__NO_SIDE_EFFECTS__*/
export function strCapitalizeWords<T>(value: T): string {
return strLetterCase(strLower(asString(value)));
}

/**
* Convert the provided value to `camelCased` string, you can optionally specify whether the
* first character is upper cased (lowercase by default) from kebab `-` or snake `_` case.
* All whitespace characters are removed
* If the value is not a string it will be converted.
* @since 0.9.0
Expand Down
4 changes: 2 additions & 2 deletions lib/test/bundle-size-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ const configs = [
{
name: "es5-min-zip",
path: "../bundle/es5/umd/ts-utils.min.js",
limit: 12 * 1024, // 12 kb in bytes
limit: 12.5 * 1024, // 12.5 kb in bytes
compress: true
},
{
name: "es6-min-zip",
path: "../bundle/es6/umd/ts-utils.min.js",
limit: 12 * 1024, // 12 kb in bytes
limit: 12.5 * 1024, // 12.5 kb in bytes
compress: true
},
{
Expand Down
42 changes: 41 additions & 1 deletion lib/test/src/common/string/conversion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { assert } from "@nevware21/tripwire-chai";
import { strCamelCase, strKebabCase, strLetterCase, strSnakeCase } from "../../../../src/string/conversion";
import { strCamelCase, strCapitalizeWords, strKebabCase, strLetterCase, strSnakeCase } from "../../../../src/string/conversion";

describe("strLetterCase", () => {
it("null/undefined", () => {
Expand Down Expand Up @@ -44,6 +44,46 @@ describe("strLetterCase", () => {
});
});

describe("strCapitalizeWords", () => {
it("null/undefined", () => {
assert.equal(strCapitalizeWords(null), "Null");
assert.equal(strCapitalizeWords(undefined), "Undefined");
});

it("Basic", () => {
assert.equal(strCapitalizeWords("hello darkness"), "Hello Darkness");
assert.equal(strCapitalizeWords("hELLo dARKness"), "Hello Darkness");
assert.equal(strCapitalizeWords("hello_darkness"), "Hello_Darkness");
assert.equal(strCapitalizeWords("_hello_darkness"), "_Hello_Darkness");
assert.equal(strCapitalizeWords("hello-darkness"), "Hello-Darkness");
assert.equal(strCapitalizeWords("hello darkness, my old friend."), "Hello Darkness, My Old Friend.");
});

it("Reverse kebabcase Basic", () => {
assert.equal(strCapitalizeWords(strKebabCase("hello darkness")), "Hello-Darkness");
assert.equal(strCapitalizeWords(strKebabCase("hello_darkness")), "Hello-Darkness");
assert.equal(strCapitalizeWords(strKebabCase("_hello_darkness")), "-Hello-Darkness");
assert.equal(strCapitalizeWords(strKebabCase("hello darkness, my old friend.")), "Hello-Darkness-My-Old-Friend-");
});

it("Reverse snakecase Basic", () => {
assert.equal(strCapitalizeWords(strSnakeCase("hello darkness")), "Hello_Darkness");
assert.equal(strCapitalizeWords(strSnakeCase("hello_darkness")), "Hello_Darkness");
assert.equal(strCapitalizeWords(strSnakeCase("_hello_darkness")), "_Hello_Darkness");
assert.equal(strCapitalizeWords(strSnakeCase("hello darkness, my old friend.")), "Hello_Darkness_My_Old_Friend_");
});

it("Differs from strLetterCase by normalizing word casing", () => {
assert.equal(strLetterCase("hELLo dARKness"), "HELLo DARKness");
assert.equal(strCapitalizeWords("hELLo dARKness"), "Hello Darkness");
});

it("Differs from strCamelCase by preserving separators", () => {
assert.equal(strCamelCase("hello-darkness"), "helloDarkness");
assert.equal(strCapitalizeWords("hello-darkness"), "Hello-Darkness");
});
});

describe("strCamelCase", () => {
it("null/undefined", () => {
assert.equal(strCamelCase(null), "null");
Expand Down
Loading