Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
axios.formToJSON
method; (#4735)
* Draft * Added `formDataToJSON` helper; Added `axios.formToJSON` method; Added client tests; Co-authored-by: Jay <jasonsaayman@gmail.com>
- Loading branch information
1 parent
934f390
commit c008e57
Showing
8 changed files
with
195 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict'; | ||
|
||
var utils = require('../utils'); | ||
|
||
function parsePropPath(name) { | ||
// foo[x][y][z] | ||
// foo.x.y.z | ||
// foo-x-y-z | ||
// foo x y z | ||
return utils.matchAll(/\w+|\[(\w*)]/g, name).map(function(match) { | ||
return match[0] === '[]' ? '' : match[1] || match[0]; | ||
}); | ||
} | ||
|
||
function arrayToObject(arr) { | ||
var obj = {}; | ||
var keys = Object.keys(arr); | ||
var i; | ||
var len = keys.length; | ||
var key; | ||
for (i = 0; i < len; i++) { | ||
key = keys[i]; | ||
obj[key] = arr[key]; | ||
} | ||
return obj; | ||
} | ||
|
||
function formDataToJSON(formData) { | ||
function buildPath(path, value, target, index) { | ||
var name = path[index++]; | ||
var isNumericKey = Number.isFinite(+name); | ||
var isLast = index >= path.length; | ||
name = !name && utils.isArray(target) ? target.length : name; | ||
|
||
if (isLast) { | ||
if (utils.hasOwnProperty(target, name)) { | ||
target[name] = [target[name], value]; | ||
} else { | ||
target[name] = value; | ||
} | ||
|
||
return !isNumericKey; | ||
} | ||
|
||
if (!target[name] || !utils.isObject(target[name])) { | ||
target[name] = []; | ||
} | ||
|
||
var result = buildPath(path, value, target[name], index); | ||
|
||
if (result && utils.isArray(target[name])) { | ||
target[name] = arrayToObject(target[name]); | ||
} | ||
|
||
return !isNumericKey; | ||
} | ||
|
||
if (utils.isFormData(formData) && utils.isFunction(formData.entries)) { | ||
var obj = {}; | ||
|
||
utils.forEachEntry(formData, function(name, value) { | ||
buildPath(parsePropPath(name), value, obj, 0); | ||
}); | ||
|
||
return obj; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
module.exports = formDataToJSON; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var formDataToJSON = require('../../../lib/helpers/formDataToJSON'); | ||
|
||
describe('formDataToJSON', function () { | ||
it('should convert a FormData Object to JSON Object', function () { | ||
const formData = new FormData(); | ||
|
||
formData.append('foo[bar][baz]', '123'); | ||
|
||
expect(formDataToJSON(formData)).toEqual({ | ||
foo: { | ||
bar: { | ||
baz: '123' | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
it('should convert repeatable values as an array', function () { | ||
const formData = new FormData(); | ||
|
||
formData.append('foo', '1'); | ||
formData.append('foo', '2'); | ||
|
||
expect(formDataToJSON(formData)).toEqual({ | ||
foo: ['1', '2'] | ||
}); | ||
}); | ||
|
||
it('should convert props with empty brackets to arrays', function () { | ||
const formData = new FormData(); | ||
|
||
formData.append('foo[]', '1'); | ||
formData.append('foo[]', '2'); | ||
|
||
expect(formDataToJSON(formData)).toEqual({ | ||
foo: ['1', '2'] | ||
}); | ||
}); | ||
|
||
it('should supported indexed arrays', function () { | ||
const formData = new FormData(); | ||
|
||
formData.append('foo[0]', '1'); | ||
formData.append('foo[1]', '2'); | ||
|
||
expect(formDataToJSON(formData)).toEqual({ | ||
foo: ['1', '2'] | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters