Skip to content

Commit

Permalink
Avoid of using Buffer.new (#3), bump deps, fix a case when data is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
killmenot committed Mar 5, 2018
1 parent e6dec16 commit 81cb736
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2,266 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
@@ -1,9 +1,7 @@
sudo: false
language: node_js
branches:
except:
- gh-pages
node_js:
- '9'
- '8'
- '7'
- '6'
Expand Down
18 changes: 12 additions & 6 deletions index.js
@@ -1,6 +1,7 @@
'use strict';

var validDataUrl = require('valid-data-url');
var NODE_MAJOR_VERSION = parseInt(process.version.slice(1), 10);

module.exports = function (s) {
var parts;
Expand All @@ -10,7 +11,7 @@ module.exports = function (s) {
return false;
}

parts = s.match(validDataUrl.regex);
parts = s.trim().match(validDataUrl.regex);
parsed = {};

if (parts[1]) {
Expand All @@ -23,12 +24,17 @@ module.exports = function (s) {

parsed.base64 = !!parts[3];

if (parts[4]) {
parsed.data = parts[4];
}
parsed.data = parts[4] || '';

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';

parsed.toBuffer = function() {
return new Buffer(parsed.data, parsed.base64 ? 'base64' : 'utf8');
return NODE_MAJOR_VERSION >= 6 ?
Buffer.from(parsed.data, encoding) :
new Buffer(parsed.data, encoding);
};

return parsed;
Expand Down

0 comments on commit 81cb736

Please sign in to comment.