Skip to content
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ const options = {
* defaults to false
*/
dotsForObjectNotation: false,

/**
* display dates in local timezone, with the timezone offset displayed.
* otherwise, all dates will be converted to the UTC timezone
*/
dateWithTimezone: false,
};

const formData = serialize(
Expand Down
16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ function isFile(value, isReactNative) {
);
}

function toTzIsoString(date) {
const off = date.getTimezoneOffset();
const absoff = Math.abs(off);
return (new Date(date.getTime() - off*60*1000).toISOString().substr(0,23) +
(off > 0 ? '-' : '+') +
Math.floor(absoff / 60).toFixed(0).padStart(2,'0') + ':' +
(absoff % 60).toString().padStart(2,'0'))
}

function initCfg(value) {
return isUndefined(value) ? false : value;
}
Expand All @@ -53,6 +62,7 @@ function serialize(obj, cfg, fd, pre) {
cfg.allowEmptyArrays = initCfg(cfg.allowEmptyArrays);
cfg.noFilesWithArrayNotation = initCfg(cfg.noFilesWithArrayNotation);
cfg.dotsForObjectNotation = initCfg(cfg.dotsForObjectNotation);
cfg.dateWithTimezone = initCfg(cfg.dateWithTimezone);

const isReactNative = typeof fd.getParts === 'function';

Expand Down Expand Up @@ -83,7 +93,11 @@ function serialize(obj, cfg, fd, pre) {
fd.append(pre + '[]', '');
}
} else if (isDate(obj)) {
fd.append(pre, obj.toISOString());
if (cfg.dateWithTimezone) {
fd.append(pre, toTzIsoString(obj));
} else {
fd.append(pre, obj.toISOString());
}
} else if (isObject(obj) && !isBlob(obj, isReactNative)) {
Object.keys(obj).forEach((prop) => {
const value = obj[prop];
Expand Down
22 changes: 22 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,28 @@ test('Date', () => {
expect(formData.get('foo')).toBe(foo.toISOString());
});

test('Date with timezone', () => {
const toTzIsoString = (date) => {
const off = date.getTimezoneOffset();
const absoff = Math.abs(off);
return (new Date(date.getTime() - off*60*1000).toISOString().substr(0,23) +
(off > 0 ? '-' : '+') +
Math.floor(absoff / 60).toFixed(0).padStart(2,'0') + ':' +
(absoff % 60).toString().padStart(2,'0'))
}
const foo = new Date(2000, 0, 1, 1, 1, 1);
const formData = serialize({
foo,
},
{
dateWithTimezone: true,
});

expect(formData.append).toHaveBeenCalledTimes(1);
expect(formData.append).toHaveBeenCalledWith('foo', toTzIsoString(foo));
expect(formData.get('foo')).toBe(toTzIsoString(foo));
});

test('File', () => {
const foo = new File([], '');
const formData = serialize({
Expand Down
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type Options = {
allowEmptyArrays?: boolean;
noFilesWithArrayNotation?: boolean;
dotsForObjectNotation?: boolean;
dateWithTimezone?: boolean;
};

export function serialize<T = {}>(
Expand Down