UNPKG

2.43 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createActions = exports.createAction = exports.connect = void 0;
7var _fantasyLand = _interopRequireDefault(require("fantasy-land"));
8var _forceArray = _interopRequireDefault(require("force-array"));
9var _utils = require("./utils");
10function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11const rFANTASY = /^fantasy-land\//;
12const alias = c => Object.getOwnPropertyNames(c).filter(n => rFANTASY.test(n)).map(n => [n.replace(rFANTASY, ''), c[n]]);
13const aliasType = c => {
14 // biome-ignore lint/complexity/noForEach: <explanation>
15 alias(c).forEach(([alias, fn]) => {
16 c[alias] = fn;
17 });
18 // biome-ignore lint/complexity/noForEach: <explanation>
19 alias(c.prototype).forEach(([alias, fn]) => {
20 c.prototype[alias] = fn;
21 });
22 return c;
23};
24const createAction = name => {
25 // biome-ignore lint/style/noVar: <explanation>
26 var action = {
27 [name]: class {
28 constructor(value) {
29 this.__value = value;
30 this['@@type'] = name;
31 }
32 static [_fantasyLand.default.of](value) {
33 return new action[name](value);
34 }
35 static of(value) {
36 return new action[name](value);
37 }
38 static is(type) {
39 return type instanceof action[name];
40 }
41 [_fantasyLand.default.map](fn) {
42 return action[name].of(fn(this.join()));
43 }
44 [_fantasyLand.default.ap](m) {
45 return m.chain(fn => this.map(fn));
46 }
47 [_fantasyLand.default.chain](fn) {
48 return fn(this.join());
49 }
50 [_fantasyLand.default.equals](a) {
51 return a.join() === this.join();
52 }
53 lift(m) {
54 return m.map(this.__value);
55 }
56 join() {
57 return this.__value;
58 }
59 unwrapOrElse(_default) {
60 return typeof this.join() === 'undefined' ? _default : this.join();
61 }
62 }
63 };
64 return aliasType(action[name]);
65};
66exports.createAction = createAction;
67const createActions = actions => (0, _forceArray.default)(actions).filter(_utils.isString).map(createAction);
68exports.createActions = createActions;
69const connect = signal => actions => createActions(actions).map(Action => {
70 Action.of = value => {
71 const action = new Action(value);
72 signal.emit(action);
73 return action;
74 };
75 Action[_fantasyLand.default.of] = Action.of;
76 return Action;
77});
78exports.connect = connect;
\No newline at end of file