Skip to content

Instantly share code, notes, and snippets.

View abiodun0's full-sized avatar

Abiodun abiodun0

View GitHub Profile
@abiodun0
abiodun0 / debounceFunctional.js
Last active February 9, 2017 15:02
debounce techniques
const EPOCH_START_DATE = new Date(0)
const debounce = (fn, delay) => {
let lastCallDate = EPOCH_START_DATE
return (...args) => {
let curDate = new Date()
const shouldSkip = new Date() - lastCallDate < delay
if (shouldSkip) {
return
}
lastCallDate = curDate
function fuzzysearch (needle, haystack) {
var hlen = haystack.length
var nlen = needle.length
if (nlen > hlen) {
return false
}
if (nlen === hlen) {
return needle === haystack
}
outer: for (var i = 0, j = 0; i < nlen; i++) {
@abiodun0
abiodun0 / asyncActions.js
Created February 26, 2017 22:02
using observables with reactive-stream
import { isObservable } from './utils';
// Action creator
const actionCreator = (func) => (...args) => {
const action = func.call(null, ...args);
action$.next(action);
if (isObservable(action.payload))
action$.next(action.payload);
return action;
};
@abiodun0
abiodun0 / simpleUpsert.js
Last active April 10, 2017 22:18
A simple idiomatic way of upserting and replace
function upsert(ary, pred, val) {
if (ary.some(item => pred(val, item))) {
return ary.map(item => (pred(val, item) ? val : item))
}
return ary.concat(val);
}
// greedy replace as it replaces all matches of predicate
const replace = (predicate, value, collection) => {
return collection.map(item => predicate(item, value) ? value : item)
}
@abiodun0
abiodun0 / classical.js
Last active October 28, 2019 12:30
Observable, Andre Stalltz
console.clear();
/**
* A contrived data source to use in our "observable"
* NOTE: this will clearly never error
*/
class DataSource {
constructor() {
let i = 0;
this._id = setInterval(() => this.emit(i++), 200);
@abiodun0
abiodun0 / whenComponent.js
Created April 22, 2017 04:10
Nifty Component Conditionals
const when = (val, whenTrue) => { if (val) { return whenTrue() } else { return null } }
{when(thingIsTrue, () => <Component />)}
@abiodun0
abiodun0 / diFunctional.js
Last active April 22, 2017 23:21
DI functional
exports.myService = {
f: function (x, y) { someServiceImpl.doThing(x, y); }
};
// functional DI
exports.myservice = function(someServicImpl) {
return {
f: function (x, y) { someServiceImpl.doThing(x, y);
}
}
@abiodun0
abiodun0 / noncritcss.md
Created April 25, 2017 14:59 — forked from scottjehl/noncritcss.md
Comparing two ways to load non-critical CSS

I wanted to figure out the fastest way to load non-critical CSS so that the impact on initial page drawing is minimal.

TL;DR: Here's the solution I ended up with: https://github.com/filamentgroup/loadCSS/


For async JavaScript file requests, we have the async attribute to make this easy, but CSS file requests have no similar standard mechanism (at least, none that will still apply the CSS after loading - here are some async CSS loading conditions that do apply when CSS is inapplicable to media: https://gist.github.com/igrigorik/2935269#file-notes-md ).

Seems there are a couple ways to load and apply a CSS file in a non-blocking manner:

@abiodun0
abiodun0 / abstractions.js
Created April 27, 2017 23:09
simple abstractions over dom
const el = document.createElement.bind(document)
const text = document.createTextNode.bind(document)
const append = (parent, ...children) => {
children.forEach(c => parent.appendChild(c))
return parent
}
@abiodun0
abiodun0 / unsafetap.js
Last active May 3, 2017 23:21
a smart console.log?
// Hide the freaking side effects
const unsafeTap = f => x => (f(x), x);
const log = unsafeTap(console.log.bind(console));