Skip to content

Commit

Permalink
fixed #16 in toJson() in combination with skipEmpty and flatList
Browse files Browse the repository at this point in the history
  • Loading branch information
brainfoolong committed Sep 27, 2021
1 parent 0361d0a commit 8d2c3e5
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 60 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 2.0.4beta - 27.09.2021
* fixed https://github.com/brainfoolong/form-data-json/issues/16 in `toJson()` in combination with `skipEmpty` and `flatList`

### 2.0.3beta - 27.09.2021
* fixed https://github.com/brainfoolong/form-data-json/issues/13 in `toJson()`
* changed option `uncheckedValue` is now false by default, instead of undefined
Expand Down
23 changes: 15 additions & 8 deletions dist/form-data-json.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
// form-data-json-convert | version: 2.0.3beta | url: https://github.com/brainfoolong/form-data-json
// form-data-json-convert | version: 2.0.4beta | url: https://github.com/brainfoolong/form-data-json

/**
* Form Data Json Converter
Expand Down Expand Up @@ -204,33 +204,40 @@ var FormDataJson = /*#__PURE__*/function () {

function output() {
returnObject = arrayfy(returnObject);
if (options.skipEmpty) returnObject = removeEmpty(returnObject) || {};
if (options.skipEmpty) returnObject = removeEmpty(returnObject) || (options.flatList ? [] : {});
return returnObject;
}
/**
* Recursively remove empty keys
* @param {Object} object
* @param {number} depth
*/


function removeEmpty(object) {
function removeEmpty(object, depth) {
var isArray = FormDataJson.isArray(object);
var newObject = isArray ? [] : {};
var count = 0;

for (var key in object) {
if (FormDataJson.isObject(object[key]) || FormDataJson.isArray(object[key])) {
object[key] = removeEmpty(object[key]) || '';
var value = object[key];

if (options.flatList && !depth) {
value = value[1];
}

if (FormDataJson.isObject(value) || FormDataJson.isArray(value)) {
value = removeEmpty(value, (depth || 0) + 1) || '';
}

if (typeof object[key] !== 'object' && FormDataJson.stringify(object[key]) === '') {
if (typeof value !== 'object' && FormDataJson.stringify(value) === '') {
continue;
}

if (isArray) {
newObject.push(object[key]);
newObject.push(value);
} else {
newObject[key] = object[key];
newObject[key] = value;
}

count++;
Expand Down
4 changes: 2 additions & 2 deletions dist/form-data-json.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions docs/scripts/form-data-json.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
// form-data-json-convert | version: 2.0.3beta | url: https://github.com/brainfoolong/form-data-json
// form-data-json-convert | version: 2.0.4beta | url: https://github.com/brainfoolong/form-data-json

/**
* Form Data Json Converter
Expand Down Expand Up @@ -204,33 +204,40 @@ var FormDataJson = /*#__PURE__*/function () {

function output() {
returnObject = arrayfy(returnObject);
if (options.skipEmpty) returnObject = removeEmpty(returnObject) || {};
if (options.skipEmpty) returnObject = removeEmpty(returnObject) || (options.flatList ? [] : {});
return returnObject;
}
/**
* Recursively remove empty keys
* @param {Object} object
* @param {number} depth
*/


function removeEmpty(object) {
function removeEmpty(object, depth) {
var isArray = FormDataJson.isArray(object);
var newObject = isArray ? [] : {};
var count = 0;

for (var key in object) {
if (FormDataJson.isObject(object[key]) || FormDataJson.isArray(object[key])) {
object[key] = removeEmpty(object[key]) || '';
var value = object[key];

if (options.flatList && !depth) {
value = value[1];
}

if (FormDataJson.isObject(value) || FormDataJson.isArray(value)) {
value = removeEmpty(value, (depth || 0) + 1) || '';
}

if (typeof object[key] !== 'object' && FormDataJson.stringify(object[key]) === '') {
if (typeof value !== 'object' && FormDataJson.stringify(value) === '') {
continue;
}

if (isArray) {
newObject.push(object[key]);
newObject.push(value);
} else {
newObject[key] = object[key];
newObject[key] = value;
}

count++;
Expand Down
Loading

0 comments on commit 8d2c3e5

Please sign in to comment.