The type of values in the input async iterable
The narrowed type when using a type predicate (extends T)
Function to test each value with its index
A transformer function that yields only values that pass the predicate
This function creates a transformer that only yields values from an async iterable that pass a predicate test. The predicate can be a synchronous function, an async function, or a type predicate for TypeScript type narrowing.
The filter function supports three different predicate types:
is User)// Filter even numbers
const evenNumbers = await G.pipe(
G.ofIterable([1, 2, 3, 4, 5, 6]),
G.filter(x => x % 2 === 0),
G.array
); // [2, 4, 6]
// Async predicate
const validUsers = await G.pipe(
G.ofIterable(users),
G.filter(async user => {
const isValid = await validateUser(user);
return isValid;
}),
G.array
);
// Type predicate (narrows the type)
interface User { id: number; name: string; }
interface Admin extends User { role: 'admin'; permissions: string[]; }
function isAdmin(user: User): user is Admin {
return 'role' in user && user.role === 'admin';
}
const admins = await G.pipe(
G.ofIterable(users),
G.filter(isAdmin), // Narrows type from User to Admin
G.array
);
Creates a transformer that filters values based on a predicate function.
The type of values in the input async iterable
Function to test each value with its index
A transformer function that yields only values that pass the predicate
This function creates a transformer that only yields values from an async iterable that pass a predicate test. The predicate can be a synchronous function, an async function, or a type predicate for TypeScript type narrowing.
The filter function supports three different predicate types:
is User)// Filter even numbers
const evenNumbers = await G.pipe(
G.ofIterable([1, 2, 3, 4, 5, 6]),
G.filter(x => x % 2 === 0),
G.array
); // [2, 4, 6]
// Async predicate
const validUsers = await G.pipe(
G.ofIterable(users),
G.filter(async user => {
const isValid = await validateUser(user);
return isValid;
}),
G.array
);
// Type predicate (narrows the type)
interface User { id: number; name: string; }
interface Admin extends User { role: 'admin'; permissions: string[]; }
function isAdmin(user: User): user is Admin {
return 'role' in user && user.role === 'admin';
}
const admins = await G.pipe(
G.ofIterable(users),
G.filter(isAdmin), // Narrows type from User to Admin
G.array
);
Creates a transformer that filters values based on a predicate function.