aboutsummaryrefslogtreecommitdiff
path: root/ext/mixed/js/dom-data-binder.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-10-08 17:38:18 -0400
committerGitHub <noreply@github.com>2020-10-08 17:38:18 -0400
commit41ead7a90ac6d2c2674b647dde3928dbf156bc9c (patch)
tree9c4f0782502c74eebce1217666f50df58432b230 /ext/mixed/js/dom-data-binder.js
parent05e51a950e3fb7da3f9a252a742c068a9daad6b2 (diff)
Add additional transforms (#899)
Diffstat (limited to 'ext/mixed/js/dom-data-binder.js')
-rw-r--r--ext/mixed/js/dom-data-binder.js29
1 files changed, 16 insertions, 13 deletions
diff --git a/ext/mixed/js/dom-data-binder.js b/ext/mixed/js/dom-data-binder.js
index 93fe956e..b33def8f 100644
--- a/ext/mixed/js/dom-data-binder.js
+++ b/ext/mixed/js/dom-data-binder.js
@@ -317,7 +317,7 @@ class DOMDataBinder {
case 'text':
return `${element.value}`;
case 'number':
- return this._getInputNumberValue(element);
+ return DOMDataBinder.convertToNumber(element.value, element);
}
break;
case 'TEXTAREA':
@@ -327,26 +327,29 @@ class DOMDataBinder {
return null;
}
- _getInputNumberValue(element) {
- let value = parseFloat(element.value);
+ // Utilities
+
+ static convertToNumber(value, constraints) {
+ value = parseFloat(value);
if (!Number.isFinite(value)) { return 0; }
- let {min, max, step} = element;
- min = this._stringValueToNumberOrNull(min);
- max = this._stringValueToNumberOrNull(max);
- step = this._stringValueToNumberOrNull(step);
+ let {min, max, step} = constraints;
+ min = DOMDataBinder.convertToNumberOrNull(min);
+ max = DOMDataBinder.convertToNumberOrNull(max);
+ step = DOMDataBinder.convertToNumberOrNull(step);
if (typeof min === 'number') { value = Math.max(value, min); }
if (typeof max === 'number') { value = Math.min(value, max); }
if (typeof step === 'number' && step !== 0) { value = Math.round(value / step) * step; }
return value;
}
- _stringValueToNumberOrNull(value) {
- if (typeof value !== 'string' || value.length === 0) {
- return null;
+ static convertToNumberOrNull(value) {
+ if (typeof value !== 'number') {
+ if (typeof value !== 'string' || value.length === 0) {
+ return null;
+ }
+ value = parseFloat(value);
}
-
- const number = parseFloat(value);
- return !Number.isNaN(number) ? number : null;
+ return !Number.isNaN(value) ? value : null;
}
}