bettersg / checkMate

GNU General Public License v3.0
5 stars 0 forks source link

stripPhone fix and unit test #231

Closed btjl closed 5 months ago

btjl commented 5 months ago

Added unit test for stripPhone utility function.

While working on unit tests I discovered a test case that wasn't working as documented in Issue #230.

Before refactoring the function, I performed some validations to make sure the findPhoneNumbersInText function was working as intended.

import { findPhoneNumbersInText } from "libphonenumber-js"

const str =
  "https://youtu.be/oglCjESHJ70?si=JznEryFFXUZ6y2ma ~Dayan +6583294024 Who are you ~SLTan米+6583294023 你是红桂桥Ada? ~Dayan +6583294023 No"

console.log(findPhoneNumbersInText(str))

console.log(
  `56: ${str.charAt(56)}; 67: ${str.charAt(67)}; Slice: ${str.slice(56, 67)}`
)

console.log(
  `87: ${str.charAt(87)}; 98: ${str.charAt(98)}; Slice: ${str.slice(87, 98)}`
)

console.log(
  `116: ${str.charAt(116)}; 127: ${str.charAt(127)}; Slice: ${str.slice(
    116,
    127
  )}`
)

yielded

56: +; 67:  ; Slice: +6583294024
87: +; 98:  ; Slice: +6583294023
116: +; 127:  ; Slice: +6583294023

Hence, there is no issue.

To simplify the function without the need to manually juggle indices, I refactored it in such a way that it takes the nearest phone number to be updated, updates it, reassigns a "new" string and repeat until there are no longer any numbers detected. A threshold value was also set to minimise the risk of an infinite being introduced for whatever reason (e.g. it couldn't format stuff properly and a number always remains detected)