add positive phones from add feature to state
This commit is contained in:
parent
afea8906e1
commit
c8c8474157
2 changed files with 56 additions and 1 deletions
|
@ -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) => {
|
const stateReducer = (state, action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case 'INIT': {
|
case 'INIT': {
|
||||||
|
@ -26,7 +42,18 @@ const stateReducer = (state, action) => {
|
||||||
|
|
||||||
case 'ADD_FEATURE': {
|
case 'ADD_FEATURE': {
|
||||||
let newFeature = action.value.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:
|
default:
|
||||||
|
|
28
src/reducers/stateReducer.phones.test.js
Normal file
28
src/reducers/stateReducer.phones.test.js
Normal 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}}}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in a new issue