Skip to content

Instantly share code, notes, and snippets.

@carlesba
carlesba / useListener.js
Last active January 6, 2022 16:58
A hook to prevent useCallback in handlers
import EventEmitter from "events";
import { useCallback, useEffect, useRef } from "react";
export default function useListener(listener = () => {}) {
const emitter = useRef(new EventEmitter());
useEffect(() => {
const currentEmitter = emitter.current;
currentEmitter.on("event", listener);
return () => {
@carlesba
carlesba / passby.js
Last active January 5, 2018 18:30
passby: look into a pipe
const passBy = by => pass => {
by(pass)
return pass
}
/*
Example:
///////////////////
const log = console.log
const get = (attribute, target) => target[attribute]
@carlesba
carlesba / path.js
Created September 17, 2017 11:09
Paths for functional pipes
import {
curryN,
update,
nth
} from 'ramda'
// Getting an array, modify just one its elements allowing different pipes in a data structure
// Number -> Function -> Array -> Array
const path = curryN(
3,
@carlesba
carlesba / create-reducer.js
Last active February 11, 2017 20:03
Create Reducer
const createReducer = (initialState, actionMap) => (state = initialState, action) => {
const {signals} = action
if (signals) {
const signal = signals.find(
(stepAction) => actionMap[stepAction.type]
)
if (signal) {
const reducer = actionMap[signal.type]
return reducer(state, signal)
} else {
@carlesba
carlesba / chain-add.js
Created February 10, 2017 10:33
Chaining add function
/*
Returns a valuable function:
x = add(2)(4) // x === 6
y = x(1)(2) // x === 6 && y === 9
z = y(10) // z === 19
*/
function add (n) {
var fn = function (m) {
return add(n+m);
@carlesba
carlesba / mock-thunk.js
Last active January 13, 2017 11:03
Util for testing thunks from redux-thunk using expect
import expect from 'expect' // using expect by @mjackson
export default (state) => {
const spy = expect.createSpy()
const getState = () => state
const dispatch = (action) => typeof action === 'function'
? action(dispatch, getState)
: spy(action)
return {spy, getState, dispatch}
}
@carlesba
carlesba / Block.js
Last active July 17, 2016 21:28
Reusable Components
/*
Block
--
Propagate any prop to the children but allowing className extension
Needs `babel-plugin-transform-object-rest-spread`
*/
const Block = ({classes, className, ...rest}) =>
<div {...rest} className={`${[classes, className].join(' ')}`} />
@carlesba
carlesba / compose.js
Last active March 16, 2016 12:07
Compose functions
const compose = (...fns) => (...args) => fns.reverse().reduce((acc, fn, index) => {
if (index) return fn(acc)
else return fn.apply(this, acc)
}, args)
@carlesba
carlesba / ImmutableArray.js
Last active November 18, 2022 13:23
Immutable builder
function ImmutableArray (arr) {
arr.forEach((key, index) => {
Object.defineProperty(this, index, {
enumberable: true,
get: () => key,
set: () => {
throw new Error('cannot be mutated')
}
})
})
@carlesba
carlesba / _object.beside.sass
Last active February 27, 2016 13:57
SASS Object Beside to create tooltips/dropdowns
/*
<div class="o-beside**">
<div class="o-beside__wrapper">
<div>content</div>
</div>
</div>
*/
.o-beside
position: absolute
height: 0