diff --git a/CHANGELOG.md b/CHANGELOG.md index 5617313..5531937 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/dist/form-data-json.js b/dist/form-data-json.js index 38c3ded..ab282df 100644 --- a/dist/form-data-json.js +++ b/dist/form-data-json.js @@ -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 @@ -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++; diff --git a/dist/form-data-json.min.js b/dist/form-data-json.min.js index 40bdd69..28ece5c 100644 --- a/dist/form-data-json.min.js +++ b/dist/form-data-json.min.js @@ -1,3 +1,3 @@ 'use strict'; -// form-data-json-convert | version: 2.0.3beta | url: https://github.com/brainfoolong/form-data-json -var FormDataJson=function(){function a(){}return a.toJson=function toJson(b,c){function d(b){if("function"==typeof c.inputFilter&&!0!==c.inputFilter(b))return!1;if(!c.includeDisabled&&b.disabled)return!1;var d=(b.type||"text").toLowerCase();return!(!c.includeButtonValues&&(b instanceof HTMLButtonElement||-1Test FormDataJson +

Testing read of pretty weird array input names. This tool is designed to create auto numbered indexes if no index @@ -587,6 +604,36 @@

})() + +

+

Testing form reset2

+ + + +
+
+ + +

Testing issue https://github.com/brainfoolong/form-data-json/issues/9

@@ -678,40 +725,71 @@

})() - + + + + + + + + + + + + +
+ + + + \ No newline at end of file diff --git a/package.json b/package.json index f1c4ff3..59d0a02 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "type": "git", "url": "git+https://github.com/brainfoolong/form-data-json.git" }, - "version": "2.0.3beta", + "version": "2.0.4beta", "scripts": { "dist": "node build/dist.js" }, diff --git a/src/form-data-json.js b/src/form-data-json.js index 36c3a00..c031e4f 100644 --- a/src/form-data-json.js +++ b/src/form-data-json.js @@ -268,29 +268,34 @@ class FormDataJson { */ 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) { const isArray = FormDataJson.isArray(object) let newObject = isArray ? [] : {} let count = 0 for (let key in object) { - if (FormDataJson.isObject(object[key]) || FormDataJson.isArray(object[key])) { - object[key] = removeEmpty(object[key]) || '' + let value = object[key] + if (options.flatList && !depth) { + value = value[1] } - if (typeof object[key] !== 'object' && FormDataJson.stringify(object[key]) === '') { + if (FormDataJson.isObject(value) || FormDataJson.isArray(value)) { + value = removeEmpty(value, (depth || 0) + 1) || '' + } + 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++ }