Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
fix(string): return a string in base64 for binary/byte/base64 formats (
Browse files Browse the repository at this point in the history
…#13)

closes #11
  • Loading branch information
derevnjuk authored Nov 19, 2019
1 parent 21219a1 commit 8529c43
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
13 changes: 7 additions & 6 deletions src/samplers/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
Expand Down
15 changes: 10 additions & 5 deletions test/unit/string.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}:){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-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
const EMAIL_REGEXP = /(.+)@(.+){2,}\.(.+){2,}/;
const BASE64_REGEXP = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/;

describe('sampleString', () => {
let res;
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 8529c43

Please sign in to comment.