In most browsers, calling btoa()
on a Unicode string or Emojis will cause an InvalidCharacterError
or DOMException
exception.
This small package provides a simple solution by making available utoa
and atou
, Unicode to and from base64 encoding.
npm i --save unicode-encode
// Import the needed functions.
const { utoa, atou } = require("unicode-encode");
// Examples:
utoa("à bientôt ☮"); // w6AgYmllbnTDdHQg4piu
atou("w6AgYmllbnTDdHQg4piu"); // "à bientôt ☮"
utoa("👋 Unicode!"); // 8J+RiyBVbmljb2RlIQ==
atou("8J+RiyBVbmljb2RlIQ=="); // "👋 Unicode!"
// Import with full type support
import { utoa, atou } from "unicode-encode";
// Functions are fully typed
const encoded: string = utoa("Hello 🌍");
const decoded: string = atou(encoded);
Encodes a Unicode string to base64.
- str: The Unicode string to encode
- Returns: Base64 encoded string
Decodes a base64 encoded string back to Unicode.
- base64: The base64 string to decode
- Returns: Decoded Unicode string
This project is written in TypeScript and requires Node.js 18+ for development.
# Clone the repository
git clone https://github.com/Accelery/unicode-encode.git
cd unicode-encode
# Install dependencies
npm install
# Build the project
npm run build
# Run tests
npm test
npm run build
- Compile TypeScript to JavaScriptnpm run clean
- Remove compiled outputnpm run rebuild
- Clean and buildnpm test
- Run tests on compiled codenpm run test:ts
- Run tests directly on TypeScript filesnpm run test:watch
- Run tests in watch modenpm run coverage
- Generate test coverage reportnpm run lint
- Check code formattingnpm run lint:fix
- Fix code formattingnpm run typecheck
- Check TypeScript types without emitting
This package uses automated publishing via GitHub Actions. To publish a new version:
- Update the version in
package.json
- Update the
CHANGELOG.md
with your changes - Create a new GitHub release with a tag matching the version
- The package will be automatically published to NPM
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass and code is properly formatted
- Submit a pull request
MIT