Skip to main content
zerotal

Documentation


Documentation / zerotal / index / Str

Variable: Str

const Str: object

Defined in: packages/core/src/helpers/str.ts:6

The Str string-manipulation utility — a collection of case converters, substring helpers, and a macro() hook for registering project-specific string methods at runtime.

Type Declaration

camelCase()

camelCase(value): string

'hello-world' | 'hello_world' | 'hello world' → 'helloWorld'

Parameters

value

string

Returns

string

snakeCase()

snakeCase(value): string

'helloWorld' → 'hello_world'

Parameters

value

string

Returns

string

slugify()

slugify(value): string

'helloWorld' | 'hello world' → 'hello-world'

Parameters

value

string

Returns

string

titleCase()

titleCase(value): string

'hello world' → 'Hello World'

Parameters

value

string

Returns

string

pascalCase()

pascalCase(value): string

'hello-world' | 'helloWorld' → 'HelloWorld'

Parameters

value

string

Returns

string

capitalize()

capitalize(value): string

Upper-case the first character only. 'hello world' → 'Hello world'

Parameters

value

string

Returns

string

lcfirst()

lcfirst(value): string

Lower-case the first character only. 'BlogPost' → 'blogPost'

Parameters

value

string

Returns

string

truncate()

truncate(value, maxLength, suffix?): string

Truncate to maxLength, appending suffix if truncated. truncate('hello world', 7) → 'hell...'

Parameters

value

string

maxLength

number

suffix?

string = "..."

Returns

string

isAlphanumeric()

isAlphanumeric(value): boolean

Returns true if the value contains only alphanumeric characters (non-empty).

Parameters

value

string

Returns

boolean

padLeft()

padLeft(value, length, char?): string

Pad left: padLeft('42', 5, '0') → '00042'

Parameters

value

string

length

number

char?

string = " "

Returns

string

kebab()

kebab(value): string

'helloWorld' | 'hello world' → 'hello-world'

Parameters

value

string

Returns

string

squish()

squish(value): string

Collapse all consecutive whitespace (including newlines) into a single space and trim.

Parameters

value

string

Returns

string

finish()

finish(value, cap): string

Ensure the string ends with cap. If it already ends with cap, returns as-is. finish('https://example.com', '/') → 'https://example.com/'

Parameters

value

string

cap

string

Returns

string

start()

start(value, prefix): string

Ensure the string starts with prefix. If it already starts with prefix, returns as-is. start('path/to/file', '/') → '/path/to/file'

Parameters

value

string

prefix

string

Returns

string

after()

after(value, needle): string

Return everything after the first occurrence of needle, or the full string if not found.

Parameters

value

string

needle

string

Returns

string

before()

before(value, needle): string

Return everything before the first occurrence of needle, or the full string if not found.

Parameters

value

string

needle

string

Returns

string

afterLast()

afterLast(value, needle): string

Return everything after the LAST occurrence of needle.

Parameters

value

string

needle

string

Returns

string

beforeLast()

beforeLast(value, needle): string

Return everything before the LAST occurrence of needle.

Parameters

value

string

needle

string

Returns

string

contains()

contains(value, substring): boolean

Return true if value contains substring (case-sensitive).

Parameters

value

string

substring

string

Returns

boolean

random()

random(length?): string

Generate a cryptographically random alphanumeric string of the given length. Uses crypto.getRandomValues — no external dependencies.

Parameters

length?

number = 32

Returns

string

replaceFirst()

replaceFirst(value, search, replace): string

Replace the first occurrence of search with replace. replaceFirst('the cat sat on the mat', 'the', 'a') → 'a cat sat on the mat'

Parameters

value

string

string

replace

string

Returns

string

replaceLast()

replaceLast(value, search, replace): string

Replace the last occurrence of search with replace.

Parameters

value

string

search

string

replace

string

Returns

string

reverse()

reverse(value): string

Reverse a string: reverse('hello') → 'olleh'

Parameters

value

string

Returns

string

words()

words(value, maxWords, suffix?): string

Limit the string to maxWords words, appending suffix if truncated. words('One two three four', 2) → 'One two...'

Parameters

value

string

maxWords

number

suffix?

string = "..."

Returns

string

macro()

macro(name, fn): void

Register a macro (custom method) on the Str object.

The function is assigned directly to Str, so you call it as Str.myMacro(...). Add a module augmentation to your project for full TypeScript type safety:

Parameters

name

string

fn

(...args) => unknown

Returns

void

Example

// In AppServiceProvider.onBooted():
Str.macro('readableSize', (bytes: number) =>
  bytes < 1024 ? `${bytes} B` : `${(bytes / 1024).toFixed(1)} KB`
);

// In types.d.ts (for type safety):
declare module '@zerotal/core' {
  interface StrMacros { readableSize(bytes: number): string; }
}

// In a controller:
(Str as typeof Str & StrMacros).readableSize(2048); // '2.0 KB'