MCPcopy
hub / github.com/autoNumeric/autoNumeric / _serialize

Method _serialize

src/AutoNumeric.js:9437–9519  ·  view source on GitHub ↗

* Serialize the form child element values to a string, or an Array. * The output format is defined with the `formatType` argument. * This is loosely based upon http://stackoverflow.com/a/40705993/2834898. * * @param {HTMLFormElement} form * @param {boolean} [intoAnAr

(form, intoAnArray = false, formatType = 'unformatted', serializedSpaceCharacter = '+', forcedOutputFormat = null)

Source from the content-addressed store, hash-verified

9435 * @private
9436 */
9437 static _serialize(form, intoAnArray = false, formatType = 'unformatted', serializedSpaceCharacter = '+', forcedOutputFormat = null) {
9438 const result = [];
9439
9440 if (typeof form === 'object' && form.nodeName.toLowerCase() === 'form') {
9441 Array.prototype.slice.call(form.elements).forEach(element => {
9442 if (element.name &&
9443 !element.disabled &&
9444 ['file', 'reset', 'submit', 'button'].indexOf(element.type) === -1) {
9445 if (element.type === 'select-multiple') {
9446 Array.prototype.slice.call(element.options).forEach(option => {
9447 if (option.selected) {
9448 //TODO Should we unformat/format/localize the selection option (which be default should be read-only)?
9449 if (intoAnArray) {
9450 result.push({ name: element.name, value: option.value });
9451 } else { // into a string
9452 result.push(`${encodeURIComponent(element.name)}=${encodeURIComponent(option.value)}`);
9453 }
9454 }
9455 });
9456 } else if (['checkbox', 'radio'].indexOf(element.type) === -1 || element.checked) {
9457 let valueResult;
9458 if (this.isManagedByAutoNumeric(element)) {
9459 let anObject;
9460 switch (formatType) {
9461 case 'unformatted':
9462 anObject = this.getAutoNumericElement(element);
9463 if (!AutoNumericHelper.isNull(anObject)) {
9464 valueResult = this.unformat(element, anObject.getSettings());
9465 }
9466
9467 break;
9468 case 'localized':
9469 anObject = this.getAutoNumericElement(element);
9470 if (!AutoNumericHelper.isNull(anObject)) {
9471 // Here I need to clone the setting object, otherwise I would modify it when changing the `outputFormat` option value
9472 const currentSettings = AutoNumericHelper.cloneObject(anObject.getSettings());
9473 if (!AutoNumericHelper.isNull(forcedOutputFormat)) {
9474 currentSettings.outputFormat = forcedOutputFormat;
9475 }
9476
9477 valueResult = this.localize(element, currentSettings);
9478 }
9479
9480 break;
9481 case 'formatted':
9482 default:
9483 valueResult = element.value;
9484 }
9485 } else {
9486 valueResult = element.value;
9487 }
9488
9489 if (AutoNumericHelper.isUndefined(valueResult)) {
9490 AutoNumericHelper.throwError('This error should never be hit. If it has, something really wrong happened!');
9491 }
9492
9493 if (intoAnArray) {
9494 result.push({ name: element.name, value: valueResult });

Callers 6

_serializeFormattedMethod · 0.95
_serializeLocalizedMethod · 0.95

Calls 9

getAutoNumericElementMethod · 0.95
unformatMethod · 0.95
localizeMethod · 0.95
isNullMethod · 0.80
getSettingsMethod · 0.80
cloneObjectMethod · 0.80
isUndefinedMethod · 0.80
throwErrorMethod · 0.80

Tested by

no test coverage detected