Skip to content

Commit 1ebdf2e

Browse files
thekevinbrownsindresorhus
authored andcommitted
Add numeric type (#17)
1 parent 9cc193b commit 1ebdf2e

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

index.d.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ interface TypeOption {
2020
cryptoRandomString({length: 10});
2121
//=> '87fc70e2b9'
2222
23-
cryptoRandomString({length: 10, type:'base64'});
23+
cryptoRandomString({length: 10, type: 'base64'});
2424
//=> 'mhsX7xmIv/'
2525
26-
cryptoRandomString({length: 10, type:'url-safe'});
26+
cryptoRandomString({length: 10, type: 'url-safe'});
2727
//=> 'VEjfNW3Yej'
28+
29+
cryptoRandomString({length: 10, type: 'numeric'});
30+
//=> '8314659141'
2831
```
2932
*/
30-
type?: 'hex' | 'base64' | 'url-safe';
33+
type?: 'hex' | 'base64' | 'url-safe' | 'numeric';
3134
}
3235

3336
interface CharactersOption {
@@ -41,7 +44,7 @@ interface CharactersOption {
4144
4245
@example
4346
```
44-
cryptoRandomString({length: 10, characters:'0123456789'});
47+
cryptoRandomString({length: 10, characters: '0123456789'});
4548
//=> '8796225811'
4649
```
4750
*/

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const crypto = require('crypto');
33

44
const urlSafeCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~'.split('');
5+
const numericCharacters = '0123456789'.split('');
56

67
const generateForCustomCharacters = (length, characters) => {
78
// Generating entropy is faster than complex math operations, so we use the simplest way
@@ -34,7 +35,8 @@ const allowedTypes = [
3435
undefined,
3536
'hex',
3637
'base64',
37-
'url-safe'
38+
'url-safe',
39+
'numeric'
3840
];
3941

4042
module.exports = ({length, type, characters}) => {
@@ -70,6 +72,10 @@ module.exports = ({length, type, characters}) => {
7072
return generateForCustomCharacters(length, urlSafeCharacters);
7173
}
7274

75+
if (type === 'numeric') {
76+
return generateForCustomCharacters(length, numericCharacters);
77+
}
78+
7379
if (characters.length === 0) {
7480
throw new TypeError('Expected `characters` string length to be greater than or equal to 1');
7581
}

index.test-d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import cryptoRandomString = require('.');
33

44
expectType<string>(cryptoRandomString({length: 10}));
55
expectType<string>(cryptoRandomString({length: 10, type: 'url-safe'}));
6+
expectType<string>(cryptoRandomString({length: 10, type: 'numeric'}));
67
expectType<string>(cryptoRandomString({length: 10, characters: '1234'}));
78

89
expectError(cryptoRandomString({type: 'url-safe'}));
910
expectError(cryptoRandomString({length: 10, type: 'url-safe', characters: '1234'}));
11+
expectError(cryptoRandomString({type: 'numeric'}));
12+
expectError(cryptoRandomString({length: 10, type: 'numeric', characters: '1234'}));

readme.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ cryptoRandomString({length: 10, type: 'base64'});
2626
cryptoRandomString({length: 10, type: 'url-safe'});
2727
//=> 'YN-tqc8pOw'
2828

29-
cryptoRandomString({length: 10, characters: '1234567890'});
30-
//=> '1791935639'
29+
cryptoRandomString({length: 10, type: 'numeric'});
30+
//=> '8314659141'
31+
32+
cryptoRandomString({length: 10, characters: 'abc'});
33+
//=> 'abaaccabac'
3134
```
3235

3336

@@ -52,7 +55,7 @@ Length of the returned string.
5255

5356
Type: `string`<br>
5457
Default: `'hex'`<br>
55-
Values: `'hex'` `'base64'` `'url-safe'`
58+
Values: `'hex'` `'base64'` `'url-safe'` `'numeric'`
5659

5760
Use only characters from a predefined set of allowed characters.
5861

test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'ava';
22
import cryptoRandomString from '.';
33

4-
// Probailistic, result is always less than or equal to actual set size, chance it is less is below 1e-256 for sizes up to 32656
4+
// Probabilistic, result is always less than or equal to actual set size, chance it is less is below 1e-256 for sizes up to 32656
55
const generatedCharacterSetSize = (options, targetSize) => {
66
const set = new Set();
77
const length = targetSize * 640;
@@ -46,6 +46,14 @@ test('url-safe', t => {
4646
t.is(generatedCharacterSetSize({type: 'url-safe'}, 66), 66);
4747
});
4848

49+
test('numeric', t => {
50+
t.is(cryptoRandomString({length: 0, type: 'numeric'}).length, 0);
51+
t.is(cryptoRandomString({length: 10, type: 'numeric'}).length, 10);
52+
t.is(cryptoRandomString({length: 100, type: 'numeric'}).length, 100);
53+
t.regex(cryptoRandomString({length: 100, type: 'numeric'}), /^[\d]*$/); // Sanity check, probabilistic
54+
t.is(generatedCharacterSetSize({type: 'numeric'}, 10), 10);
55+
});
56+
4957
test('characters', t => {
5058
t.is(cryptoRandomString({length: 0, characters: '1234'}).length, 0);
5159
t.is(cryptoRandomString({length: 10, characters: '1234'}).length, 10);

0 commit comments

Comments
 (0)