Skip to content

Releases: CrispenGari/utils

version 2.0.1

Choose a tag to compare

@CrispenGari CrispenGari released this 27 Jan 16:22

"@crispengari/utils": v2.0.1

Features

Compression and Decompression Functions

  • Added: compressJSON function for compressing JSON objects.
  • Added: decompressJSON function 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

Choose a tag to compare

@CrispenGari CrispenGari released this 12 Apr 07:41

Fixing declarative types.

version:1.0.0

Choose a tag to compare

@CrispenGari CrispenGari released this 12 Apr 07:21

Encoding and Decoding object to and from base64

Examples.

  1. 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' }
  1. 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 base64 the 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,
  }
}