parse-data-url
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -0,2 +1,11 @@ | ||
# CHANGELOG | ||
<a name="0.1.6"></a> | ||
### 0.1.6 (2018-03-06) | ||
* avoid of using Buffer.new in Node 6+ | ||
* fix a problem when data is empty | ||
* bump deps | ||
<a name="0.1.5"></a> | ||
@@ -3,0 +12,0 @@ ### 0.1.5 (2017-08-15) |
18
index.js
'use strict'; | ||
var validDataUrl = require('valid-data-url'); | ||
var NODE_MAJOR_VERSION = parseInt(process.version.slice(1), 10); | ||
@@ -13,3 +14,3 @@ module.exports = function (s) { | ||
parts = s.match(validDataUrl.regex); | ||
parts = s.trim().match(validDataUrl.regex); | ||
parsed = {}; | ||
@@ -27,8 +28,13 @@ | ||
if (parts[4]) { | ||
parsed.data = parts[4]; | ||
} | ||
parsed.data = parts[4] || ''; | ||
parsed.toBuffer = function() { | ||
return new Buffer(parsed.data, parsed.base64 ? 'base64' : 'utf8'); | ||
parsed.toBuffer = function () { | ||
// new Buffer(string[, encoding]) is deprecated since: v6.0.0 | ||
// https://nodejs.org/docs/latest-v6.x/api/buffer.html#buffer_new_buffer_string_encoding | ||
var encoding = parsed.base64 ? 'base64' : 'utf8'; | ||
return NODE_MAJOR_VERSION >= 6 ? | ||
Buffer.from(parsed.data, encoding) : | ||
new Buffer(parsed.data, encoding); | ||
}; | ||
@@ -35,0 +41,0 @@ |
{ | ||
"name": "parse-data-url", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Parse data URL string", | ||
@@ -40,15 +40,15 @@ "main": "index.js", | ||
"dependencies": { | ||
"valid-data-url": "^0.1.4" | ||
"valid-data-url": "^0.1.5" | ||
}, | ||
"devDependencies": { | ||
"buffer-equals": "^1.0.4", | ||
"chai": "^4.1.1", | ||
"coveralls": "^2.13.1", | ||
"grunt": "^1.0.1", | ||
"chai": "^4.1.2", | ||
"coveralls": "^3.0.0", | ||
"grunt": "^1.0.2", | ||
"grunt-contrib-jshint": "^1.1.0", | ||
"istanbul": "^0.4.5", | ||
"load-grunt-tasks": "^3.5.2", | ||
"mocha": "^3.5.0", | ||
"mocha": "^3.5.3", | ||
"pre-commit": "^1.2.2" | ||
} | ||
} |
23
test.js
@@ -17,3 +17,8 @@ /* globals describe, it */ | ||
it('return false', function () { | ||
it('return false when no argument passed', function () { | ||
parsed = parseDataUrl(); | ||
expect(parsed).to.be.false; | ||
}); | ||
it('return false when invalid data url', function () { | ||
parsed = parseDataUrl('data:HelloWorld'); | ||
@@ -50,2 +55,11 @@ expect(parsed).to.be.false; | ||
it('parse with empty data ', function () { | ||
parsed = parseDataUrl('data:,'); | ||
expect(parsed).to.be.an('object'); | ||
expect(parsed.mediaType).to.be.undefined; | ||
expect(parsed.base64).to.be.false; | ||
expect(parsed.charset).to.be.undefined; | ||
expect(parsed.data).to.equal(''); | ||
}); | ||
it('parse base64 encoded data with simple media type', function () { | ||
@@ -100,2 +114,9 @@ parsed = parseDataUrl('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D'); | ||
}); | ||
it('export buffer from parsed data with empty data', function () { | ||
parsed = parseDataUrl('data:,'); | ||
var buffer = new Buffer(parsed.data, 'utf8'); | ||
var parsedBuffer = parsed.toBuffer(); | ||
expect(bufferEquals(buffer, parsedBuffer)).to.be.true; | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
12094
148
12
Updatedvalid-data-url@^0.1.5