Refactoring (#17)

Reviewed-on: #17
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
phoenix
2025-11-27 02:10:51 +00:00
committed by phoenix
parent fc1f3fa814
commit 18d1d2730f
8 changed files with 20 additions and 19 deletions
+169
View File
@@ -0,0 +1,169 @@
package parser
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"unicode"
"git.kundeng.us/phoenix/textsender-models/tx0/contact"
)
const Letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
type NumberParser struct {
FilePath string
}
func (prsr *NumberParser) FileDump() []contact.Contact {
fmt.Println("Dumping files")
objs := []contact.Contact{}
for _, line := range prsr.readLines(prsr.FilePath) {
if strings.ContainsAny(line, Letters) {
continue
}
myObj := contact.Contact{PhoneNumber: line}
if myObj.PhoneNumber != "" {
objs = append(objs, myObj)
}
}
objs = prsr.updateContacts(objs)
objs = prsr.removeDups(objs)
return objs
}
func (prsr *NumberParser) updateContacts(contacts []contact.Contact) []contact.Contact {
updated := []contact.Contact{}
for _, ct := range contacts {
parsed := ct.PhoneNumber
if len(parsed) < 2 {
fmt.Println("Invalid number:", parsed)
continue
}
pars := prsr.removeSomeData(parsed)
parsed = pars
fmt.Println(parsed)
updatedParsed := prsr.addPrefix(parsed)
updated = append(updated, contact.Contact{PhoneNumber: updatedParsed})
}
return updated
}
func (prsr *NumberParser) removeDups(contacts []contact.Contact) []contact.Contact {
updated := []contact.Contact{}
uniqueItems := make(map[string]string)
for _, val := range contacts {
uniqueItems[val.PhoneNumber] = val.PhoneNumber
}
for _, item := range uniqueItems {
updated = append(updated, contact.Contact{PhoneNumber: item})
}
return updated
}
func (prsr NumberParser) PrintValues(contacts []contact.Contact, printTotal bool) {
fmt.Println("Printing numbers")
for _, ct := range contacts {
fmt.Println("Number:", ct.PhoneNumber)
}
if printTotal {
fmt.Println("Total Numbers:", len(contacts))
}
}
func (prsr NumberParser) SaveFile(vals []contact.Contact) {
filename := "numbers.json"
// Create json and save it to the filesystem
jsonData, err := json.MarshalIndent(&vals, "", " ")
if err != nil {
fmt.Println("Error marshaling JSON:", err)
return
}
err = os.WriteFile(filename, jsonData, 0644)
if err != nil {
fmt.Println("Error writing file:", err)
return
}
}
func (prsr NumberParser) readLines(filename string) []string {
// Open the file
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// Create a scanner to read the file line by line
scanner := bufio.NewScanner(file)
lines := []string{}
// Read line by line
for scanner.Scan() {
line := scanner.Text()
lines = append(lines, line)
}
// Check for any errors during scanning
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
func (prsr NumberParser) removeSomeData(unparsed string) string {
parsed := ""
for _, ch := range unparsed {
if ch == ' ' {
continue
} else {
if unicode.IsDigit(ch) {
parsed += string(ch)
}
}
}
fmt.Println("Parsed:", parsed)
return parsed
}
func (prsr NumberParser) addPrefix(raw string) string {
parsed := raw
firstChar := raw[0]
secondChar := raw[1]
if firstChar == '1' {
parsed = "+" + parsed
} else if firstChar != '+' {
if secondChar != '1' {
parsed = "+1" + parsed
} else {
parsed = "+1" + parsed
}
}
return parsed
}
+70
View File
@@ -0,0 +1,70 @@
package parser
import (
"fmt"
"testing"
"git.kundeng.us/phoenix/textsender-models/tx0/contact"
)
const Test_File_Path = "../../tests/numbers.txt"
// Remove duplicates
func Test_RemoveDups(t *testing.T) {
parser := NumberParser{FilePath: Test_File_Path}
finalAmount := parser.FileDump()
if len(finalAmount) < 1 {
t.Error("Dump should not be empty")
}
testValues := []contact.Contact{
{PhoneNumber: "+12175550194"},
{PhoneNumber: "+16025550177"},
{PhoneNumber: "+17135550167"},
{PhoneNumber: "+16085550190"},
{PhoneNumber: "+13055550112"},
{PhoneNumber: "+14155550135"},
{PhoneNumber: "+13175550123"},
{PhoneNumber: "+13605550158"},
}
aSize := len(finalAmount)
bSize := len(testValues)
if aSize != bSize {
t.Error("Data has varrying sizes A", aSize, " B", bSize)
}
if !dataEqual(finalAmount, testValues) {
fmt.Println("Printing final amount")
for _, val := range finalAmount {
fmt.Println("PhoneNumber:", val.PhoneNumber)
}
fmt.Println("Printing test values")
for _, val := range testValues {
fmt.Println("PhoneNumber:", val.PhoneNumber)
}
t.Error("Data has not be updated")
}
}
func dataEqual(a, b []contact.Contact) bool {
if len(a) != len(b) {
return false
}
for i := range a {
foundIt := false
for j := range b {
if a[i] == b[j] {
foundIt = true
}
}
if !foundIt {
return false
}
}
return true
}