-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config option to disable closed world assumption (#158)
- Loading branch information
1 parent
8b75fda
commit 123dc9d
Showing
3 changed files
with
22 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,18 +57,28 @@ const wrappers = { | |
|
||
} | ||
|
||
// Recursively remove all fields starting with _ from response | ||
// Gets called in `returnJSON` and `handleDownload`. Shouldn't be used anywhere else. | ||
const cleanJSON = (json) => { | ||
/** | ||
* Recursively remove certain fields from response | ||
* | ||
* Gets called in `returnJSON` and `handleDownload`. Shouldn't be used anywhere else. | ||
* | ||
* @param {(Object|Object[])} json JSON object or array of objects | ||
* @param {number} [depth=0] Should not be set when called from outside | ||
*/ | ||
const cleanJSON = (json, depth = 0) => { | ||
if (_.isArray(json)) { | ||
json.forEach(cleanJSON) | ||
json.forEach(value => cleanJSON(value, depth)) | ||
} else if (_.isObject(json)) { | ||
_.forOwn(json, (value, key) => { | ||
if (key.startsWith("_")) { | ||
// remove from object | ||
if ( | ||
// Remove top level empty arrays/objects if closedWorldAssumption is set to false | ||
(depth === 0 && !config.closedWorldAssumption && (_.isEqual(value, {}) || _.isEqual(value, [])) ) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
stefandesu
Author
Member
|
||
// Remove all fields started with _ | ||
|| key.startsWith("_") | ||
) { | ||
_.unset(json, key) | ||
} else { | ||
cleanJSON(value) | ||
cleanJSON(value, depth + 1) | ||
} | ||
}) | ||
} | ||
|
The removal should not be limited to the top level but applied recursively.