Skip to content

Commit

Permalink
fix(filter): fix conditional to ensure we catch properties with a fal…
Browse files Browse the repository at this point in the history
…sy value (#423)

fixes #406
  • Loading branch information
chazzmoney authored and dbanksdesign committed Sep 28, 2020
1 parent 5a17070 commit a3a69c2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
50 changes: 50 additions & 0 deletions __tests__/filterProperties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ var sizeLarge = {
]
}

var not_kept = {
"value": 0,
"original": {
"value": 0,
},
"name": "falsy_values-not_kept",
"attributes": { category: "falsy_values" },
"path": [
"falsy_values",
"not_kept"
]
}

var kept = {
"value": 0,
"original": {
"value": 0,
},
"name": "falsy_values-kept",
"attributes": { category: "falsy_values" },
"path": [
"falsy_values",
"kept"
]
}

var properties = {
"color": {
"red": colorRed,
Expand All @@ -79,11 +105,21 @@ var properties = {
}
};

var falsy_values = {
"kept": kept,
"not_kept": not_kept,
};

var dictionary = {
"properties": properties,
"allProperties": flattenProperties(properties)
}

var falsy_dictionary = {
"properties": falsy_values,
"allProperties": flattenProperties(falsy_values)
}

describe('filterProperties', () => {

beforeEach(() => {
Expand Down Expand Up @@ -112,6 +148,20 @@ describe('filterProperties', () => {
expect(filteredDictionary.properties).not.toHaveProperty('color');
});

it('should work with falsy values and a filter function', () => {
var filter = function(property) {
return property.path.includes("kept");
}

var filteredDictionary = filterProperties(falsy_dictionary, filter);
_.each(filteredDictionary.allProperties, function(property) {
expect(property).not.toBe(not_kept);
});
expect(filteredDictionary.allProperties).toEqual([kept]);
expect(filteredDictionary.properties).toHaveProperty('kept');
expect(filteredDictionary.properties).not.toHaveProperty('not_kept');
});

describe('should throw if', () => {
it('filter is a string', () => {
expect(
Expand Down
2 changes: 1 addition & 1 deletion lib/filterProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function filterPropertyObject(properties, filter) {
// If the value has a `value` member we know it's a property, pass it to
// the filter function and either include it in the final `result` object or
// exclude it (by returning the `result` object without it added).
} else if (value.value) {
} else if (typeof value.value !== 'undefined') {
return filter(value) ? _.assign(result, { [key]: value }) : result
// If we got here we have an object that is not a property. We'll assume
// it's an object containing multiple properties and recursively filter it
Expand Down

0 comments on commit a3a69c2

Please sign in to comment.