Skip to content

Commit 4c6c83c

Browse files
committed
Added BASE32 encoding
1 parent 55d59f5 commit 4c6c83c

16 files changed

+348
-46
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/node_modules/
22
/lib/
3-
/samples/
3+
/examples/

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
/tsconfig.json
88
/tslint.json
99
/docs/
10+
/examples/

.vscode/launch.json

+27-15
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,73 @@
77
{
88
"type": "node",
99
"request": "launch",
10-
"name": "[LiteRT/Encodings.js] Sample 01 - BASE64Url Encodings",
11-
"program": "${workspaceFolder}/src/samples/01-base64url.ts",
10+
"name": "[LiteRT/Encodings.js] Example 01 - BASE64Url Encodings",
11+
"program": "${workspaceFolder}/src/examples/01-base64url.ts",
1212
"sourceMaps": true,
1313
"cwd": "${workspaceFolder}",
1414
"outFiles": [
1515
"${workspaceFolder}/lib/*.js",
16-
"${workspaceFolder}/samples/*.js"
16+
"${workspaceFolder}/examples/*.js"
1717
]
1818
},
1919
{
2020
"type": "node",
2121
"request": "launch",
22-
"name": "[LiteRT/Encodings.js] Sample 02 - Strict-URI Encoding",
23-
"program": "${workspaceFolder}/src/samples/02-strict_uri.ts",
22+
"name": "[LiteRT/Encodings.js] Example 02 - Strict-URI Encoding",
23+
"program": "${workspaceFolder}/src/examples/02-strict_uri.ts",
2424
"sourceMaps": true,
2525
"cwd": "${workspaceFolder}",
2626
"outFiles": [
2727
"${workspaceFolder}/lib/*.js",
28-
"${workspaceFolder}/samples/*.js"
28+
"${workspaceFolder}/examples/*.js"
2929
]
3030
},
3131
{
3232
"type": "node",
3333
"request": "launch",
34-
"name": "[LiteRT/Encodings.js] Sample 03 - URL Encodings",
35-
"program": "${workspaceFolder}/src/samples/03-urlencode.ts",
34+
"name": "[LiteRT/Encodings.js] Example 03 - URL Encodings",
35+
"program": "${workspaceFolder}/src/examples/03-urlencode.ts",
3636
"sourceMaps": true,
3737
"cwd": "${workspaceFolder}",
3838
"outFiles": [
3939
"${workspaceFolder}/lib/*.js",
40-
"${workspaceFolder}/samples/*.js"
40+
"${workspaceFolder}/examples/*.js"
4141
]
4242
},
4343
{
4444
"type": "node",
4545
"request": "launch",
46-
"name": "[LiteRT/Encodings.js] Sample 04 - Hex API",
47-
"program": "${workspaceFolder}/src/samples/04-hex.ts",
46+
"name": "[LiteRT/Encodings.js] Example 04 - Hex API",
47+
"program": "${workspaceFolder}/src/examples/04-hex.ts",
4848
"sourceMaps": true,
4949
"cwd": "${workspaceFolder}",
5050
"outFiles": [
5151
"${workspaceFolder}/lib/*.js",
52-
"${workspaceFolder}/samples/*.js"
52+
"${workspaceFolder}/examples/*.js"
5353
]
5454
},
5555
{
5656
"type": "node",
5757
"request": "launch",
58-
"name": "[LiteRT/Encodings.js] Sample 05 - Base62x Encodings",
59-
"program": "${workspaceFolder}/src/samples/05-base62x.ts",
58+
"name": "[LiteRT/Encodings.js] Example 05 - Base62x Encodings",
59+
"program": "${workspaceFolder}/src/examples/05-base62x.ts",
6060
"sourceMaps": true,
6161
"cwd": "${workspaceFolder}",
6262
"outFiles": [
6363
"${workspaceFolder}/lib/*.js",
64-
"${workspaceFolder}/samples/*.js"
64+
"${workspaceFolder}/examples/*.js"
65+
]
66+
},
67+
{
68+
"type": "node",
69+
"request": "launch",
70+
"name": "[LiteRT/Encodings.js] Example 06 - Base32 Encodings",
71+
"program": "${workspaceFolder}/src/examples/06-base32.ts",
72+
"sourceMaps": true,
73+
"cwd": "${workspaceFolder}",
74+
"outFiles": [
75+
"${workspaceFolder}/lib/*.js",
76+
"${workspaceFolder}/examples/*.js"
6577
]
6678
}
6779
]

CHANGES.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
# Changes Logs
22

3+
## v2.1.0
4+
5+
- Added encoding `base32`.
6+
7+
## v2.0.0
8+
9+
- Simplified the APIs.
10+
311
## v1.1.1
412

513
- Fixed the `convert` result from `base62x` to `buffer`, `hex`, `base64`, `utf8`.
6-
- Improved the sample `04-compare` with **binary-safe** tests.
14+
- Improved the example `04-compare` with **binary-safe** tests.
715
- Added supports for encoding `urlencode`.
816

