chore: format unitNumber to alphabet
This commit is contained in:
@@ -32,3 +32,36 @@ export const convertAlphabetString = (input: string): string => {
|
||||
.filter((s) => s !== '')
|
||||
.join('')
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a two-digit string number (01-26) into its corresponding alphabet character (A-Z).
|
||||
* @param numStr The two-digit numeric string to convert.
|
||||
* @returns The alphabet character, or an empty string if invalid.
|
||||
*/
|
||||
export const numberStringToAlphabet = (numStr: string): string => {
|
||||
if (!numStr || numStr.length !== 2) return ''
|
||||
|
||||
const position = parseInt(numStr, 10)
|
||||
if (isNaN(position) || position < 1 || position > 26) return ''
|
||||
|
||||
// A is 65 (1 + 64)
|
||||
return String.fromCharCode(position + 64)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a string of numbers into a string of alphabet characters.
|
||||
* Example: "010203" -> "ABC"
|
||||
* @param input The string of numbers (must be even length).
|
||||
* @returns The converted alphabet string.
|
||||
*/
|
||||
export const convertNumberStringToAlphabet = (input: string): string => {
|
||||
if (!input || input.length % 2 !== 0) return ''
|
||||
|
||||
const result: string[] = []
|
||||
for (let i = 0; i < input.length; i += 2) {
|
||||
const part = input.substring(i, i + 2)
|
||||
result.push(numberStringToAlphabet(part))
|
||||
}
|
||||
|
||||
return result.join('')
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { alphabetToNumberString } from '~/helpers/alphabet_conversion'
|
||||
import { alphabetToNumberString, convertNumberStringToAlphabet } from '~/helpers/alphabet_conversion'
|
||||
import { sendQueryToThirdParty } from '../commands/command'
|
||||
import { DeviceQueryParams } from '../schema'
|
||||
|
||||
@@ -39,7 +39,7 @@ export default abstract class QueryService {
|
||||
message: 'command accepted',
|
||||
data: {
|
||||
floorName: request.data.data.floor,
|
||||
unitNumber: request.data.data.unit,
|
||||
unitNumber: convertNumberStringToAlphabet(request.data.data.unit),
|
||||
deviceName: request.data.data.device,
|
||||
roomName: request.data.data.room,
|
||||
deviceType: request.data.data.devType,
|
||||
|
||||
Reference in New Issue
Block a user