Skip to content

Instantly share code, notes, and snippets.

View nicowernli's full-sized avatar

Nicolás Wernli nicowernli

View GitHub Profile
@nicowernli
nicowernli / rxjsmap.js
Last active March 23, 2018 11:26
Medium rxjs.map operator
const Rx = require('rxjs');
Rx.Observable.interval(1000)
.map(intervalValue => intervalValue * 2)
.subscribe(console.log); // 0, 2, 4, 6, 8, 10, 12, 14, 16, etc
@nicowernli
nicowernli / rxjs-flatMap.js
Last active March 23, 2018 11:54
Medium flatMap example
const Rx = require('rxjs');
const long$ = Rx.Observable.interval(1000).take(4);
const short$ = Rx.Observable.interval(500).take(4);
long$
.flatMap(long => short$.map(short => console.log({ long, short })))
.subscribe();
/** Output
@nicowernli
nicowernli / rxjs-switchMap.js
Created March 23, 2018 11:53
Medium switchMap
const Rx = require('rxjs');
const long$ = Rx.Observable.interval(1000).take(4);
const short$ = Rx.Observable.interval(500).take(4);
long$
.switchMap(long => short$.map(short => console.log({ long, short })))
.subscribe();
/** Output
@nicowernli
nicowernli / planet.service.ts
Created August 31, 2018 07:43
Planet service
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Planet } from './planet';
@Injectable()
export class PlanetService {
constructor(private http: HttpClient) {}
@nicowernli
nicowernli / planet.ts
Last active August 31, 2018 10:56
Planet interface
export interface Planet {
name: string;
rotation_period: number;
orbital_period: number;
diameter: number;
climate: string;
gravity: string;
terrain: string;
surface_water: number;
population: number;
@nicowernli
nicowernli / app.module.ts
Created August 31, 2018 07:52
App Module with ReactiveForms
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { SearchBarComponent } from './search-bar/search-bar.component';
import { PlanetService } from './planet.service';
@NgModule({
@nicowernli
nicowernli / searc-bar.component.ts
Created August 31, 2018 09:54
The Search Bar Component base
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { PlanetService } from '../planet.service';
import { Planet } from '../planet';
@Component({
selector: 'app-search-bar',
templateUrl: './search-bar.component.html'
@nicowernli
nicowernli / search-bar.component.html
Created August 31, 2018 09:58
Search bar component template
<div class="p-4">
<p class="mb-2">search-bar works!</p>
<div class="relative">
<input [formControl]="search" type="text" class="border border-grey rounded shadow-inner p-2 w-full" placeholder="Search...">
<div class="absolute mt-1 bg-grey w-full rounded shadow">
<ul class="list-reset">
<li *ngFor="let planet of (planets$ | async)" class="p-2">{{ planet.name }}</li>
</ul>
</div>
@nicowernli
nicowernli / search-bar.component.ts
Created August 31, 2018 10:16
Debounce time included
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { switchMap, debounceTime } from 'rxjs/operators';
import { PlanetService } from '../planet.service';
import { Planet } from '../planet';
@Component({
selector: 'app-search-bar',
templateUrl: './search-bar.component.html'
// GetItem finds a returns a single item from a dynamodb table.
// It returns an element and an error if any.
// If no element is found, will return nil without an error
func GetItem(id string, dest interface{}) error {
tableName := "table-name"
// create get operation information
item := &dynamodb.GetItemInput{
TableName: aws.String(tableName),
Key: map[string]*dynamodb.AttributeValue{