String
String utility class that provides various methods for strings.
The String class is extended from the built-in String object. You can continue to use whatever we normally use.
Usage
To use this module, you first need to import the String class:
import { String } from '@ponsetya/core'
Static Methods
String.decode()
The decode() method decrypts the given encrypted value and returns a text.
String.decode(str: string): string
Parameters
- str:
string
- A string representing the value you want to decrypt.
- Returns:
string
- Decrypted value.
Examples
const encoded = 'SGVsbG8gd29ybGQh'
const decoded = String.decode(encoded)
console.log(decoded) // "Hello world!"
String.encode()
The encode() method encrypts the given value and returns a string.
String.encode(str: string): string
Parameters
- str:
string
- A String representing the value you want to encrypt.
- Returns:
string
- Encrypted value.
Examples
const str = 'Hello world!'
const encoded = String.encode(str)
console.log(encoded) // "SGVsbG8gd29ybGQh"
String.is()
The is() method determines whether the passed value is an String.
String.is(value: unknown): boolean
Parameters
- value:
unknown
- The value to be checked.
- Returns:
boolean
- A boolean indicating whether the variable is a string.
Examples
console.log(Number.is('x')) // true
String.template()
The template() method lets you change variables in the text.
String.template(str: string, params: Record<string, string | number>): string
Parameters
- str:
string
- A string that specifies the text you want to replace.
- params:
Record<string, string | number>
- Values to insert into string instead of placeholder.
- Returns:
string
- A string replaced in variables.
Examples
const str = "Hello, I'm {{name}} and I'm {{age}} years old."
const message = String.template(str, {
name: 'John',
age: 27
})
console.log(message) // "Hello, I'm John and I'm 27 years old."
String.truncate()
The truncate() method truncates the text from a full word.
String.truncate(str: string, limit?: number): string
Parameters
- str:
string
- A String that specifies the text you want to shorten.
- limit?:
number
- A Number to limit the text character size.
- Returns:
string
- A truncated or empty string.
Examples
const str = 'Lorem ipsum dolor sit amet.'
console.log(String.truncate(str, 11)) // Lorem ipsum...