json-form-data
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -39,3 +39,7 @@ const includePolyfills = function(files) { | ||
var build = Math.random().toString(36).substring(2, 15); | ||
config.set({ | ||
browserNoActivityTimeout: 120000, | ||
browserDisconnectTimeout: 120000, | ||
frameworks: ['mocha', 'chai', 'include-polyfills'], | ||
@@ -66,3 +70,4 @@ files: ['tests/**/*.js'], | ||
browserStack: { | ||
project: 'json-form-data' | ||
project: 'json-form-data', | ||
build: build | ||
}, | ||
@@ -69,0 +74,0 @@ customLaunchers: { |
{ | ||
"name": "json-form-data", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "A library to convert javascript objects into form data.", | ||
@@ -28,6 +28,6 @@ "main": "./src/jsonToFormData.js", | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"formdata-polyfill": "^3.0.9", | ||
"karma": "^2.0.0", | ||
"karma-browserstack-launcher": "^1.3.0", | ||
"chai": "^4.2.0", | ||
"formdata-polyfill": "^3.0.13", | ||
"karma": "^3.1.4", | ||
"karma-browserstack-launcher": "^1.4.0", | ||
"karma-chai": "^0.1.0", | ||
@@ -37,5 +37,5 @@ "karma-chrome-launcher": "^2.2.0", | ||
"karma-mocha-reporter": "^2.2.5", | ||
"mocha": "^5.0.4", | ||
"mocha": "^5.2.0", | ||
"weakmap-polyfill": "^2.0.0" | ||
} | ||
} |
@@ -114,2 +114,7 @@ # json-form-data | ||
## Contributors | ||
- <a href="https://github.com/hyperatom">hyperatom</a> | ||
- <a href="https://github.com/illiatdesdindes">illiatdesdindes</a> | ||
## Sponsors | ||
@@ -116,0 +121,0 @@ |
@@ -20,2 +20,17 @@ (function (root, factory) { | ||
function mergeObjects(object1, object2) { | ||
var objectsToMerge = [object1, object2]; | ||
return objectsToMerge.reduce(function (carry, objectToMerge) { | ||
Object.keys(objectToMerge).forEach(function (objectKey) { | ||
carry[objectKey] = objectToMerge[objectKey]; | ||
}); | ||
return carry; | ||
}, {}); | ||
} | ||
function isArray(val) { | ||
@@ -33,4 +48,15 @@ | ||
function convert(jsonObject, parentKey, carryFormData) { | ||
function convert(jsonObject, options) { | ||
var defaultOptions = { | ||
showLeafArrayIndexes: true | ||
}; | ||
var mergedOptions = mergeObjects(defaultOptions, options || {}); | ||
return convertRecursively(jsonObject, mergedOptions); | ||
} | ||
function convertRecursively(jsonObject, options, parentKey, carryFormData) { | ||
var formData = carryFormData || new FormData(); | ||
@@ -55,3 +81,7 @@ | ||
propName = parentKey + '[' + index + ']'; | ||
if (isArray(jsonObject[key]) || isObject(jsonObject[key]) || options.showLeafArrayIndexes ) { | ||
propName = parentKey + '[' + index + ']'; | ||
} else { | ||
propName = parentKey + '[]'; | ||
} | ||
} | ||
@@ -72,3 +102,3 @@ | ||
convert(jsonObject[key], propName, formData); | ||
convertRecursively(jsonObject[key], options, propName, formData); | ||
@@ -75,0 +105,0 @@ } else if (typeof jsonObject[key] === 'boolean') { |
@@ -200,2 +200,28 @@ describe('jsonToFormData', function() { | ||
}); | ||
it('should use the showLeafArrayIndexes option to hide leaf array indexes', function() { | ||
var testObject = { | ||
prop1: [ | ||
[11, 'test', true, false] | ||
], | ||
prop2: { | ||
prop3: [ | ||
[11, 'test', true, false] | ||
] | ||
} | ||
}; | ||
var formDataResult = window.jsonToFormData(testObject, { showLeafArrayIndexes: false }); | ||
expect(formDataResult.getAll('prop1[0][]')[0]).to.equal('11'); | ||
expect(formDataResult.getAll('prop1[0][]')[1]).to.equal('test'); | ||
expect(formDataResult.getAll('prop1[0][]')[2]).to.equal('1'); | ||
expect(formDataResult.getAll('prop1[0][]')[3]).to.equal('0'); | ||
expect(formDataResult.getAll('prop2[prop3][0][]')[0]).to.equal('11'); | ||
expect(formDataResult.getAll('prop2[prop3][0][]')[1]).to.equal('test'); | ||
expect(formDataResult.getAll('prop2[prop3][0][]')[2]).to.equal('1'); | ||
expect(formDataResult.getAll('prop2[prop3][0][]')[3]).to.equal('0'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19726
375
125