From 741af7f76e9ffabce56dcc27d24fc41865811e10 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Wed, 4 Dec 2019 19:33:17 -0800 Subject: [PATCH] add support for mulitgraph positive and negative phones --- src/reducers/stateReducer.js | 20 ++++++----- src/reducers/stateReducer.phones.test.js | 43 +++++++++++++++++++++++- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/reducers/stateReducer.js b/src/reducers/stateReducer.js index a9429bf..80888ee 100644 --- a/src/reducers/stateReducer.js +++ b/src/reducers/stateReducer.js @@ -5,17 +5,20 @@ const initState = () => { } const addPhones = (phones, phone) => { + let node = {} phone.split('').forEach((graph, index) => { - if (!phones[graph]) phones[graph] = {} + if (index) node[graph] = {} + if (!index && !phones[graph]) phones[graph] = {} + node = index === 0 ? phones[graph] : node[graph]; }) return phones; } -const addPositiveFeature = (phones, positivePhone, feature) => { +const addFeatureToPhone = (phones, phone, featureKey, featureValue) => { 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} + phone.split('').forEach((graph, index) => { + node = index === 0 ? phones[graph] : node[graph]; + if (index === phone.split('').length - 1) node.features = {...node.features, [featureKey]: featureValue} }) return phones; } @@ -48,11 +51,12 @@ const stateReducer = (state, action) => { let newPhoneObject = [ ...positivePhones, ...negativePhones ].reduce((phoneObject, phone) => addPhones(phoneObject, phone), state.phones) - if (positivePhones) positivePhones = positivePhones.reduce( - (phoneObject, positivePhone) => addPositiveFeature(phoneObject, positivePhone, newFeature) + (phoneObject, positivePhone) => addFeatureToPhone(phoneObject, positivePhone, newFeature, true) + , newPhoneObject); + if (negativePhones) negativePhones = negativePhones.reduce( + (phoneObject, positivePhone) => addFeatureToPhone(phoneObject, positivePhone, newFeature, false) , newPhoneObject); - return {...state, features:[...state.features, newFeature], phones: newPhoneObject} } diff --git a/src/reducers/stateReducer.phones.test.js b/src/reducers/stateReducer.phones.test.js index 76c1ec6..42990ec 100644 --- a/src/reducers/stateReducer.phones.test.js +++ b/src/reducers/stateReducer.phones.test.js @@ -9,7 +9,11 @@ describe('Phones', () => { phones: { n: n_phone } }; - + beforeEach(()=> { + state.features = [ 'nasal' ]; + state.phones= { n: n_phone }; + }) + it('phones returned unaltered', () => { const action = {type: ''}; expect(stateReducer(state, action)).toBe(state); @@ -25,4 +29,41 @@ describe('Phones', () => { ) }) + it('feature addition returns new feature list with negative phones update', () => { + const action = {type: 'ADD_FEATURE', value: {feature: 'sonorant', negativePhones: ['t']}}; + expect(stateReducer(state, action)).toEqual( + {...state, + features:[...state.features, action.value.feature], + phones:{...state.phones, t:{features:{sonorant: false}}} + } + ); + }); + + it('feature addition returns new feature list with positive and negative phones update', () => { + const action = {type: 'ADD_FEATURE', value: {feature: 'sonorant', positivePhones: ['n'], negativePhones: ['t']}}; + expect(stateReducer(state, action)).toEqual( + {...state, + features:[...state.features, action.value.feature], + phones:{...state.phones, + t:{features:{sonorant: false}}, + n:{...state.phones.n, features: {...state.phones.n.features, sonorant: true}} + } + } + ); + }); + + it('feature addition returns new feature list with multi-graph phones updated', () => { + const action = {type: 'ADD_FEATURE', value: {feature: 'aspirated', positivePhones: ['ntʰ'], negativePhones: ['n','t']}}; + expect(stateReducer(state, action)).toEqual( + {...state, + features:[...state.features, action.value.feature], + phones:{...state.phones, + t:{features:{aspirated: false}}, + n:{...state.phones.n, features: {...state.phones.n.features, aspirated: false}, + t: {ʰ:{features:{aspirated:true}}} + } + } + } + ); + }); }); \ No newline at end of file