Skip to content

Instantly share code, notes, and snippets.

@famzil
famzil / example.js
Created November 15, 2021 10:42
Meal API
const API_URL = "https://www.themealdb.com/api/json/v1/1/search.php?s=";
function fetchMealData() {
let request = new XMLHttpRequest();
request.open("GET", API_URL);
request.send();
request.onload = () => {
console.log("REQUEST: ", request);
if (request.status == 200) {
@famzil
famzil / exmaple.js
Created November 15, 2021 10:16
Jokes API
function get_joke_of_the_day() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Access the result here
alert(this.responseText);
}
};
xhttp.open("GET", "https://api.jokes.one/jod?category=animal", true);
xhttp.setRequestHeader("Content-type", "application/json");
@famzil
famzil / example.js
Created November 15, 2021 10:11
Background removal
var data = new FormData();
data.append("image_file", fileInput.files[0], "@path/to/image");
data.append("image_url", "url_to_image");
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
@famzil
famzil / completeexample.js
Created November 2, 2021 08:17
Complete test suite example
import { TestScheduler } from 'rxjs/testing';
import { delay, take } from 'rxjs/operators';
import { expect } from 'chai';
// Starting the test suite
describe('RxBookTracker Tests', () => {
// declaring our scheduler (outside, because we will be using it in all our tests)
let scheduler;
@famzil
famzil / test3.js
Created November 2, 2021 07:56
Testing a cold observable
...
scheduler.run(helpers => {
const source$ = helpers.cold("some produced values data");
// create the expected value, whatever you are expecting to be returned by the observable
const expected = .... ;
// Tetsing: take(number) for example is a cold observable
@famzil
famzil / test2.js
Created November 2, 2021 07:50
Helpers example
import { TestScheduler } from 'rxjs/testing';
// Using helpers.
scheduler.run(helpers => {
// creating the source$ and the expected observable
// ....
// Testing equality
helpers.expectObservable(source$).toBe(expected);
@famzil
famzil / test.js
Last active November 2, 2021 07:41
Test example
import { TestScheduler } from 'rxjs/testing';
// All what you can do with the helpers object
const { cold, hot, expectObservable, expectSubscriptions, flush, time, animate } = helpers;
// Test example
const expected = '400ms (a-b|)';
const values = {
a: 'value emitted',
b: 'another value emitted',
@famzil
famzil / testing observables
Created November 2, 2021 07:26
Example to TestScheduler
import { TestScheduler } from 'rxjs/testing';
let scheduler = new TestScheduler((actual, expected) => {
// code to test
});
@famzil
famzil / index.js
Created October 28, 2021 06:39
Example
import './style.css';
import { of, merge, queueScheduler, asapScheduler, asyncScheduler } from 'rxjs';
console.log('Start');
let queue$ = of('QueueScheduler (synchronous)', queueScheduler);
let asap$ = of('AsapScheduler (async micro task)', asapScheduler);
@famzil
famzil / index.js
Last active October 18, 2021 10:17
PublishBehavior example
function publishBehaviorDemo() {
/* Create the source observabe with publishBehavior operator. */
let source$ = interval(1000).pipe(take(4), publishBehavior(6), refCount());
/* First subscriber, outputs the received value with his name 'Observer 1' */
source$.subscribe((value) => console.log(`Observer 1: ${value}`));
/* Second subscriber after 1 second, outputs the received value with his name 'Observer 2' */
setTimeout(() => {