BACK
ПрактикаInterview: custom operator onlyAdmins()
ПРАКТИКА · 80 · 練

Interview: custom operator onlyAdmins()

Создайте custom operator, который оставляет admins и возвращает их имена.

  1. Решите задачу в редакторе без длинной теории.
  2. Запустите проверку и сравните вывод с ожидаемым.
  3. Если застряли, откройте решение и перенесите подход в свой код.
Решение spoiler · click to reveal
const { from, filter, map } = Rx;

function onlyAdmins() {
  return source$ => source$.pipe(
    filter(user => user.role === 'admin'),
    map(user => user.name)
  );
}

const users$ = from([
  { name: 'Ada', role: 'admin' },
  { name: 'Lin', role: 'user' },
]);

users$.pipe(onlyAdmins()).subscribe(value => console.log(value));
script.ts // TypeScript
CONSOLE · Console Output
Нажмите на запуск, чтобы увидеть результат...