checkpoint
This commit is contained in:
43
frontend/src/services/User.ts
Normal file
43
frontend/src/services/User.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import type { Spending } from './Spending'
|
||||
|
||||
export class User {
|
||||
name: string
|
||||
spendings: Array<Spending>
|
||||
|
||||
constructor(name: string) {
|
||||
this.name = name
|
||||
this.spendings = Array<Spending>()
|
||||
}
|
||||
|
||||
addSpending(spending: Spending): void {
|
||||
spending.setSpender(this)
|
||||
this.spendings.push(spending)
|
||||
}
|
||||
|
||||
getTotalSpending(): number {
|
||||
return this.spendings.map((w) => w.amountCt).reduce((acc, num) => acc + num, 0)
|
||||
}
|
||||
|
||||
getInitials(): string {
|
||||
if (!this.name) return ''
|
||||
|
||||
const words = this.name
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.filter((word) => word.length > 0)
|
||||
|
||||
if (words.length === 0) return ''
|
||||
|
||||
if (words.length === 1) {
|
||||
const firstWord = words[0]
|
||||
if (!firstWord || firstWord.length === 0) return ''
|
||||
return firstWord.substring(0, 2).toUpperCase()
|
||||
}
|
||||
|
||||
const firstWord = words[0]
|
||||
const secondWord = words[1]
|
||||
if (!firstWord || !secondWord || firstWord.length === 0 || secondWord.length === 0) return ''
|
||||
|
||||
return (firstWord[0]! + secondWord[0]!).toUpperCase()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user