parse-data-url
Advanced tools
Comparing version 0.1.6 to 0.2.0
# CHANGELOG | ||
<a name="0.2.0"></a> | ||
### 0.2.0 (2018-03-09) | ||
* add contentType property that contains only `type "/" subtype` | ||
* support multiple media type parameters exposed via properties | ||
<a name="0.1.6"></a> | ||
@@ -4,0 +11,0 @@ ### 0.1.6 (2018-03-06) |
17
index.js
@@ -8,2 +8,3 @@ 'use strict'; | ||
var parts; | ||
var mediaTypeParts; | ||
var parsed; | ||
@@ -20,12 +21,16 @@ | ||
parsed.mediaType = parts[1].toLowerCase(); | ||
} | ||
if (parts[2]) { | ||
parsed.charset = parts[2].split('=')[1].toLowerCase(); | ||
mediaTypeParts = parts[1].split(';').map(function (s) { return s.toLowerCase(); }); | ||
parsed.contentType = mediaTypeParts[0]; | ||
mediaTypeParts.slice(1).forEach(function (attribute) { | ||
var p = attribute.split('='); | ||
parsed[p[0]] = p[1]; | ||
}); | ||
} | ||
parsed.base64 = !!parts[3]; | ||
parsed.base64 = !!parts[parts.length - 2]; | ||
parsed.data = parts[parts.length - 1] || ''; | ||
parsed.data = parts[4] || ''; | ||
parsed.toBuffer = function () { | ||
@@ -32,0 +37,0 @@ // new Buffer(string[, encoding]) is deprecated since: v6.0.0 |
{ | ||
"name": "parse-data-url", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "Parse data URL string", | ||
@@ -40,3 +40,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"valid-data-url": "^0.1.5" | ||
"valid-data-url": "^0.1.6" | ||
}, | ||
@@ -43,0 +43,0 @@ "devDependencies": { |
25
test.js
@@ -10,3 +10,3 @@ /* globals describe, it */ | ||
describe('module', function () { | ||
describe('parse-data-url', function () { | ||
var parsed; | ||
@@ -32,2 +32,3 @@ | ||
expect(parsed.mediaType).to.be.undefined; | ||
expect(parsed.contentType).to.be.undefined; | ||
expect(parsed.base64).to.be.false; | ||
@@ -42,2 +43,3 @@ expect(parsed.charset).to.be.undefined; | ||
expect(parsed.mediaType).to.be.undefined; | ||
expect(parsed.contentType).to.be.undefined; | ||
expect(parsed.base64).to.be.false; | ||
@@ -52,2 +54,3 @@ expect(parsed.charset).to.be.undefined; | ||
expect(parsed.mediaType).to.equal('text/html'); | ||
expect(parsed.contentType).to.equal('text/html'); | ||
expect(parsed.base64).to.be.false; | ||
@@ -62,2 +65,3 @@ expect(parsed.charset).to.be.undefined; | ||
expect(parsed.mediaType).to.be.undefined; | ||
expect(parsed.contentType).to.be.undefined; | ||
expect(parsed.base64).to.be.false; | ||
@@ -72,2 +76,3 @@ expect(parsed.charset).to.be.undefined; | ||
expect(parsed.mediaType).to.equal('text/plain'); | ||
expect(parsed.contentType).to.equal('text/plain'); | ||
expect(parsed.base64).to.be.true; | ||
@@ -82,2 +87,3 @@ expect(parsed.charset).to.be.undefined; | ||
expect(parsed.mediaType).to.equal('image/svg+xml'); | ||
expect(parsed.contentType).to.equal('image/svg+xml'); | ||
expect(parsed.base64).to.be.true; | ||
@@ -92,2 +98,3 @@ expect(parsed.charset).to.be.undefined; | ||
expect(parsed.mediaType).to.equal('application/vnd.ms-excel'); | ||
expect(parsed.contentType).to.equal('application/vnd.ms-excel'); | ||
expect(parsed.base64).to.be.true; | ||
@@ -98,11 +105,23 @@ expect(parsed.charset).to.be.undefined; | ||
it('parse data with complex media type and charset', function () { | ||
it('parse data with complex media type and single attribute', function () { | ||
parsed = parseDataUrl('data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%22%20height%3D%22100%22%3E%3Crect%20fill%3D%22%2300B1FF%22%20width%3D%22100%22%20height%3D%22100%22%2F%3E%3C%2Fsvg%3E'); | ||
expect(parsed).to.be.an('object'); | ||
expect(parsed.mediaType).to.equal('image/svg+xml;charset=utf-8'); | ||
expect(parsed.contentType).to.equal('image/svg+xml'); | ||
expect(parsed.charset).to.equal('utf-8'); | ||
expect(parsed.base64).to.be.false; | ||
expect(parsed.charset).to.equal('utf-8'); | ||
expect(parsed.data).to.equal('%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%22%20height%3D%22100%22%3E%3Crect%20fill%3D%22%2300B1FF%22%20width%3D%22100%22%20height%3D%22100%22%2F%3E%3C%2Fsvg%3E'); | ||
}); | ||
it('parse data with media type and multiple attributes', function () { | ||
parsed = parseDataUrl('data:image/png;name=foo.bar;baz=quux;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC'); | ||
expect(parsed).to.be.an('object'); | ||
expect(parsed.mediaType).to.equal('image/png;name=foo.bar;baz=quux'); | ||
expect(parsed.contentType).to.equal('image/png'); | ||
expect(parsed.name).to.equal('foo.bar'); | ||
expect(parsed.baz).to.equal('quux'); | ||
expect(parsed.base64).to.be.true; | ||
expect(parsed.data).to.equal('iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC'); | ||
}); | ||
it('export buffer from parsed data with base64', function () { | ||
@@ -109,0 +128,0 @@ parsed = parseDataUrl('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D'); |
13803
170
Updatedvalid-data-url@^0.1.6