Skip to content
Snippets Groups Projects
Commit f29aa5f5 authored by Alvaro Cabrera's avatar Alvaro Cabrera
Browse files

Fix: restore clean() for pruning empty objects

parent a091de25
No related branches found
No related tags found
No related merge requests found
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
// TODO: tsify
function isArray(obj) {
return obj && Array.isArray(obj);
}
function isObject(obj) {
return obj && obj !== null && typeof obj === 'object';
}
function hasNothing(obj) {
if (isArray(obj)) {
return obj.length === 0;
}
if (isObject(obj)) {
return Object.keys(obj).length === 0;
}
return typeof obj === 'undefined' || obj === null;
}
function removeProps(obj, key?, parent?) {
var i,
value,
isFullyEmpty = true;
if (isArray(obj)) {
for (i = 0; i < obj.length; ++i) {
value = obj[i];
if (isObject(value)) {
removeProps(value, i, obj);
}
if (hasNothing(value)) {
obj.splice(i--, 1);
} else {
isFullyEmpty = false;
}
}
} else {
for (i in obj) {
value = obj[i];
if (isObject(value)) {
removeProps(value, i, obj);
}
if (hasNothing(value)) {
delete obj[i];
} else {
isFullyEmpty = false;
}
}
}
if (typeof key !== 'undefined' && isFullyEmpty) {
delete parent[key];
removeProps(obj);
}
}
export = function(obj: any) {
removeProps(obj);
return obj;
};
import clean = require('./clean');
import random = require('./random');
import ParseError = require('./error');
import inferType = require('./infer');
......@@ -91,7 +92,7 @@ function traverse(schema: JsonSchema, path: SchemaPath, resolve: Function) {
}
}
return copy;
return clean(copy);
}
export = traverse;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment