These files were already out of step with the project's Prettier config before this branch of work; running `pnpm format` finally swept them up. No semantic changes. Kept separate from the functional commits so the reformatting does not bury them in diff noise.
1.4 KiB
1.4 KiB
title, tags, author
| title | tags | author | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ECMAScript 2025 Syntax Sugar Guide |
|
|
Pattern Matching
Traditional Approach
function processResponse(response) {
if (response.status === 200 && response.data) {
return { success: true, data: response.data }
} else if (response.status === 404) {
return { success: false, error: "Not found" }
} else if (response.status >= 500) {
return { success: false, error: "Server error" }
} else {
return { success: false, error: "Unknown error" }
}
}
Pattern Matching Approach
function processResponse(response) {
return match (response) {
when ({ status: 200, data }) -> ({ success: true, data })
when ({ status: 404 }) -> ({ success: false, error: 'Not found' })
when ({ status: status if status >= 500 }) -> ({ success: false, error: 'Server error' })
default -> ({ success: false, error: 'Unknown error' })
};
}
Handling Array Length Branches Gracefully
function handleArray(arr) {
return match (arr) {
when ([]) -> "Empty array"
when ([first]) -> `Only one element: ${first}`
when ([first, second]) -> `Two elements: ${first}, ${second}`
when ([first, ...rest]) -> `First element: ${first}, others: ${rest.length} items`
};
}