2
0
mirror of https://github.com/frappe/books.git synced 2024-11-09 23:30:56 +00:00

fix: TypeError, .round is not a func

- type vue files for Currency, Float, Int, Data
- use Number for safe parse cause faster
This commit is contained in:
18alantom 2023-03-31 11:16:09 +05:30 committed by Alan
parent 7c99a76e20
commit 368714a84a
6 changed files with 72 additions and 39 deletions

View File

@ -1,9 +1,9 @@
import { Fyo } from 'fyo';
import { Doc } from 'fyo/model/doc';
import { DateTime } from 'luxon';
import { Money } from 'pesa';
import { Field, FieldType, FieldTypeEnum } from 'schemas/types';
import { getIsNullOrUndef, safeParseFloat, titleCase } from 'utils';
import { isPesa } from '.';
import {
DEFAULT_CURRENCY,
DEFAULT_DATE_FORMAT,
@ -138,8 +138,8 @@ function formatNumber(value: unknown, fyo: Fyo): string {
value = fyo.pesa(value.toFixed(20));
}
if ((value as Money).round) {
const floatValue = safeParseFloat((value as Money).round());
if (isPesa(value)) {
const floatValue = safeParseFloat(value.round());
return numberFormatter.format(floatValue);
}

View File

@ -9,7 +9,7 @@
class="text-end"
:class="[inputClasses, containerClasses]"
:type="inputType"
:value="value?.round()"
:value="round(value)"
:placeholder="inputPlaceholder"
:readonly="isReadOnly"
:tabindex="isReadOnly ? '-1' : '0'"
@ -29,13 +29,16 @@
</div>
</div>
</template>
<script>
<script lang="ts">
// @ts-nocheck
import { isPesa } from 'fyo/utils';
import { Money } from 'pesa';
import { fyo } from 'src/initFyo';
import { nextTick } from 'vue';
import { safeParseFloat } from 'utils/index';
import { defineComponent, nextTick } from 'vue';
import Float from './Float.vue';
export default {
export default defineComponent({
name: 'Currency',
extends: Float,
emits: ['input', 'focus'],
@ -51,17 +54,44 @@ export default {
this.showInput = true;
this.$emit('focus', e);
},
parse(value) {
return fyo.pesa(value);
round(v: unknown) {
if (!isPesa(v)) {
v = this.parse(v);
}
if (isPesa(v)) {
return v.round();
}
return fyo.pesa(0).round();
},
onBlur(e) {
let { value } = e.target;
if (value !== 0 && !value) {
value = fyo.pesa(0).round();
parse(value: unknown): Money {
if (isPesa(value)) {
return value;
}
if (typeof value === 'string') {
value = safeParseFloat(value);
}
if (typeof value === 'number') {
return fyo.pesa(value);
}
if (typeof value === 'bigint') {
return fyo.pesa(value);
}
return fyo.pesa(0);
},
onBlur(e: FocusEvent) {
const target = e.target;
if (!(target instanceof HTMLInputElement)) {
return;
}
this.showInput = false;
this.triggerChange(value);
this.triggerChange(target.value);
},
activateInput() {
if (this.isReadOnly) {
@ -79,5 +109,5 @@ export default {
return fyo.format(this.value ?? fyo.pesa(0), this.df, this.doc);
},
},
};
});
</script>

View File

@ -1,13 +1,14 @@
<script>
import Base from './Base';
<script lang="ts">
import Base from './Base.vue';
import { defineComponent } from 'vue';
export default {
export default defineComponent({
name: 'Data',
extends: Base,
computed: {
inputType() {
return 'text';
}
}
};
},
},
});
</script>

View File

@ -1,19 +1,20 @@
<script>
<script lang="ts">
import { safeParseFloat } from 'utils/index';
import { defineComponent } from 'vue';
import Int from './Int.vue';
export default {
export default defineComponent({
name: 'Float',
extends: Int,
computed: {
inputType() {
return 'number';
}
},
methods: {
parse(value) {
return safeParseFloat(value)
},
},
};
methods: {
parse(value: unknown): number {
return safeParseFloat(value);
},
},
});
</script>

View File

@ -1,7 +1,9 @@
<script>
import Data from "./Data.vue";
<script lang="ts">
import Data from './Data.vue';
import { defineComponent } from 'vue';
import { safeParseInt } from 'utils/index';
export default {
export default defineComponent({
name: 'Int',
extends: Data,
computed: {
@ -10,10 +12,9 @@ export default {
},
},
methods: {
parse(value) {
const parsedValue = parseInt(value, 10);
return isNaN(parsedValue) ? 0 : parsedValue;
parse(value: unknown): number {
return safeParseInt(value);
},
},
};
});
</script>

View File

@ -180,11 +180,11 @@ function safeParseNumber(value: unknown, parser: (v: string) => number) {
}
export function safeParseFloat(value: unknown): number {
return safeParseNumber(value, parseFloat);
return safeParseNumber(value, Number);
}
export function safeParseInt(value: unknown): number {
return safeParseNumber(value, parseInt);
return safeParseNumber(value, (v: string) => Math.trunc(Number(v)));
}
export function joinMapLists<A, B>(