Skip to content

Update ParseFile to handle a data URI containing a number #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ParseFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type FileSource = {
};

var dataUriRegexp =
/^data:([a-zA-Z]*\/[a-zA-Z+.-]*);(charset=[a-zA-Z0-9\-\/\s]*,)?base64,/;
/^data:([a-zA-Z]+\/[-a-zA-Z0-9+.]+)(;charset=[a-zA-Z0-9\-\/]*)?;base64,/;

function b64Digit(number: number): string {
if (number < 26) {
Expand Down
26 changes: 25 additions & 1 deletion src/__tests__/ParseFile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,38 @@ describe('ParseFile', () => {
expect(file._source.type).toBe('');
});

it('can extact data type from base64', () => {
it('can extract data type from base64', () => {
var file = new ParseFile('parse.txt', {
base64: 'data:image/png;base64,ParseA=='
});
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('image/png');
});

it('can extract data type from base64 with data type containing a number', () => {
var file = new ParseFile('parse.m4a', {
base64: 'data:audio/m4a;base64,ParseA=='
});
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('audio/m4a');
});

it('can extract data type from base64 with a complex mime type', () => {
var file = new ParseFile('parse.kml', {
base64: 'data:application/vnd.google-earth.kml+xml;base64,ParseA=='
});
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('application/vnd.google-earth.kml+xml');
});

it('can extract data type from base64 with a charset param', () => {
var file = new ParseFile('parse.kml', {
base64: 'data:application/vnd.3gpp.pic-bw-var;charset=utf-8;base64,ParseA=='
});
expect(file._source.base64).toBe('ParseA==');
expect(file._source.type).toBe('application/vnd.3gpp.pic-bw-var');
});

it('can create files with byte arrays', () => {
var file = new ParseFile('parse.txt', [61, 170, 236, 120]);
expect(file._source.base64).toBe('ParseA==');
Expand Down