Skip to content

Commit

Permalink
Refactor numberWithPow10 helper more generically
Browse files Browse the repository at this point in the history
  • Loading branch information
longzheng committed Jul 25, 2024
1 parent 7f2338a commit 89a1503
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
19 changes: 19 additions & 0 deletions src/number.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest';
import { numberWithPow10 } from './number';

describe('numberWithPow10', () => {
it('should calculate positive power of ten', () => {
const result = numberWithPow10(1, 2);
expect(result).toBe(100);
});

it('should calculate negative power of ten', () => {
const result = numberWithPow10(1005, -1);
expect(result).toBe(100.5);
});

it('should calculate zero power of ten', () => {
const result = numberWithPow10(1000, 0);
expect(result).toBe(1000);
});
});
4 changes: 4 additions & 0 deletions src/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ export function safeParseHexString(value: string): number {
export function numberToHex(value: number): string {
return value.toString(16).toUpperCase();
}

export function numberWithPow10(number: number, pow10: number): number {
return number * 10 ** pow10;
}
21 changes: 1 addition & 20 deletions src/sep2/models/activePower.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { it, expect } from 'vitest';
import { parseStringPromise } from 'xml2js';
import { getMockFile } from '../helpers/mocks';
import type { ActivePower } from './activePower';
import { activePowerToWatts, parseActivePowerXmlObject } from './activePower';
import { parseActivePowerXmlObject } from './activePower';

it('should parse active power XML with multiplier 2', async () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
Expand Down Expand Up @@ -37,21 +36,3 @@ it('should parse active power XML with multiplier 0', async () => {
expect(link.value).toBe(2512);
expect(link.multiplier).toBe(0);
});

it('activePowerToWatts should convert ActivePower to watts with multiplier 2', () => {
const limitWatts = {
value: 15,
multiplier: 2,
} satisfies ActivePower;

expect(activePowerToWatts(limitWatts)).toBe(1500);
});

it('activePowerToWatts should convert ActivePower to watts with multiplier 0', () => {
const limitWatts = {
value: 2512,
multiplier: 0,
} satisfies ActivePower;

expect(activePowerToWatts(limitWatts)).toBe(2512);
});
5 changes: 0 additions & 5 deletions src/sep2/models/activePower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ export function parseActivePowerXmlObject(xmlObject: any): ActivePower {
};
}

// helper function to convert ActivePower to watts
export function activePowerToWatts(activePower: ActivePower): number {
return activePower.value * 10 ** activePower.multiplier;
}

export function generateActivePowerResponse({
value,
multiplier,
Expand Down

0 comments on commit 89a1503

Please sign in to comment.