Namespace: encode
References​
IS_BROWSER​
Re-exports IS_BROWSER
Functions​
arrayBufferToString​
â–¸ arrayBufferToString(array
): string
Convert array buffer to string
[internal usage]
Parameters​
Name | Type | Description |
---|---|---|
array | ArrayBuffer | The ArrayBuffer to convert to string. |
Returns​
string
The converted string.
Example
const buffer = new ArrayBuffer(5);
const view = new Uint8Array(buffer);
[72, 101, 108, 108, 111].forEach((x, idx) => (view[idx] = x));
const result = encode.arrayBufferToString(buffer);
// result = "Hello"
Defined in​
utf8ToArray​
â–¸ utf8ToArray(str
): Uint8Array
Convert utf8-string to Uint8Array
[internal usage]
Parameters​
Name | Type | Description |
---|---|---|
str | string | The UTF-8 string to convert. |
Returns​
Uint8Array
The encoded Uint8Array.
Example
const myString = 'Hi';
const result = encode.utf8ToArray(myString);
// result = Uint8Array(2) [ 72, 105 ]
Defined in​
stringToArrayBuffer​
â–¸ stringToArrayBuffer(str
): Uint8Array
Convert utf8-string to Uint8Array
Parameters​
Name | Type |
---|---|
str | string |
Returns​
Uint8Array
Deprecated
equivalent to 'utf8ToArray', alias will be removed
Defined in​
atobUniversal​
â–¸ atobUniversal(a
): Uint8Array
Convert string to array buffer (browser and node compatible)
Parameters​
Name | Type | Description |
---|---|---|
a | string | The Base64 encoded string to convert. |
Returns​
Uint8Array
The decoded Uint8Array.
Example
const base64String = 'SGVsbG8='; // 'Hello' in Base64
const result = encode.atobUniversal(base64String);
// result = Uint8Array(5) [ 72, 101, 108, 108, 111 ]
Defined in​
btoaUniversal​
â–¸ btoaUniversal(b
): string
Convert array buffer to string (browser and node compatible)
Parameters​
Name | Type | Description |
---|---|---|
b | ArrayBuffer | The Array buffer. |
Returns​
string
The Base64 encoded string.
Example
const buffer = new Uint8Array([72, 101, 108, 108, 111]); // Array with ASCII values for 'Hello'
const result = encode.btoaUniversal(buffer);
// result = "SGVsbG8="
Defined in​
buf2hex​
â–¸ buf2hex(buffer
): string
Convert array buffer to hex-string
Parameters​
Name | Type | Description |
---|---|---|
buffer | Uint8Array | The encoded Uint8Array. |
Returns​
string
The hex-string
Example
const buffer = new Uint8Array([72, 101, 108, 108, 111]); // Array with ASCII values for 'Hello'
const result = encode.buf2hex(buffer);
// result = "48656c6c6f"
Defined in​
removeHexPrefix​
â–¸ removeHexPrefix(hex
): string
Remove hex prefix '0x' from hex-string
Parameters​
Name | Type | Description |
---|---|---|
hex | string | hex-string |
Returns​
string
The hex-string
Example
const hexStringWithPrefix = '0x48656c6c6f';
const result = encode.removeHexPrefix(hexStringWithPrefix);
// result: "48656c6c6f"
Defined in​
addHexPrefix​
â–¸ addHexPrefix(hex
): string
Add hex prefix '0x' to base16-string
Parameters​
Name | Type | Description |
---|---|---|
hex | string | base16-string |
Returns​
string
The hex-string
Example
const plainHexString = '48656c6c6f';
const result = encode.addHexPrefix(plainHexString);
// result: "0x48656c6c6f"
Defined in​
padLeft​
â–¸ padLeft(str
, length
, padding?
): string
Prepend string (default with '0')
Pads a string to a certain length with a specific string. The padding can be applied only to the left of the input string.
Parameters​
Name | Type | Default value | Description |
---|---|---|---|
str | string | undefined | The string to pad. |
length | number | undefined | The target length for the padded string. |
padding? | string | STRING_ZERO | The string to use for padding. Defaults to '0'. |
Returns​
string
The padded string.
Example
const myString = '1A3F';
const result = encode.padLeft(myString, 10);
// result: '0000001A3F'
Defined in​
calcByteLength​
â–¸ calcByteLength(str
, byteSize?
): number
Calculate byte length of string
[no internal usage]
Calculates the byte length of a string based on a specified byte size. The function rounds up the byte count to the nearest multiple of the specified byte size.
Parameters​
Name | Type | Default value | Description |
---|---|---|---|
str | string | undefined | The string whose byte length is to be calculated. |
byteSize? | number | 8 | The size of the byte block to round up to. Defaults to 8. |
Returns​
number
The calculated byte length, rounded to the nearest multiple of byteSize.
Example
const myString = 'Hello';
const result = encode.calcByteLength(myString, 4);
// result = 8 (rounded up to the nearest multiple of 4)
Defined in​
sanitizeBytes​
â–¸ sanitizeBytes(str
, byteSize?
, padding?
): string
Prepend '0' to string bytes
[no internal usage]
- Prepends padding to the left of a string to ensure it matches a specific byte length.
The function uses a specified padding character and rounds up the string length to the nearest multiple of
byteSize
.
Parameters​
Name | Type | Default value | Description |
---|---|---|---|
str | string | undefined | The string to be padded. |
byteSize? | number | 8 | The byte block size to which the string length should be rounded up. Defaults to 8. |
padding? | string | STRING_ZERO | The character to use for padding. Defaults to '0'. |
Returns​
string
The padded string.
Example
const myString = '123';
const result = encode.sanitizeBytes(myString);
// result: '00000123' (padded to 8 characters)
Defined in​
sanitizeHex​
â–¸ sanitizeHex(hex
): string
Sanitizes a hex-string by removing any existing '0x' prefix, padding the string with '0' to ensure it has even length, and then re-adding the '0x' prefix.
[no internal usage]
Parameters​
Name | Type | Description |
---|---|---|
hex | string | hex-string |
Returns​
string
format: hex-string
Example
const unevenHex = '0x23abc';
const result = encode.sanitizeHex(unevenHex);
// result = '0x023abc' (padded to ensure even length)
Defined in​
pascalToSnake​
â–¸ pascalToSnake(text
): string
String transformation util
Pascal case to screaming snake case
Parameters​
Name | Type | Description |
---|---|---|
text | string | The PascalCase string to convert. |
Returns​
string
The converted snake_case string in uppercase.
Example
const pascalString = 'PascalCaseExample';
const result = encode.pascalToSnake(pascalString);
// result: 'PASCAL_CASE_EXAMPLE'