Skip to content

Instantly share code, notes, and snippets.

@ca057
ca057 / keybase.md
Created August 3, 2016 21:26
profile proof for keybase.io

Keybase proof

I hereby claim:

  • I am ca057 on github.
  • I am christianost (https://keybase.io/christianost) on keybase.
  • I have a public key ASBmdr_2SLX6UID5Ht5-KM71NKvqJigG7uNzNnLvzxMEngo

To claim this, I am signing this object:

@ca057
ca057 / DebugOutput.js
Last active March 28, 2017 16:38
Simple component which renders all keys and values of an object as debug output, inspired by @Andruschenko
import React, { PropTypes } from 'react';
import {
StyleSheet,
View,
Text,
} from 'react-native';
const styles = StyleSheet.create({
textContainer: {
flexDirection: 'row',
// @flow
import { flip, concat } from 'ramda';
type AppendString =
| ((end: string) => (base: string) => string)
| ((end: string, base: string) => string);
/**
* Appends a string to another string, can be used curried.
*
* @example
// @flow
import { compose, objOf, mergeAll, map, view, lensProp } from 'ramda';
type MergeByKey = (key: string) => (Array<Object>) => Object;
/**
* Provided a key, this function returns a function which merges an array of nested objects.
*
* @example
* // Given two objects:
* const data1 = { key: { subStructure1: value1 } };
@ca057
ca057 / assignIds.js
Last active January 30, 2018 07:03
Assign __uuid-property to every object ({}) of a given collection.
import { v4 } from 'uuid';
import { map, is } from 'ramda';
const isObject = is(Object);
const isDate = is(Date);
const isRegExp = is(RegExp);
export const assignIds = data => {
if (Array.isArray(data)) {
return data.map(assignIds);
@ca057
ca057 / duplicates.js
Last active February 4, 2018 22:13
Find duplicates inside an array.
const duplicates = input => {
if (!Array.isArray(input)) return [];
const workingCopy = [...input];
const duplicatesSet = new Set();
// length of 1: no duplicate or already found
while (workingCopy.length > 1) {
const element = workingCopy.shift();
if (workingCopy.includes(element)) {
@ca057
ca057 / retryAsync.js
Created February 8, 2018 15:02
Asynchronously retry a given task until it resolves or the maximum amount of retries is reached
/**
* Asynchronously retry a given task the given times, but as least as long as the task does not
* throw an error or returns the first time. The return value is a promise with the value
* of the rejected/resolved task.
*/
export const retryAsync = (times, task) => {
if (!task || typeof task !== 'function' || times < 0) {
throw new TypeError(
'No task passed or task is not of type function or times is a negative value.'
);
@ca057
ca057 / expressRequestDurationMiddleware.js
Last active March 29, 2018 13:47
Middleware to measure request duration for express using the Performance Timing API.
const { performance } = require('perf_hooks');
const express = require('express');
const expressRequestDurationMiddleware = (req, res, next) => {
performance.mark('request.start');
const end = res.end;
res.end = (...args) => {
performance.mark('request.end');
@ca057
ca057 / condResolveEmptyList.js
Created April 3, 2018 07:09
Apollo HOF to conditionally resolve with an empty list
/**
* Curried higher-order function taking a predicate and an actual resolver function. In case the
* predicate returns true, an empty list is returned, otherwise the result of the actual resolver function.
*
* Both the predicate and the resolver function will get the all arguments as a standard resolver function.
*/
const condResolveEmptyList = predicate => resolver => (
data,
args,
context,
@ca057
ca057 / withClearStateOnAction.ts
Created September 8, 2020 08:57
higher-order reducer to clear redux state on specific action
import { combineReducers, Action } from '@reduxjs/toolkit';
const withClearStateOnAction = <S = any>(clearStateAction: Action) => (
reducer: Reducer<S | undefined, Action>,
) => (state: S, action: Action) => {
if (clearStateAction.type === action.type) {
return reducer(undefined, clearStateAction);
}
return reducer(state, action);
};