Skip to content

Instantly share code, notes, and snippets.

View jayphelps's full-sized avatar
💭
I may be slow to respond.

Jay Phelps jayphelps

💭
I may be slow to respond.
View GitHub Profile
@jayphelps
jayphelps / stuff.js
Created February 11, 2017 21:54
testing epics by mocking
// api.js
// your API call helpers
import { ajax } from 'rxjs/observable/dom/ajax';
export const fetchSomething = id =>
ajax.getJSON(`/somethings/${id}`);
// the epic
import { webSocket } from 'rxjs/observable/dom/webSocket';
import { timer } from 'rxjs/observable/timer';
import { fromEvent } from 'rxjs/observable/fromEvent';
// Lazy, doesn't connect when no one is subscribed,
// but to multiplex we need a single static instance
// so that they all use the same socket
const socket$ = webSocket('ws://stock/endpoint');
// So this is bi-direction multiplexing; multiple concurrent
@jayphelps
jayphelps / redux-thunk-examples.js
Created February 7, 2017 05:44 — forked from markerikson/redux-thunk-examples.js
Redux-Thunk examples
// The classic AJAX call - dispatch before the request, and after it comes back
function myThunkActionCreator(someValue) {
return (dispatch, getState) => {
dispatch({type : "REQUEST_STARTED"});
myAjaxLib.post("/someEndpoint", {data : someValue})
.then(response => dispatch({type : "REQUEST_SUCCEEDED", payload : response})
.catch(error => dispatch({type : "REQUEST_FAILED", error : error});
};
}
/**
* README FIRST::::::::::::::::::
*
* This isn't meant to necessarily be an example of "best practices"
* but rather to generally show how you might convert the redux-thunk examples
* here: https://gist.github.com/markerikson/ea4d0a6ce56ee479fe8b356e099f857e
*
* The redux-thunk examples used generic placeholder names, so it's possible
* I misunderstood the usecase/intent.
*
const oauthUserEpic = (action$) =>
action$
.ofType(OAUTH_USER)
.concatMap(({ token }) =>
get({
endpoint: 'user',
params: { access_token: token }
}) // assuming get() returns an Observable
.map((user) =>
addUser(
@jayphelps
jayphelps / index.js
Created May 24, 2016 23:47
async window.open()
window.onclick = () => {
// You MUST create the window on the same event
// tick/stack as the user-initiated event (e.g. click callback)
const googleWindow = window.open();
// Do your async work
fakeAjax(response => {
// Change the URL of the window you created once you
// know what the full URL is!
googleWindow.location.replace(`https://google.com?q=${response}`);
@jayphelps
jayphelps / isValidEmail.js
Last active September 24, 2015 05:03
Validates an email, error on the side of letting bad ones through (best practice). Accepts emails with plus signs.
function isValidEmail(str) {
/**
* These comments use the following terms from RFC2822:
* local-part, domain, domain-literal and dot-atom.
* Does the address contain a local-part followed an @ followed by a domain?
* Note the use of lastIndexOf to find the last @ in the address
* since a valid email address may have a quoted @ in the local-part.
* Does the domain name have at least two parts, i.e. at least one dot,
* after the @? If not, is it a domain-literal?
*
@jayphelps
jayphelps / ofPropertyPathChanges.js
Last active June 15, 2021 18:09
RxJS observable of property value changes, given an object and property path
function isObject(value) {
// Avoid an old bug in Chrome 19-20
// See https://code.google.com/p/v8/issues/detail?id=2291
const type = typeof value;
return type === 'function' || (!!value && type === 'object');
}
function ofPropertyChanges(obj, key) {
if (isObject(obj) === false) {
return Rx.Observable.return(undefined);
@jayphelps
jayphelps / Object.resolve.js
Last active August 29, 2015 14:16
Resolve property paths
/**
Object.resolve(document, 'body.style')
*/
Object.resolve = function resolve(obj, propPath) {
return [obj]
.concat(propPath.split('.'))
.reduce((prevObj, currKey) => prevObj[currKey]);
};
var parseLocation = (function () {
var a = document.createElement('a');
function parseLocation(url) {
a.href = url;
var protocol = (!a.protocol || a.protocol === ':') ? location.protocol : a.protocol;
// IE8 inconsistently returns leading slash
var pathname = (a.pathname.match(/^\//)) ? a.pathname : '/' + a.pathname;