refactor features reducer for cleanliness

This commit is contained in:
Sorrel Bri 2020-02-13 21:02:51 -08:00
parent 298c153f88
commit bcf79aa28c

View file

@ -12,32 +12,37 @@ export type featureAction = {
const addPhones = (phones: {}, phone: string): {} => { const addPhones = (phones: {}, phone: string): {} => {
let node = {}; let node = {};
phone.split('').forEach((graph, index) => { phone.split('').forEach((graph, index) => {
if (index) node[graph] = {} if (index) node[graph] = {}
if (!index && !phones[graph]) phones[graph] = {} if (!index && !phones[graph]) phones[graph] = {}
node = index === 0 ? phones[graph] : node[graph]; node = index === 0 ? phones[graph] : node[graph];
if (index === phone.length - 1) node.grapheme = phone; if (index === phone.length - 1) node.grapheme = phone;
}) })
return phones; return phones;
} }
const findPhone = (phones: {}, phone: string): {} => { const findPhone = (phones: {}, phone: string): {} => {
let node = {}; return phone
phone.split('').forEach((graph, index) => { .split('')
.reduce((node, graph, index) => {
node = index === 0 ? phones[graph] : node[graph]; node = index === 0 ? phones[graph] : node[graph];
});
return node; return node;
}, {});
} }
const addFeatureToPhone = ( const addFeatureToPhone = (
phones: {}, phone: string, featureKey: string, featureValue: boolean phones: {}, phone: string, featureKey: string, featureValue: boolean
): {} => ): {} => {
{
let node = {} let node = {}
phone.split('').forEach((graph, index) => { phone.split('').forEach((graph, index) => {
node = index === 0 ? phones[graph] : node[graph]; node = index === 0 ? phones[graph] : node[graph];
if (index === phone.split('').length - 1) node.features = {...node.features, [featureKey]: featureValue}
}) if (index === phone.split('').length - 1) {
node.features = {...node.features, [featureKey]: featureValue}
}
});
return phones; return phones;
} }
@ -47,7 +52,8 @@ export const addFeature = (state: stateType, action: featureAction): stateType =
let newFeatureName = action.value.feature; let newFeatureName = action.value.feature;
let newPhoneObject = [ let newPhoneObject = [
...positivePhones, ...negativePhones ...positivePhones, ...negativePhones
].reduce((phoneObject, phone) => addPhones(phoneObject, phone), state.phones) ]
.reduce((phoneObject, phone) => addPhones(phoneObject, phone), state.phones)
if (positivePhones) { if (positivePhones) {