diff --git a/src/samplers/string.js b/src/samplers/string.js index ffd9ab0..2a0fa13 100644 --- a/src/samplers/string.js +++ b/src/samplers/string.js @@ -54,18 +54,18 @@ function uriSample() { return faker.internet.url(); } -function byteSample() { - return Buffer.from(faker.lorem.word()).toString('base64'); -} - function binarySample() { - return Buffer.from(faker.lorem.word()); + return toBase64(faker.random.words(3)); } function uuidSample() { return faker.random.uuid(); } +function toBase64(data) { + return Buffer.from(data).toString('base64'); +} + function patternSample(min, max, pattern) { const res = new randexp(pattern).gen(); @@ -89,8 +89,9 @@ const stringFormats = { 'ipv6': ipv6Sample, 'hostname': hostnameSample, 'uri': uriSample, - 'byte': byteSample, + 'byte': binarySample, 'binary': binarySample, + 'base64': binarySample, 'uuid': uuidSample, 'pattern': patternSample, 'default': defaultSample diff --git a/test/unit/string.spec.js b/test/unit/string.spec.js index 2461093..d20cd1f 100644 --- a/test/unit/string.spec.js +++ b/test/unit/string.spec.js @@ -6,6 +6,7 @@ const HOSTNAME_REGEXP = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.) const URI_REGEXP = new RegExp('([A-Za-z][A-Za-z0-9+\\-.]*):(?:(//)(?:((?:[A-Za-z0-9\\-._~!$&\'()*+,;=:]|%[0-9A-Fa-f]{2})*)@)?((?:\\[(?:(?:(?:(?:[0-9A-Fa-f]{1,4}:){6}|::(?:[0-9A-Fa-f]{1,4}:){5}|(?:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,1}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}|(?:(?:[0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}|(?:(?:[0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}:|(?:(?:[0-9A-Fa-f]{1,4}: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+/]{3}=|[A-Za-z0-9+/]{2}==)?$/; describe('sampleString', () => { let res; @@ -90,15 +91,19 @@ describe('sampleString', () => { expect(res).to.match(pattern); }); - it('should return buffer', () => { + it('should return base64 for binary', () => { res = sampleString({format: 'binary'}); - expect(Buffer.isBuffer(res)).to.be.equal(true); + expect(res).to.match(BASE64_REGEXP); }); - it('should return byte', () => { + it('should return base64 for byte', () => { res = sampleString({format: 'byte'}); - const buffered = Buffer.from(res, 'base64'); - expect(Buffer.isBuffer(buffered)).to.be.equal(true); + expect(res).to.match(BASE64_REGEXP); + }); + + it('should return base64 for base64', () => { + res = sampleString({format: 'base64'}); + expect(res).to.match(BASE64_REGEXP); }); it('should return valid email', () => {