Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Excluding Keys in Nested Objects #127

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/object_hash.js

Large diffs are not rendered by default.

5,783 changes: 2,877 additions & 2,906 deletions dist/object_hash_test.js

Large diffs are not rendered by default.

17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ function typeHasher(options, writeTo, context){
}
};

var keysPath = [];

return {
dispatch: function(value){
if (options.replacer) {
Expand All @@ -193,6 +195,7 @@ function typeHasher(options, writeTo, context){
var pattern = (/\[object (.*)\]/i);
var objString = Object.prototype.toString.call(object);
var objType = pattern.exec(objString);
var baseKeysPath = keysPath.slice(); // clone
if (!objType) { // object type did not match [object ...]
objType = 'unknown:[' + objString + ']';
} else {
Expand Down Expand Up @@ -239,12 +242,14 @@ function typeHasher(options, writeTo, context){
}

if (options.excludeKeys) {
keys = keys.filter(function(key) { return !options.excludeKeys(key); });
keys = keys.filter(function(key) { return !options.excludeKeys(key, baseKeysPath.concat([key])); });
}

write('object:' + keys.length + ':');
var self = this;
return keys.forEach(function(key){
keysPath = baseKeysPath.concat([key]);

self.dispatch(key);
write(':');
if(!options.excludeValues) {
Expand All @@ -257,11 +262,17 @@ function typeHasher(options, writeTo, context){
_array: function(arr, unordered){
unordered = typeof unordered !== 'undefined' ? unordered :
options.unorderedArrays !== false; // default to options.unorderedArrays

var baseKeysPath = keysPath.slice(); // clone

if (options.excludeKeys) {
arr = arr.filter(function (entry, index) { return !options.excludeKeys(null, baseKeysPath.concat([index])); });
}

var self = this;
write('array:' + arr.length + ':');
if (!unordered || arr.length <= 1) {
return arr.forEach(function(entry) {
return arr.forEach(function(entry, index) {
keysPath = baseKeysPath.concat([index]);
return self.dispatch(entry);
});
}
Expand Down
30 changes: 30 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,36 @@ describe('hash', function() {
assert.equal(ha, hb, 'Hashing should ignore key `b`');
});

it('excludeKeys path works', function() {
var ha, hb, hc;
ha = hash({a: 1, b: 4, c:{a:5, b:6}}, { excludeKeys: function(key, keyPath) { return keyPath.join(".") == "c.b" } });
hb = hash({a: 1, b: 4, c:{a:5}});
hc = hash({a: 1, b: 4, c:{}});

assert.equal(ha, hb, 'Hashing should ignore key `c.b`');
assert.notEqual(hb, hc, 'Hash get sub difference');
});

it('excludeKeys path collection works', function() {
var ha, hb, hc;
ha = hash({a: 1, b: 4, c:[{a:2, b:3},{a:5, b:6}]}, { excludeKeys: function(key, keyPath) { return keyPath.join(".") == "c.1.b" } });
hb = hash({a: 1, b: 4, c:[{a:2, b:3},{a:5}]});
hc = hash({a: 1, b: 4, c:[{}]});

assert.equal(ha, hb, 'Hashing should ignore key `c.1.b`');
assert.notEqual(hb, hc, 'Hash get sub difference');
});

it('excludeKeys path array works', function() {
var ha, hb, hc;
ha = hash({a: 1, b: 4, c:[{a:2, b:3},{a:5, b:6}]}, { excludeKeys: function(key, keyPath) { return keyPath.join(".") == "c.1" } });
hb = hash({a: 1, b: 4, c:[{a:2, b:3}]});
hc = hash({a: 1, b: 4, c:[{}]});

assert.equal(ha, hb, 'Hashing should ignore key `c.1.b`');
assert.notEqual(hb, hc, 'Hash get sub difference');
});

if (typeof Set !== 'undefined') {
it('unorderedSets = false', function() {
var opt = { unorderedSets: false };
Expand Down