Skip to content

Commit

Permalink
Fixed length issue in bytes and string methods (#3) (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldenver committed Sep 22, 2022
1 parent c7a85a3 commit 85b27e7
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ coverage/
.vscode/
package-lock.json
yarn.lock
*.log
*.log
test/__*__.js
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file.

## [v1.0.2](https://github.com/pauldenver/generate-api-key/compare/v1.0.1...v1.0.2)

### Bug Fixes

* Updated how the key length is determined when using the `bytes` method ([#3](https://github.com/pauldenver/generate-api-key/issues/3)).
* Fixed a typo in the `chance.natural` usage in the `string` method.

## [v1.0.1](https://github.com/pauldenver/generate-api-key/compare/v1.0.0...v1.0.1)

### Features
Expand Down
2 changes: 1 addition & 1 deletion dist/generate_api_key.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 11 additions & 17 deletions dist/generate_api_key.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/generate_api_key.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "generate-api-key",
"description": "A library for generating random API key/access tokens",
"version": "1.0.1",
"version": "1.0.2",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"keywords": [
Expand Down
26 changes: 11 additions & 15 deletions src/generate_api_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
* @returns The API key.
*/
const getCryptoApiKey = (options: BytesGenerationOptions): string => {
let totalBytes: number;
let apiKey: string;

// Get the options.
Expand All @@ -50,24 +49,21 @@ const getCryptoApiKey = (options: BytesGenerationOptions): string => {
// Get a 'Chance' instance.
const chance = new Chance();

if (options.length) {
totalBytes = Math.ceil(options.length / 2);
} else {
// Get a random number.
const numVal = chance.natural({ min: options.min, max: options.max });
// Set the total bytes.
totalBytes = Math.ceil(numVal / 2);
}
// Determine the length for the key.
const length = options.length ?? chance.natural({
min: options.min,
max: options.max
});

// Set the total bytes.
const totalBytes = Math.ceil(length / 2);

// Generate the API key.
apiKey = randomBytes(totalBytes).toString('hex');

// Check the key length.
if (options.length && (apiKey.length > options.length)) {
const endIndex = apiKey.length - (apiKey.length - options.length);
apiKey = apiKey.slice(0, endIndex);
} else if (apiKey.length > options.max) {
const endIndex = apiKey.length - (apiKey.length - options.max);
if (apiKey.length > length) {
const endIndex = apiKey.length - (apiKey.length - length);
apiKey = apiKey.slice(0, endIndex);
}

Expand All @@ -94,7 +90,7 @@ const getRandomStringApiKey = (options: StringGenerationOptions): string => {
// Determine the length for the key.
const length = options.length ?? chance.natural({
min: options.min,
max: options.min
max: options.max
});

// Generate the string.
Expand Down

0 comments on commit 85b27e7

Please sign in to comment.