917
## v1.1.0

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
[![GitHub Releases](https://img.shields.io/github/release/litert/encodings.js.svg)](https://github.com/litert/encodings.js/releases "Stable Release")
77

88
The buffer-based encoding utility method for node.js.
9+
910
Following types of encodings are supported:
1011

1112
Name | Description | Binary-Safe
1213
-------------|---------------------------------|:-----------:
1314
`base64` | The standard BASE64 encoding. | Yes
1415
`base64url` | The URL-safe BASE64 encoding. | Yes
1516
`base62x` | The BASE62x encoding. | Yes
17+
`base32` | The standard BASE32 encoding. | Yes
1618
`hex` | The hexadecimal encoding. | Yes
1719
`urlencode` | The purely URL-safe encoding. | Yes
1820
`strict_uri` | The extended URL-safe encoding. | No
@@ -65,6 +67,7 @@ const hex = Enc.stringToHex("hello world"); // Encode a string into
6567
const b62 = Enc.stringToBase62("hello world"); // Encode a string into BASE62
6668
const b64 = Enc.stringToBase64("hello world"); // Encode a string into BASE64
6769
const b64url = Enc.stringToBase64Url("hello world"); // Encode a string into BASE64URL
70+
const b32 = Enc.stringToBase32("hello world"); // Encode a string into BASE32
6871
const url = Enc.stringToUrlencode("hello world"); // Encode a string into URL
6972
const strict = Enc.stringToStrictUri("hello world"); // Encode a string into Strict-URI
7073

@@ -74,20 +77,23 @@ Enc.bufferToHex(Buffer.from("hello world"));
7477
Enc.bufferToBase62(Buffer.from("hello world"));
7578
Enc.bufferToBase64(Buffer.from("hello world"));
7679
Enc.bufferToBase64Url(Buffer.from("hello world"));
80+
Enc.bufferToBase32(Buffer.from("hello world"));
7781
Enc.bufferToUrlencode(Buffer.from("hello world"));
7882
Enc.bufferToStrictUri(Buffer.from("hello world"));
7983

8084
Enc.stringFromHex(hex); // Decode a hex-encoded data as a string
8185
Enc.stringFromBase62(b62); // Decode a BASE62-encoded data as a string
8286
Enc.stringFromBase64(b64); // Decode a BASE64-encoded data as a string
8387
Enc.stringFromBase64Url(b64url); // Decode a BASE64URL-encoded data as a string
88+
Enc.stringFromBase32(b32); // Decode a BASE32-encoded data as a string
8489
Enc.stringFromUrlencode(url); // Decode a urlencoded data as a string
8590
Enc.stringFromStrictUri(strict); // Decode a Strict-URI-encoded data as a string
8691

8792
Enc.bufferFromHex(hex);
8893
Enc.bufferFromBase62(b62);
8994
Enc.bufferFromBase64(b64);
9095
Enc.bufferFromBase64Url(b64url);
96+
Enc.bufferFromBase32(b32);
9197
Enc.bufferFromUrlencode(url);
9298
Enc.bufferFromStrictUri(strict);
9399
```

package-lock.json

-20
This file was deleted.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"name": "@litert/encodings",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "The buffer-based encoding utility method for node.js.",
55
"main": "lib/index.js",
66
"scripts": {
77
"prepare": "npm run rebuild",
88
"build": "echo Using TypeScript && tsc -v && tsc -p .",
99
"build-watch": "echo Using TypeScript && tsc -v && tsc -w -p .",
1010
"rebuild": "npm run clean && npm run lint && npm run build",
11-
"test": "echo See directory sources/samples",
12-
"clean": "rm -rf lib samples",
11+
"test": "echo See directory sources/examples",
12+
"clean": "rm -rf lib examples",
1313
"lint": "tslint -p . -c tslint.json"
1414
},
1515
"repository": {
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/samples/05-base62x.ts src/examples/05-base62x.ts

+26-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,32 @@
1818

1919
import * as Enc from "../lib";
2020

21-
const RAW_DATA = "Hello world!@#$%^&*()~`\":<>?,./[]{}\\|-=_+;'";
21+
const RAND_SOURCE = "abcdefghijlkmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ0123456";
2222

23-
const base62 = Enc.stringToBase62(RAW_DATA);
23+
function randStr(len: number): string {
2424

25-
console.info(`Base62: ${base62}`);
25+
return Array(len).fill(0).map((v) => RAND_SOURCE[Math.floor(Math.random() * RAND_SOURCE.length)]).join("");
26+
}
2627

27-
console.info(`RawData: ${Enc.stringFromBase62(base62)}`);
28+
for (let i = 0; i < 256; i++) {
29+
30+
const origin = randStr(i);
31+
32+
console.info(`Origin: ${origin}`);
33+
34+
const base62 = Enc.stringToBase62(origin);
35+
36+
console.info(`Base62: ${base62}`);
37+
38+
const decoded = Enc.stringFromBase62(base62);
39+
40+
if (origin === decoded) {
41+
42+
console.info(`Decoded: Matched`);
43+
}
44+
else {
45+
46+
console.error(`Decoded: ${decoded}`);
47+
break;
48+
}
49+
}

src/examples/06-base32.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2019 Angus.Fenying <fenying@litert.org>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// tslint:disable:no-console
18+
19+
import * as Enc from "../lib";
20+
21+
const RAND_SOURCE = "abcdefghijlkmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ0123456";
22+
23+
function randStr(len: number): string {
24+
25+
return Array(len).fill(0).map((v) => RAND_SOURCE[Math.floor(Math.random() * RAND_SOURCE.length)]).join("");
26+
}
27+
28+
for (let i = 0; i < 256; i++) {
29+
30+
const origin = randStr(i);
31+
32+
console.info(`Origin: ${origin}`);
33+
34+
const base32 = Enc.stringToBase32(origin);
35+
36+
console.info(`Base32: ${base32}`);
37+
38+
const decoded = Enc.stringFromBase32(base32);
39+
40+
if (origin === decoded) {
41+
42+
console.info(`Decoded: Matched`);
43+
}
44+
else {
45+
46+
console.error(`Decoded: ${decoded}`);
47+
break;
48+
}
49+
}

0 commit comments

Comments
 (0)