base62-ts
Version:
Custom Base-62 Encoder in TypeScript
80 lines (53 loc) • 3.22 kB
Markdown
# base62-ts
_Custom Base-62 Encoder_





This repository contains the version of my encoder/decoder for Base-62 in two languages:
* [Golang](https://github.com/cyrildever/base62);
* TypeScript.
### Motivation
I needed an efficient way to apply a Base-62 encoding/decoding algorithm working the same way in TypeScript/Javascript, Python and Golang environments.
### Usage
Both versions use the same following dictionary:
| Value | Character | Value | Character | Value | Character | Value | Character | Value | Character |
|:-----:|:---------:|:-----:|:---------:|:-----:|:---------:|:-----:|:---------:|:-----:|:---------:|
| 0 | `0` | 13 | `d` | 26 | `q` | 39 | `D` | 52 | `Q` |
| 1 | `1` | 14 | `e` | 27 | `r` | 40 | `E` | 53 | `R` |
| 2 | `2` | 15 | `f` | 28 | `s` | 41 | `F` | 54 | `S` |
| 3 | `3` | 16 | `g` | 29 | `t` | 42 | `G` | 55 | `T` |
| 4 | `4` | 17 | `h` | 30 | `u` | 43 | `H` | 56 | `U` |
| 5 | `5` | 18 | `i` | 31 | `v` | 44 | `I` | 57 | `V` |
| 6 | `6` | 19 | `j` | 32 | `w` | 45 | `J` | 58 | `W` |
| 7 | `7` | 20 | `k` | 33 | `x` | 46 | `K` | 59 | `X` |
| 8 | `8` | 21 | `l` | 34 | `y` | 47 | `L` | 60 | `Y` |
| 9 | `9` | 22 | `m` | 35 | `z` | 48 | `M` | 61 | `Z` |
| 10 | `a` | 23 | `n` | 36 | `A` | 49 | `N` | | |
| 11 | `b` | 24 | `o` | 37 | `B` | 50 | `O` | | |
| 12 | `c` | 25 | `p` | 38 | `C` | 51 | `P` | | |
In other words, they use the following base: `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`.
```console
npm i base62-ts
```
```typescript
import * as base62 from 'base62-ts'
const value = 18969
const encoded = base62.encode(value)
// 4VX
console.log(encoded)
const decoded = base62.decode(encoded)
console.assert(value === decoded)
```
### Dependencies
base62-ts depends on my general utility library for computing euclidean division: [ts-utls](https://www.npmjs.com/package/ts-utls).
Besides, to run the tests, you would need to install [`live-server`](https://www.npmjs.com/package/live-server):
```console
npm i -g live-server
```
_NB: Tests run on port `10001`, beware if another process runs on the same port as such a conflict may disable them._
### License
Both versions are available under a MIT license (see [LICENSE](LICENSE)).
<hr />
© 2021-2025 Cyril Dever. All rights reserved