Last active
December 18, 2017 03:41
-
-
Save VitorLuizC/b21575d81c81692ebe44bfa0bc5f96eb to your computer and use it in GitHub Desktop.
Using composition on a Store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const unique = (values) => [ ...new Set(values) ] | |
export const add = (value, position = 'first') => (values) => { | |
return position === 'first' | |
? [ value, ...values ] | |
: [ ...values, value ] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default (...λs) => (value) => λs.reduce((value, λ) => λ(value), value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as collection from './collection' | |
import compose from './compose' | |
export const DEFAULT_OPTION = { | |
label: 'Todos', | |
value: undefined | |
} | |
export const toOption = (value) => ({ | |
value, | |
label: value | |
}) | |
export const toOptions = (default_option = DEFAULT_OPTION) => compose( | |
collection.unique, | |
(items) => items.map(toOption), | |
collection.add({ ...DEFAULT_OPTION, ...default_option }, 'first') | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import compose from './compose' | |
import { toOptions } from './options' | |
import * as types from './types' | |
export const state = { | |
addresses: [ | |
{ | |
city: 'São Paulo', | |
state: 'São Paulo' | |
}, | |
{ | |
city: 'Jundiaí', | |
state: 'São Paulo' | |
} | |
] | |
} | |
const getCities = compose( | |
(addresses) => addresses.map(address => address.city), | |
toOptions({ label: 'Todos' }) | |
) | |
const getStates = compose( | |
(addresses) => addresses.map(address => address.state), | |
toOptions({ label: 'Todos' }) | |
) | |
export const getters = { | |
[types.ADDRESSES_CITIES]: ({ addresses }) => getCities(addresses), | |
[types.ADDRESSES_STATES]: ({ addresses }) => getStates(addresses) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment