add state flow type to state reducer

This commit is contained in:
Sorrel Bri 2019-12-09 22:59:04 -08:00
parent d7f75bb018
commit 02426fb383
2 changed files with 42 additions and 14 deletions

View file

@ -1,4 +1,8 @@
[ignore]
.*/node_modules/.*
.*/src/serviceWorker\.js
.*/src/index\.js
.*\.test
[include]
@ -7,5 +11,6 @@
[lints]
[options]
; all=true
[strict]

View file

@ -1,4 +1,24 @@
const addPhones = (phones, phone) => {
// @flow
type stateType = {
lexicon: Array<?string>,
epochs: Array<?{name: string, changes: Array<string>}>,
phones: {[key: string]: phoneType},
options: {},
results: {},
errors: {},
features: featureType
}
type phoneType = {
grapheme: string,
features: {[key: string]: boolean}
}
type featureType = {
[key: string]: {[key: string]: Array<phoneType>}
}
const addPhones = (phones: {}, phone: string): {} => {
let node = {};
phone.split('').forEach((graph, index) => {
if (index) node[graph] = {}
@ -9,15 +29,18 @@ 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) => {
const addFeatureToPhone = (
phones: {}, phone: string, featureKey: string, featureValue: boolean
): {} =>
{
let node = {}
phone.split('').forEach((graph, index) => {
node = index === 0 ? phones[graph] : node[graph];
@ -26,7 +49,7 @@ const addFeatureToPhone = (phones, phone, 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'