Comparing version
# bnid ChangeLog | ||
## 2.0.0 - 2020-05-22 | ||
### Changed | ||
- **BREAKING**: `multibase` now defaults to `true` for all APIs. | ||
- Remove travis-ci testing in favor of GitHub Actions. | ||
### Added | ||
- Command line tool `bnid`. | ||
- `min/maxEncodedIdBytes` utiltiies. Useful for variable length encodings such | ||
as `base58`. | ||
## 1.0.0 - 2020-05-07 | ||
@@ -4,0 +15,0 @@ |
75
main.js
@@ -47,4 +47,3 @@ /*! | ||
if(bitLength > maxLength) { | ||
throw new Error( | ||
`Input length greater than ${maxLength} bits.`); | ||
throw new Error(`Input length greater than ${maxLength} bits.`); | ||
} | ||
@@ -157,3 +156,3 @@ return maxLength; | ||
* base on input byte size. | ||
* @param {boolean} [options.multibase=false] - Use multibase encoding. | ||
* @param {boolean} [options.multibase=true] - Use multibase encoding. | ||
* | ||
@@ -166,3 +165,3 @@ * @returns {IdEncoder} - New IdEncoder. | ||
fixedBitLength, | ||
multibase = false | ||
multibase = true | ||
} = {}) { | ||
@@ -228,3 +227,3 @@ switch(encoding) { | ||
* with leading non-zero data will error. | ||
* @param {boolean} [options.multibase=false] - Use multibase encoding to | ||
* @param {boolean} [options.multibase=true] - Use multibase encoding to | ||
* detect the id format. | ||
@@ -237,3 +236,3 @@ * | ||
fixedBitLength, | ||
multibase = false | ||
multibase = true | ||
} = {}) { | ||
@@ -332,1 +331,65 @@ this.encoding = encoding; | ||
} | ||
/** | ||
* Minimum number of bytes needed to encode an id of a given bit length. | ||
* | ||
* @param {object} options - The options to use. | ||
* @param {string} [options.encoding='base58'] - Encoding format. | ||
* @param {number} [options.bitLength=128] - Number of id bits. | ||
* @param {boolean} [options.multibase=true] - Account for multibase encoding. | ||
* | ||
* @returns {number} - The minimum number of encoded bytes. | ||
*/ | ||
export function minEncodedIdBytes({ | ||
encoding = 'base58', | ||
bitLength = 128, | ||
multibase = true | ||
} = {}) { | ||
let plainBytes; | ||
switch(encoding) { | ||
case 'hex': | ||
case 'base16': | ||
case 'base16upper': | ||
plainBytes = bitLength / 4; | ||
break; | ||
case 'base58': | ||
case 'base58btc': | ||
plainBytes = bitLength / 8; | ||
break; | ||
default: | ||
throw new Error(`Unknown encoding type: "${encoding}".`); | ||
} | ||
return plainBytes + (multibase ? 1 : 0); | ||
} | ||
/** | ||
* Maximum number of bytes needed to encode an id of a given bit length. | ||
* | ||
* @param {object} options - The options to use. | ||
* @param {string} [options.encoding='base58'] - Encoding format. | ||
* @param {number} [options.bitLength=128] - Number of id bits. | ||
* @param {boolean} [options.multibase=true] - Account for multibase encoding. | ||
* | ||
* @returns {number} - The maximum number of encoded bytes. | ||
*/ | ||
export function maxEncodedIdBytes({ | ||
encoding = 'base58', | ||
bitLength = 128, | ||
multibase = true | ||
} = {}) { | ||
let plainBytes; | ||
switch(encoding) { | ||
case 'hex': | ||
case 'base16': | ||
case 'base16upper': | ||
plainBytes = bitLength / 4; | ||
break; | ||
case 'base58': | ||
case 'base58btc': | ||
plainBytes = Math.ceil(bitLength / Math.log2(58)); | ||
break; | ||
default: | ||
throw new Error(`Unknown encoding type: "${encoding}".`); | ||
} | ||
return plainBytes + (multibase ? 1 : 0); | ||
} |
{ | ||
"name": "bnid", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Base-N Id Generator", | ||
@@ -29,2 +29,3 @@ "license": "BSD-3-Clause", | ||
}, | ||
"bin": "./bnid", | ||
"engines": { | ||
@@ -52,13 +53,14 @@ "node": ">=10" | ||
"base58-universal": "^1.0.0", | ||
"benchmark": "^2.1.4", | ||
"esm": "^3.2.22" | ||
"esm": "^3.2.22", | ||
"yargs": "^15.3.1" | ||
}, | ||
"devDependencies": { | ||
"benchmark": "^2.1.4", | ||
"chai": "^4.2.0", | ||
"chai-bytes": "^0.1.2", | ||
"cross-env": "^6.0.3", | ||
"eslint": "^6.8.0", | ||
"eslint-config-digitalbazaar": "^2.4.0", | ||
"eslint-plugin-jsdoc": "^24.0.6", | ||
"karma": "^5.0.4", | ||
"eslint": "^7.0.0", | ||
"eslint-config-digitalbazaar": "^2.5.0", | ||
"eslint-plugin-jsdoc": "^25.4.2", | ||
"karma": "^5.0.9", | ||
"karma-chai": "^0.1.0", | ||
@@ -65,0 +67,0 @@ "karma-chrome-launcher": "^3.1.0", |
# JavaScript Base-N Id Generator _(bnid)_ | ||
[](https://travis-ci.org/digitalbazaar/bnid) | ||
[](https://github.com/digitalbazaar/bnid/actions?query=workflow%3A%22Node.js+CI%22) | ||
@@ -14,2 +14,3 @@ > A JavaScript library for Web browsers and Node.js apps to generate random | ||
- [API](#api) | ||
- [CLI](#cli) | ||
- [Contribute](#contribute) | ||
@@ -69,3 +70,4 @@ - [Commercial Support](#commercial-support) | ||
To generate a default [Base58][], 128 bit, non-fixed-length id: | ||
To generate a default [Base58][], 128 bit, non-fixed-length, multibase encoded | ||
id: | ||
@@ -179,3 +181,3 @@ ```js | ||
(default: 0) | ||
- `multibase`: `true` to use multibase encoding. (default: `false`) | ||
- `multibase`: `true` to use multibase encoding. (default: `true`) | ||
@@ -204,2 +206,64 @@ #### `encode(bytes)` | ||
### `minEncodedIdBytes(options)` | ||
Minimum number of bytes needed to encode an id of a given bit length. | ||
Options: | ||
- `encoding`: Encoding. (default: `base58`) | ||
- `bitLength`: Number of id bits. (default: 128) | ||
- `multibase`: Account for multibase encoding. (default: true) | ||
### `maxEncodedIdBytes(options)` | ||
Maximum number of bytes needed to encode an id of a given bit length. | ||
Options: | ||
- `encoding`: Encoding. (default: `base58`) | ||
- `bitLength`: Number of id bits. (default: 128) | ||
- `multibase`: Account for multibase encoding. (default: true) | ||
## CLI | ||
A command line interface tool called `bnid` is provided to generate and encode | ||
ids. | ||
`bnid` can be run installed, run directly, or run via `npx`: | ||
``` | ||
npm install -g bnid | ||
bnid [OPTIONS] | ||
``` | ||
``` | ||
./bnid [OPTIONS] | ||
``` | ||
``` | ||
npx bnid [OPTIONS] | ||
``` | ||
The options follow the API. See help for more information: | ||
``` | ||
npx bnid --help | ||
``` | ||
Examples: | ||
``` | ||
npx bnid | ||
> zL8ajDGq3G44VpTnB7UVMq2 | ||
npx bnid -e base16 --no-multibase -n 64 | ||
> da3cc9f90f9f8427 | ||
npx bnid -e base16 --no-multibase -n 64 -b 128 | ||
> 000000000000000063bb5478d65f80ab | ||
npx bnid -n 32 | ||
z6uGJaE | ||
npx bnid -n 32 | ||
zipFBr | ||
# Note: -f used to ensure fixed length | ||
npx bnid -n 32 -f | ||
z17vsf8 | ||
npx bnid -n 32 -b 64 | ||
z111113TqAT2 | ||
``` | ||
## Contribute | ||
@@ -206,0 +270,0 @@ |
24125
26.01%9
12.5%412
17.38%289
28.44%18
5.88%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed