-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
BREAKING CHANGE: apply minimize by default when updating document
- Loading branch information
Showing
4 changed files
with
70 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
const { isPOJO } = require('../utils'); | ||
|
||
module.exports = minimize; | ||
|
||
/** | ||
* Minimizes an object, removing undefined values and empty objects | ||
* | ||
* @param {Object} object to minimize | ||
* @return {Object|undefined} | ||
* @api private | ||
*/ | ||
|
||
function minimize(obj) { | ||
const keys = Object.keys(obj); | ||
let i = keys.length; | ||
let hasKeys; | ||
let key; | ||
let val; | ||
|
||
while (i--) { | ||
key = keys[i]; | ||
val = obj[key]; | ||
|
||
if (isPOJO(val)) { | ||
obj[key] = minimize(val); | ||
} | ||
|
||
if (undefined === obj[key]) { | ||
delete obj[key]; | ||
continue; | ||
} | ||
|
||
hasKeys = true; | ||
} | ||
|
||
return hasKeys | ||
? obj | ||
: undefined; | ||
} |
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