Strings in Kawkab
Strings are a set of text values that can be used in various parts of the project. These strings are stored in a central location, making them easy to manage and modify without needing to repeat them in the application code. Kawkab provides a flexible way to organize strings, making editing and translation easier.
import { string } from "kawkab";Available Functions:
trim(value: string): string
Removes whitespace from the beginning and end of the string. Example:
string.trim(" Hello, World! "); // returns: "Hello, World!"lower(value: string): string
Converts the string to lowercase. Example:
string.lower("HELLO WORLD"); // returns: "hello world"upper(value: string): string
Converts the string to uppercase. Example:
string.upper("hello world"); // returns: "HELLO WORLD"length(value: string): number
Returns the length of the string. Example:
string.length("Hello"); // returns: 5contains(haystack: string, needle: string): boolean
Checks if the string contains a specific part. Example:
string.contains("Hello, World!", "World"); // returns: truestartsWith(haystack: string, needle: string): boolean
Checks if the string starts with a specific part. Example:
string.startsWith("Hello, World!", "Hello"); // returns: trueendsWith(haystack: string, needle: string): boolean
Checks if the string ends with a specific part. Example:
string.endsWith("Hello, World!", "World!"); // returns: truereplaceFirst(haystack: string, search: string, replace: string): string
Replaces the first occurrence of a specific part with another string. Example:
string.replaceFirst("Hello, World!", "o", "0"); // returns: "Hell0, World!"replaceLast(haystack: string, search: string, replace: string): string
Replaces the last occurrence of a specific part with another string. Example:
string.replaceLast("Hello, World!", "o", "0"); // returns: "Hello, W0rld!"random(length: number = 16): string
Creates a random string of the specified length. Example:
string.random(8); // returns a random string of length 8, e.g., "a1B2c3D4"camel(value: string): string
Converts the string to camelCase. Example:
string.camel("hello_world"); // returns: "helloWorld"snake(value: string): string
Converts the string to snake_case. Example:
string.snake("helloWorld"); // returns: "hello_world"kebab(value: string): string
Converts the string to kebab-case. Example:
string.kebab("helloWorld"); // returns: "hello-world"slug(value: string): string
Converts the string to a format suitable for a URL slug. Example:
string.slug("Hello, World!"); // returns: "hello-world"after(value: string, search: string): string
Returns the part of the string that follows a specific part. Example:
string.after("Hello, World!", ","); // returns: " World!"afterLast(value: string, search: string): string
Returns the part of the string that follows the last occurrence of a specific part. Example:
string.afterLast("Hello, World, Again!", ","); // returns: " Again!"ascii(value: string): string
Removes non-ASCII characters from the string. Example:
string.ascii("Héllö Wörld"); // returns: "Hell Wrld"before(value: string, search: string): string
Returns the part of the string that precedes a specific part. Example:
string.before("Hello, World!", ","); // returns: "Hello"beforeLast(value: string, search: string): string
Returns the part of the string that precedes the last occurrence of a specific part. Example:
string.beforeLast("Hello, World, Again!", ","); // returns: "Hello, World"between(value: string, start: string, end: string): string
Returns the part of the string between two specific parts. Example:
string.between("Hello [World] Again", "[", "]"); // returns: "World"betweenFirst(value: string, start: string, end: string): string
Returns the part of the string between the first occurrences of two specific parts. Example:
string.betweenFirst("Hello [World] [Again]", "[", "]"); // returns: "World"charAt(value: string, index: number): string
Returns the character at a specific index in the string. Example:
string.charAt("Hello", 1); // returns: "e"chopStart(value: string, count: number): string
Removes a specified number of characters from the beginning of the string. Example:
string.chopStart("Hello", 2); // returns: "llo"chopEnd(value: string, count: number): string
Removes a specified number of characters from the end of the string. Example:
string.chopEnd("Hello", 2); // returns: "Hel"containsAll(value: string, needles: string[]): boolean
Checks if all specified parts are present in the string. Example:
string.containsAll("Hello, World!", ["Hello", "World"]); // returns: trueexcerpt(value: string, phrase: string, options: { radius?: number; omission?: string }): string | null
Extracts an excerpt from the string around a specific phrase. Example:
string.excerpt("This is a long text with a specific phrase to find.", "specific phrase", { radius: 10 });
// returns: "...text with a specific phrase to find..."finish(value: string, cap: string): string
Ensures that the string ends with a specific string. Example:
string.finish("Hello", "!"); // result: "Hello!"
string.finish("Hello!", "!"); // result: "Hello!"headline(value: string): string
Converts the string to headline format (capitalizes the first letter of each word). Example:
string.headline("hello world"); // result: "Hello World"inlineMarkdown(value: string): string
Converts inline Markdown formatting in the string to HTML. Example:
string.inlineMarkdown("This is **bold** and *italic*"); // result: "This is <strong>bold</strong> and <em>italic</em>"is(value: string, pattern: string): boolean
Checks if the string matches a specific pattern. Example:
string.is("foo", "f*"); // result: trueisAscii(value: string): boolean
Checks if the string contains only ASCII characters. Example:
string.isAscii("Hello"); // result: true
string.isAscii("Héllo"); // result: falseisJson(value: string): boolean
Checks if the string is in valid JSON format. Example:
string.isJson('{"name":"John", "age":30}'); // result: true
string.isJson("Not JSON"); // result: falseisUlid(value: string): boolean
Checks if the string is in valid ULID format. Example:
string.isUlid("01ARZ3NDEKTSV4RRFFQ69G5FAV"); // result: true
string.isUlid("not-ulid"); // result: falseisUrl(value: string): boolean
Checks if the string is in valid URL format. Example:
string.isUrl("https://www.example.com"); // result: true
string.isUrl("not-url"); // result: falseisUuid(value: string): boolean
Checks if the string is in valid UUID format. Example:
string.isUuid("550e8400-e29b-41d4-a716-446655440000"); // result: true
string.isUuid("not-uuid"); // result: falselcfirst(value: string): string
Converts the first character of the string to lowercase. Example:
string.lcfirst("Hello"); // result: "hello"limit(value: string, limit: number = 100, end: string = '...'): string
Limits the length of the string, adding an ellipsis if the text is truncated. Example:
string.limit("This is a long text", 10); // result: "This is a..."padBoth(value: string, length: number, pad: string = ' '): string
Adds padding to both sides of the string until it reaches the specified length. Example:
string.padBoth("Hello", 10, "-"); // result: "--Hello---"padLeft(value: string, length: number, pad: string = ' '): string
Adds padding to the left side of the string until it reaches the specified length. Example:
string.padLeft("Hello", 10, "-"); // result: "-----Hello"padRight(value: string, length: number, pad: string = ' '): string
Adds padding to the right side of the string until it reaches the specified length. Example:
string.padRight("Hello", 10, "-"); // result: "Hello-----"plural(value: string, count: number = 2): string
Converts the string to plural form based on the count. Example:
string.plural("apple", 1); // result: "apple"
string.plural("apple", 2); // result: "apples"removeWhitespace(value: string): string
Removes all whitespace from the string. Example:
string.removeWhitespace("Hello World"); // result: "HelloWorld"repeat(value: string, times: number): string
Repeats the string a specified number of times. Example:
string.repeat("Hello", 3); // result: "HelloHelloHello"reverse(value: string): string
Reverses the characters in the string. Example:
string.reverse("Hello"); // result: "olleH"rotate(value: string, positions: number): string
Rotates the string by a specified number of positions (positive for right, negative for left). Example:
string.rotate("Hello", 2); // result: "loHel"
string.rotate("Hello", -1); // result: "elloH"simplifySlug(value: string): string
Converts the string to a simplified format suitable for use as a URL slug. Example:
string.simplifySlug("Hello, World!"); // result: "hello-world"sort(value: string): string
Sorts the characters in the string in alphabetical order. Example:
string.sort("hello"); // result: "ehllo"split(value: string, separator: string): string[]
Splits the string into an array of substrings based on a specified separator. Example:
string.split("a,b,c", ","); // result: ["a", "b", "c"]shuffle(value: string): string
Shuffles the characters in the string randomly. Example:
string.shuffle("hello"); // result: random order like "lloeh"stripTags(value: string): string
Removes HTML tags from the string. Example:
string.stripTags("<p>Hello <b>World</b></p>"); // result: "Hello World"title(value: string, exclusions: string[] = []): string
Converts the string to title format (capitalizes the first letter of each word). Example:
string.title("hello world"); // result: "Hello World"
string.title("a fire upon the deep", ["a", "the", "upon"]); // result: "A Fire upon the Deep"transform(value: string, callback: (char: string) => string): string
Transforms the string by applying a callback function to each character in the text. Example:
string.transform("hello", char => char.toUpperCase()); // result: "HELLO"trimLeft(value: string, char: string = ' '): string
Removes characters from the left side of the string. Example:
string.trimLeft(" Hello"); // result: "Hello"
string.trimLeft("!!!Hello", "!"); // result: "Hello"trimRight(value: string, char: string = ' '): string
Removes characters from the right side of the string. Example:
string.trimRight("Hello "); // result: "Hello"
string.trimRight("Hello!!!", "!"); // result: "Hello"truncate(value: string, length: number, end: string = '...'): string
Truncates the string to a specified length, replacing any excess with an ellipsis (…). Example:
string.truncate("This is a long text", 10); // result: "This is..."unescapeHtml(value: string): string
Unescapes HTML entities in the string. Example:
string.unescapeHtml("<p>Hello</p>"); // result: "<p>Hello</p>"ucwords(value: string): string
Converts the first character of each word to uppercase. Example:
string.ucwords("hello world"); // result: "Hello World"urlencode(value: string): string
Converts the string to URL-encoded format. Example:
string.urlencode("Hello World!"); // result: "Hello%20World%21"urldecode(value: string): string
Decodes the string from URL-encoded format. Example:
string.urldecode("Hello%20World%21"); // result: "Hello World!"uuid(value: string): string
Converts the string to UUID format (32 hexadecimal characters, separated by hyphens). Example:
string.uuid("550e8400e29b41d4a716446655440000"); // result: "550e8400-e29b-41d4-a716-446655440000"ucfirst(value: string): string
Converts the first character of the string to uppercase. Example:
string.ucfirst("hello"); // result: "Hello"