Skip to content

Commit

Permalink
chapter on computed signals
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieGreenman committed Nov 29, 2024
1 parent 83bf37f commit 8e870c7
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions signals/computed-signals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Moving from Angular Pipes to Computed Signals

With Angular Signals and computed(), I've found compelling reasons to move away from Angular pipes in favor of utility functions. Here's why:

## Key Benefits
- **Pure Utility Functions**: Instead of pipes, we can use simple TypeScript functions that are easier to understand and maintain
- **Simplified Testing**: Unit tests become more straightforward without pipe-specific testing logic
- **Better Component Integration**: Functions integrate more naturally with components and computed signals

## Real-World Example

Let's look at a practical example of converting names to acronyms:
```
// title-to-acronym.ts
export function titleToAcronym(title: string): string {
return title
.split(' ')
.filter(word => word.length > 0)
.map(word => word[0]?.toUpperCase() || '')
.join('');
}
// title-to-acronym.spec.ts
// super simple unit testing
describe('titleToAcronym', () => {
it('should convert title to acronym', () => {
expect(titleToAcronym('Hello World')).toBe('HW');
expect(titleToAcronym('Test Driven Development')).toBe('TDD');
expect(titleToAcronym('Single Word')).toBe('SW');
});
});
// test.component.ts
export class TicketsDataTableComponent {
// ..
workspaceAcronym = computed(() => {
const userFullName = this.user()?.fullName;
return titleToAcronym(userFullName);
});
//..
}
```

0 comments on commit 8e870c7

Please sign in to comment.