Skip to content

Commit

Permalink
fix(create|handle-action): remove lodash requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
BerkeleyTrue committed Aug 13, 2021
1 parent 7cdd1ab commit 19d5d2a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/create-action.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import _ from 'lodash';
import invariant from 'invariant';

import type { Action } from './types';
import toString from './utils/toString';

const identity = (x: any) => x;

export default function createAction<Payload, Meta>(
_type: string,
payloadCreator: (...any: any) => Payload = _.identity,
payloadCreator: (...any: any) => Payload = identity,
metaCreator?: (...any: any) => Meta,
): (...arg: any) => Action {
invariant(_type, 'type cannot be undefined or null');
invariant(
_.isFunction(payloadCreator) || _.isNull(payloadCreator),
typeof payloadCreator === 'function' || payloadCreator === null,
'Expected payloadCreator to be a function, undefined or null',
);

const finalPayloadCreator =
_.isNull(payloadCreator) || payloadCreator === _.identity
? _.identity
payloadCreator === null || payloadCreator === identity
? identity
: (head: any, ...args: any[]) =>
head instanceof Error ? head : payloadCreator(head, ...args);

const hasMeta = _.isFunction(metaCreator);
const type = _type.toString();
const hasMeta = typeof metaCreator === 'function';
const type = toString(_type);

const actionCreator = (...args: any[]) => {
const payload = (finalPayloadCreator as typeof payloadCreator)(...args);
Expand Down
6 changes: 3 additions & 3 deletions src/handle-actions.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import _ from 'lodash';
import invariant from 'invariant';

import type { Action, Reducer, Handlers } from './types';
import handleAction from './handle-action';
import isPlainObject from './utils/isPlainObject';

function createReducers<S>(
handlers: Handlers,
defaultState: S,
): Array<Reducer> {
return Object.keys(handlers).map((type) => {
invariant(
_.isFunction(handlers[type]),
typeof handlers[type] === 'function',
'handleActions expects a function for each key but found %s for %s',
handlers[type],
type,
Expand All @@ -25,7 +25,7 @@ export default function handleActions<S>(
defaultState: S,
): Reducer {
invariant(
_.isPlainObject(handlers),
isPlainObject(handlers),
'handleActions expects an object for handlers but found %s',
handlers,
);
Expand Down

0 comments on commit 19d5d2a

Please sign in to comment.