Browse Source

fix(nc-gui): update roundUpToPrecision function

pull/8874/head
Ramesh Mane 5 months ago
parent
commit
e6774e1524
  1. 8
      packages/nc-gui/components/cell/Decimal.vue
  2. 60
      packages/nocodb-sdk/src/lib/helperFunctions.ts

8
packages/nc-gui/components/cell/Decimal.vue

@ -1,5 +1,6 @@
<script lang="ts" setup>
import type { VNodeRef } from '@vue/runtime-core'
import { roundUpToPrecision } from 'nocodb-sdk'
interface Props {
// when we set a number, then it is number type
@ -35,13 +36,6 @@ const meta = computed(() => {
const _vModel = useVModel(props, 'modelValue', emits)
function roundUpToPrecision(value: number, precision = 1) {
const factor = Math.pow(10, precision)
const roundedValue = Math.round(value * factor) / factor
return roundedValue.toFixed(precision)
}
const displayValue = computed(() => {
if (_vModel.value === null) return null

60
packages/nocodb-sdk/src/lib/helperFunctions.ts

@ -108,31 +108,46 @@ const getAvailableRollupForUiType = (type: string) => {
}
};
const getFileName = ({
name,
count,
ext
}) => `${name}${count ? `(${count})` : ''}${ext? `${ext}` : ''}`
const getFileName = ({ name, count, ext }) =>
`${name}${count ? `(${count})` : ''}${ext ? `${ext}` : ''}`;
// add count before extension if duplicate name found
function populateUniqueFileName(
fileName: string,
attachments: string[]
) {
return fileName.replace(/^(.+?)(?:\((\d+)\))?(\.(?:tar|min)\.(?:\w{2,4})|\.\w+)$/, (fileName,name, count, ext) =>{
let genFileName = fileName;
let c = count || 1;
// iterate until a unique name
while (attachments.some((fn) => fn === genFileName)) {
genFileName = getFileName({
name,
ext,
count: c++
});
function populateUniqueFileName(fileName: string, attachments: string[]) {
return fileName.replace(
/^(.+?)(?:\((\d+)\))?(\.(?:tar|min)\.(?:\w{2,4})|\.\w+)$/,
(fileName, name, count, ext) => {
let genFileName = fileName;
let c = count || 1;
// iterate until a unique name
while (attachments.some((fn) => fn === genFileName)) {
genFileName = getFileName({
name,
ext,
count: c++,
});
}
return genFileName;
}
return genFileName;
});
);
}
function roundUpToPrecision(number: number, precision: number = 0) {
precision =
precision == null
? 0
: precision >= 0
? Math.min(precision, 292)
: Math.max(precision, -292);
if (precision) {
// Shift with exponential notation to avoid floating-point issues.
// See [MDN](https://mdn.io/round#Examples) for more details.
let pair = `${number}e`.split('e');
const value = Math.round(Number(`${pair[0]}e${+pair[1] + precision}`));
pair = `${value}e`.split('e');
return +`${pair[0]}e${+pair[1] - precision}`;
}
return Math.round(number);
}
export {
@ -145,4 +160,5 @@ export {
stringifyRolesObj,
getAvailableRollupForUiType,
populateUniqueFileName,
roundUpToPrecision,
};

Loading…
Cancel
Save