44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
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()
|
|
}
|
|
}
|