Browse Source

Default behaviour for `typedAction` if no `fn`.

Herbert Vojčík 6 years ago
parent
commit
3891358375
3 changed files with 10 additions and 3 deletions
  1. 4 1
      README.md
  2. 3 1
      lib/typed-action.js
  3. 3 1
      src/typed-action.js

+ 4 - 1
README.md

@@ -191,7 +191,7 @@ using `[...obj]`, not using `{...obj}`.
 That way you can create eg. `const c = cowValueObject("person", 34, "name")`
 to access `obj.person[34].name` with `c(obj)` / `c(obj, val)`.
 
-### `typedAction(type, fn)`
+### `typedAction(type, [fn])`
 
 This creates an action creator function that amends existing `fn` by:
   - adding a `type` property to the result, as well as
@@ -213,6 +213,9 @@ since `answerQuestion.TYPE === "answer question"`.
 IOW, this removes the two-space problem of having `const FOO_BAR_TYPE`
 as well as `fooBar` action creator.
 
+
+In case you don't pass the `fn` argument, the action creator only creates `{type}`.
+
 ### `cowWorkshop(keys, fn = x => x)(obj, [options])`
 
 This is multipurpose enumerate-and-act function to manipulate objects

+ 3 - 1
lib/typed-action.js

@@ -7,8 +7,10 @@ Object.defineProperty(exports, "__esModule", {
 var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
 
 var typedAction = exports.typedAction = function typedAction(type, fn) {
-    var result = function result() {
+    var result = fn ? function () {
         return _extends({}, fn.apply(undefined, arguments), { type: type });
+    } : function () {
+        return { type: type };
     };
     result.TYPE = type;
     return result;

+ 3 - 1
src/typed-action.js

@@ -1,5 +1,7 @@
 export const typedAction = (type, fn) => {
-    const result = (...args) => ({...fn(...args), type: type});
+    const result = fn ?
+        (...args) => ({...fn(...args), type}) :
+        () => ({type});
     result.TYPE = type;
     return result;
 };