Skip to content

Commit

Permalink
update deps, es6ify code
Browse files Browse the repository at this point in the history
  • Loading branch information
y-a-v-a committed Mar 29, 2019
1 parent f8d237b commit a4e0cfd
Show file tree
Hide file tree
Showing 4 changed files with 1,937 additions and 137 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ os:
- linux

node_js:
- "4"
- "6"
- "7"
- "8"
- "10"
- "11"

sudo: required

Expand Down
67 changes: 34 additions & 33 deletions js/node-gd.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
var util = require('util');
var fs = require('fs');
const fs = require('fs');
const path = require('path');

var bindings;
var versionMessage = 'Node-gd: method __METHOD__ only available from libgd2 version 2.1.1. '
let bindings;

let versionMessage = 'Node-gd: method __METHOD__ only available from libgd2 version 2.1.1. '
+ 'Current installed is ';

function openFormatFn(fmt) {
return function() {
var args = [];
for (var i = 0; i < arguments.length; i++) {
const args = [];
for (let i = 0; i < arguments.length; i++) {
args[i] = (arguments[i]);
}

var file = args.shift();
var cb = args.pop();
const file = args.shift();
const cb = args.pop();

if (typeof cb !== "function") {
return bindings["createFrom" + fmt].apply(this, arguments);
return bindings[`createFrom${fmt}`].apply(this, arguments);
}

return fs.readFile(file, function (err, data) {
var img;
let img;
if (err) {
return cb(err);
} else {
try {
img = bindings["createFrom" + fmt + "Ptr"](data);
} catch(e) {
img = bindings[`createFrom${fmt}Ptr`](data);
} catch (e) {
return cb(e);
}
return cb(null, img);
Expand All @@ -39,28 +40,28 @@ function saveFormatFn(format) {
format = format.toLowerCase();

return function () {
var data;
var args = [];
for (var i = 0; i < arguments.length; i++) {
const args = [];
for (let i = 0; i < arguments.length; i++) {
args[i] = (arguments[i]);
}

var filename = args.shift();
var callback = args.pop();
const filename = args.shift();
const callback = args.pop();

if (typeof callback !== "function") {
return this[format].apply(this, arguments);
}

data = this[format + "Ptr"].apply(this, args);
const data = this[`${format}Ptr`].apply(this, args);
return fs.writeFile(filename, data, "binary", callback);
};
}

function exportFormats() {
var format, formats, valid, version = bindings.getGDVersion();
let valid;
const version = bindings.getGDVersion();

formats = {
const formats = {
Jpeg: [1, 1],
Png: [1, 1],
Gif: [1, 1],
Expand All @@ -82,20 +83,20 @@ function exportFormats() {
formats.Webp = [1, 1];
}

for (format in formats) {
for (const format in formats) {
valid = formats[format];
if (!!valid[0]) {
bindings["open" + format] = openFormatFn(format);
bindings[`open${format}`] = openFormatFn(format);
}
if (!!valid[1]) {
bindings.Image.prototype["save" + format] = saveFormatFn(format);
bindings.Image.prototype[`save${format}`] = saveFormatFn(format);
}
}
}

var libPaths = [
__dirname + '/../build/Release/node_gd.node',
__dirname + '/../build/default/node_gd.node'
const libPaths = [
path.normalize(`${__dirname}/../build/Release/node_gd.node`),
path.normalize(`${__dirname}/../build/default/node_gd.node`)
];

try {
Expand Down Expand Up @@ -130,32 +131,32 @@ if (bindings.getGDVersion() >= '2.1.1') {
};

bindings.openFile = function() {
var args = [];
var image;
for (var i = 0; i < arguments.length; i++) {
const args = [];
let image;
for (let i = 0; i < arguments.length; i++) {
args[i] = (arguments[i]);
}

var callback = args[args.length - 1];
const callback = args[args.length - 1];
if (typeof callback !== "function") {
return this.createFromFile.apply(this, args);
}

try {
image = this.createFromFile.apply(this, args);
} catch(e) {
} catch (e) {
return callback(e);
}
return callback(null, image);
};
} else {
bindings.Image.prototype.saveFile = function() {
throw new Error(versionMessage.replace('__METHOD__', 'gd.Image#saveFile()'));
}
};

bindings.openFile = function() {
throw new Error(versionMessage.replace('__METHOD__', 'gd.openFile()'));
}
};
}

module.exports = bindings;
Loading

0 comments on commit a4e0cfd

Please sign in to comment.