Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
"precompile": "rm -rf lib",
"compile": "tsc --outDir lib",
"compile:watch": "yarn compile -- --watch",
"lint": "eslint src --config eslintrc.json",
"lint": "yarn lint:js && yarn lint:ts",
"lint:js": "eslint src --config eslintrc.json",
"lint:ts": "tslint src/**/*.ts --config tslint.json",
"prerelease": "git fetch --tags && yarn validate-dependencies && yarn validate-commits && yarn build",
"release": "git add dist lib && standard-version -a",
"test": "jest --config jest-config.js",
Expand All @@ -44,6 +46,8 @@
},
"dependencies": {
"@bigcommerce/request-sender": "git+ssh://git@github.com/bigcommerce/request-sender-js.git#0.1.0",
"@types/jest": "^21.1.10",
"@types/lodash": "^4.14.92",
"bigpay-client": "git+ssh://git@github.com/bigcommerce-labs/bigpay-client-js.git#2.9.1",
"form-poster": "git+ssh://git@github.com/bigcommerce-labs/form-poster-js.git#1.1.1",
"lodash": "^4.17.4",
Expand All @@ -60,6 +64,7 @@
"standard-version": "^4.2.0",
"ts-jest": "^21.2.3",
"ts-loader": "^3.2.0",
"tslint": "^5.9.1",
"typescript": "^2.6.2",
"typescript-eslint-parser": "^9.0.1",
"uglifyjs-webpack-plugin": "^1.1.1",
Expand Down
6 changes: 6 additions & 0 deletions src/data-store/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default interface Action<TPayload = any, TMeta = any> {
type: string;
error?: boolean;
meta?: TMeta;
payload?: TPayload;
}
7 changes: 0 additions & 7 deletions src/data-store/action.typedef.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/data-store/combine-reducers.js

This file was deleted.

23 changes: 23 additions & 0 deletions src/data-store/combine-reducers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Action from './action';
import Reducer from './reducer';

export default function combineReducers<TState, TAction extends Action>(
reducers: ReducerMap<TState, TAction>
): Reducer<TState, TAction> {
return (state, action) =>
Object.keys(reducers).reduce((result: TState, key) => {
const reducer = reducers[key as keyof TState];
const currentState = state ? state[key as keyof TState] : undefined;
const newState = reducer(currentState, action);

if (currentState === newState && result) {
return result;
}

return Object.assign({}, result, { [key]: newState });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come we're no longer using object spread here anymore? Is does TS not play well with spread?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently there's an ongoing issue with the use of the spread operator with generics. microsoft/TypeScript#13557

}, state);
}

export type ReducerMap<TState, TAction extends Action> = {
[Key in keyof TState]: Reducer<TState[Key], TAction>;
};
12 changes: 0 additions & 12 deletions src/data-store/compose-reducers.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import composeReducers from './compose-reducers';

describe('composeReducers()', () => {
const fooReducer = (state, action) => {
const fooReducer = (state = '', action) => {
switch (action.type) {
case 'FOO':
return 'foo';
Expand All @@ -14,7 +14,7 @@ describe('composeReducers()', () => {
}
};

const barReducer = (state, action) => {
const barReducer = (state = '', action) => {
switch (action.type) {
case 'BAR':
return 'bar';
Expand Down
13 changes: 13 additions & 0 deletions src/data-store/compose-reducers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { curryRight, flowRight } from 'lodash';
import Action from './action';
import Reducer from './reducer';

export default function composeReducers<TState, TAction extends Action>(
...reducers: Array<Reducer<Partial<TState>, TAction>>
): Reducer<TState, TAction> {
return (state, action) =>
flowRight.apply(
null,
reducers.map(reducer => curryRight(reducer)(action))
)(state);
}
19 changes: 0 additions & 19 deletions src/data-store/create-action.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('createAction()', () => {
expect(action).toEqual({ type: 'ACTION' });
});

it('throws an error if `type` is not provided', () => {
expect(() => createAction()).toThrow();
it('throws an error if `type` is not provided or empty', () => {
expect(() => createAction('')).toThrow();
});
});
14 changes: 14 additions & 0 deletions src/data-store/create-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { omitBy } from 'lodash';
import Action from './action';

export default function createAction<TPayload, TMeta>(
type: string,
payload?: TPayload,
meta?: TMeta
): Action<TPayload, TMeta> {
if (typeof type !== 'string' || type === '') {
throw new Error('`type` must be a string');
}

return { type, ...omitBy({ payload, meta }, value => value === undefined) };
}
16 changes: 0 additions & 16 deletions src/data-store/create-data-store.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/data-store/create-data-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Action from './action';
import combineReducers, { ReducerMap } from './combine-reducers';
import DataStore, { DataStoreOptions } from './data-store';
import Reducer from './reducer';

export default function createDataStore<TState, TAction extends Action, TTransformedState = TState>(
reducer: Reducer<Partial<TState>, TAction> | ReducerMap<Partial<TState>, TAction>,
initialState: Partial<TState>,
options: DataStoreOptions<TState, TAction, TTransformedState>
): DataStore<TState, TAction, TTransformedState> {
if (typeof reducer === 'function') {
return new DataStore(reducer, initialState, options);
}

return new DataStore(combineReducers(reducer), initialState, options);
}
14 changes: 0 additions & 14 deletions src/data-store/create-error-action.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('createErrorAction()', () => {
expect(action).toEqual({ type: 'ACTION', error: true });
});

it('throws an error if `type` is not provided', () => {
expect(() => createErrorAction()).toThrow();
it('throws an error if `type` is not provided or empty', () => {
expect(() => createErrorAction('')).toThrow();
});
});
13 changes: 13 additions & 0 deletions src/data-store/create-error-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Action from './action';
import createAction from './create-action';

export default function createErrorAction<TPayload, TMeta>(
type: string,
payload?: TPayload,
meta?: TMeta
): Action<TPayload, TMeta> {
return {
...createAction(type, payload, meta),
error: true,
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Observable } from 'rxjs';
import Action from './action';
import DataStore from './data-store';

describe('DataStore', () => {
Expand Down Expand Up @@ -31,7 +32,7 @@ describe('DataStore', () => {

it('dispatches error actions and rejects with payload', async () => {
const store = new DataStore(state => state);
const action = { error: true, payload: 'foobar' };
const action = { type: 'FOOBAR', error: true, payload: 'foobar' };

try {
await store.dispatch(action);
Expand All @@ -52,7 +53,7 @@ describe('DataStore', () => {
expect(await store.dispatch(Observable.of(
{ type: 'APPEND', payload: 'foo' },
{ type: 'APPEND', payload: 'bar' },
{ type: 'APPEND', payload: '!!!' },
{ type: 'APPEND', payload: '!!!' }
))).toEqual({ message: 'foobar!!!' });
});

Expand Down Expand Up @@ -156,8 +157,8 @@ describe('DataStore', () => {
const store = new DataStore(reducer);

reducer.mockClear();
store.dispatch({});
store.dispatch({ payload: 'foobar' });
store.dispatch({ type: '' });
store.dispatch({ type: '', payload: 'foobar' });

expect(reducer).not.toHaveBeenCalled();
});
Expand All @@ -167,7 +168,7 @@ describe('DataStore', () => {
const store = new DataStore(reducer);

reducer.mockClear();
store.dispatch(Observable.of({}, { payload: 'foobar' }));
store.dispatch(Observable.of({ type: '', payload: 'foo' }, { type: '', payload: 'bar' }));

expect(reducer).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -290,7 +291,7 @@ describe('DataStore', () => {
default:
return state;
}
});
}, { foo: '', bar: '' });
const subscriber = jest.fn();

store.subscribe(subscriber, (state) => state.foo);
Expand Down Expand Up @@ -323,7 +324,7 @@ describe('DataStore', () => {
default:
return state;
}
});
}, { foo: '', bar: '', foobar: '' });
const subscriber = jest.fn();

store.subscribe(
Expand Down
Loading