From 02426fb383cb8dbe6ac8d407f55f20dd0f504c7e Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Mon, 9 Dec 2019 22:59:04 -0800 Subject: [PATCH] add state flow type to state reducer --- .flowconfig | 5 ++++ src/reducers/stateReducer.js | 51 ++++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/.flowconfig b/.flowconfig index 1fed445..ce05603 100644 --- a/.flowconfig +++ b/.flowconfig @@ -1,4 +1,8 @@ [ignore] +.*/node_modules/.* +.*/src/serviceWorker\.js +.*/src/index\.js +.*\.test [include] @@ -7,5 +11,6 @@ [lints] [options] +; all=true [strict] diff --git a/src/reducers/stateReducer.js b/src/reducers/stateReducer.js index c530d2c..e03273b 100644 --- a/src/reducers/stateReducer.js +++ b/src/reducers/stateReducer.js @@ -1,4 +1,24 @@ -const addPhones = (phones, phone) => { +// @flow +type stateType = { + lexicon: Array, + epochs: Array}>, + phones: {[key: string]: phoneType}, + options: {}, + results: {}, + errors: {}, + features: featureType +} + +type phoneType = { + grapheme: string, + features: {[key: string]: boolean} +} + +type featureType = { + [key: string]: {[key: string]: Array} +} + +const addPhones = (phones: {}, phone: string): {} => { let node = {}; phone.split('').forEach((graph, index) => { if (index) node[graph] = {} @@ -9,24 +29,27 @@ const addPhones = (phones, phone) => { return phones; } -const findPhone = (phones, phone) => { - let node; +const findPhone = (phones: {}, phone: string): {} => { + let node = {}; phone.split('').forEach((graph, index) => { node = index === 0 ? phones[graph] : node[graph]; }); return node; } -const addFeatureToPhone = (phones, phone, featureKey, featureValue) => { - let node = {} - 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; +const addFeatureToPhone = ( + phones: {}, phone: string, featureKey: string, featureValue: boolean + ): {} => + { + let node = {} + 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; } -const findFeatures = (phones, lexeme) => { +const findFeatures = (phones: {}, lexeme:string): [] => { let featureBundle = [] let lastIndex = lexeme.length - 1; let node = {}; @@ -45,14 +68,14 @@ const findFeatures = (phones, lexeme) => { return featureBundle; } -const decomposeRule = rule => { +const decomposeRule = (rule: string): string[] => { let decomposedChange = rule.split('>'); decomposedChange = [decomposedChange[0], ...decomposedChange[1].split('/')] decomposedChange = [decomposedChange[0], decomposedChange[1], ...decomposedChange[2].split('_')]; return [...decomposedChange]; } -export const stateReducer = (state, action) => { +export const stateReducer = (state: stateType, action: {type: string, value: {}}) => { switch (action.type) { case 'INIT': { return initState(); @@ -154,7 +177,7 @@ export const stateReducer = (state, action) => { } } -export const initState = changesArgument => { +export const initState = (changesArgument: number = -1): stateType => { const state = { lexicon: [ 'anta', 'anat', 'anət', 'anna', 'tan', 'ənta'