2024-05-21 13:21:07 +00:00
|
|
|
/**
|
|
|
|
* Function will replace the middle of the string with 3 dots if length greater then
|
|
|
|
* offset value
|
|
|
|
* @param str string to shorten
|
|
|
|
* @param offset of how many chars to keep in the beginning and the end
|
|
|
|
* eg. 3 will keep first 3 chars and last 3 chars between the dots
|
|
|
|
*/
|
2024-02-28 16:49:44 +00:00
|
|
|
export const shorten = (str: string, offset = 9) => {
|
|
|
|
// return original string if it is not long enough
|
|
|
|
if (str.length < offset * 2 + 4) return str
|
|
|
|
|
|
|
|
return `${str.slice(0, offset)}...${str.slice(
|
|
|
|
str.length - offset,
|
|
|
|
str.length
|
|
|
|
)}`
|
|
|
|
}
|
2024-04-08 12:45:51 +00:00
|
|
|
|
|
|
|
export const stringToHex = (str: string) => {
|
|
|
|
// Convert the string to an array of UTF-16 code units using the spread operator
|
|
|
|
const codeUnits = [...str]
|
|
|
|
|
|
|
|
// Map each code unit to its hexadecimal representation
|
|
|
|
const hexChars = codeUnits.map((codeUnit) => {
|
|
|
|
// Convert the code unit to its hexadecimal representation with leading zeros
|
|
|
|
const hex = codeUnit.charCodeAt(0).toString(16).padStart(2, '0')
|
|
|
|
return hex
|
|
|
|
})
|
|
|
|
|
|
|
|
// Join the hexadecimal characters into a single string
|
|
|
|
const hexString = hexChars.join('')
|
|
|
|
|
|
|
|
// Return the resulting hexadecimal string
|
|
|
|
return hexString
|
|
|
|
}
|
|
|
|
|
|
|
|
export const hexToString = (hex: string) => {
|
|
|
|
// Split the hex string into pairs of two characters
|
|
|
|
const pairs = hex.match(/.{1,2}/g) || []
|
|
|
|
|
|
|
|
// Convert each pair from hexadecimal to its decimal equivalent,
|
|
|
|
// then convert each decimal value to its character representation
|
|
|
|
const chars = pairs.map((pair) => String.fromCharCode(parseInt(pair, 16)))
|
|
|
|
|
|
|
|
// Join the resulting characters into a single string
|
|
|
|
return chars.join('')
|
|
|
|
}
|
2024-04-16 08:29:56 +00:00
|
|
|
|
|
|
|
// Function to convert a Uint8Array to a hexadecimal string
|
|
|
|
export const uint8ArrayToHexString = (uint8Array: Uint8Array) => {
|
|
|
|
// Convert each byte in the Uint8Array to a hexadecimal string,
|
|
|
|
// pad it with leading zeros if necessary, and join the results
|
|
|
|
return Array.from(uint8Array)
|
|
|
|
.map((byte) => byte.toString(16).padStart(2, '0')) // Convert byte to hexadecimal string
|
|
|
|
.join('') // Join all hexadecimal strings
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function to convert a hexadecimal string to a Uint8Array
|
|
|
|
export const hexStringToUint8Array = (hexString: string) => {
|
|
|
|
// Create a new Uint8Array with half the length of the hex string
|
|
|
|
const uint8Array = new Uint8Array(hexString.length / 2)
|
|
|
|
// Iterate over pairs of characters in the hex string
|
|
|
|
for (let i = 0; i < hexString.length; i += 2) {
|
|
|
|
// Parse the pair of characters as a hexadecimal value and store it in the Uint8Array
|
|
|
|
uint8Array[i / 2] = parseInt(hexString.substr(i, 2), 16)
|
|
|
|
}
|
|
|
|
// Return the resulting Uint8Array
|
|
|
|
return uint8Array
|
|
|
|
}
|
2024-04-18 11:12:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses JSON content asynchronously.
|
|
|
|
* @param content The JSON content to parse.
|
|
|
|
* @returns A Promise that resolves to the parsed JSON object.
|
|
|
|
*/
|
2024-05-14 09:27:05 +00:00
|
|
|
export const parseJson = <T>(content: string): Promise<T> => {
|
2024-04-18 11:12:11 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
// Attempt to parse the JSON content
|
2024-05-14 09:27:05 +00:00
|
|
|
const json = JSON.parse(content) as T
|
2024-04-18 11:12:11 +00:00
|
|
|
// Resolve the promise with the parsed JSON object
|
|
|
|
resolve(json)
|
|
|
|
} catch (error) {
|
|
|
|
// If parsing fails, reject the promise with the error
|
|
|
|
reject(error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-05-28 09:22:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Capitalizes the first character in the string
|
|
|
|
* @param str string to modify
|
|
|
|
* @returns modified string
|
|
|
|
*/
|
|
|
|
export const capitalizeFirstLetter = (str: string) =>
|
|
|
|
str.charAt(0).toUpperCase() + str.slice(1).toLowerCase()
|
2024-06-28 09:24:14 +00:00
|
|
|
|
|
|
|
export const formatTimestamp = (timestamp: number) => {
|
|
|
|
const date = new Date(timestamp)
|
|
|
|
|
|
|
|
// Extract date parts
|
|
|
|
const day = date.getDate().toString().padStart(2, '0')
|
|
|
|
const month = date.toLocaleString('default', { month: 'short' })
|
|
|
|
const year = date.getFullYear()
|
|
|
|
|
|
|
|
// Format time parts
|
|
|
|
const hours = date.getHours()
|
|
|
|
const minutes = date.getMinutes().toString().padStart(2, '0')
|
|
|
|
const ampm = hours >= 12 ? 'PM' : 'AM'
|
|
|
|
const formattedHours = (hours % 12 || 12).toString().padStart(2, '0')
|
|
|
|
|
|
|
|
// Combine parts into the desired format
|
|
|
|
return `${day} ${month} ${year} ${formattedHours}:${minutes} ${ampm}`
|
|
|
|
}
|
2024-07-05 08:38:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the leading slash from a given string if it exists.
|
|
|
|
*
|
|
|
|
* @param str - The string from which to remove the leading slash.
|
|
|
|
* @returns The string without the leading slash.
|
|
|
|
*/
|
|
|
|
export const removeLeadingSlash = (str: string): string => {
|
|
|
|
// Check if the string starts with a leading slash
|
|
|
|
if (str.startsWith('/')) {
|
|
|
|
// If it does, return the string without the leading slash
|
|
|
|
return str.slice(1)
|
|
|
|
}
|
|
|
|
// If it doesn't, return the original string
|
|
|
|
return str
|
|
|
|
}
|