Fimp helps you work with Immutable.js data structures in a more functional way. Think of it as Ramda or lodash/fp for Immutable.
Fimp provides composable, curried, data-last functions that work with immutable objects. If you are used to functional programming you are hopefully thinking "Nice, this makes sense, I can use this". If not, you might be thinking "Why would I want this?".
The aim of Fimp is to help you write cleaner, more understandable code. Composable functions makes it easier to create more complex functions by combining smaller building blocks. this article, explaining the rationale behind Ramda applies to Fimp as well. The only difference is that Fimp is built for use with Immutable data structures, whereas Ramda is used with plain javascript object.
The following functions are supported:
const add5 = map((a) => a + 5)
const filterEven = filter((a) => !(a % 2))
const sum = reduce((total, a) => a + total, 0)
const addFilterAndSum = compose(sum, filterEven, add5)
addFilterAndSum(fromJS([1, 2, 3]) // 14
const concat456 = concat(fromJS([4, 5, 6]))
concat456(fromJS([1, 2, 3])).toJS() // [1, 2, 3, 4, 5, 6]
const containsA = contains('a')
containsA(fromJS(['a', 'b', 'c'])) // true
const subtract = curry((a, b) => b - a)
const subtract2 = subtract(2)
subtract2(3) // 1
subtract(2, 3) // 1
entrySeq(fromJS({a: 'foo', b: 'bar', c: 'baz'})).toJS()
// [['a', 'foo'], ['b', 'bar'], ['c', 'baz']]
const allEven = every(isEven)
allEven(fromJS([1, 4, 8])) // false
allEven(fromJS([2, 4, 100])) // true
const filterEven = filter(isEven)
filterEven(fromJS([1, 2, 3, 4, 5, 6, 7, 8])).toJS() // [2, 4, 6, 8]
const findEven = find(isEven)
findEven(fromJS([1, 4, 3])) // 4
const shallowFlatten = flatten(true)
shallowFlatten(fromJS([1, 2, [3, 4, [5, 6]]])) // List [1, 2, 3, 4, [5, 6]]
})
const deeperFlatten = flatten(2)
deeperFlatten(fromJS([1, 2, [3, 4, [5, [6]]]])) // List [1, 2, 3, 4, 5, [6]]
})
const deepFlatten = flatten(false)
deepFlatten(fromJS([1, 2, [3, 4, [5, [6]]]])) //[1, 2, 3, 4, 5, 6])
const user = fromJS({
id: 1,
address: {
street: 'Simborgargatan 12',
},
})
const getAddress = get(['address'])
getAddress(user) // Map {street: 'Simborgargatan 12'}
keySeq(fromJS({a: 'foo', b: 'bar', c: 'baz'})).toJS() //['a', 'b', 'c']
const add5 = map((a) => a + 5)
add5(fromJS([1, 2, 3])) // List [6, 7, 8]
const sum = reduce((a, b) => a + b, 0)
sum(fromJS([1, 2, 3, 4, 5, 6])) // 21
reverse(fromJS([1, 2, 3, 4, 5])).toJS() //[5, 4, 3, 2, 1]
const user = fromJS({
id: 1,
address: {
street: 'Simborgargatan 12',
},
})
const setStreet = set(['address', 'street']) // Map user
const skip3 = skip(3)
skip3(fromJS([1, 2, 3, 4, 5])).toJS() // [4, 5]
const skipLast3 = skipLast(3)
skipLast3(fromJS([1, 2, 3, 4, 5])).toJS() // [1, 2]
const someEven = some(isEven)
someEven(fromJS([1, 4, 3])) // true
const sortHighest = sort((a, b) => b - a)
sortHighest(fromJS([100, 20, 3, 44, 5])).toJS() // [100, 44, 20, 5, 3]
const take3 = take(3)
take3(fromJS([1, 2, 3, 4, 5])).toJS() // [1, 2, 3]
const takeLast3 = takeLast(3)
takeLast3(fromJS([1, 2, 3, 4, 5])).toJS() // [3, 4, 5]
valueSeq(fromJS({a: 'foo', b: 'bar', c: 'baz'})).toJS() // ['foo', 'bar', 'baz']