checking in docs for github.io page

This commit is contained in:
Ken
2019-02-19 23:41:11 -08:00
parent e88ba9c448
commit 164db9dd93
194 changed files with 103939 additions and 5 deletions

View File

@@ -0,0 +1,10 @@
import { reducer } from '../index';
describe('reducers', () => {
it('should add item to the list', () => {
const newStore = reducer({ todos: {}, filter: 'all' }, { type: 'add', label: 'hello' });
const keys = Object.keys(newStore.todos);
expect(keys.length).toBe(1);
expect(newStore.todos[keys[0]].label).toBe('hello');
});
});

View File

@@ -0,0 +1,10 @@
import { ActionTypes, TodoActionLookup, actions } from '../actions';
import { createGenericReducer, HandlerMap, ImmerReducer } from '../redux-utils/reducer';
import { Reducer } from 'redux';
export function createReducer<T, AM extends ActionTypes | never = never>(
initialState: T,
handlerOrMap: HandlerMap<T, typeof actions> | ImmerReducer<T, TodoActionLookup[AM]>
): Reducer<T> {
return createGenericReducer<T, typeof actions, AM>(initialState, handlerOrMap);
}

View File

@@ -0,0 +1,42 @@
import { createReducer } from './createReducer';
import { Store, FilterTypes } from '../store';
import { combineReducers } from 'redux';
export const reducer = combineReducers<Store>({
todos: createReducer<Store['todos']>(
{},
{
add(draft, action) {
draft[action.id] = { label: action.label, completed: false };
return draft;
},
remove(draft, action) {
delete draft[action.id];
return draft;
},
complete(draft, action) {
draft[action.id].completed = !draft[action.id].completed;
return draft;
},
clear(draft) {
Object.keys(draft).forEach(id => {
if (draft[id].completed) {
delete draft[id];
}
});
return draft;
},
edit(draft, action) {
draft[action.id].label = action.label;
return draft;
}
}
),
filter: createReducer<Store['filter'], 'setFilter'>('all', (draft, action) => {
return action.filter as FilterTypes;
})
});