Primitives
Functions for working with strings, formatting, generating random values, and other basic operations.
Method List
String Validation
| Function | Signature | Description |
|---|---|---|
isEmptyString | (string: string | null | undefined): boolean | Checks if a string is empty, null, undefined, or contains only invisible characters. |
isNullOrEmpty | (string: string | null | undefined): boolean | Checks if a string is empty, null, or undefined. |
isWhitespace | (string: string): boolean | Checks if a string contains only whitespace characters. |
String Transformation
| Function | Signature | Description |
|---|---|---|
upperFirst | (string: string): string | Converts the first character of a string to uppercase. |
lowerFirst | (string: string): string | Converts the first character of a string to lowercase. |
upper | (string: string): string | Converts the entire string to uppercase. |
lower | (string: string): string | Converts the entire string to lowercase. |
replaceAll | (find: string, replace: string, string: string): string | Replaces all occurrences of a substring in a string. |
truncate | (string: string, maxLength: number, suffix?: string, cutFromStart?: boolean): string | Truncates a string to the specified length and adds a suffix if the string was truncated. |
toCamelCase | (string: string): string | Converts a string to camelCase. |
toKebabCase | (string: string): string | Converts a string to kebab-case. |
toSnakeCase | (string: string): string | Converts a string to snake_case. |
reverse | (string: string): string | Reverses a string. |
Data Formatting
| Function | Signature | Description |
|---|---|---|
formatBytes | (bytes: number, decimals?: number, forcedLocale?: string): string | Converts bytes to a human-readable format (e.g., "1.5 KB"). |
pluralize | (number: number, forms: [string, string, string], showNumber?: boolean): string | Returns the correct form of a word depending on the number. |
Random Value Generation
| Function | Signature | Description |
|---|---|---|
randString | (length: number, charset?: 'alphanumeric' | 'alpha' | 'numeric' | 'hex'): string | Generates a random string of the specified length. |
Data Masking
| Function | Signature | Description |
|---|---|---|
mask | (string: string, startVisible?: number, endVisible?: number, maskChar?: string): string | Masks part of a string with a character (useful for hiding sensitive data). |
Usage Examples
String Validation
1. isEmptyString
typescript
import { isEmptyString } from 'snappykit';
console.log(isEmptyString(' ')); // true
console.log(isEmptyString('\u200B\uFEFF')); // true
console.log(isEmptyString('Hello')); // false2. isNullOrEmpty
typescript
import { isNullOrEmpty } from 'snappykit';
console.log(isNullOrEmpty(null)); // true
console.log(isNullOrEmpty('')); // true
console.log(isNullOrEmpty('Hello')); // false3. isWhitespace
typescript
import { isWhitespace } from 'snappykit';
console.log(isWhitespace(' ')); // true
console.log(isWhitespace(' Hello ')); // falseString Transformation
4. upperFirst
typescript
import { upperFirst } from 'snappykit';
console.log(upperFirst('hello')); // 'Hello'
console.log(upperFirst('')); // ''5. lowerFirst
typescript
import { lowerFirst } from 'snappykit';
console.log(lowerFirst('Hello')); // 'hello'
console.log(lowerFirst('')); // ''6. upper
typescript
import { upper } from 'snappykit';
console.log(upper('hello')); // 'HELLO'7. lower
typescript
import { lower } from 'snappykit';
console.log(lower('HELLO')); // 'hello'8. replaceAll
typescript
import { replaceAll } from 'snappykit';
console.log(replaceAll('a', 'o', 'banana')); // 'bonono'9. truncate
typescript
import { truncate } from 'snappykit';
console.log(truncate('Hello, World!', 5)); // 'Hello...'
console.log(truncate('Hello, World!', 5, '---', true)); // '---World!'10. toCamelCase
typescript
import { toCamelCase } from 'snappykit';
console.log(toCamelCase('hello-world')); // 'helloWorld'
console.log(toCamelCase('Hello World')); // 'helloWorld'
console.log(toCamelCase('snake_case')); // 'snakeCase'11. toKebabCase
typescript
import { toKebabCase } from 'snappykit';
console.log(toKebabCase('helloWorld')); // 'hello-world'
console.log(toKebabCase('Hello World')); // 'hello-world'
console.log(toKebabCase('snake_case')); // 'snake-case'12. toSnakeCase
typescript
import { toSnakeCase } from 'snappykit';
console.log(toSnakeCase('helloWorld')); // 'hello_world'
console.log(toSnakeCase('Hello World')); // 'hello_world'
console.log(toSnakeCase('kebab-case')); // 'kebab_case'13. reverse
typescript
import { reverse } from 'snappykit';
console.log(reverse('hello')); // 'olleh'Data Formatting
14. formatBytes
typescript
import { formatBytes } from 'snappykit';
console.log(formatBytes(1024)); // "1.00 KB" (depends on browser locale)
console.log(formatBytes(1500, 1, 'ru')); // "1.5 КБ"
console.log(formatBytes(1048576, 0, 'en')); // "1 MB"15. pluralize
typescript
import { pluralize } from 'snappykit';
console.log(pluralize(1, ['apple', 'apples', 'apples'])); // "1 apple"
console.log(pluralize(2, ['apple', 'apples', 'apples'])); // "2 apples"
console.log(pluralize(5, ['apple', 'apples', 'apples'])); // "5 apples"
console.log(pluralize(10, ['apple', 'apples', 'apples'], false)); // "apples"Random Value Generation
16. randString
typescript
import { randString } from 'snappykit';
console.log(randString(10)); // "aB3xY7pL9K" (random string)
console.log(randString(5, 'numeric')); // "12345" (digits only)
console.log(randString(8, 'hex')); // "a1b2c3d4" (hexadecimal string)Data Masking
17. mask
typescript
import { mask } from 'snappykit';
console.log(mask('1234567890')); // "1234****90"
console.log(mask('1234567890', 2, 2)); // "12******90"
console.log(mask('1234567890', 3, 3, '-')); // "123-----890"