Skip to content

Commit df105b5

Browse files
Merge branch 'feature/empty-array' into main
2 parents 6991922 + 0ea504c commit df105b5

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Js object utils
1+
# Js object utils [![NPM Module](https://img.shields.io/npm/v/@idapgroup/js-object-utils.svg)](https://www.npmjs.com/package/@idapgroup/js-object-utils)
22

33
Javascript functions for transform or mutate object
44

@@ -49,7 +49,7 @@ const options = {
4949
booleanMapper: (val: boolean) => (val ? '1' : '0'),
5050

5151
/**
52-
* allow empty string
52+
* allow empty string or array
5353
* defaults to false
5454
*/
5555
allowEmptyValues: false,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@idapgroup/js-object-utils",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "Javascript functions for transform or mutate object",
55
"main": "build/main/index.js",
66
"typings": "build/main/index.d.ts",

src/lib/create-form-data.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const fillFormData = (
1818
index,
1919
booleanMapper,
2020
allowNullableValues,
21-
allowEmptyValues,
21+
allowEmptyValues
2222
} = config;
2323

2424
if (value === undefined || value === null) {
@@ -30,13 +30,17 @@ const fillFormData = (
3030
} else if (value instanceof Date) {
3131
formData.append(keyPrefix, value.toISOString());
3232
} else if (Array.isArray(value)) {
33-
value.forEach((item, index) => {
34-
fillFormData(
35-
item,
36-
{ ...config, keyPrefix: `${keyPrefix}[${index}]` },
37-
formData
38-
);
39-
});
33+
if (value.length === 0 && allowEmptyValues) {
34+
formData.append(`${keyPrefix}[]`, '');
35+
} else {
36+
value.forEach((item, index) => {
37+
fillFormData(
38+
item,
39+
{ ...config, keyPrefix: `${keyPrefix}[${index}]` },
40+
formData
41+
);
42+
});
43+
}
4044
} else if (typeof value === 'object') {
4145
if (value instanceof File || value instanceof Blob) {
4246
formData.append(
@@ -77,7 +81,7 @@ export const createFormData = (
7781
index: null,
7882
booleanMapper: (val: boolean) => (val ? '1' : '0'),
7983
allowNullableValues: false,
80-
allowEmptyValues: false,
84+
allowEmptyValues: false
8185
},
8286
options || {}
8387
);

test/create form-data.spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,12 @@ describe('createFormData function', () => {
137137
expect(fd).to.have.been.equal(initial);
138138
});
139139
it('should be success with empty values', () => {
140-
const fd = createFormData({name: ''}, {allowEmptyValues: true});
140+
const fd = createFormData({name: '', products: []}, {allowEmptyValues: true});
141141
expect(fd.get(`name`)).to.have.been.equal('');
142-
const fd2 = createFormData({name: ''}, {allowEmptyValues: false});
142+
expect(fd.get(`products[]`)).to.have.been.equal('');
143+
const fd2 = createFormData({name: '', products: []}, {allowEmptyValues: false});
143144
expect(fd2.has(`name`)).to.have.been.false;
145+
expect(fd2.has(`products[]`)).to.have.been.false;
144146
const fd3 = createFormData({name: '', email: null}, {allowEmptyValues: true, allowNullableValues: true});
145147
expect(fd3.get(`name`)).to.have.been.equal('');
146148
expect(fd3.get(`email`)).to.have.been.equal('');

0 commit comments

Comments
 (0)