Skip to content

Commit a4c63c8

Browse files
Add alphanumeric character set (#25)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent daa6f5a commit a4c63c8

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ interface TypeOption {
1919
2020
The `ascii-printable` set contains all [printable ASCII characters](https://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters): ``!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~`` Useful for generating passwords where all possible ASCII characters should be used.
2121
22+
The `alphanumeric` set contains uppercase letters, lowercase letters, and digits: `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`. Useful for generating [nonce](https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/nonce) values.
23+
2224
@example
2325
```
2426
cryptoRandomString({length: 10});
@@ -38,9 +40,12 @@ interface TypeOption {
3840
3941
cryptoRandomString({length: 10, type: 'ascii-printable'});
4042
//=> '`#Rt8$IK>B'
43+
44+
cryptoRandomString({length: 10, type: 'alphanumeric'});
45+
//=> 'DMuKL8YtE7'
4146
```
4247
*/
43-
type?: 'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable' | 'ascii-printable';
48+
type?: 'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable' | 'ascii-printable' | 'alphanumeric';
4449
}
4550

4651
interface CharactersOption {

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const urlSafeCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0
88
const numericCharacters = '0123456789'.split('');
99
const distinguishableCharacters = 'CDEHKMPRTUWXY012458'.split('');
1010
const asciiPrintableCharacters = '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'.split('');
11+
const alphanumericCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.split('');
1112

1213
const generateForCustomCharacters = (length, characters) => {
1314
// Generating entropy is faster than complex math operations, so we use the simplest way
@@ -77,7 +78,8 @@ const allowedTypes = [
7778
'url-safe',
7879
'numeric',
7980
'distinguishable',
80-
'ascii-printable'
81+
'ascii-printable',
82+
'alphanumeric'
8183
];
8284

8385
const createGenerator = (generateForCustomCharacters, generateRandomBytes) => ({length, type, characters}) => {
@@ -125,6 +127,10 @@ const createGenerator = (generateForCustomCharacters, generateRandomBytes) => ({
125127
return generateForCustomCharacters(length, asciiPrintableCharacters);
126128
}
127129

130+
if (type === 'alphanumeric') {
131+
return generateForCustomCharacters(length, alphanumericCharacters);
132+
}
133+
128134
if (characters.length === 0) {
129135
throw new TypeError('Expected `characters` string length to be greater than or equal to 1');
130136
}

readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ cryptoRandomString({length: 6, type: 'distinguishable'});
3333
cryptoRandomString({length: 10, type: 'ascii-printable'});
3434
//=> '`#Rt8$IK>B'
3535

36+
cryptoRandomString({length: 10, type: 'alphanumeric'});
37+
//=> 'DMuKL8YtE7'
38+
3639
cryptoRandomString({length: 10, characters: 'abc'});
3740
//=> 'abaaccabac'
3841
```
@@ -62,7 +65,7 @@ Length of the returned string.
6265

6366
Type: `string`\
6467
Default: `'hex'`\
65-
Values: `'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable' | 'ascii-printable'`
68+
Values: `'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable' | 'ascii-printable' | 'alphanumeric'`
6669

6770
Use only characters from a predefined set of allowed characters.
6871

@@ -72,6 +75,8 @@ The `distinguishable` set contains only uppercase characters that are not easily
7275

7376
The `ascii-printable` set contains all [printable ASCII characters](https://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters): ``!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~`` Useful for generating passwords where all possible ASCII characters should be used.
7477

78+
The `alphanumeric` set contains uppercase letters, lowercase letters, and digits: `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`. Useful for generating [nonce](https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/nonce) values.
79+
7580
##### characters
7681

7782
Type: `string`\

test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ test('ascii-printable', t => {
7676
t.regex(cryptoRandomString({length: 100, type: 'ascii-printable'}), /^[!"#$%&'()*+,-./\d:;<=>?@A-Z[\\\]^_`a-z{|}~]*$/); // Sanity check, probabilistic
7777
});
7878

79+
test('alphanumeric', t => {
80+
t.is(cryptoRandomString({length: 0, type: 'alphanumeric'}).length, 0);
81+
t.is(cryptoRandomString({length: 10, type: 'alphanumeric'}).length, 10);
82+
t.is(cryptoRandomString({length: 100, type: 'alphanumeric'}).length, 100);
83+
t.regex(cryptoRandomString({length: 100, type: 'alphanumeric'}), /^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789]*$/); // Sanity check, probabilistic
84+
t.is(generatedCharacterSetSize({type: 'alphanumeric'}, 19), 62);
85+
});
86+
7987
test('characters', t => {
8088
t.is(cryptoRandomString({length: 0, characters: '1234'}).length, 0);
8189
t.is(cryptoRandomString({length: 10, characters: '1234'}).length, 10);

0 commit comments

Comments
 (0)