Skip to content

Commit a144fba

Browse files
committed
Export charSets in random
1 parent 8be31b7 commit a144fba

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Efficiently generate cryptographically strong random strings of specified entrop
4747
48-bit string using hex characters:
4848

4949
```js
50+
const CharSet = require('entropy-string').CharSet
51+
5052
string = random.string(48, CharSet.base16)
5153
```
5254

@@ -64,6 +66,8 @@ Efficiently generate cryptographically strong random strings of specified entrop
6466
Base 32 character string with a 1 in a million chance of a repeat in 30 such strings:
6567

6668
```js
69+
const entropy = require('entropy-string').entropy
70+
6771
bits = entropy.bits(30, 1000000)
6872
string = random.string(bits)
6973
```
@@ -150,12 +154,11 @@ Let's use `entropy-string` to help this developer by generating 5 IDs:
150154
```js
151155
const entropy = require('entropy-string').entropy
152156
const random = require('entropy-string').random
153-
const CharSet = require('entropy-string').CharSet
154157

155158
let bits = entropy.bits(10000, 1000000)
156159
let strings = Array()
157160
for (let i = 0; i < 5; i++) {
158-
let string = random.string(bits, CharSet.base16)
161+
let string = random.string(bits, random.charSet16)
159162
strings.push(string)
160163
}
161164
```
@@ -259,7 +262,7 @@ As we've seen in the previous sections, `entropy-string` provides default charac
259262

260263
> ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
261264
262-
The available CharSets are `base64`, `base32`, `base16`, `base8`, `base4` and `base2`. The default characters for each were chosen as follows:
265+
The available CharSets are `base64`, `base32`, `base16`, `base8`, `base4` and `base2`. For convenience, the character sets are also exported as `random` fields `charSet64`, `charSet32`, `charSet16`, `charSet8`, `charSet4` and `charSet2`. The default characters for each were chosen as follows:
263266

264267
- CharSet 64: **ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_**
265268
* The file system and URL safe char set from [RFC 4648](https://tools.ietf.org/html/rfc4648#section-5).
@@ -289,37 +292,36 @@ Being able to easily generate random strings is great, but what if you want to s
289292

290293
```js
291294
const random = require('entropy-string').random
292-
const CharSet = require('entropy-string').CharSet
293-
let flips = random.string(10, CharSet.base2)
295+
296+
let flips = random.string(10, random.charSet2)
294297
```
295298

296299
> flips: 1111001011
297300
298301
The resulting string of __0__'s and __1__'s doesn't look quite right. Perhaps you want to use the characters __H__ and __T__ instead.
299302

300303
```js
301-
CharSet.base2.use('HT')
302-
flips = random.string(10, CharSet.base2)
304+
random.charSet2.use('HT')
305+
flips = random.string(10, random.charSet2)
303306
```
304307

305308
> flips: THHTHTTHHT
306309
307310
As another example, we saw in [Character Sets](#CharacterSets) the default characters for CharSet 16 are **0123456789abcdef**. Suppose you like uppercase hexadecimal letters instead.
308311

309312
```js
310-
CharSet.base16.use('0123456789ABCDEF')
311-
let string = random.string(48, CharSet.base16)
313+
random.charSet16.use('0123456789ABCDEF')
314+
let string = random.string(48, random.charSet16)
312315

313316
```
314317

315318
> string: 08BB82C0056A
316319
317-
`CharSet.baseNN.use(string)` throws an `Error` if the number of characters doesn't match the number required for the CharSet or if the characters are not all unique.
320+
`random.charSetNN.use(string)` throws an `Error` if the number of characters doesn't match the number required for the CharSet or if the characters are not all unique.
318321

319322
```js
320-
const CharSet = require('entropy-string').CharSet
321323
try {
322-
CharSet.base8.use('abcdefh')
324+
random.charSet8.use('abcdefh')
323325
}
324326
catch(error) {
325327
console.log(error.message)
@@ -330,7 +332,7 @@ As another example, we saw in [Character Sets](#CharacterSets) the default chara
330332
331333
```js
332334
try {
333-
CharSet.base8.use('01233210')
335+
random.charSet8.use('01233210')
334336
}
335337
catch(error) {
336338
console.log(error.message)
@@ -408,7 +410,7 @@ The __bytes__ provided can come from any source. However, the number of bytes mu
408410

409411
```js
410412
try {
411-
string = random.string(32, CharSet.base32, bytes)
413+
string = random.string(32, bytes)
412414
}
413415
catch(error) {
414416
console.log(error.message)

lib/random.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,12 @@ module.exports = {
9898
string,
9999
stringRandom,
100100
stringWithBytes,
101-
bytesNeeded
101+
bytesNeeded,
102+
103+
charSet64: CharSet.base64,
104+
charSet32: CharSet.base32,
105+
charSet16: CharSet.base16,
106+
charSet8: CharSet.base8,
107+
charSet4: CharSet.base4,
108+
charSet2: CharSet.base2
102109
}

0 commit comments

Comments
 (0)