Skip to content

Instantly share code, notes, and snippets.

View nygardk's full-sized avatar

Klaus Nygård nygardk

View GitHub Profile
import AppDispatcher from 'AppDispatcher';
import when from 'when';
function asyncDispatch(actionType, promise, actionObj = {}) {
AppDispatcher.handleViewAction({
type: actionType.LOADING
});
return promise.then(response => {
AppDispatcher.handleViewAction({...{
@nygardk
nygardk / resolveUrl.js
Last active October 22, 2015 08:07
Recursive url resolver
function resolveUrl() {
return [...arguments]
.map(function stripArgument(argument, index) {
if (argument[argument.length - 1] === '/') {
return stripArgument(argument.slice(0, argument.length - 1), index);
}
if (index !== 0 && argument[0] === '/') {
return stripArgument(argument.slice(1, argument.length), index);
}
@nygardk
nygardk / objectToGetParams.js
Last active December 14, 2015 11:29
Transform an object to a string of get parameters
function objectToGetParams(object) {
const params = Object.keys(object)
.filter(key => !!object[key])
.map(key => `${key}=${encodeURIComponent(object[key])}`)
.join('&');
return params.length ? '?' + params : '';
}
@nygardk
nygardk / BEMClassModifier.js
Last active November 24, 2015 12:36
Add BEM-style modifiers to block names
function BEMBlockModifier(blockName = '', modifiers = {}) {
return (blockName + ' ' + Object.keys(modifiers)
.map(key => !!modifiers[key] ? `${blockName}--${key}` : '')
.join(' '))
.replace(/\s\s+/g, ' ')
.trim();
}
@nygardk
nygardk / connectToStore.js
Last active December 14, 2015 10:43
Simple higher order function to connect a React component to a Flux store
function connectToStore(Store, mapStateToProps) {
const requiredStoreMethods = ["addChangeListener", "removeChangeListener"];
requiredStoreMethods.forEach(method => {
if (!Store.hasOwnProperty(method)) {
throw new Error(`Attempted to connect to a store without `
+ `${method} method`);
}
});
@nygardk
nygardk / connect.js
Created January 13, 2016 16:08
Connect a component to Flux stores and map state to props
import React from "react";
const defaultConfig = {
addChangeListener: "addChangeListener",
removeChangeListener: "removeChangeListener",
};
function validateStoreMappings(storeMappings, config) {
if (!storeMappings) {
throw new Error("No storeMappings provided.");
@nygardk
nygardk / pseudo-uid.js
Created March 14, 2016 08:40
A small utility to create uids for testing purposes and where collisions are not probable
function uid() {
return Math.random().toString(36).substr(2, 9);
}
function promiseRetrier(func, config = {}) {
const options = {
timeout: 1000,
...config,
};
let retries = 0;
return function retry(...args) {
return func(...args).then(res => res, err => {
function promiseCache(ttl, method) {
const updated = {};
const results = {};
return key => (...args) => {
const keyUpdated = updated[key];
if (keyUpdated === undefined || ((keyUpdated + ttl) <= Date.now()) || !results[key]) {
return method(...args).then(res => {
updated[key] = Date.now();
@nygardk
nygardk / flattenObj.js
Created March 21, 2017 10:22
Flatten object keys with a join character
const flattenObj = (obj, join = '.') => {
const toReturn = {};
for (const i in obj) {
if (!obj.hasOwnProperty(i)) {
continue;
}
if ((typeof obj[i]) === 'object') {
const flatObject = flattenObj(obj[i]);