@trayio/connector-utils
Advanced tools
Comparing version 0.3.6 to 0.4.0
@@ -1,19 +0,7 @@ | ||
const _ = require('lodash'); | ||
const { isEmpty, isObjectLike, isPlainObject } = require('lodash'); | ||
const validateValueIsNotObjectLike = value => { | ||
if ( | ||
typeof value === 'number' || | ||
typeof value === 'boolean' || | ||
(typeof value === 'string' && value !== '') | ||
) { | ||
return value; | ||
} | ||
return undefined; | ||
}; | ||
/** | ||
* Recursively removes empty objects, arrays and strings from a collection. | ||
* It's important to note that this method will remove objects if they become empty | ||
* as a result of the nested key/value containing an empty object (the same goes | ||
* for arrays). | ||
* It's important to note that this method will remove objects & arrays if they | ||
* become empty as a result of the nested key/value containing an empty object. | ||
* | ||
@@ -23,30 +11,30 @@ * @param {Object} collection The collection from which to remove empty objects. | ||
const removeEmptyObjects = collection => { | ||
const removeEmptyObjects = (collection = {}) => { | ||
const isEmptyAndNotBoolOrNum = (val) => { | ||
// 1st 'isEmpty' saves some recursive calls, 2nd ensures parent objects ignored if containing only empty values. | ||
return ( | ||
!(typeof val === 'boolean' || Number.isFinite(val)) && | ||
(isEmpty(val) || isEmpty(removeEmptyObjects(val))) | ||
); | ||
}; | ||
if (Array.isArray(collection)) { | ||
// cleaning remaining empty values | ||
return _.compact(collection.map(removeEmptyObjects)); | ||
return collection | ||
.filter((item) => !isEmptyAndNotBoolOrNum(item)) | ||
.map(removeEmptyObjects); | ||
} | ||
if (_.isPlainObject(collection)) { | ||
const clean_object = _.reduce( | ||
collection, | ||
(acc, value, key) => { | ||
if (_.isObjectLike(value)) { | ||
// checking if nested elements are empty | ||
if (_.isEmpty(removeEmptyObjects(value))) { | ||
return acc; | ||
} | ||
acc[key] = removeEmptyObjects(value); | ||
} | ||
// checking for regular, non-objectLike values | ||
if (validateValueIsNotObjectLike(value)) { | ||
acc[key] = value; | ||
} | ||
return acc; | ||
}, | ||
{}, | ||
); | ||
// setting null values to remove via _.compact if empty. | ||
// this will remove empty objects if the child element was populated on first iteration and is now empty. | ||
return _.isEmpty(clean_object) ? null : clean_object; | ||
if (isPlainObject(collection)) { | ||
return Object.entries(collection).reduce((acc, [key, value]) => { | ||
return isEmptyAndNotBoolOrNum(value) | ||
? acc | ||
: { | ||
...acc, | ||
[key]: isObjectLike(value) | ||
? removeEmptyObjects(value) | ||
: value, | ||
}; | ||
}, {}); | ||
} | ||
return collection; | ||
@@ -53,0 +41,0 @@ }; |
{ | ||
"name": "@trayio/connector-utils", | ||
"version": "0.3.6", | ||
"version": "0.4.0", | ||
"description": "Common utility functions used in connectors.", | ||
@@ -42,3 +42,3 @@ "main": "lib/index.js", | ||
"jest-json-schema": "^2.1.0", | ||
"jsdoc-to-markdown": "^6.0.1", | ||
"jsdoc-to-markdown": "^7.0.1", | ||
"prettier": "^2.1.2" | ||
@@ -45,0 +45,0 @@ }, |
56994
854