/// import { recipe, UI } from "commontools"; interface State { text: string; searchTerm: string; items: number[]; start: number; end: number; threshold: number; factor: number; names: string[]; prefix: string; prices: number[]; discount: number; taxRate: number; users: Array<{ name: string; age: number; active: boolean }>; minAge: number; words: string[]; separator: string; } export default recipe("MethodChains", (state) => { return { [UI]: (

Chained String Methods

{/* Simple chain */}

Trimmed lower: {state.text.trim().toLowerCase()}

{/* Chain with reactive argument */}

Contains search:{" "} {state.text.toLowerCase().includes(state.searchTerm.toLowerCase())}

{/* Longer chain */}

Processed:{" "} {state.text.trim().toLowerCase().replace("old", "new").toUpperCase()}

Array Method Chains

{/* Filter then length */}

Count above threshold:{" "} {state.items.filter((x) => x > state.threshold).length}

{/* Filter then map */}
    {state.items.filter((x) => x > state.threshold).map((x) => (
  • Value: {x * state.factor}
  • ))}
{/* Multiple filters */}

Double filter count:{" "} {state.items.filter((x) => x > state.start).filter((x) => x < state.end ).length}

Methods with Reactive Arguments

{/* Slice with reactive indices */}

Sliced items: {state.items.slice(state.start, state.end).join(", ")}

{/* String methods with reactive args */}

Starts with:{" "} {state.names.filter((n) => n.startsWith(state.prefix)).join(", ")}

{/* Array find with reactive predicate */}

First match: {state.names.find((n) => n.includes(state.searchTerm))}

Complex Method Combinations

{/* Map with chained operations inside */}
    {state.names.map((name) => (
  • {name.trim().toLowerCase().replace(" ", "-")}
  • ))}
{/* Reduce with reactive accumulator */}

Total with discount: {state.prices.reduce( (sum, price) => sum + price * (1 - state.discount), 0, )}

{/* Method result used in computation */}

Average * factor:{" "} {(state.items.reduce((a, b) => a + b, 0) / state.items.length) * state.factor}

Methods on Computed Values

{/* Method on binary expression result */}

Formatted price: {(state.prices[0]! * (1 - state.discount)).toFixed(2)}

{/* Method on conditional result */}

Conditional trim:{" "} {(state.text.length > 10 ? state.text : state.prefix).trim()}

{/* Method chain on computed value */}

Complex:{" "} {(state.text + " " + state.prefix).trim().toLowerCase().split(" ") .join("-")}

Array Methods with Complex Predicates

{/* Filter with multiple conditions */}

Active adults:{" "} {state.users.filter((u) => u.age >= state.minAge && u.active).length}

{/* Map with conditional logic */}
    {state.users.map((u) => (
  • {u.active ? u.name.toUpperCase() : u.name.toLowerCase()}
  • ))}
{/* Some/every with reactive predicates */}

Has adults:{" "} {state.users.some((u) => u.age >= state.minAge) ? "Yes" : "No"}

All active: {state.users.every((u) => u.active) ? "Yes" : "No"}

Method Calls in Expressions

{/* Method result in arithmetic */}

Length sum: {state.text.trim().length + state.prefix.trim().length}

{/* Method result in comparison */}

Is long: {state.text.trim().length > state.threshold ? "Yes" : "No"}

{/* Multiple method results combined */}

Joined: {state.words.join(state.separator).toUpperCase()}

), }; });