add positive phones from add feature to state

This commit is contained in:
Sorrel Bri 2019-12-04 18:13:46 -08:00
parent afea8906e1
commit c8c8474157
2 changed files with 56 additions and 1 deletions

View file

@ -4,6 +4,22 @@ const initState = () => {
}
}
const addPhones = (phones, phone) => {
phone.split('').forEach((graph, index) => {
if (!phones[graph]) phones[graph] = {}
})
return phones;
}
const addPositiveFeature = (phones, positivePhone, feature) => {
let node = {}
positivePhone.split('').forEach((graph, index) => {
node = index === 0 ? node = phones[graph] : node = node[graph];
if (index === positivePhone.split('').length - 1) node.features = {...node.features, [feature]: true}
})
return phones;
}
const stateReducer = (state, action) => {
switch (action.type) {
case 'INIT': {
@ -26,7 +42,18 @@ const stateReducer = (state, action) => {
case 'ADD_FEATURE': {
let newFeature = action.value.feature;
return {...state, features:[...state.features, newFeature]}
let positivePhones = action.value.positivePhones || [];
let negativePhones = action.value.negativePhones || [];
let newPhoneObject = [
...positivePhones, ...negativePhones
].reduce((phoneObject, phone) => addPhones(phoneObject, phone), state.phones)
if (positivePhones) positivePhones = positivePhones.reduce(
(phoneObject, positivePhone) => addPositiveFeature(phoneObject, positivePhone, newFeature)
, newPhoneObject);
return {...state, features:[...state.features, newFeature], phones: newPhoneObject}
}
default:

View file

@ -0,0 +1,28 @@
import {stateReducer} from './stateReducer';
describe('Phones', () => {
const n_phone = {features: {nasal: true}}
const state = {
features: [
'nasal'
],
phones: { n: n_phone }
};
it('phones returned unaltered', () => {
const action = {type: ''};
expect(stateReducer(state, action)).toBe(state);
});
it('feature addition returns new feature list with positive phones updated', () => {
const action = {type: 'ADD_FEATURE', value: {feature: 'anterior', positivePhones: ['n']}};
expect(stateReducer(state, action)).toEqual(
{...state,
features:[...state.features, action.value.feature],
phones:{...state.phones, n:{...state.phones.n, features: {...state.phones.n.features, anterior: true}}}
}
)
})
});