Skip to content

Instantly share code, notes, and snippets.

@carlesba
carlesba / passby.js
Last active Jan 5, 2018
passby: look into a pipe
View passby.js
const passBy = by => pass => {
by(pass)
return pass
}
/*
Example:
///////////////////
const log = console.log
const get = (attribute, target) => target[attribute]
@carlesba
carlesba / path.js
Created Sep 17, 2017
Paths for functional pipes
View path.js
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,
View create-reducer.js
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 Feb 10, 2017
Chaining add function
View chain-add.js
/*
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 Jan 13, 2017
Util for testing thunks from redux-thunk using expect
View mock-thunk.js
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 Jul 17, 2016
Reusable Components
View Block.js
/*
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 Mar 16, 2016
Compose functions
View compose.js
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 Mar 13, 2016
Immutable builder
View ImmutableArray.js
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 Feb 27, 2016
SASS Object Beside to create tooltips/dropdowns
View _object.beside.sass
/*
<div class="o-beside**">
<div class="o-beside__wrapper">
<div>content</div>
</div>
</div>
*/
.o-beside
position: absolute
height: 0
@carlesba
carlesba / index.js
Created Jan 12, 2016
Basic RxJS with Flux example
View index.js
// Store
import AppDispatcher from './dispatcher/AppDispatcher'
const list$ = Rx.Subject.startWith([])
function addElement (action) {
const newElement = action.data
const currentValue = list$.getValue()
list$.onNext(currentValue.concat(newElement))
}