Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ignoreEmptyArrays option. #48

Merged
merged 1 commit into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions form-urlencoded.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare namespace formUrlEncoded {
sorted?: boolean;
skipIndex?: boolean;
ignorenull?: boolean;
ignoreEmptyArrays?: boolean;
skipBracket?: boolean,
useDot?: boolean
whitespace?: string
Expand Down
4 changes: 2 additions & 2 deletions form-urlencoded.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default (data, opts = {}) => {
const {
sorted, skipIndex, ignorenull, skipBracket, useDot, whitespace = '+'
sorted, skipIndex, ignorenull, ignoreEmptyArrays, skipBracket, useDot, whitespace = '+'

Check failure on line 3 in form-urlencoded.mjs

View workflow job for this annotation

GitHub Actions / build

This line has a length of 91. Maximum allowed is 80
} = opts;

const encode = value => String(value)
Expand All @@ -22,7 +22,7 @@
? filterjoin(arr.map((elem, index) => skipIndex
? nest(name + brackets, elem)
: nest(name + '[' + index + ']', elem)))
: encode(name + brackets);
: !ignoreEmptyArrays && encode(name + brackets);

const setnest = (name, set) => filterjoin(
Array.from(set).map(elem => nest(name, elem)));
Expand Down
16 changes: 16 additions & 0 deletions form-urlencoded.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ test('return encoded data, without null', t => t.is(
'propArr1%5B2%5D=1'
));

test('return encoded data, with empty arrays', t => t.is(
formurlencoded({
propArr1 : [ 1, 2, 3 ],
propArr2 : []
}),
'propArr1%5B0%5D=1&propArr1%5B1%5D=2&propArr1%5B2%5D=3&propArr2%5B%5D'
));

test('return encoded data, without empty arrays', t => t.is(
formurlencoded({
propArr1 : [ 1, 2, 3 ],
propArr2 : []
}, { ignoreEmptyArrays : true }),
'propArr1%5B0%5D=1&propArr1%5B1%5D=2&propArr1%5B2%5D=3'
));

test('return encoded set', t => t.is(
formurlencoded({
set : new Set([ 1, 'two' ])
Expand Down
Loading