UNPKG

monad-ts

Version:

Monad-ts is a small library implements some of key monads and way to chain them in a pipe (flow) in JavaScript and TypeScript. Angular 2+ compatible.

1,513 lines (1,512 loc) 172 kB
{ "type": "File", "start": 0, "end": 1931, "loc": { "start": { "line": 1, "column": 0 }, "end": { "line": 64, "column": 0 } }, "program": { "type": "Program", "start": 0, "end": 1931, "loc": { "start": { "line": 1, "column": 0 }, "end": { "line": 64, "column": 0 } }, "sourceType": "module", "body": [ { "type": "ImportDeclaration", "start": 0, "end": 32, "loc": { "start": { "line": 1, "column": 0 }, "end": { "line": 1, "column": 32 } }, "specifiers": [ { "type": "ImportSpecifier", "start": 9, "end": 14, "loc": { "start": { "line": 1, "column": 9 }, "end": { "line": 1, "column": 14 } }, "imported": { "type": "Identifier", "start": 9, "end": 14, "loc": { "start": { "line": 1, "column": 9 }, "end": { "line": 1, "column": 14 }, "identifierName": "Monad" }, "name": "Monad" }, "local": { "type": "Identifier", "start": 9, "end": 14, "loc": { "start": { "line": 1, "column": 9 }, "end": { "line": 1, "column": 14 }, "identifierName": "Monad" }, "name": "Monad" } } ], "source": { "type": "StringLiteral", "start": 22, "end": 31, "loc": { "start": { "line": 1, "column": 22 }, "end": { "line": 1, "column": 31 } }, "extra": { "rawValue": "./monad", "raw": "\"./monad\"" }, "value": "./monad" } }, { "type": "ImportDeclaration", "start": 34, "end": 66, "loc": { "start": { "line": 2, "column": 0 }, "end": { "line": 2, "column": 32 } }, "specifiers": [ { "type": "ImportSpecifier", "start": 43, "end": 48, "loc": { "start": { "line": 2, "column": 9 }, "end": { "line": 2, "column": 14 } }, "imported": { "type": "Identifier", "start": 43, "end": 48, "loc": { "start": { "line": 2, "column": 9 }, "end": { "line": 2, "column": 14 }, "identifierName": "Maybe" }, "name": "Maybe" }, "local": { "type": "Identifier", "start": 43, "end": 48, "loc": { "start": { "line": 2, "column": 9 }, "end": { "line": 2, "column": 14 }, "identifierName": "Maybe" }, "name": "Maybe" } } ], "source": { "type": "StringLiteral", "start": 56, "end": 65, "loc": { "start": { "line": 2, "column": 22 }, "end": { "line": 2, "column": 31 } }, "extra": { "rawValue": "./maybe", "raw": "\"./maybe\"" }, "value": "./maybe" } }, { "type": "ImportDeclaration", "start": 68, "end": 109, "loc": { "start": { "line": 3, "column": 0 }, "end": { "line": 3, "column": 41 } }, "specifiers": [ { "type": "ImportSpecifier", "start": 77, "end": 82, "loc": { "start": { "line": 3, "column": 9 }, "end": { "line": 3, "column": 14 } }, "imported": { "type": "Identifier", "start": 77, "end": 82, "loc": { "start": { "line": 3, "column": 9 }, "end": { "line": 3, "column": 14 }, "identifierName": "clone" }, "name": "clone" }, "local": { "type": "Identifier", "start": 77, "end": 82, "loc": { "start": { "line": 3, "column": 9 }, "end": { "line": 3, "column": 14 }, "identifierName": "clone" }, "name": "clone" } } ], "source": { "type": "StringLiteral", "start": 90, "end": 108, "loc": { "start": { "line": 3, "column": 22 }, "end": { "line": 3, "column": 40 } }, "extra": { "rawValue": "./services/clone", "raw": "\"./services/clone\"" }, "value": "./services/clone" } }, { "type": "ImportDeclaration", "start": 111, "end": 144, "loc": { "start": { "line": 4, "column": 0 }, "end": { "line": 4, "column": 33 } }, "specifiers": [ { "type": "ImportSpecifier", "start": 120, "end": 126, "loc": { "start": { "line": 4, "column": 9 }, "end": { "line": 4, "column": 15 } }, "imported": { "type": "Identifier", "start": 120, "end": 126, "loc": { "start": { "line": 4, "column": 9 }, "end": { "line": 4, "column": 15 }, "identifierName": "ErrorM" }, "name": "ErrorM" }, "local": { "type": "Identifier", "start": 120, "end": 126, "loc": { "start": { "line": 4, "column": 9 }, "end": { "line": 4, "column": 15 }, "identifierName": "ErrorM" }, "name": "ErrorM" } } ], "source": { "type": "StringLiteral", "start": 134, "end": 143, "loc": { "start": { "line": 4, "column": 23 }, "end": { "line": 4, "column": 32 } }, "extra": { "rawValue": "./error", "raw": "\"./error\"" }, "value": "./error" }, "trailingComments": [ { "type": "CommentBlock", "value": "*\r\n * Class Flow - for composing monads in a flow (pipe).\r\n * @extends {Monad}\r\n ", "start": 146, "end": 231, "loc": { "start": { "line": 5, "column": 0 }, "end": { "line": 8, "column": 3 } } } ] }, { "type": "ExportNamedDeclaration", "start": 233, "end": 1869, "loc": { "start": { "line": 9, "column": 0 }, "end": { "line": 62, "column": 1 } }, "specifiers": [], "source": null, "declaration": { "type": "ClassDeclaration", "start": 240, "end": 1869, "loc": { "start": { "line": 9, "column": 7 }, "end": { "line": 62, "column": 1 } }, "id": { "type": "Identifier", "start": 246, "end": 250, "loc": { "start": { "line": 9, "column": 13 }, "end": { "line": 9, "column": 17 }, "identifierName": "Flow" }, "name": "Flow", "leadingComments": null }, "superClass": { "type": "Identifier", "start": 259, "end": 264, "loc": { "start": { "line": 9, "column": 26 }, "end": { "line": 9, "column": 31 }, "identifierName": "Monad" }, "name": "Monad" }, "body": { "type": "ClassBody", "start": 265, "end": 1869, "loc": { "start": { "line": 9, "column": 32 }, "end": { "line": 62, "column": 1 } }, "body": [ { "type": "ClassMethod", "start": 510, "end": 971, "loc": { "start": { "line": 15, "column": 4 }, "end": { "line": 32, "column": 5 } }, "computed": false, "key": { "type": "Identifier", "start": 510, "end": 521, "loc": { "start": { "line": 15, "column": 4 }, "end": { "line": 15, "column": 15 }, "identifierName": "constructor" }, "name": "constructor", "leadingComments": null }, "static": false, "kind": "constructor", "id": null, "generator": false, "expression": false, "async": false, "params": [ { "type": "Identifier", "start": 522, "end": 527, "loc": { "start": { "line": 15, "column": 16 }, "end": { "line": 15, "column": 21 }, "identifierName": "initV" }, "name": "initV" }, { "type": "AssignmentPattern", "start": 529, "end": 547, "loc": { "start": { "line": 15, "column": 23 }, "end": { "line": 15, "column": 41 } }, "left": { "type": "Identifier", "start": 529, "end": 540, "loc": { "start": { "line": 15, "column": 23 }, "end": { "line": 15, "column": 34 }, "identifierName": "encapsulate" }, "name": "encapsulate" }, "right": { "type": "BooleanLiteral", "start": 543, "end": 547, "loc": { "start": { "line": 15, "column": 37 }, "end": { "line": 15, "column": 41 } }, "value": true } } ], "body": { "type": "BlockStatement", "start": 549, "end": 971, "loc": { "start": { "line": 15, "column": 43 }, "end": { "line": 32, "column": 5 } }, "body": [ { "type": "ExpressionStatement", "start": 560, "end": 568, "loc": { "start": { "line": 16, "column": 8 }, "end": { "line": 16, "column": 16 } }, "expression": { "type": "CallExpression", "start": 560, "end": 567, "loc": { "start": { "line": 16, "column": 8 }, "end": { "line": 16, "column": 15 } }, "callee": { "type": "Super", "start": 560, "end": 565, "loc": { "start": { "line": 16, "column": 8 }, "end": { "line": 16, "column": 13 } } }, "arguments": [] }, "trailingComments": [ { "type": "CommentBlock", "value": "*\r\n * keep initial flow (pipe) value.\r\n * @type {any}\r\n ", "start": 578, "end": 662, "loc": { "start": { "line": 17, "column": 8 }, "end": { "line": 20, "column": 11 } } } ] }, { "type": "ExpressionStatement", "start": 672, "end": 719, "loc": { "start": { "line": 21, "column": 8 }, "end": { "line": 21, "column": 55 } }, "expression": { "type": "AssignmentExpression", "start": 672, "end": 718, "loc": { "start": { "line": 21, "column": 8 }, "end": { "line": 21, "column": 54 } }, "operator": "=", "left": { "type": "MemberExpression", "start": 672, "end": 681, "loc": { "start": { "line": 21, "column": 8 }, "end": { "line": 21, "column": 17 } }, "object": { "type": "ThisExpression", "start": 672, "end": 676, "loc": { "start": { "line": 21, "column": 8 }, "end": { "line": 21, "column": 12 } }, "leadingComments": null }, "property": { "type": "Identifier", "start": 677, "end": 681, "loc": { "start": { "line": 21, "column": 13 }, "end": { "line": 21, "column": 17 }, "identifierName": "flow" }, "name": "flow" }, "computed": false, "leadingComments": null }, "right": { "type": "ConditionalExpression", "start": 684, "end": 718, "loc": { "start": { "line": 21, "column": 20 }, "end": { "line": 21, "column": 54 } }, "test": { "type": "Identifier", "start": 684, "end": 695, "loc": { "start": { "line": 21, "column": 20 }, "end": { "line": 21, "column": 31 }, "identifierName": "encapsulate" }, "name": "encapsulate" }, "consequent": { "type": "CallExpression", "start": 698, "end": 710, "loc": { "start": { "line": 21, "column": 34 }, "end": { "line": 21, "column": 46 } }, "callee": { "type": "Identifier", "start": 698, "end": 703, "loc": { "start": { "line": 21, "column": 34 }, "end": { "line": 21, "column": 39 }, "identifierName": "clone" }, "name": "clone" }, "arguments": [ { "type": "Identifier", "start": 704, "end": 709, "loc": { "start": { "line": 21, "column": 40 }, "end": { "line": 21, "column": 45 }, "identifierName": "initV" }, "name": "initV" } ] }, "alternate": { "type": "Identifier", "start": 713, "end": 718, "loc": { "start": { "line": 21, "column": 49 }, "end": { "line": 21, "column": 54 }, "identifierName": "initV" }, "name": "initV" } }, "leadingComments": null }, "leadingComments": [ { "type": "CommentBlock", "value": "*\r\n * keep initial flow (pipe) value.\r\n * @type {any}\r\n ", "start": 578, "end": 662, "loc": { "start": { "line": 17, "column": 8 }, "end": { "line": 20, "column": 11 } } } ], "trailingComments": [ { "type": "CommentBlock", "value": "*\r\n * the instance of Maybe.\r\n * @type {Maybe}\r\n ", "start": 729, "end": 806, "loc": { "start": { "line": 22, "column": 8 }, "end": { "line": 25, "column": 11 } } } ] }, { "type": "ExpressionStatement", "start": 816, "end": 841, "loc": { "start": { "line": 26, "column": 8 }, "end": { "line": 26, "column": 33 } }, "expression": { "type": "AssignmentExpression", "start": 816, "end": 840, "loc": { "start": { "line": 26, "column": 8 }, "end": { "line": 26, "column": 32 } }, "operator": "=", "left": { "type": "MemberExpression", "start": 816, "end": 826, "loc": { "start": { "line": 26, "column": 8 }, "end": { "line": 26, "column": 18 } }, "object": { "type": "ThisExpression", "start": 816, "end": 820, "loc": { "start": { "line": 26, "column": 8 }, "end": { "line": 26, "column": 12 } }, "leadingComments": null }, "property": { "type": "Identifier", "start": 821, "end": 826, "loc": { "start": { "line": 26, "column": 13 }, "end": { "line": 26, "column": 18 }, "identifierName": "maybe" }, "name": "maybe" }, "computed": false, "leadingComments": null }, "right": { "type": "NewExpression", "start": 829, "end": 840, "loc": { "start": { "line": 26, "column": 21 }, "end": { "line": 26, "column": 32 } }, "callee": { "type": "Identifier", "start": 833, "end": 838, "loc": { "start": { "line": 26, "column": 25 }, "end": { "line": 26, "column": 30 }, "identifierName": "Maybe" }, "name": "Maybe" }, "arguments": [] }, "leadingComments": null }, "leadingComments": [ { "type": "CommentBlock", "value": "*\r\n * the instance of Maybe.\r\n * @type {Maybe}\r\n ", "start": 729, "end": 806, "loc": { "start": { "line": 22, "column": 8 }, "end": { "line": 25, "column": 11 } } } ], "trailingComments": [ { "type": "CommentBlock", "value": "*\r\n * the instance of ErrorM.\r\n * @type {ErrorM}\r\n ", "start": 851, "end": 930, "loc": { "start": { "line": 27, "column": 8 }, "end": { "line": 30, "column": 11 } } } ] }, { "type": "ExpressionStatement", "start": 940, "end": 964, "loc": { "start": { "line": 31, "column": 8 }, "end": { "line": 31, "column": 32 } }, "expression": { "type": "AssignmentExpression", "start": 940, "end": 963, "loc": { "start": { "line": 31, "column": 8 }, "end": { "line": 31, "column": 31 } }, "operator": "=", "left": { "type": "MemberExpression", "start": 940, "end": 948, "loc": { "start": { "line": 31, "column": 8 }, "end": { "line": 31, "column": 16 } }, "object": { "type": "ThisExpression", "start": 940, "end": 944, "loc": { "start": { "line": 31, "column": 8 }, "end": { "line": 31, "column": 12 } }, "leadingComments": null }, "property": { "type": "Identifier", "start": 945, "end": 948, "loc": { "start": { "line": 31, "column": 13 }, "end": { "line": 31, "column": 16 }, "identifierName": "err" }, "name": "err" }, "computed": false, "leadingComments": null }, "right": { "type": "NewExpression", "start": 951, "end": 963, "loc": { "start": { "line": 31, "column": 19 }, "end": { "line": 31, "column": 31 } }, "callee": { "type": "Identifier", "start": 955, "end": 961, "loc": { "start": { "line": 31, "column": 23 }, "end": { "line": 31, "column": 29 }, "identifierName": "ErrorM" }, "name": "ErrorM" }, "arguments": [] }, "leadingComments": null }, "leadingComments": [ { "type": "CommentBlock", "value": "*\r\n * the instance of ErrorM.\r\n * @type {ErrorM}\r\n ", "start": 851, "end": 930, "loc": { "start": { "line": 27, "column": 8 }, "end": { "line": 30, "column": 11 } } } ] } ], "directives": [], "trailingComments": null }, "leadingComments": [ { "type": "CommentBlock", "value": "*\r\n * Create an instance of class AsyncFlow.\r\n * @param {any} initV - initial value of new flow (pipe).\r\n * @param {boolean} [encapsulate = true] encapsulate - flag, if true then the init value will be cloned.\r\n ", "start": 272, "end": 504, "loc": { "start": { "line": 10, "column": 4 }, "end": { "line": 14, "column": 7 } } } ], "trailingComments": [ { "type": "CommentBlock", "value": "*\r\n * Chains the operations on a monadic values.\r\n * @method bind\r\n * @param {function(v: T) => Pr<U>} f - transformation function for a main flow value.\r\n * @param {any} [v = this.flow] - underlying value for a monad.\r\n * @return {Flow<T>} transformed by f() value v or throw Error or null.\r\n ", "start": 977, "end": 1299, "loc": { "start": { "line": 33, "column": 4 }, "end": { "line": 39, "column": 7 } } } ] }, { "type": "ClassMethod", "start": 1305, "end": 1435, "loc": { "start": { "line": 40, "column": 4 }, "end": { "line": 43, "column": 5 } }, "computed": false, "key": { "type": "Identifier", "start": 1305, "end": 1309, "loc": { "start": { "line": 40, "column": 4 }, "end": { "line": 40, "column": 8 }, "identifierName": "bind" }, "name": "bind", "leadingComments": null }, "static": false, "kind": "method", "id": null, "generator": false, "expression": false, "async": false, "params": [ { "type": "Identifier", "start": 1310, "end": 1311, "loc": { "start": { "line": 40, "column": 9 }, "end": { "line": 40, "column": 10 }, "identifierName": "f" }, "name": "f" }, { "type": "AssignmentPattern", "start": 1313, "end": 1326, "loc": { "start": { "line": 40, "column": 12 }, "end": { "line": 40, "column": 25 } }, "left": { "type": "Identifier", "start": 1313, "end": 1314, "loc": { "start": { "line": 40, "column": 12 }, "end": { "line": 40, "column": 13 }, "identifierName": "v" }, "name": "v" }, "right": { "type": "MemberExpression", "start": 1317, "end": 1326, "loc": { "start": { "line": 40, "column": 16 }, "end": { "line": 40, "column": 25 } }, "object": { "type": "ThisExpression", "start": 1317, "end": 1321, "loc": { "start": { "line": 40, "column": 16 }, "end": { "line": 40, "column": 20 } } }, "property": { "type": "Identifier", "start": 1322, "end": 1326, "loc": { "start": { "line": 40, "column": 21 }, "end": { "line": 40, "column": 25 }, "identifierName": "flow" }, "name": "flow" }, "computed": false } } ], "body": { "type": "BlockStatement", "start": 1328, "end": 1435, "loc": { "start": { "line": 40, "column": 27 }, "end": { "line": 43, "column": 5 } }, "body": [ { "type": "ExpressionStatement", "start": 1339, "end": 1406, "loc": { "start": { "line": 41, "column": 8 }, "end": { "line": 41, "column": 75 } }, "expression": { "type": "AssignmentExpression", "start": 1339, "end": 1405, "loc": { "start": { "line": 41, "column": 8 }, "end": { "line": 41, "column": 74 } }, "operator": "=", "left": { "type": "MemberExpression", "start": 1339, "end": 1348, "loc": { "start": { "line": 41, "column": 8 }, "end": { "line": 41, "column": 17 } }, "object": { "type": "ThisExpression", "start": 1339, "end": 1343, "loc": { "start": { "line": 41, "column": 8 }, "end": { "line": 41, "column": 12 } } }, "property": { "type": "Identifier", "start": 1344, "end": 1348, "loc": { "start": { "line": 41, "column": 13 }, "end": { "line": 41, "column": 17 }, "identifierName": "flow" }, "name": "flow" }, "computed": false }, "right": { "type": "CallExpression", "start": 1351, "end": 1405, "loc": { "start": { "line": 41, "column": 20 }, "end": { "line": 41, "column": 74 } }, "callee": { "type": "MemberExpression", "start": 1351, "end": 1364, "loc": { "start": { "line": 41, "column": 20 }, "end": { "line": 41, "column": 33 } }, "object": { "type": "MemberExpression", "start": 1351, "end": 1359, "loc": { "start": { "line": 41, "column": 20 }, "end": { "line": 41, "column": 28 } }, "object": { "type": "ThisExpression", "start": 1351, "end": 1355, "loc": { "start": { "line": 41, "column": 20 }, "end": { "line": 41, "column": 24 } } }, "property": { "type": "Identifier", "start": 1356, "end": 1359, "loc": { "start": { "line": 41, "column": 25 }, "end": { "line": 41, "column": 28 }, "identifierName": "err" }, "name": "err" }, "computed": false }, "property": { "type": "Identifier", "start": 1360, "end": 1364, "loc": { "start": {