CKN Technology Core · String Extensions

CKN Technology Core
String Extensions

This page documents all extended String.prototype functions from @ckn-technology/core. Each function includes a description, TypeScript signature, and executable example.

Import

To enable all String extensions described here, import the core module once at your application entry point. This registers the extensions globally on String.prototype.

import "@ckn-technology/core";

All examples on this page assume this import has already been executed at startup.

1. Core Iteration & Transformation

print()

Description
Logs the current string to console.log and returns the original string object, allowing method chaining afterwards.

Signature

print(): String

Example

"Hello".print();                  // logs: Hello
"Hello".print().toUpperCase();   // still works as a string object

edit(editFunc)

Description
Transforms each character using a callback and returns a new string. For each character, editFunc(char) is called and the returned value is appended to the output.

Signature

edit(editFunc: (item: string) => string): string

Example

"abc".edit(c => c.toUpperCase());
// "ABC"

"1234".edit(c => (c === "2" ? "X" : c));
// "1X34"

each(eachFunc)

Description
Iterates over each character and calls the callback with that character. Does not construct or return a new string; use for side effects only.

Signature

each(eachFunc: (item: string) => void): void

Example

let result = "";
"abc".each(c => { result += `[${c}]`; });
// result === "[a][b][c]"

2. Substring Helpers

first(n = 1)

Description
Returns the first n characters of the string. If n <= 0, returns an empty string.

Signature

first(n?: number): string

Example

"abcdef".first();    // "a"
"abcdef".first(3);   // "abc"
"abcdef".first(0);   // ""

last(n = 1)

Description
Returns the last n characters of the string. If n <= 0, returns an empty string.

Signature

last(n?: number): string

Example

"abcdef".last();     // "f"
"abcdef".last(2);    // "ef"
"abcdef".last(10);   // "abcdef"

reverse()

Description
Reverses the character order of the string.

Signature

reverse(): string

Example

"abc".reverse();     // "cba"
"กขค".reverse();    // "คขก"

3. Distinct, Count, Remove

distinct(distinctFunc?)

Description
Returns a new string with unique characters in order of first appearance. Optional callback can control whether a character should be treated as duplicate.

Signature

distinct(
  distinctFunc?: (item: string, output: string) => boolean
): string

Example

"banana".distinct();             
// "ban"

"banana".distinct((ch, out) => out.includes(ch.toLowerCase()));
// same as default in this case

count(countFunc?)

Description
Counts characters in the string. Optional callback allows counting only characters where the predicate returns true.

Signature

count(countFunc?: (item: string) => boolean): number

Example

"banana".count();                    // 6
"banana".count(ch => ch === "a");    // 3

remove(removeFunc?)

Description
Removes characters based on a predicate. If no predicate is given, returns the original string unchanged.

Signature

remove(removeFunc?: (item: string) => boolean): string

Example

"banana".remove(ch => ch === "a");
// "bnn"

"hello world".remove(ch => ch === " ");
// "helloworld"

4. Padding

padLeft(n, c = " ")

Pads on the left with character c until total length is n. If original length is already ≥ n, returns unchanged.

Signature

padLeft(n: number, c?: string): string

Example

"42".padLeft(5, "0");   // "00042"
"abc".padLeft(5);       // "  abc"

padRight(n, c = " ")

Pads on the right with character c until total length is n. If original length is already ≥ n, returns unchanged.

Signature

padRight(n: number, c?: string): string

Example

"42".padRight(5, "0");  // "42000"
"abc".padRight(5);      // "abc  "

padCenter(n, c = " ")

Centers the string inside a field of length n, padding with character c split between left and right.

Signature

padCenter(n: number, c?: string): string

Example

"Hi".padCenter(6, "-");
// "--Hi--"

"Hi".padCenter(5);
// " Hi  "

5. Type Conversions

toNumber()

Converts the string to a number using Number(this).

Signature

toNumber(): number

Example

"123.45".toNumber();    // 123.45
"abc".toNumber();       // NaN

toBoolean()

Case-insensitive conversion to boolean. Returns true if trimmed, lowercased value is "true", "1", "yes", or "y". Otherwise false.

Signature

toBoolean(): boolean

Example

"true".toBoolean();     // true
"YES".toBoolean();      // true
"0".toBoolean();        // false
"no".toBoolean();       // false

toDate(), toTime(), toDateTime()

toDate() parses a date string and normalizes to a date-only value.
toTime() parses a time of day on base date 1970-01-01.
toDateTime() directly uses new Date(dateString).

Signatures

toDate(): Date
toTime(): Date
toDateTime(): Date

Example

"2025-03-05".toDate();
"13:45:30".toTime();
"2025-03-05T13:45:30Z".toDateTime();

