Skip to content

Commit 122fa82

Browse files
committed
Remove support for old module systems, make zbase32 a standard JavaScript module
1 parent 938d3d6 commit 122fa82

File tree

7 files changed

+219
-253
lines changed

7 files changed

+219
-253
lines changed

.babelrc

Lines changed: 0 additions & 4 deletions
This file was deleted.

.gitlab-ci.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

.prettierrc.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"bracketSpacing": true,
3-
"jsxBracketSameLine": true,
43
"printWidth": 100,
54
"singleQuote": true,
65
"tabWidth": 4,

bin/test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export NODE_PATH="$DIR/../lib/"
1010
echo node path: $NODE_PATH
1111

1212
npx mocha --ui tdd --reporter spec test/test.js
13+

index.js

Lines changed: 112 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -6,138 +6,126 @@
66
* it under the terms of copyleft-next 0.3.1. See copyleft-next-0.3.1.txt.
77
*/
88

9-
'use strict';
10-
11-
(function (factory) {
12-
const imports = ['require'];
13-
14-
if (typeof define === 'function' && define.amd) {
15-
define(imports, factory);
16-
} else if (typeof module === 'object' && module.exports) {
17-
module.exports = factory(require);
18-
} else {
19-
console.log('Module system not recognized, please use AMD or CommonJS');
20-
}
21-
})(function (require) {
22-
const MNET32 = 'ybndrfg8ejkmcpqxot1uwisza345h769';
23-
const MNET32Reverse = {};
24-
for (let i = 0; i < MNET32.length; i++) {
25-
MNET32Reverse[MNET32[i]] = i;
26-
}
27-
28-
function IntegerTooLargeException(message) {
29-
this.message = message;
30-
this.name = 'IntegerTooLargeException';
31-
}
32-
33-
function getBit(byte, offset) {
34-
return (byte >> offset) & 0x01;
35-
}
36-
37-
function to5bit(ab) {
38-
const bytes = new Uint8Array(ab);
39-
40-
let bitCount = 0;
41-
let chunks = [];
42-
let currentChunk = 0;
43-
for (let i = bytes.length - 1; i >= 0; i--) {
44-
for (let j = 0; j < 8; j++) {
45-
currentChunk = (currentChunk >> 1) | (getBit(bytes[i], j) << 4);
46-
if (++bitCount > 4) {
47-
bitCount = 0;
48-
chunks.push(currentChunk);
49-
currentChunk = 0;
50-
}
9+
const MNET32 = 'ybndrfg8ejkmcpqxot1uwisza345h769';
10+
const MNET32Reverse = {};
11+
for (let i = 0; i < MNET32.length; i++) {
12+
MNET32Reverse[MNET32[i]] = i;
13+
}
14+
15+
function IntegerTooLargeException(message) {
16+
this.message = message;
17+
this.name = 'IntegerTooLargeException';
18+
}
19+
20+
function getBit(byte, offset) {
21+
return (byte >> offset) & 0x01;
22+
}
23+
24+
export function to5bit(ab) {
25+
const bytes = new Uint8Array(ab);
26+
27+
let bitCount = 0;
28+
let chunks = [];
29+
let currentChunk = 0;
30+
for (let i = bytes.length - 1; i >= 0; i--) {
31+
for (let j = 0; j < 8; j++) {
32+
currentChunk = (currentChunk >> 1) | (getBit(bytes[i], j) << 4);
33+
if (++bitCount > 4) {
34+
bitCount = 0;
35+
chunks.push(currentChunk);
36+
currentChunk = 0;
5137
}
5238
}
53-
54-
if (bitCount) {
55-
chunks.push(currentChunk >> (5 - bitCount));
56-
}
57-
58-
chunks.reverse();
59-
return chunks;
6039
}
6140

62-
function from5bit(ab) {
63-
const chunks = new Uint8Array(ab);
64-
65-
let bitCount = 0;
66-
let bytes = [];
67-
let currentByte = 0;
68-
for (let i = chunks.length - 1; i >= 0; i--) {
69-
for (let j = 0; j < 5; j++) {
70-
currentByte = (currentByte >> 1) | (getBit(chunks[i], j) << 7);
71-
if (++bitCount > 7) {
72-
bitCount = 0;
73-
bytes.push(currentByte);
74-
currentByte = 0;
75-
}
76-
}
77-
}
78-
79-
bytes.reverse();
80-
return new Uint8Array(bytes);
81-
}
82-
83-
function toNumber(ab) {
84-
return (
85-
(ab[0] << 30) |
86-
(ab[1] << 25) |
87-
(ab[2] << 20) |
88-
(ab[3] << 15) |
89-
(ab[4] << 10) |
90-
(ab[5] << 5) |
91-
ab[6]
92-
);
41+
if (bitCount) {
42+
chunks.push(currentChunk >> (5 - bitCount));
9343
}
9444

95-
function fromNumber(num) {
96-
if (num < 0 || num > 0x7fffffff) {
97-
throw new IntegerTooLargeException('fromNumber expects a 32-bit signed integer');
45+
chunks.reverse();
46+
return chunks;
47+
}
48+
49+
export function from5bit(ab) {
50+
const chunks = new Uint8Array(ab);
51+
52+
let bitCount = 0;
53+
let bytes = [];
54+
let currentByte = 0;
55+
for (let i = chunks.length - 1; i >= 0; i--) {
56+
for (let j = 0; j < 5; j++) {
57+
currentByte = (currentByte >> 1) | (getBit(chunks[i], j) << 7);
58+
if (++bitCount > 7) {
59+
bitCount = 0;
60+
bytes.push(currentByte);
61+
currentByte = 0;
62+
}
9863
}
99-
100-
// Although javascript Numbers can safely represent integers up to (2^53)-1, the
101-
// bitwise operators operate on them as 32-bit signed integers.
102-
// FIXME: look for a popular bigint library which can easily convert big integers as
103-
// ArrayBuffers.
104-
return new Uint8Array([
105-
(num >>> 30) & 0x03,
106-
(num >>> 25) & 0x1f,
107-
(num >>> 20) & 0x1f,
108-
(num >>> 15) & 0x1f,
109-
(num >>> 10) & 0x1f,
110-
(num >>> 5) & 0x1f,
111-
(num >>> 0) & 0x1f,
112-
]);
113-
}
114-
115-
function decode32bitNumber(str) {
116-
return toNumber(decode(str));
117-
}
118-
119-
function encode32bitNumber(int) {
120-
return encode(fromNumber(int));
121-
}
122-
123-
function decode(x) {
124-
return from5bit(x.split('').map((chr) => MNET32Reverse[chr]));
12564
}
12665

127-
function encode(x) {
128-
return to5bit(x)
129-
.map((value) => MNET32[value])
130-
.join('');
66+
bytes.reverse();
67+
return new Uint8Array(bytes);
68+
}
69+
70+
export function toNumber(ab) {
71+
return (
72+
(ab[0] << 30) |
73+
(ab[1] << 25) |
74+
(ab[2] << 20) |
75+
(ab[3] << 15) |
76+
(ab[4] << 10) |
77+
(ab[5] << 5) |
78+
ab[6]
79+
);
80+
}
81+
82+
export function fromNumber(num) {
83+
if (num < 0 || num > 0x7fffffff) {
84+
throw new IntegerTooLargeException('fromNumber expects a 32-bit signed integer');
13185
}
13286

133-
return {
134-
decode: decode,
135-
encode: encode,
136-
from5bit: from5bit,
137-
to5bit: to5bit,
138-
fromNumber: fromNumber,
139-
toNumber: toNumber,
140-
decode32bitNumber: decode32bitNumber,
141-
encode32bitNumber: encode32bitNumber,
142-
};
143-
});
87+
// Although javascript Numbers can safely represent integers up to (2^53)-1, the
88+
// bitwise operators operate on them as 32-bit signed integers.
89+
// FIXME: look for a popular bigint library which can easily convert big integers as
90+
// ArrayBuffers.
91+
return new Uint8Array([
92+
(num >>> 30) & 0x03,
93+
(num >>> 25) & 0x1f,
94+
(num >>> 20) & 0x1f,
95+
(num >>> 15) & 0x1f,
96+
(num >>> 10) & 0x1f,
97+
(num >>> 5) & 0x1f,
98+
(num >>> 0) & 0x1f,
99+
]);
100+
}
101+
102+
export function decode32bitNumber(str) {
103+
return toNumber(decode(str));
104+
}
105+
106+
export function encode32bitNumber(int) {
107+
return encode(fromNumber(int));
108+
}
109+
110+
export function decode(x) {
111+
return from5bit(x.split('').map((chr) => MNET32Reverse[chr]));
112+
}
113+
114+
export function encode(x) {
115+
return to5bit(x)
116+
.map((value) => MNET32[value])
117+
.join('');
118+
}
119+
120+
const zbase32 = {
121+
decode: decode,
122+
encode: encode,
123+
from5bit: from5bit,
124+
to5bit: to5bit,
125+
fromNumber: fromNumber,
126+
toNumber: toNumber,
127+
decode32bitNumber: decode32bitNumber,
128+
encode32bitNumber: encode32bitNumber,
129+
};
130+
131+
export default zbase32;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "zbase32",
3-
"version": "1.0.5",
3+
"version": "2.0.0",
44
"description": "z-base-32 encoding/decoding library",
55
"main": "index.js",
66
"typings": "index.d.ts",
77
"scripts": {
88
"test": "bin/test"
99
},
10+
"type": "module",
1011
"author": "Kuno Woudt <kuno@frob.nl>",
1112
"license": "copyleft-next-0.3.1",
1213
"repository": "https://github.com/warpr/zbase32.git",

0 commit comments

Comments
 (0)