Releases: CrispenGari/utils
Releases 路 CrispenGari/utils
Release list
version 2.0.1
"@crispengari/utils": v2.0.1
Features
Compression and Decompression Functions
- Added:
compressJSONfunction for compressing JSON objects. - Added:
decompressJSONfunction for decompressing base64-encoded strings into JSON objects.
These changes introduce two new functions for compressing and decompressing JSON data using the pako library in version 2.0.1 of the package.
version:1.0.1
version:1.0.0
Encoding and Decoding object to and from base64
Examples.
- encoding strings to
base64
The encode2Base64 utility function is used to decode a string representation from utf8 to base64 string. Here is how you can use it.
import { encode2Base64 } from "@crispengari/utils";
const userId: string = "642fe19989416c012ccf062f";
console.log({ encodedId: encode2Base64(userId) });Output:
{ encodedId: 'NjQyZmUxOTk4OTQxNmMwMTJjY2YwNjJm' }- encoding strings to
utf8
import { decodeFromBase64 } from "@crispengari/utils";
console.log({
decodedId: decodeFromBase64("NjQyZmUxOTk4OTQxNmMwMTJjY2YwNjJm"),
});Output:
{ decodedId: '642fe19989416c012ccf062f' }Note that you can also decode and encode object-strings from and to
base64the following are the examples on how it can be done.
const user = {
id: "642fe19989416c012ccf062f",
username: "username",
email: "email@gmail.com",
age: 23,
};
console.log({ encodedUser: encode2Base64(JSON.stringify(user)) });Output:
{
encodedUser: 'eyJpZCI6IjY0MmZlMTk5ODk0MTZjMDEyY2NmMDYyZiIsInVzZXJuYW1lIjoidXNlcm5hbWUiLCJlbWFpbCI6ImVtYWlsQGdtYWlsLmNvbSIsImFnZSI6MjN9'
}To decode the encoded string of user object you can do it as follows:
console.log({
decodedUser: JSON.parse(
decodeFromBase64(
"eyJpZCI6IjY0MmZlMTk5ODk0MTZjMDEyY2NmMDYyZiIsInVzZXJuYW1lIjoidXNlcm5hbWUiLCJlbWFpbCI6ImVtYWlsQGdtYWlsLmNvbSIsImFnZSI6MjN9"
)
),
});Output:
{
user: {
id: "642fe19989416c012ccf062f",
username: "username",
email: "email@gmail.com",
age: 23,
}
}