Skip to content

Commit

Permalink
Merge pull request #10 from freewil/dup-define
Browse files Browse the repository at this point in the history
don't redefine `stdout`, `stderr`
  • Loading branch information
bcelenza committed Apr 7, 2014
2 parents e1a4176 + b71c180 commit 24310da
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions lib/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ var util = require('util'),

var Encoder = exports.Encoder = function(){
events.EventEmitter.call(this);


}
util.inherits(Encoder, events.EventEmitter);

Expand All @@ -20,57 +20,58 @@ Encoder.prototype.default_options = {
type : null // Default to null type, removing this flag qrencode < v3.4.0
};

Encoder.prototype.types = ['PNG','EPS','SVG','ANSI','ANSI256','ASCII','ASCIIi','UTF8','ANSIUTF8', null];
Encoder.prototype.types = ['PNG', 'EPS', 'SVG', 'ANSI', 'ANSI256', 'ASCII',
'ASCIIi', 'UTF8', 'ANSIUTF8', null];

/**
* Converts a string value to QR Code PNG data, and optionally saves to a file
*
* @param String value The value to be encoded
* @param String path Where to save the PNG file (optional)
* @param Object options A hash of options (optional)
* @return void
* @param Object options A hash of options (optional)
* @return void
*/
Encoder.prototype.encode = function(value, path, options)
{
// preserve scope in callbacks with self
var self = this,
cmd_options, qrencode_args, stdout, stderr, qrencode, exitcode;

try {
// check for undefined value
if(value == undefined) {
throw new Error('No value specified for encode method');
}

// create new buffer for value
value = new Buffer(value);

// if options are given, override defaults
cmd_options = {};
if(options == null) options = {};
for(var key in this.default_options) {
cmd_options[key] = (options[key] == undefined) ?
this.default_options[key]
cmd_options[key] = (options[key] == undefined) ?
this.default_options[key]
: options[key];
}

// start with base set of args that we'll always pass
qrencode_args = [
'-s', cmd_options.dot_size,
'-s', cmd_options.dot_size,
'-m', cmd_options.margin,
'-l', cmd_options.level,
'-v', cmd_options.version
];

// only set foreground and background colors if they differ from
// defaults to maintain compatibility with qrencode pre-v3.4.0
if(cmd_options.foreground_color !== self.default_options.foreground_color
|| cmd_options.background_color !== self.default_options.background_color) {

// remove # symbol from color codes because qrencoder does not like it
cmd_options.foreground_color = cmd_options.foreground_color.replace('#', '');
cmd_options.background_color = cmd_options.background_color.replace('#', '');

qrencode_args.push(
'--foreground=' + cmd_options.foreground_color,
'--background=' + cmd_options.background_color
Expand All @@ -88,7 +89,7 @@ Encoder.prototype.encode = function(value, path, options)

// if case-sensitivity is disabled, add flag
if(!cmd_options.case_sensitive) qrencode_args.push('-i');

// if we have a path, write to the path
// otherwise, it will write to stdout
qrencode_args.push('-o');
Expand All @@ -97,30 +98,27 @@ Encoder.prototype.encode = function(value, path, options)
} else {
qrencode_args.push('-');
}

// add the value to be encoded
qrencode_args.push(value);

// create stdout data container
var stdout, stderr;

// spawn the child process
qrencode = child_process.spawn(
'qrencode',
'qrencode',
qrencode_args
);

// add event listener for stdout data and populate stdout var
// in the event no path was given
qrencode.stdout.on('data', function(data) {
stdout = data;
});

// add event listener for stderr
qrencode.stderr.on('data', function(data) {
stderr = data;
});

// add listener for process exit and save exit code
qrencode.on('exit', function(code) {
exitcode = code;
Expand All @@ -135,7 +133,7 @@ Encoder.prototype.encode = function(value, path, options)
self.emit('end', stdout);
}
});

} catch(err) {
this.emit('error', err);
}
Expand Down

0 comments on commit 24310da

Please sign in to comment.