-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Answer: 43 #1149
Answer: 43 #1149
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,3 +42,5 @@ Thumbs.db | |
TODO.md | ||
.nx/cache | ||
.nx/workspace-data | ||
|
||
.cursorrules |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export type User = { | ||
name: string; | ||
lastName?: string; | ||
age?: number; | ||
category?: Category; | ||
}; | ||
|
||
export type Category = 'Youth' | 'Junior' | 'Open' | 'Senior'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,40 +2,28 @@ import { TitleCasePipe } from '@angular/common'; | |
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
Input, | ||
OnChanges, | ||
computed, | ||
input, | ||
} from '@angular/core'; | ||
|
||
type Category = 'Youth' | 'Junior' | 'Open' | 'Senior'; | ||
const ageToCategory = (age: number): Category => { | ||
if (age < 10) return 'Youth'; | ||
else if (age < 18) return 'Junior'; | ||
else if (age < 35) return 'Open'; | ||
return 'Senior'; | ||
}; | ||
import { ageToCategory } from './utils'; | ||
|
||
@Component({ | ||
selector: 'app-user', | ||
standalone: true, | ||
imports: [TitleCasePipe], | ||
template: ` | ||
{{ fullName | titlecase }} plays tennis in the {{ category }} category!! | ||
{{ fullName() | titlecase }} plays tennis in the {{ category() }} category! | ||
`, | ||
host: { | ||
class: 'text-xl text-green-800', | ||
}, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class UserComponent implements OnChanges { | ||
@Input({ required: true }) name!: string; | ||
@Input() lastName?: string; | ||
@Input() age?: string; | ||
|
||
fullName = ''; | ||
category: Category = 'Junior'; | ||
export class UserComponent { | ||
readonly name = input.required<string>(); | ||
readonly lastName = input<string>(''); | ||
readonly age = input<string>(''); | ||
|
||
ngOnChanges(): void { | ||
this.fullName = `${this.name} ${this.lastName ?? ''}`; | ||
this.category = ageToCategory(Number(this.age) ?? 0); | ||
} | ||
readonly fullName = computed(() => `${this.name()} ${this.lastName() ?? ''}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need the ?? '', lastName has a default value. |
||
readonly category = computed(() => ageToCategory(Number(this.age()) ?? 0)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Category } from './models'; | ||
|
||
export const ageToCategory = (age: number): Category => { | ||
if (age < 0 || !Number.isInteger(age)) { | ||
throw new Error('Age must be a positive integer'); | ||
} | ||
|
||
const categoryRanges: { [key: number]: Category } = { | ||
10: 'Youth', | ||
18: 'Junior', | ||
35: 'Open', | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not putting this const outside of the scope of the function, so you don't recreate it at each call. |
||
|
||
const threshold = Object.keys(categoryRanges) | ||
.map(Number) | ||
.find((limit) => age < limit); | ||
|
||
return threshold ? categoryRanges[threshold] : 'Senior'; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
import { bootstrapApplication } from '@angular/platform-browser'; | ||
import { AppComponent } from './app/app.component'; | ||
import { appConfig } from './app/app.config'; | ||
|
||
bootstrapApplication(AppComponent, appConfig).catch((err) => | ||
console.error(err), | ||
); | ||
bootstrapApplication(AppComponent).catch((err) => console.error(err)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can add a transform method to make it a number with a default value to 0