Skip to content

Instantly share code, notes, and snippets.

@mhartington
Created May 11, 2020 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhartington/8d83590509b2603129ee365ffe8fb462 to your computer and use it in GitHub Desktop.
Save mhartington/8d83590509b2603129ee365ffe8fb462 to your computer and use it in GitHub Desktop.

Nullish

Before

  private readUrlEffect$ = this.route.queryParams.pipe(
    map(({ query }) =>
      this.searchForm.setValue({ search: !!query ? query ; '' })
    )
  );

After

  private readUrlEffect$ = this.route.queryParams.pipe(
    map(({ query }) =>
      this.searchForm.setValue({ search: query ?? '' })
    )
  );

Optional Chaining + Nullish

Before

  search(query: string): Observable<any> {
    const searchTypes = ['songs', 'albums', 'playlists'];
    return from(
      this.musicKitInstance.api.search(query, {
        types: searchTypes,
        limit: 50,
      })
    ).pipe(
      map((res: any) => {
        return {
          albums: res.albums.data,
          songs: res.songs.data,
          playlists: res.playlists.data,
        };
      }),
      retryWhen((error) => error.pipe(delay(500))),
      timeout(5000)
    );
  }

After

  search(query: string): Observable<any> {
    const searchTypes = ['songs', 'albums', 'playlists'];
    return from(
      this.musicKitInstance.api.search(query, {
        types: searchTypes,
        limit: 50,
      })
    ).pipe(
      map((res: any) => ({
        albums: res?.albums?.data ?? null,
        songs: res?.songs?.data ?? null,
        playlists: res?.playlists?.data ?? null,
      })),
      retryWhen((error) => error.pipe(delay(500))),
      timeout(5000)
    );
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment