-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype.js
More file actions
62 lines (52 loc) · 1.76 KB
/
type.js
File metadata and controls
62 lines (52 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { createStore, applyMiddleware } from 'redux';
import createSagaMiddleware from 'redux-saga';
import { composeWithDevTools } from 'redux-devtools-extension';
// Actions
const setTypingText = text => ({ type: 'SET_TYPING_TEXT', payload: text });
const setKeysPressed = keys => ({ type: 'SET_KEYS_PRESSED', payload: keys });
const calculateAccuracy = () => ({ type: 'CALCULATE_ACCURACY' });
// Reducer
const initialState = {
typing: {
text: '',
keysPressed: 0,
accuracy: 100,
},
};
const typingReducer = (state = initialState.typing, action) => {
switch (action.type) {
case 'SET_TYPING_TEXT':
return { ...state, text: action.payload };
case 'SET_KEYS_PRESSED':
return { ...state, keysPressed: action.payload };
case 'CALCULATE_ACCURACY':
const { text, keysPressed } = state;
const accuracy = calculateAccuracyPercentage(text, keysPressed);
return { ...state, accuracy };
default:
return state;
}
};
const rootReducer = combineReducers({
typing: typingReducer,
// Add other reducers if needed
});
// Sagas
function* typingSaga() {
yield takeLatest('SET_KEYS_PRESSED', calculateAccuracySaga);
}
function* calculateAccuracySaga() {
yield put({ type: 'CALCULATE_ACCURACY' });
}
// Utility function to calculate accuracy percentage
function calculateAccuracyPercentage(expected, actual) {
if (expected.length === 0) return 100;
const errors = levenshteinDistance(expected, actual);
const accuracy = ((expected.length - errors) / expected.length) * 100;
return accuracy.toFixed(2);
}
// Utility function to calculate Levenshtein distance
function levenshteinDistance(expected, actual) {
const m = expected.length;
const n = actual.length;
const dp = Array.from(Array(m + 1), () => Array(n + 1).fill(0