Skip to content

Commit

Permalink
fix: cast values to numbers before commiting them closes #2403
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Oct 8, 2019
1 parent dda9abb commit 3b022e1
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/utils/events.ts
@@ -1,4 +1,4 @@
import { isCallable, toArray } from './index';
import { isCallable, toArray, isNaN } from './index';

export const isEvent = (evt: any): evt is Event => {
if (!evt) {
Expand All @@ -18,15 +18,30 @@ export const isEvent = (evt: any): evt is Event => {
return false;
};

type BoundInputElement = HTMLInputElement & {
_vModifiers?: { number?: boolean; trim?: boolean };
};

export function normalizeEventValue(value: unknown): any {
if (!isEvent(value)) {
return value;
}

const input = value.target as HTMLInputElement;
const input = value.target as BoundInputElement;
if (input.type === 'file' && input.files) {
return toArray(input.files);
}

// If the input has a `v-model.number` modifier applied.
if (input._vModifiers && input._vModifiers.number) {
// as per the spec the v-model.number uses parseFloat
const valueAsNumber = parseFloat(input.value);
if (isNaN(valueAsNumber)) {
return input.value;
}

return valueAsNumber;
}

return input.value;
}

0 comments on commit 3b022e1

Please sign in to comment.