Comparing version 1.0.0 to 2.0.0
# 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)_ | ||
[![Build Status](https://travis-ci.org/digitalbazaar/bnid.png?branch=master)](https://travis-ci.org/digitalbazaar/bnid) | ||
[![Node.js CI](https://github.com/digitalbazaar/bnid/workflows/Node.js%20CI/badge.svg)](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 @@ |
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
24125
9
412
289
18
+ Addedyargs@^15.3.1
+ Addedansi-regex@5.0.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedcamelcase@5.3.1(transitive)
+ Addedcliui@6.0.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedfind-up@4.1.0(transitive)
+ Addedget-caller-file@2.0.5(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedlocate-path@5.0.0(transitive)
+ Addedp-limit@2.3.0(transitive)
+ Addedp-locate@4.1.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedpath-exists@4.0.0(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequire-main-filename@2.0.0(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedwhich-module@2.0.1(transitive)
+ Addedwrap-ansi@6.2.0(transitive)
+ Addedy18n@4.0.3(transitive)
+ Addedyargs@15.4.1(transitive)
+ Addedyargs-parser@18.1.3(transitive)
- Removedbenchmark@^2.1.4
- Removedbenchmark@2.1.4(transitive)
- Removedlodash@4.17.21(transitive)
- Removedplatform@1.3.6(transitive)