@toreda/strong-types
Version:
Better TypeScript code in fewer lines.
1 lines • 2.92 kB
Source Map (JSON)
{"version":3,"sources":["../src/rule/node.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAC,MAAM,EAAC,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAC,QAAQ,EAAC,MAAM,cAAc,CAAC;AACtC,OAAO,EAAC,cAAc,EAAC,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAC;AAE/C;;GAEG;AACH,qBAAa,QAAQ,CAAC,CAAC;IACtB,SAAgB,EAAE,EAAE,MAAM,CAAC;IAC3B,SAAgB,QAAQ,EAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;IAC9C,SAAgB,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;IAC9B,SAAgB,IAAI,EAAE,YAAY,CAAC;IAC5B,MAAM,EAAE,cAAc,CAAC;IACvB,YAAY,EAAE,OAAO,CAAC;gBAEjB,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ;IAclE,OAAO,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO;CASjC","file":"node.d.ts","sourcesContent":["/**\n *\tMIT License\n *\n *\tCopyright (c) 2019 - 2021 Toreda, Inc.\n *\n *\tPermission is hereby granted, free of charge, to any person obtaining a copy\n *\tof this software and associated documentation files (the \"Software\"), to deal\n *\tin the Software without restriction, including without limitation the rights\n *\tto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n *\tcopies of the Software, and to permit persons to whom the Software is\n *\tfurnished to do so, subject to the following conditions:\n\n * \tThe above copyright notice and this permission notice shall be included in all\n * \tcopies or substantial portions of the Software.\n *\n * \tTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n *\tIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n *\tFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * \tAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n *\tLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n *\tOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * \tSOFTWARE.\n *\n */\n\nimport {RuleFn} from './fn';\nimport {RuleMods} from '../rule/mods';\nimport {RuleNodeTarget} from './node/target';\nimport {RuleNodeType} from '../rule/node/type';\n\n/**\n * @category Rules\n */\nexport class RuleNode<T> {\n\tpublic readonly id: string;\n\tpublic readonly children: RuleNode<unknown>[];\n\tpublic readonly fn: RuleFn<T>;\n\tpublic readonly type: RuleNodeType;\n\tpublic target: RuleNodeTarget;\n\tpublic invertResult: boolean;\n\n\tconstructor(id: string, type: RuleNodeType, fn: RuleFn<T>, mods: RuleMods) {\n\t\tthis.id = id;\n\n\t\tif (typeof fn !== 'function') {\n\t\t\tthrow new Error(`Bad rule init - fn arg is not a function.`);\n\t\t}\n\n\t\tthis.type = type;\n\t\tthis.children = [];\n\t\tthis.fn = fn;\n\t\tthis.target = mods.target;\n\t\tthis.invertResult = mods.invert === true ? true : false;\n\t}\n\n\tpublic execute(value: T): boolean {\n\t\tconst result = this.fn(value);\n\n\t\tif (!this.invertResult) {\n\t\t\treturn result;\n\t\t} else {\n\t\t\treturn !result;\n\t\t}\n\t}\n}\n"]}