Comparing version
const baseSize = 45; | ||
const baseSizeSquared = 2025; | ||
const baseSizeSquared = baseSize * baseSize; | ||
const chunkSize = 2; | ||
const encodedChunkSize = 3; | ||
const smallEncodedChunkSize = 2; | ||
const byteSize = 256; | ||
const encoding = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:".split(''); | ||
const decoding = Object.fromEntries(encoding.map((l, i) => [l, i])); | ||
const encoding = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; | ||
const decoding = new Map(encoding.split('').map((l, i) => [l, i])); | ||
/** | ||
@@ -16,11 +15,11 @@ * Encode binary data to base45 | ||
const wholeChunkCount = (byteArrayArg.length / chunkSize | 0); | ||
const resultSize = wholeChunkCount * encodedChunkSize + (byteArrayArg.length % chunkSize === 1 ? smallEncodedChunkSize : 0); | ||
const resultSize = wholeChunkCount * encodedChunkSize + | ||
(byteArrayArg.length % chunkSize === 1 ? smallEncodedChunkSize : 0); | ||
const result = new Array(resultSize); | ||
let resultIndex = 0; | ||
const wholeChunkLength = wholeChunkCount * chunkSize; | ||
for (let i = 0; i < wholeChunkLength;) { | ||
const value = byteArrayArg[i++] * byteSize + byteArrayArg[i++]; | ||
result[resultIndex++] = encoding[value % baseSize]; | ||
result[resultIndex++] = encoding[(value / baseSize | 0) % baseSize]; | ||
result[resultIndex++] = encoding[(value / baseSizeSquared | 0) % baseSize]; | ||
for (let i = 0; i < byteArrayArg.length - 1; i += 2) { | ||
let value = (byteArrayArg[i] << 8) | byteArrayArg[i + 1]; | ||
const resultIndex = 3 * i / 2; | ||
result[resultIndex] = encoding[value % baseSize]; | ||
result[resultIndex + 1] = encoding[(value / baseSize | 0) % baseSize]; | ||
result[resultIndex + 2] = encoding[(value / baseSizeSquared | 0) % baseSize]; | ||
} | ||
@@ -51,3 +50,3 @@ if (byteArrayArg.length % chunkSize) { | ||
const char = utf8StringArg[i]; | ||
const found = decoding[char]; | ||
const found = decoding.get(char); | ||
if (found === undefined) | ||
@@ -59,11 +58,12 @@ throw new Error(`Invalid character '${char}' at position ${i}.`); | ||
const result = new Uint8Array(wholeChunkCount * chunkSize + (remainderSize === chunkSize ? 1 : 0)); | ||
let resultIndex = 0; | ||
const wholeChunkLength = wholeChunkCount * encodedChunkSize; | ||
for (let i = 0; i < wholeChunkLength;) { | ||
const val = buffer[i++] + baseSize * buffer[i++] + baseSizeSquared * buffer[i++]; | ||
result[resultIndex++] = (val / byteSize | 0); //result is always in the range 0-255 - % ByteSize omitted. | ||
result[resultIndex++] = val % byteSize; | ||
for (let i = 0; i < buffer.length - 2; i += 3) { | ||
const val = buffer[i] + baseSize * buffer[i + 1] + baseSizeSquared * buffer[i + 2]; | ||
const resultIndex = 2 * i / 3; | ||
result[resultIndex] = val >> 8; //result is always in the range 0-255 - % ByteSize omitted. | ||
result[resultIndex + 1] = val & 0xff; | ||
} | ||
if (remainderSize) | ||
result[result.length - 1] = buffer[buffer.length - 2] + baseSize * buffer[buffer.length - 1]; //result is always in the range 0-255 - % ByteSize omitted. | ||
result[result.length - 1] = | ||
buffer[buffer.length - 2] + | ||
baseSize * buffer[buffer.length - 1]; //result is always in the range 0-255 - % ByteSize omitted. | ||
return result; | ||
@@ -70,0 +70,0 @@ } |
@@ -7,3 +7,3 @@ { | ||
"types": "./lib/base45.d.ts", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"homepage": "https://github.com/lovasoe/base45-ts", | ||
@@ -10,0 +10,0 @@ "author": { |
const baseSize = 45; | ||
const baseSizeSquared = 2025; | ||
const baseSizeSquared = baseSize * baseSize; | ||
const chunkSize = 2; | ||
const encodedChunkSize = 3; | ||
const smallEncodedChunkSize = 2; | ||
const byteSize = 256; | ||
const encoding = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:".split(''); | ||
const decoding = Object.fromEntries(encoding.map((l, i) => [l, i])); | ||
const encoding = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; | ||
const decoding = new Map(encoding.split('').map((l, i) => [l, i])); | ||
@@ -18,12 +17,13 @@ /** | ||
const wholeChunkCount = (byteArrayArg.length / chunkSize | 0); | ||
const resultSize = wholeChunkCount * encodedChunkSize + (byteArrayArg.length % chunkSize === 1 ? smallEncodedChunkSize : 0); | ||
const resultSize = | ||
wholeChunkCount * encodedChunkSize + | ||
(byteArrayArg.length % chunkSize === 1 ? smallEncodedChunkSize : 0); | ||
const result = new Array(resultSize); | ||
let resultIndex = 0; | ||
const wholeChunkLength = wholeChunkCount * chunkSize; | ||
for (let i = 0; i < wholeChunkLength;) { | ||
const value = byteArrayArg[i++] * byteSize + byteArrayArg[i++]; | ||
result[resultIndex++] = encoding[value % baseSize]; | ||
result[resultIndex++] = encoding[(value / baseSize | 0) % baseSize]; | ||
result[resultIndex++] = encoding[(value / baseSizeSquared | 0) % baseSize]; | ||
for (let i = 0; i < byteArrayArg.length - 1; i += 2) { | ||
let value = (byteArrayArg[i] << 8) | byteArrayArg[i + 1]; | ||
const resultIndex = 3 * i / 2; | ||
result[resultIndex] = encoding[value % baseSize]; | ||
result[resultIndex + 1] = encoding[(value / baseSize | 0) % baseSize]; | ||
result[resultIndex + 2] = encoding[(value / baseSizeSquared | 0) % baseSize]; | ||
} | ||
@@ -57,3 +57,3 @@ | ||
const char = utf8StringArg[i]; | ||
const found = decoding[char]; | ||
const found = decoding.get(char); | ||
if (found === undefined) | ||
@@ -66,12 +66,13 @@ throw new Error(`Invalid character '${char}' at position ${i}.`); | ||
const result = new Uint8Array(wholeChunkCount * chunkSize + (remainderSize === chunkSize ? 1 : 0)); | ||
let resultIndex = 0; | ||
const wholeChunkLength = wholeChunkCount * encodedChunkSize; | ||
for (let i = 0; i < wholeChunkLength;) { | ||
const val = buffer[i++] + baseSize * buffer[i++] + baseSizeSquared * buffer[i++]; | ||
result[resultIndex++] = (val / byteSize | 0); //result is always in the range 0-255 - % ByteSize omitted. | ||
result[resultIndex++] = val % byteSize; | ||
for (let i = 0; i < buffer.length - 2; i += 3) { | ||
const val = buffer[i] + baseSize * buffer[i + 1] + baseSizeSquared * buffer[i + 2]; | ||
const resultIndex = 2 * i / 3; | ||
result[resultIndex] = val >> 8; //result is always in the range 0-255 - % ByteSize omitted. | ||
result[resultIndex + 1] = val & 0xff; | ||
} | ||
if (remainderSize) | ||
result[result.length - 1] = buffer[buffer.length - 2] + baseSize * buffer[buffer.length - 1]; //result is always in the range 0-255 - % ByteSize omitted. | ||
result[result.length - 1] = | ||
buffer[buffer.length - 2] + | ||
baseSize * buffer[buffer.length - 1]; //result is always in the range 0-255 - % ByteSize omitted. | ||
@@ -78,0 +79,0 @@ return result; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
235
0.43%33067
-0.36%