Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile
@NyaGarcia
NyaGarcia / marble-testing.spec.ts
Last active April 5, 2020 19:48
Marble testing with TestScheduler
import { TestScheduler } from "rxjs/testing";
import { Observable } from "rxjs";
import { filter } from "rxjs/operators";
describe("Awesome testing with Marble Diagrams", () => {
const scheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});
const isMultipleOfTen = (number: number) => number % 10 === 0;
@NyaGarcia
NyaGarcia / marble-testing-pokemon.spec.ts
Created March 27, 2020 18:31
Marble testing with TestScheduler and pokemon
import { TestScheduler } from "rxjs/testing";
import { filter, map } from "rxjs/operators";
describe("Awesome testing with Marble Diagrams", () => {
const scheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});
it("should filter non-Water type pokemon and add attack property", () => {
scheduler.run(({ cold, expectObservable }) => {
@NyaGarcia
NyaGarcia / range.ts
Created April 17, 2020 15:55
Creating Observables with the range() function
import { range } from "rxjs";
// Observable will emit a sequence of numbers from 0 to 4 and complete
const number$ = range(5);
// Observable will emit a sequence of numbers from 1 to 5 and complete
const range$ = range(1, 5);
// Observable will emit a sequence of 10 numbers starting at 10 (from 10 to 19)
const moreNumber$ = range(10, 10);
@NyaGarcia
NyaGarcia / fromEvent.ts
Last active April 24, 2020 12:56
Creating Observables with the fromEvent() function
import { fromEvent } from "rxjs";
import { map } from 'rxjs/operators';
// Creating an Observable from mouse clicks
const click$ = fromEvent<MouseEvent>(document, "click");
// Creating an Observable from pressed keys
const keyPressed$ = fromEvent<KeyboardEvent>(document, "keydown");
// Creating an Observable from scroll changes
@NyaGarcia
NyaGarcia / ajax.ts
Created April 17, 2020 19:44
Creating Observables with the ajax() function
import { ajax } from "rxjs/ajax";
// Observable will emit the entire AjaxResponse object
const ghibliFilmsResponse$ = ajax("https://ghibliapi.herokuapp.com/films");
// Observable will only emit the response data
const ghibliFilm$ = ajax.getJSON("https://ghibliapi.herokuapp.com/films");
// What if we need custom headers? No problem!
const ghibliFilmWithHeaders$ = ajax({
@NyaGarcia
NyaGarcia / ajax-subscription.ts
Created April 17, 2020 20:00
Subscription to ajax Observables
ghibliFilmsResponse$.subscribe(console.log);
// Output: AjaxResponse {xhr: {}, request: {}...}
ghibliFilm$.subscribe(films => console.log(films));
// Output: [Object, Object, Object...]
ghibliFilmWithHeaders$.subscribe(console.log);
// Output: AjaxResponse {xhr: {}, request: {}...}
@NyaGarcia
NyaGarcia / ajaxResponse.ts
Created April 17, 2020 21:04
Observable created with ajax()
import { ajax } from "rxjs/ajax";
// Observable will emit the entire AjaxResponse object
const ghibliFilmsResponse$ = ajax("https://ghibliapi.herokuapp.com/films");
ghibliFilmsResponse$.subscribe(console.log);
// Output: AjaxResponse {xhr: {}, request: {}...}
@NyaGarcia
NyaGarcia / ajax-getJSON.ts
Created April 17, 2020 21:10
Observable created with ajax, data fetched with getJSON
import { ajax } from "rxjs/ajax";
// Observable will only emit the response data
const ghibliFilm$ = ajax.getJSON("https://ghibliapi.herokuapp.com/films");
ghibliFilm$.subscribe(films => console.log(films));
// Output: [Object, Object, Object...]
@NyaGarcia
NyaGarcia / ajax-custom.ts
Created April 17, 2020 21:15
OBservable created with ajax(), with custom headers
// What if we need custom headers? No problem!
const ghibliFilmWithHeaders$ = ajax({
url: 'https://ghibliapi.herokuapp.com/films',
method: 'GET',
headers: {
'Content-Type': 'json'
},
body: {
message: "Custom message 'cause we're so cool"
}
@NyaGarcia
NyaGarcia / generate.ts
Created April 23, 2020 14:02
Creating an Observable with generate()
import { generate } from "rxjs";
const number$ = generate(
1,
x => x < 10,
x => x + 1
);
number$.subscribe(console.log);
// Output: 1, 2, 3, 4, 5, 6, 7, 8, 9