Skip to content

Instantly share code, notes, and snippets.

@peduarte
Last active November 2, 2016 13:15
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 peduarte/11d6d8e9c0a0b4a9101bb91bacc1b898 to your computer and use it in GitHub Desktop.
Save peduarte/11d6d8e9c0a0b4a9101bb91bacc1b898 to your computer and use it in GitHub Desktop.
RxJs Observable – Get Shits from Shit Pedro Says
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<!-- put additional styles and scripts here -->
</head>
<body>
<!-- put markup and other contents here -->
<input type="text" id="search" placeholder="Search for a shit" autocomplete="off" />
<div id="shits"></div>
</body>
</html>
import { Observable } from 'rxjs';
function getShits$(keyword) {
return Observable
.fromPromise(
fetch('https://shitpedrosays.herokuapp.com/shits.json')
.then(response => response.json())
)
.map(data => data.shits.filter(shit => shit.content.toLowerCase().includes(keyword)));
}
const searchInput = document.getElementById('search');
const shitsHolder = document.getElementById('shits');
Observable.fromEvent(searchInput, 'keyup')
.map(event => event.target.value)
.distinctUntilChanged()
.do(x => console.log(x))
.filter(value => value.length > 2)
.debounceTime(300)
.concatMap(value => getShits$(value))
.subscribe(data => {
shitsHolder.innerHTML = `
<ul>
${data.map(shit => `<li>${shit.content}</li>`).join('')}
</ul>
`;
});
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"rxjs": "5.0.0-rc.1"
}
}
'use strict';
var _rxjs = require('rxjs');
function getShits$(keyword) {
return _rxjs.Observable.fromPromise(fetch('https://shitpedrosays.herokuapp.com/shits.json').then(function (response) {
return response.json();
})).map(function (data) {
return data.shits.filter(function (shit) {
return shit.content.toLowerCase().includes(keyword);
});
});
}
var searchInput = document.getElementById('search');
var shitsHolder = document.getElementById('shits');
_rxjs.Observable.fromEvent(searchInput, 'keyup').map(function (event) {
return event.target.value;
}).distinctUntilChanged().do(function (x) {
return console.log(x);
}).filter(function (value) {
return value.length > 2;
}).debounceTime(300).concatMap(function (value) {
return getShits$(value);
}).subscribe(function (data) {
shitsHolder.innerHTML = '\n <ul>\n ' + data.map(function (shit) {
return '<li>' + shit.content + '</li>';
}).join('') + '\n </ul>\n ';
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment