Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2b9d61

Browse files
committed
Remove randomStringWithBytes
Use arg type for function override
1 parent 25f307a commit b2b9d61

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

lib/entropy.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ const bitsWithPowers = (tPower, rPower) => {
5252
return N
5353
}
5454

55-
const randomString = (entropy, charSet, crypto = true) => {
56-
const bytes = crypto ? _cryptoBytes(entropy, charSet) : _randomBytes(entropy, charSet)
55+
const randomString = (entropy, charSet, opt) => {
56+
let bytes = (opt instanceof Array) ? opt :
57+
(opt === false) ? _randomBytes(entropy, charSet) : _cryptoBytes(entropy, charSet)
5758
return randomStringWithBytes(entropy, charSet, bytes)
5859
}
5960

test/entropy.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,9 @@ test('Custom 64 chars', t => {
453453
let charSet = entropy.charSet64
454454
try {
455455
charSet.use('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ9876543210_-')
456-
let string = entropy.randomStringWithBytes(72, charSet,
457-
[0x9d, 0x99, 0x4e, 0xa5, 0xd2, 0x3f, 0x8c, 0x86, 0x80])
456+
let bytes = [0x9d, 0x99, 0x4e, 0xa5, 0xd2, 0x3f, 0x8c, 0x86, 0x80]
457+
let string = entropy.randomString(72, charSet, bytes)
458+
458459
t.is(string, 'NzLoPDi-JiAa')
459460
}
460461
catch(error) {
@@ -470,8 +471,7 @@ test('Custom 32 chars', t => {
470471
let charSet = entropy.charSet32
471472
try {
472473
charSet.use('2346789BDFGHJMNPQRTbdfghjlmnpqrt')
473-
let string = entropy.randomStringWithBytes(55, charSet,
474-
[0xd2, 0xe3, 0xe9, 0xda, 0x19, 0x97, 0x52])
474+
let string = entropy.randomString(55, charSet, [0xd2, 0xe3, 0xe9, 0xda, 0x19, 0x97, 0x52])
475475
t.is(string, 'mHRrbgQlTqF')
476476
}
477477
catch(error) {
@@ -487,7 +487,7 @@ test('Custom 16 chars', t => {
487487
let charSet = entropy.charSet16
488488
try {
489489
charSet.use('0123456789ABCDEF')
490-
let string = entropy.randomStringWithBytes(20, charSet, [0xc7, 0xc9, 0x00])
490+
let string = entropy.randomString(20, charSet, [0xc7, 0xc9, 0x00])
491491
t.is(string, 'C7C90')
492492
}
493493
catch(error) {
@@ -503,7 +503,7 @@ test('Custom 8 chars', t => {
503503
let charSet = entropy.charSet8
504504
try {
505505
charSet.use('abcdefgh')
506-
let string = entropy.randomStringWithBytes(30, charSet, [0xc7, 0xc9, 0x07, 0xc9])
506+
let string = entropy.randomString(30, charSet, [0xc7, 0xc9, 0x07, 0xc9])
507507
t.is(string, 'gbheeeahgc')
508508
}
509509
catch(error) {
@@ -519,7 +519,7 @@ test('Custom 4 chars', t => {
519519
let charSet = entropy.charSet4
520520
try {
521521
charSet.use('atcg')
522-
let string = entropy.randomStringWithBytes(16, charSet, [0x20, 0xf1])
522+
let string = entropy.randomString(16, charSet, [0x20, 0xf1])
523523
t.is(string, 'acaaggat')
524524
}
525525
catch(error) {
@@ -535,7 +535,7 @@ test('Custom 2 chars', t => {
535535
let charSet = entropy.charSet2
536536
try {
537537
charSet.use('HT')
538-
let string = entropy.randomStringWithBytes(16, charSet, [0xe3, 0xe9])
538+
let string = entropy.randomString(16, charSet, [0xe3, 0xe9])
539539
t.is(string, 'TTTHHHTTTTTHTHHT')
540540
}
541541
catch(error) {

0 commit comments

Comments
 (0)