Text
The Text object of the TextManager class provides methods for working with text data, including encoding and decoding HTML entities, generating random strings, converting values to various data types, and changing the case and format of strings.
It uses the
Luxonlibrary for date and time operations.
import { Text, LoggerBrowser } from '@bitrix24/b24jssdk'
const $logger = LoggerBrowser.build('Test', import.meta.env?.DEV === true)
$logger.info(`${Text.getDateForLog()} UuidRfc4122:`, Text.getUuidRfc4122())
// 2012-04-12 09:53:51 UuidRfc4122: 019323ac-8ace-725b-a3dc-6a7c333da066 ////Methods
getRandom
getRandom(
length = 8
): stringGenerates a random string of the specified length, consisting of characters [a-z0-9].
$logger.info(Text.getRandom(15))
// cavomfautfjwr7n ////getUniqId
getUniqId(): stringGenerates a unique identifier in UUID format.
$logger.info(Text.getUniqId())
// 212c0ca2-4lse-4e03-b0a8-adc1f661d64b ////getUuidRfc4122
getUuidRfc4122(): stringGenerates a version 7 UUID.
$logger.info(Text.getUuidRfc4122())
// 019323b3-7926-70c1-9e2a-81c2e48fb04c ////encode
encode(
value: string
): stringEncodes all unsafe HTML entities in a string.
const testString = `<${'s'}cript>alert('test');<\/${'s'}cript>`
$logger.info(Text.encode(testString))
// <script>alert('test');</script> ////decode
decode(
value: string
): stringDecodes all encoded HTML entities in a string.
const testString = `<script>alert('test');</script>`
$logger.info(Text.decode(testString)) // <script>alert('test');</script> ////toNumber
toNumber(
value: any
): numberConverts a value to a number. Returns 0.0 if conversion is not possible.
$logger.info(Text.toNumber(`123.44`)) // 123.44 ////toInteger
toInteger(
value: any
): numberConverts a value to an integer.
$logger.info(Text.toInteger(`123.44`)) // 123 ////
$logger.info(Text.toInteger(123.44)) // 123 ////toBoolean
toBoolean(
value: any,
trueValues = []
): booleanConverts a value to a boolean. Compares the value with 'true', y, 1, true, and additional values from trueValues.
$logger.info(Text.toBoolean(`1`)) // true ////
$logger.info(Text.toBoolean(1)) // true ////
$logger.info(Text.toBoolean(`0`)) // false ////
$logger.info(Text.toBoolean(0)) // false ////
$logger.info(Text.toBoolean('y')) // true ////
$logger.info(Text.toBoolean('Y')) // true ////
$logger.info(Text.toBoolean('true')) // true ////
$logger.info(Text.toBoolean('TRUE')) // true ////
$logger.info(Text.toBoolean('ok', ['ok', 'success'])) // true ////
$logger.info(Text.toBoolean('success', ['ok', 'success'])) // true ////
$logger.info(Text.toBoolean('fail', ['ok', 'success'])) // false ////toCamelCase
toCamelCase(
str: string
): stringConverts a string to camelCase.
$logger.info(Text.toCamelCase('sOmE StrIng')) // someString ////toPascalCase
toPascalCase(
str: string
): stringConverts a string to PascalCase.
$logger.info(Text.toPascalCase('sOmE StrIng')) // SomeString ////toKebabCase
toKebabCase(
str: string
): stringConverts a string to kebab-case.
$logger.info(Text.toPascalCase('sOmE StrIng')) // s-om-e-str-ing ////
$logger.info(Text.toKebabCase('some string')) // some-string ////
$logger.info(Text.toKebabCase('someString')) // some-string ////capitalize
capitalize(
str: string
): stringReturns a string with the first letter capitalized.
$logger.info(Text.capitalize('some string')) // Some string ////numberFormat
numberFormat(
number: number,
decimals: number = 0,
decPoint: string = '.',
thousandsSep: string = ','
): stringFormats a number with the specified number of decimal places, decimal point, and thousands separator.
$logger.info(Text.numberFormat(15678.987)) // '15,679' ////
$logger.info(Text.numberFormat(15678.987, 2)) // '15,678.99' ////
$logger.info(Text.numberFormat(15678.987, 2, ',', ' ')) // '15 678,99' ////
$logger.info(Text.numberFormat(15678.984, 2, ',', ' ')) // '15 678,98' ////toDateTime
toDateTime(
dateString: string,
template?: string,
opts?: DateTimeOptions
): DateTimeConverts a string to a DateTime object from ISO 8601 or using a specified template.
Similar function for decoding
$logger.info(
Text.toDateTime('2012-04-12T09:53:51').toFormat('HH:mm:ss y-MM-dd')
)
// '09:53:51 2012-04-12' ////getDateForLog
getDateForLog(): stringReturns the current date and time.
$logger.info(Text.getDateForLog())
// '2012-04-12 09:53:51' ////buildQueryString
buildQueryString(
params: any
): stringCreates a query string from an object of parameters.
$logger.info(Text.buildQueryString({
par1: 'val_1',
par2: [
'val_21',
'val_22',
],
}))
// 'par1=val_1&par2%5B0%5D=val_21&par2%5B1%5D=val_22' ////
// 'par1=val_1&par2[0]=val_21&par2[1]=val_22' ////