@alttiri/base85
Advanced tools
Comparing version 1.5.0 to 1.5.1
@@ -0,0 +0,0 @@ /** 85 unique characters string */ |
const ascii85 = charsetToMap(`!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_\`abcdefghijklmnopqrstu`); | ||
const z85 = charsetToMap(`0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#`); | ||
const pow2 = 85 * 85; | ||
const pow3 = 85 * 85 * 85; | ||
const pow4 = 85 * 85 * 85 * 85; | ||
const pow2 = 85 ** 2; | ||
const pow3 = 85 ** 3; | ||
const pow4 = 85 ** 4; | ||
@@ -11,3 +11,5 @@ /** @param {"ascii85" | "z85" | string} [charset="z85"] | ||
function getMap(charset = "z85") { | ||
if (charset === "ascii85") {return ascii85;} | ||
if (charset === "ascii85") { | ||
return ascii85; | ||
} | ||
if (charset?.length === 85) { | ||
@@ -27,2 +29,3 @@ return charsetToMap(charset); | ||
} | ||
/** @param {Uint8Array} mapOrig | ||
@@ -48,3 +51,3 @@ * @return {Uint8Array} */ | ||
const last5Length = remain ? remain + 1 : 0; | ||
const length = Math.ceil(ui8a.length * 5/4); | ||
const length = Math.ceil(ui8a.length * 5 / 4); | ||
const target = new Uint8Array(length); | ||
@@ -57,3 +60,3 @@ | ||
for (let k = 4; k >= 0; k--) { | ||
target[k + i*5] = charMap[num % 85]; | ||
target[k + i * 5] = charMap[num % 85]; | ||
num = Math.trunc(num / 85); | ||
@@ -90,3 +93,3 @@ } | ||
const map = getMap(charset); | ||
const revMap = getReverseMap(map); | ||
const revMap = getReverseMap(map); | ||
@@ -100,8 +103,8 @@ const base85ab = new TextEncoder().encode(base85); | ||
for (; i < base85ab.length / 5 - 1; i++) { | ||
const c1 = revMap[base85ab[i*5 + 4]]; | ||
const c2 = revMap[base85ab[i*5 + 3]] * 85; | ||
const c3 = revMap[base85ab[i*5 + 2]] * pow2; | ||
const c4 = revMap[base85ab[i*5 + 1]] * pow3; | ||
const c5 = revMap[base85ab[i*5 ]] * pow4; | ||
dw.setUint32(i * 4, c1+c2+c3+c4+c5); | ||
const c1 = revMap[base85ab[i * 5 + 4]]; | ||
const c2 = revMap[base85ab[i * 5 + 3]] * 85; | ||
const c3 = revMap[base85ab[i * 5 + 2]] * pow2; | ||
const c4 = revMap[base85ab[i * 5 + 1]] * pow3; | ||
const c5 = revMap[base85ab[i * 5 ]] * pow4; | ||
dw.setUint32(i * 4, c1 + c2 + c3 + c4 + c5); | ||
} | ||
@@ -117,5 +120,5 @@ | ||
const c5 = revMap[lastPart[0]] * pow4; | ||
dw.setUint32(0, c1+c2+c3+c4+c5); | ||
dw.setUint32(0, c1 + c2 + c3 + c4 + c5); | ||
for (let j = 0; j < 4 - pad; j++) { | ||
ints[i*4 + j] = lastPart[j]; | ||
ints[i * 4 + j] = lastPart[j]; | ||
} | ||
@@ -122,0 +125,0 @@ |
{ | ||
"name": "@alttiri/base85", | ||
"version": "1.5.0", | ||
"version": "1.5.1", | ||
"description": "Pretty fast base85 JavaScript library", | ||
@@ -5,0 +5,0 @@ "author": "alttiri", |
# base85 | ||
[Pretty fast](https://github.com/AlttiRi/base85/blob/7a6edf4b9c0b16e0d63e35c0c102c1875f78ddb0/tests/test-2-speed.js#L23-L52) base85 JavaScript library (with TS support). | ||
It is designed to encode binary data (`Uint8Array`) into a "base85" text string. | ||
It is designed to encode binary data (`Uint8Array`) into a "base85" text string and vice versa. | ||
```bash | ||
npm install @alttiri/base85 | ||
``` | ||
## API | ||
#### `function encode(ui8a: Uint8Array, charset?: "ascii85" | "z85" | string) : string` | ||
#### `function encode(ui8a: Uint8Array, charset?: "ascii85" | "z85" | string): string` | ||
[`encode`](https://github.com/AlttiRi/base85/blob/42343e624f27ec68aa936a274c297ccd6c15c8cb/index.js#L42) encodes the input `Uint8Array` into base85 `String`. | ||
[`encode`](https://github.com/AlttiRi/base85/blob/42343e624f27ec68aa936a274c297ccd6c15c8cb/index.js#L42) encodes the input `Uint8Array` into base85 `string`. | ||
#### `function decode(base85: string, charset?: "ascii85" | "z85" | string) : Uint8Array` | ||
#### `function decode(base85: string, charset?: "ascii85" | "z85" | string): Uint8Array` | ||
[`decode`](https://github.com/AlttiRi/base85/blob/42343e624f27ec68aa936a274c297ccd6c15c8cb/index.js#L84) decodes the input base85 `String` into `Uint8Array`. | ||
[`decode`](https://github.com/AlttiRi/base85/blob/42343e624f27ec68aa936a274c297ccd6c15c8cb/index.js#L84) decodes the input base85 `string` into `Uint8Array`. | ||
@@ -91,7 +94,7 @@ `charset` is "z85" by default. | ||
```bash | ||
npm install git+https://github.com/alttiri/base85.git#semver:1.4.0 | ||
npm install git+https://github.com/alttiri/base85.git#semver:1.5.1 | ||
``` | ||
Or add | ||
``` | ||
"@alttiri/base85": "github:alttiri/base85#semver:1.4.0" | ||
"@alttiri/base85": "github:alttiri/base85#semver:1.5.1" | ||
``` | ||
@@ -98,0 +101,0 @@ as `dependencies` in `package.json` file. |
import {decode, encode} from "../base85.js"; | ||
const input = "QWERTY1234".repeat(100_000*5); | ||
const input = "QWERTY1234".repeat(100_000 * 5); | ||
console.log(`Input: ${input.length} chars string`); | ||
// ----------------------------------- | ||
@@ -11,4 +13,4 @@ console.log("\n--- AlttiRi/base85 ---"); | ||
console.time("encode"); | ||
const encodedAb = new TextEncoder().encode(input); | ||
const encoded1 = encode(encodedAb); | ||
const inputAb = new TextEncoder().encode(input); | ||
const encoded1 = encode(inputAb); | ||
console.timeEnd("encode"); | ||
@@ -24,2 +26,3 @@ | ||
/* | ||
@@ -54,16 +57,15 @@ Input: 5000000 chars string | ||
output1 === output4: true | ||
*/ | ||
*/ | ||
// Uncomment and install "base85" | ||
// ----------------------------------- | ||
// Uncomment and "npm install base85" (4.68 MB of node_modules) | ||
// https://github.com/noseglid/base85 | ||
// ----------------------------------- | ||
/* | ||
// ----------------------------------- | ||
console.log("\n--- noseglid/base85 ---"); | ||
// https://github.com/noseglid/base85 | ||
import base85 from "base85"; | ||
console.time("total"); | ||
console.time("encode"); | ||
const encoded2 = base85.encode(input).toString(); | ||
const encoded2 = base85.encode(input); | ||
console.timeEnd("encode"); | ||
@@ -76,14 +78,13 @@ | ||
console.log("input === output2:", input === output2); | ||
console.log("output1 === output2:", output1 === output2); | ||
console.log("input === output2: ", input === output2); | ||
console.log("encoded1 === encoded2:", encoded1 === encoded2); | ||
console.log("output1 === output2: ", output1 === output2); | ||
*/ | ||
// ----------------------------------- | ||
// Uncomment and copy-paste the code from GitHub | ||
// https://github.com/Sheep-y/Base85/blob/master/javascript/base85.js | ||
// ----------------------------------- | ||
/* | ||
// ----------------------------------- | ||
console.log("\n--- Sheep-y/Base85 ---"); | ||
// put code here from | ||
// https://github.com/Sheep-y/Base85/blob/master/javascript/base85.js | ||
(function() {})( | ||
@@ -93,3 +94,2 @@ ... | ||
console.time("total"); | ||
@@ -105,13 +105,13 @@ console.time("encode"); | ||
console.log("input === output3:", input === output3); | ||
console.log("output1 === output3:", output1 === output3); | ||
console.log("input === output3: ", input === output3); | ||
console.log("encoded1 === encoded3:", encoded1 === encoded3); | ||
console.log("output1 === output3: ", output1 === output3); | ||
*/ | ||
// ----------------------------------- | ||
// Uncomment and install "npm install ascii85" | ||
// https://github.com/huandu/node-ascii85 | ||
// ----------------------------------- | ||
/* | ||
// Uncomment and install "ascii85" | ||
// ----------------------------------- | ||
console.log("\n--- huandu/node-ascii85 ---"); | ||
// https://github.com/huandu/node-ascii85 | ||
import ascii85 from "ascii85"; | ||
@@ -123,12 +123,13 @@ | ||
console.time("encode"); | ||
const encoded3 = ZeroMQ.encode(input).toString(); | ||
const encoded4 = ZeroMQ.encode(input).toString(); | ||
console.timeEnd("encode"); | ||
console.time("decode"); | ||
const output4 = ZeroMQ.decode(encoded3).toString(); | ||
const output4 = ZeroMQ.decode(encoded4).toString(); | ||
console.timeEnd("decode"); | ||
console.timeEnd("total"); | ||
console.log("input === output4:", input === output4); | ||
console.log("output1 === output4:", output1 === output4); | ||
*/ | ||
console.log("input === output4: ", input === output4); | ||
console.log("encoded1 === encoded4:", encoded1 === encoded4); | ||
console.log("output1 === output4: ", output1 === output4); | ||
*/ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22920
356
131