An educational pure functional programming library in TypeScript
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Refactor branded-types.md examples to expression style

+5 -8
+5 -8
docs/guides/concepts/branded-types.md
··· 136 136 137 137 type OrderId = Branded<string, "OrderId"> 138 138 139 - const OrderId = (value: string): Option<OrderId> => { 140 - if (!value.startsWith("order-")) { 141 - return none 142 - } 143 - return some(value as OrderId) 144 - } 139 + const OrderId = (value: string): Option<OrderId> => 140 + value.startsWith("order-") ? some(value as OrderId) : none 145 141 ``` 146 142 147 143 Now invalid IDs are caught at creation: ··· 210 206 const Email = (s: string): Option<Email> => 211 207 /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s) ? some(s as Email) : none 212 208 209 + // Note: try/catch is an allowed exception to the "no early returns" rule 210 + // because it bridges impure JavaScript APIs 213 211 const Url = (s: string): Option<Url> => { 214 212 try { 215 - new URL(s) 216 - return some(s as Url) 213 + return (new URL(s), some(s as Url)) 217 214 } catch { 218 215 return none 219 216 }