6. Case & Naming Conversions

toTitleCase(), toSentenceCase()

toTitleCase() capitalizes the first letter of each word.
toSentenceCase() capitalizes only the first letter of each sentence and lowercases the rest.

Signatures

toTitleCase(): string
toSentenceCase(): string

Example

"hello world".toTitleCase();        
// "Hello World"

"hello world. this is TEST!".toSentenceCase();
// "Hello world. This is test!"

Naming Styles

A full set of helpers to convert between JavaScript-friendly naming styles.

Signatures

toCamelCase(): string
toPascalCase(): string
toSnakeCase(): string
toScreamingSnakeCase(): string
toCamelSnakeCase(): string
toKebabCase(): string
toScreamingKebabCase(): string
toCamelKebabCase(): string

Example

"hello world".toCamelCase();          // "helloWorld"
"hello world".toPascalCase();         // "HelloWorld"
"Hello World".toSnakeCase();          // "hello_world"
"Hello World".toScreamingSnakeCase(); // "HELLO_WORLD"
"Hello World".toKebabCase();          // "hello-world"
"Hello World".toScreamingKebabCase(); // "HELLO-WORLD"

7. Base64, Binary, JSON & Search

Base64 & Binary

Helpers for encoding/decoding between text and Base64/binary forms.

Signatures

toBase64(): string
fromBase64(): string
toBinary(): string
fromBinary(): string

Example

"hello".toBase64();          // "aGVsbG8="
"aGVsbG8=".fromBase64();    // "hello"
"A".toBinary();             // "01000001"
"01000001".fromBinary();    // "A"

JSON & Search

Parse JSON and quickly find substrings.

Signatures

toJSONString(): any
find(word: string): number

Example

'{"a":1,"b":2}'.toJSONString();  // { a: 1, b: 2 }
"hello world".find("world");     // 6
"hello".find("x");               // -1

8. Emptiness & Whitespace

isEmpty()

Checks if the string length is exactly 0.

Signature

isEmpty(): boolean

Example

"".isEmpty();       // true
" ".isEmpty();      // false

isBlank()

Checks if the string is blank after trimming whitespace using the native trim.

Signature

isBlank(): boolean

Example

"".isBlank();       // true
"   ".isBlank();    // true
"  a ".isBlank();   // false

trim(characters?)

Overrides native trim to optionally remove specific characters from both ends. Without parameters, behaves like native trim().

Signature

trim(characters?: string[]): string

Example

"  hello  ".trim();                  // "hello"
"--test--".trim(["-"]);             // "test"
"__value__".trim(["_", " "]);       // "value"

9–11. Matching, Repetition, Splitting & Slugs

Matching Helpers

Case-sensitive and case-insensitive string matching helpers.

Signatures

contains(word: string, ignoreCase?: boolean): boolean
startsWithIgnoreCase(prefix: string): boolean
endsWithIgnoreCase(suffix: string): boolean
equalsIgnoreCase(other: string): boolean

Example

"Hello World".contains("world");            // false
"Hello World".contains("world", true);     // true
"Hello".startsWithIgnoreCase("he");        // true
"myfile.TXT".endsWithIgnoreCase(".txt");   // true
"Hello".equalsIgnoreCase("hello");         // true

Repetition & Splitting

Useful utilities for repeating strings and splitting them into logical units.

Signatures

repeatN(n: number): string
splitLines(): string[]
words(): string[]

Example

"ab".repeatN(3);   // "ababab"

"line1\nline2\r\nline3".splitLines();
// ["line1", "line2", "line3"]

"  hello   world  ".words();
// ["hello", "world"]

Slugs & Affixes

Create URL-friendly slugs and safely add/remove prefixes or suffixes.

Signatures

slug(): string
ensurePrefix(prefix: string): string
ensureSuffix(suffix: string): string
stripPrefix(prefix: string): string
stripSuffix(suffix: string): string

Example

"Hello, World!".slug();         // "hello-world"
"Crème brûlée".slug();          // "creme-brulee"

"world".ensurePrefix("hello-"); // "hello-world"
"file".ensureSuffix(".txt");    // "file.txt"

"hello-world".stripPrefix("hello-"); // "world"
"file.txt".stripSuffix(".txt");      // "file"

Summary

The CKN Technology Core | String Extensions add a rich set of utilities on top of native JavaScript strings: iteration helpers, substring tools, distinct/count/remove logic, padding, type conversions, naming-style transformers, Base64/binary utilities, JSON parsing, whitespace checks, matching helpers, repetition, splitting, slug generation, and prefix/suffix management. Together, they help you write more expressive and readable string manipulation code in your JavaScript/TypeScript projects.