Skip to content

Commit

Permalink
nist to number fix (#3725)
Browse files Browse the repository at this point in the history
Co-authored-by: Cole Blanchard <[email protected]>
  • Loading branch information
blanchco and Cole Blanchard authored May 29, 2024
1 parent dbd7d41 commit a0fc4d1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ watch(
() => props.modelValue,
(newValue) => {
if (isNistType) {
maskedValue.value = numberToNist(newValue.toString());
maskedValue.value = numberToNist(newValue?.toString() ?? '');
}
}
);
onMounted(() => {
if (isNistType) {
maskedValue.value = numberToNist(props.modelValue.toString());
maskedValue.value = numberToNist(props.modelValue?.toString() ?? '');
}
});
Expand Down
7 changes: 3 additions & 4 deletions packages/client/hmi-client/src/utils/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ export function numberToNist(num: string) {
* @param {string} num - The number string in NIST form to convert.
* @returns {string} The number in normal form.
*/
export function nistToNumber(num: string): string {
export function nistToNumber(numStr: string): number {
// Remove any spaces from the formatted number
let numStr = num.replace(/\s/g, '');
numStr = parseFloat(numStr).toString();
return numStr;
numStr = numStr.replace(/\s/g, '');
return parseFloat(numStr);
}

/**
Expand Down
21 changes: 6 additions & 15 deletions packages/client/hmi-client/tests/unit/utils/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,33 +65,24 @@ describe('number util tests', () => {

describe('nistToNumber', () => {
it('should correctly convert numbers without a decimal part', () => {
expect(nistToNumber('1')).to.eq('1');
expect(nistToNumber('12')).to.eq('12');
expect(nistToNumber('123')).to.eq('123');
expect(nistToNumber('1 234 567')).to.eq('1234567');
expect(nistToNumber('1 234 567')).to.eq(1234567);
});

it('should correctly convert numbers with a decimal part', () => {
expect(nistToNumber('1.1')).to.eq('1.1');
expect(nistToNumber('12.12')).to.eq('12.12');
expect(nistToNumber('123.123')).to.eq('123.123');
expect(nistToNumber('1 234 567.123 456 7')).to.eq('1234567.1234567');
expect(nistToNumber('1 234 567.123 456 7')).to.eq(1234567.1234567);
});

it('should correctly convert negative numbers', () => {
expect(nistToNumber('-1')).to.eq('-1');
expect(nistToNumber('-12')).to.eq('-12');
expect(nistToNumber('-123')).to.eq('-123');
expect(nistToNumber('-1 234 567.123 456 7')).to.eq('-1234567.1234567');
expect(nistToNumber('-1 234 567.123 456 7')).to.eq(-1234567.1234567);
});

it('should return "NaN" for non-numeric strings', () => {
expect(nistToNumber('abc')).to.eq('NaN');
expect(nistToNumber('1.23abc')).to.eq('1.23');
expect(Number.isNaN(nistToNumber('abc'))).to.equal(true);
expect(nistToNumber('1.23abc')).to.eq(1.23);
});

it('should correctly handle multiple spaces between groups', () => {
expect(nistToNumber('1 234.123 4')).to.eq('1234.1234');
expect(nistToNumber('1 234.123 4')).to.eq(1234.1234);
});
});

Expand Down

0 comments on commit a0fc4d1

Please sign in to comment.