Stop rewriting the constant name in each entry use a factory for this job
npm
npm install constant-factory --save-dev
yarn
yarn add constant-factory --dev
// normal
constant(CONSTANT_NAME<STRING>, CONSTANT_ENTRIES<ARRAY>)
// advanced
constant(CONSTANT_COLLETION<ARRAY>)
Before:
export const MY_CONSTANT = {
TODO: 'MY_CONSTANT_TODO',
ISSUES: 'MY_CONSTANT_ISSUES',
}
Now:
import c from 'constant-factory'
export const MY_CONSTANT = c('MY_CONSTANT', [
'TODO',
'ISSUES'
])
// returns
// {
// TODO: 'MY_CONSTANT_TODO',
// ISSUES: 'MY_CONSTANT_ISSUES'
// }
Advanced:
import c from 'constant-factory'
export const { MY_CONSTANT, ANOTHER_CONSTANT } = c({
'MY_CONSTANT': [
'TODO', 'ISSUES'
],
'ANOTHER_CONSTANT': [
'DATES'
]
})
// returns
// {
// MY_CONSTANT: {
// TODO: 'MY_CONSTANT_TODO',
// ISSUES: 'MY_CONSTANT_ISSUES'
// },
// ANOTHER_CONSTANT: {
// DATES: 'ANOTHER_CONSTANT_DATES'
// }
// }
Expert:
Add additional subs in subs
import c from 'constant-factory'
export const { MY_CONSTANT, ANOTHER_CONSTANT } = c({
'MY_CONSTANT': [
'TODO',
{
'STATUS': [
'DONE',
'OPEN',
]
}
],
})
// returns
// {
// MY_CONSTANT: {
// TODO: 'MY_CONSTANT_TODO',
// STATUS: {
// DONE: 'MY_CONSTANT_STATUS_DONE',
// OPEN: 'MY_CONSTANT_STATUS_OPEN'
// }
// }
// }