Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Move remove-undefined-objects to a new function
Browse files Browse the repository at this point in the history
We can add more edge case tests easier this way
  • Loading branch information
domharrington committed Dec 18, 2018
1 parent 73932d8 commit 61c0af0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
37 changes: 1 addition & 36 deletions packages/api-explorer/src/lib/oas-to-har.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const querystring = require('querystring');
const extensions = require('@readme/oas-extensions');
const getSchema = require('./get-schema');
const configureSecurity = require('./configure-security');
const removeUndefinedObjects = require('./remove-undefined-objects');

// const format = {
// value: v => `__START_VALUE__${v}__END__`,
Expand Down Expand Up @@ -74,42 +75,6 @@ function isPrimitive(val) {
return typeof val === 'string' || typeof val === 'number' || typeof val === 'boolean';
}

function isEmptyObject(obj) {
// Then remove all empty objects from the top level object
return typeof obj === 'object' && Object.keys(obj).length === 0;
}

// Modified from here: https://stackoverflow.com/a/43781499
function stripEmptyObjects(obj) {
Object.keys(obj).forEach(key => {
const value = obj[key];
if (typeof value === 'object') {
// Recurse, strip out empty objects from children
stripEmptyObjects(value);
// Then remove all empty objects from the top level object
if (isEmptyObject(value)) {
delete obj[key];
}
}
});
}

function removeUndefinedObjects(obj) {
// JSON.stringify removes undefined values
const withoutUndefined = JSON.parse(JSON.stringify(obj));

// Then we recursively remove all empty objects
stripEmptyObjects(withoutUndefined);

// If the only thing that's leftover is an empty object
// then return nothing so we don't end up with default
// code samples with:
// --data '{}'
if (isEmptyObject(withoutUndefined)) return undefined;

return withoutUndefined;
}

module.exports = (
oas,
pathOperation = { path: '', method: '' },
Expand Down
37 changes: 37 additions & 0 deletions packages/api-explorer/src/lib/remove-undefined-objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function isEmptyObject(obj) {
// Then remove all empty objects from the top level object
return typeof obj === 'object' && Object.keys(obj).length === 0;
}

// Modified from here: https://stackoverflow.com/a/43781499
function stripEmptyObjects(obj) {
Object.keys(obj).forEach(key => {
const value = obj[key];
if (typeof value === 'object') {
// Recurse, strip out empty objects from children
stripEmptyObjects(value);
// Then remove all empty objects from the top level object
if (isEmptyObject(value)) {
delete obj[key];
}
}
});
}

function removeUndefinedObjects(obj) {
// JSON.stringify removes undefined values
const withoutUndefined = JSON.parse(JSON.stringify(obj));

// Then we recursively remove all empty objects
stripEmptyObjects(withoutUndefined);

// If the only thing that's leftover is an empty object
// then return nothing so we don't end up with default
// code samples with:
// --data '{}'
if (isEmptyObject(withoutUndefined)) return undefined;

return withoutUndefined;
}

module.exports = removeUndefinedObjects;

0 comments on commit 61c0af0

Please sign in to comment.