Skip to content

Commit

Permalink
Merge fb6ca99 into f214c5f
Browse files Browse the repository at this point in the history
  • Loading branch information
trescube committed Dec 11, 2018
2 parents f214c5f + fb6ca99 commit 33c21cc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
31 changes: 17 additions & 14 deletions index.js
Expand Up @@ -58,22 +58,25 @@ var hashes = crypto.getHashes ? crypto.getHashes().slice() : ['sha1', 'md5'];
hashes.push('passthrough');
var encodings = ['buffer', 'hex', 'binary', 'base64'];

function applyDefaults(object, options){
options = options || {};
options.algorithm = options.algorithm || 'sha1';
options.encoding = options.encoding || 'hex';
options.excludeValues = options.excludeValues ? true : false;
function applyDefaults(object, sourceOptions){
var sourceOptions = sourceOptions || {};

// create a copy rather than mutating
var options = {};
options.algorithm = sourceOptions.algorithm || 'sha1';
options.encoding = sourceOptions.encoding || 'hex';
options.excludeValues = sourceOptions.excludeValues ? true : false;
options.algorithm = options.algorithm.toLowerCase();
options.encoding = options.encoding.toLowerCase();
options.ignoreUnknown = options.ignoreUnknown !== true ? false : true; // default to false
options.respectType = options.respectType === false ? false : true; // default to true
options.respectFunctionNames = options.respectFunctionNames === false ? false : true;
options.respectFunctionProperties = options.respectFunctionProperties === false ? false : true;
options.unorderedArrays = options.unorderedArrays !== true ? false : true; // default to false
options.unorderedSets = options.unorderedSets === false ? false : true; // default to false
options.unorderedObjects = options.unorderedObjects === false ? false : true; // default to true
options.replacer = options.replacer || undefined;
options.excludeKeys = options.excludeKeys || undefined;
options.ignoreUnknown = sourceOptions.ignoreUnknown !== true ? false : true; // default to false
options.respectType = sourceOptions.respectType === false ? false : true; // default to true
options.respectFunctionNames = sourceOptions.respectFunctionNames === false ? false : true;
options.respectFunctionProperties = sourceOptions.respectFunctionProperties === false ? false : true;
options.unorderedArrays = sourceOptions.unorderedArrays !== true ? false : true; // default to false
options.unorderedSets = sourceOptions.unorderedSets === false ? false : true; // default to false
options.unorderedObjects = sourceOptions.unorderedObjects === false ? false : true; // default to true
options.replacer = sourceOptions.replacer || undefined;
options.excludeKeys = sourceOptions.excludeKeys || undefined;

if(typeof object === 'undefined') {
throw new Error('Object argument required.');
Expand Down
14 changes: 14 additions & 0 deletions test/index.js
Expand Up @@ -22,6 +22,20 @@ describe('hash', function() {
}, 'bad encoding');
});

it('copies options rather than mutating', function() {
var options = {
algorithm: 'MD5',
encoding: 'HEX'
}

hash({foo: 'bar'}, options)

assert.deepEqual(options, {
algorithm: 'MD5',
encoding: 'HEX'
}, 'source options have neither been modified nor added to')
});

it('hashes a simple object', function() {
assert.ok(validSha1.test(hash({foo: 'bar', bar: 'baz'})), 'hash object');
});
Expand Down

0 comments on commit 33c21cc

Please sign in to comment.