Skip to content

Commit

Permalink
Format code using prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
warpr committed Jul 4, 2019
1 parent 5ea2f91 commit 5210be2
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 269 deletions.
251 changes: 127 additions & 124 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,137 +6,140 @@
* it under the terms of copyleft-next 0.3.1. See copyleft-next-0.3.1.txt.
*/

'use strict';

(function (factory) {
const imports = [
'require',
];

if (typeof define === 'function' && define.amd) {
define (imports, factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory (require);
} else {
console.log ('Module system not recognized, please use AMD or CommonJS');
}
} (function (require) {
const MNET32 = 'ybndrfg8ejkmcpqxot1uwisza345h769';
const MNET32Reverse = {};
for (let i = 0; i < MNET32.length; i++) {
MNET32Reverse[MNET32[i]] = i;
}

function IntegerTooLargeException (message) {
this.message = message;
this.name = 'IntegerTooLargeException';
}

function getBit (byte, offset) {
return (byte >> offset) & 0x01;
}

function to5bit (ab) {
const bytes = new Uint8Array (ab);

let bitCount = 0;
let chunks = [];
let currentChunk = 0;
for (let i = bytes.length - 1; i >= 0; i--) {
for (let j = 0; j < 8; j++) {
currentChunk = (currentChunk >> 1) | (getBit (bytes[i], j) << 4);
if (++bitCount > 4) {
bitCount = 0;
chunks.push (currentChunk);
currentChunk = 0;
}
}
}

if (bitCount) {
chunks.push (currentChunk >> (5 - bitCount));
"use strict";

(function(factory) {
const imports = ["require"];

if (typeof define === "function" && define.amd) {
define(imports, factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory(require);
} else {
console.log("Module system not recognized, please use AMD or CommonJS");
}
})(function(require) {
const MNET32 = "ybndrfg8ejkmcpqxot1uwisza345h769";
const MNET32Reverse = {};
for (let i = 0; i < MNET32.length; i++) {
MNET32Reverse[MNET32[i]] = i;
}

function IntegerTooLargeException(message) {
this.message = message;
this.name = "IntegerTooLargeException";
}

function getBit(byte, offset) {
return (byte >> offset) & 0x01;
}

function to5bit(ab) {
const bytes = new Uint8Array(ab);

let bitCount = 0;
let chunks = [];
let currentChunk = 0;
for (let i = bytes.length - 1; i >= 0; i--) {
for (let j = 0; j < 8; j++) {
currentChunk = (currentChunk >> 1) | (getBit(bytes[i], j) << 4);
if (++bitCount > 4) {
bitCount = 0;
chunks.push(currentChunk);
currentChunk = 0;
}

chunks.reverse ();
return chunks;
}
}

function from5bit (ab) {
const chunks = new Uint8Array (ab);

let bitCount = 0;
let bytes = [];
let currentByte = 0;
for (let i = chunks.length - 1; i >= 0; i--) {
for (let j = 0; j < 5; j++) {
currentByte = (currentByte >> 1) | (getBit (chunks[i], j) << 7);
if (++bitCount > 7) {
bitCount = 0;
bytes.push (currentByte);
currentByte = 0;
}
}
}

bytes.reverse ();
return new Uint8Array (bytes);
}

function toNumber (ab) {
return (ab[0] << 30
| ab[1] << 25
| ab[2] << 20
| ab[3] << 15
| ab[4] << 10
| ab[5] << 5
| ab[6]
);
if (bitCount) {
chunks.push(currentChunk >> (5 - bitCount));
}

function fromNumber (num) {
if (num < 0 || num > 0x7FFFFFFF) {
throw new IntegerTooLargeException ('fromNumber expects a 32-bit signed integer');
chunks.reverse();
return chunks;
}

function from5bit(ab) {
const chunks = new Uint8Array(ab);

let bitCount = 0;
let bytes = [];
let currentByte = 0;
for (let i = chunks.length - 1; i >= 0; i--) {
for (let j = 0; j < 5; j++) {
currentByte = (currentByte >> 1) | (getBit(chunks[i], j) << 7);
if (++bitCount > 7) {
bitCount = 0;
bytes.push(currentByte);
currentByte = 0;
}

// Although javascript Numbers can safely represent integers up to (2^53)-1, the
// bitwise operators operate on them as 32-bit signed integers.
// FIXME: look for a popular bigint library which can easily convert big integers as
// ArrayBuffers.
return new Uint8Array ([
(num >>> 30) & 0x03,
(num >>> 25) & 0x1f,
(num >>> 20) & 0x1f,
(num >>> 15) & 0x1f,
(num >>> 10) & 0x1f,
(num >>> 5) & 0x1f,
(num >>> 0) & 0x1f
]);
}

function decode32bitNumber (str) {
return toNumber (decode (str));
}

function encode32bitNumber (int) {
return encode (fromNumber (int));
}

function decode (x) {
return from5bit (x.split ('').map ((chr) => MNET32Reverse[chr]));
}
}

function encode (x) {
return to5bit (x).map ((value) => MNET32[value]).join ('');
bytes.reverse();
return new Uint8Array(bytes);
}

function toNumber(ab) {
return (
(ab[0] << 30) |
(ab[1] << 25) |
(ab[2] << 20) |
(ab[3] << 15) |
(ab[4] << 10) |
(ab[5] << 5) |
ab[6]
);
}

function fromNumber(num) {
if (num < 0 || num > 0x7fffffff) {
throw new IntegerTooLargeException(
"fromNumber expects a 32-bit signed integer"
);
}

return {
decode: decode,
encode: encode,
from5bit: from5bit,
to5bit: to5bit,
fromNumber: fromNumber,
toNumber: toNumber,
decode32bitNumber: decode32bitNumber,
encode32bitNumber: encode32bitNumber,
};
}));
// Although javascript Numbers can safely represent integers up to (2^53)-1, the
// bitwise operators operate on them as 32-bit signed integers.
// FIXME: look for a popular bigint library which can easily convert big integers as
// ArrayBuffers.
return new Uint8Array([
(num >>> 30) & 0x03,
(num >>> 25) & 0x1f,
(num >>> 20) & 0x1f,
(num >>> 15) & 0x1f,
(num >>> 10) & 0x1f,
(num >>> 5) & 0x1f,
(num >>> 0) & 0x1f
]);
}

function decode32bitNumber(str) {
return toNumber(decode(str));
}

function encode32bitNumber(int) {
return encode(fromNumber(int));
}

function decode(x) {
return from5bit(x.split("").map(chr => MNET32Reverse[chr]));
}

function encode(x) {
return to5bit(x)
.map(value => MNET32[value])
.join("");
}

return {
decode: decode,
encode: encode,
from5bit: from5bit,
to5bit: to5bit,
fromNumber: fromNumber,
toNumber: toNumber,
decode32bitNumber: decode32bitNumber,
encode32bitNumber: encode32bitNumber
};
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {},
"devDependencies": {
"chai": "^3.5.0",
"husky": "^3.0.0",
"husky": "^2.6.0",
"jsverify": "^0.7.1",
"lint-staged": "^9.0.2",
"mocha": "^2.5.3",
Expand Down
Loading

0 comments on commit 5210be2

Please sign in to comment.