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

Commit 61c0af0

Browse files
committed
Move remove-undefined-objects to a new function
We can add more edge case tests easier this way
1 parent 73932d8 commit 61c0af0

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

packages/api-explorer/src/lib/oas-to-har.js

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const querystring = require('querystring');
33
const extensions = require('@readme/oas-extensions');
44
const getSchema = require('./get-schema');
55
const configureSecurity = require('./configure-security');
6+
const removeUndefinedObjects = require('./remove-undefined-objects');
67

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

77-
function isEmptyObject(obj) {
78-
// Then remove all empty objects from the top level object
79-
return typeof obj === 'object' && Object.keys(obj).length === 0;
80-
}
81-
82-
// Modified from here: https://stackoverflow.com/a/43781499
83-
function stripEmptyObjects(obj) {
84-
Object.keys(obj).forEach(key => {
85-
const value = obj[key];
86-
if (typeof value === 'object') {
87-
// Recurse, strip out empty objects from children
88-
stripEmptyObjects(value);
89-
// Then remove all empty objects from the top level object
90-
if (isEmptyObject(value)) {
91-
delete obj[key];
92-
}
93-
}
94-
});
95-
}
96-
97-
function removeUndefinedObjects(obj) {
98-
// JSON.stringify removes undefined values
99-
const withoutUndefined = JSON.parse(JSON.stringify(obj));
100-
101-
// Then we recursively remove all empty objects
102-
stripEmptyObjects(withoutUndefined);
103-
104-
// If the only thing that's leftover is an empty object
105-
// then return nothing so we don't end up with default
106-
// code samples with:
107-
// --data '{}'
108-
if (isEmptyObject(withoutUndefined)) return undefined;
109-
110-
return withoutUndefined;
111-
}
112-
11378
module.exports = (
11479
oas,
11580
pathOperation = { path: '', method: '' },
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function isEmptyObject(obj) {
2+
// Then remove all empty objects from the top level object
3+
return typeof obj === 'object' && Object.keys(obj).length === 0;
4+
}
5+
6+
// Modified from here: https://stackoverflow.com/a/43781499
7+
function stripEmptyObjects(obj) {
8+
Object.keys(obj).forEach(key => {
9+
const value = obj[key];
10+
if (typeof value === 'object') {
11+
// Recurse, strip out empty objects from children
12+
stripEmptyObjects(value);
13+
// Then remove all empty objects from the top level object
14+
if (isEmptyObject(value)) {
15+
delete obj[key];
16+
}
17+
}
18+
});
19+
}
20+
21+
function removeUndefinedObjects(obj) {
22+
// JSON.stringify removes undefined values
23+
const withoutUndefined = JSON.parse(JSON.stringify(obj));
24+
25+
// Then we recursively remove all empty objects
26+
stripEmptyObjects(withoutUndefined);
27+
28+
// If the only thing that's leftover is an empty object
29+
// then return nothing so we don't end up with default
30+
// code samples with:
31+
// --data '{}'
32+
if (isEmptyObject(withoutUndefined)) return undefined;
33+
34+
return withoutUndefined;
35+
}
36+
37+
module.exports = removeUndefinedObjects;

0 commit comments

Comments
 (0)