UNPKG

11.1 kBSource Map (JSON)View Raw
1{"version":3,"file":"throwingFunctions.js","sources":["../src/throwingFunctions.js"],"sourcesContent":["/**\n * Created by Andy Likuski on 2017.07.03\n * Copyright (c) 2017 Andy Likuski\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n *\n * Functions that the trowing versions from functions.js\n */\nimport {reqPath, reqPathPropEq, findOne, onlyOne, onlyOneValue, keyStringToLensPath} from './functions';\nimport * as Result from 'folktale/result';\nimport * as R from 'ramda';\nimport {inspect} from 'util';\n\n/**\n * Throw and exception if Result is Result.Error\n * @param {Result} result Result.Error value is an array of Errors to throw. Result.Ok value is success to return\n * @returns {Object} Throws or returns the contents of Result.Ok. Values are '; ' separated\n */\nexport const throwIfResultError = result =>\n result.mapError(\n // Throw if Result.Error\n resultErrorValue => {\n throw new Error(R.join('; ', resultErrorValue));\n }\n ).unsafeGet();\n\n/**\n * Throw and exception if Result is Result.Error\n * @param {String} message The custom error message to precede the error dump\n * @param {Result} either Result.Error value is a single error\n * @returns {Object} Throws or returns the contents of Result.Ok\n */\nexport const throwIfSingleResultError = R.curry((message, result) =>\n result.mapError(\n // Throw if Result.Error\n resultErrorValue => {\n throw new Error(`${message}: ${inspect(resultErrorValue, {showHidden: false, depth: 3})}`);\n }\n ).unsafeGet()\n);\n\n/**\n * Like throwIfResultError but allows mapping of the unformatted Error values in Result\n * @param {Result} either Result.Error value is an error to throw. Result.Ok value is success to return\n * The Result value (not just the Result itself) must be a Container in order to apply the mapping function\n * @param {Function} func Mapping function that maps Result.Error value. If this value is\n * an array it maps each value. If not it maps the single value\n * @returns {Result.Ok} Throws the mapped values or returns Result.Ok. Error values are '; ' separated\n */\nexport const mappedThrowIfResultError = R.curry((func, result) => {\n return result.mapError(\n // Throw if Result.Error\n error => {\n throw new Error(\n R.join('; ', R.map(\n e => {\n return func(e);\n },\n error)\n )\n );\n }).map(\n // Return the Result.Ok value\n R.identity\n );\n }\n);\n\n/**\n * Calls functions.reqPath and throws if the reqPath does not resolve to a non-nil\n * @params {[String]} Path the Ramda lens style path, e.g. ['x', 1, 'y']\n * @params {Object} obj The obj to query\n * @returns {Object|Exception} The value of the sought path or throws\n * reqPath:: string -> obj -> a or throws\n */\nexport const reqPathThrowing = R.curry((pathList, obj) =>\n reqPath(pathList, obj).mapError(\n leftValue => {\n // If left throw a helpful error\n throw new Error(\n R.join(' ', [\n R.ifElse(\n R.length,\n resolved => `Only found non-nil path up to ${R.join('.', resolved)}`,\n R.always('Found no non-nil value')\n )(leftValue.resolved),\n `of path ${R.join('.', pathList)} for obj ${inspect(obj, {depth: 3})}`\n ]\n )\n );\n },\n // If right return the value\n R.identity\n ).unsafeGet()\n);\n\n/**\n * Expects a prop path and returns a function expecting props,\n * which resolves the prop indicated by the string. Throws if there is no match.\n * Any detected standalone numbrer is assumed to be an index and converted to an int\n * @param {String} str dot-separated prop path\n * @param {Object} props Object to resolve the path in\n * @return {function(*=)}\n */\nexport const reqStrPathThrowing = R.curry(\n (str, props) => reqPathThrowing(keyStringToLensPath(str), props)\n);\n\n/**\n * Calls functions.reqPathPropEq and throws if the reqPath does not resolve to a non-nil\n * @params {[String]} Path the Ramda lens style path, e.g. ['x', 1, 'y']\n * @params {*} Value to compare to result of reqPath\n * @params {Object} obj The obj to query\n * @returns {Boolean|Exception} true|false if the path is valid depending whether if the\n * resulting value matches val. throws if the path is invalid\n * reqPath:: Boolean b = string -> obj -> b or throws\n */\nexport const reqPathPropEqThrowing = R.curry((path, val, obj) =>\n reqPathPropEq(path, val, obj).mapError(\n leftValue => {\n // If left throw a helpful error\n throw new Error(\n [leftValue.resolved.length ?\n `Only found non-nil path up to ${leftValue.resolved.join('.')}` :\n 'Found no non-nil value of path',\n `of ${path.join('.')} for obj ${inspect(obj, {depth: 2})}`\n ].join(' '));\n }\n ).unsafeGet()\n);\n\n/**\n * Like R.find but expects only one match and works on both arrays and objects\n * @param {Function} predicate\n * @param {Array|Object} obj Container that should only match once with predicate\n * @returns {Object} The single item container or throws\n */\nexport const findOneThrowing = R.curry((predicate, obj) =>\n throwIfSingleResultError('Did not find exactly one match', findOne(predicate, obj))\n);\n\n/**\n * Like findOne but without a predicate\n * @param {Array|Object} obj Container that should only match once with predicate\n * @returns {Object} The single item container or throws\n */\nexport const onlyOneThrowing = obj =>\n throwIfSingleResultError('Did not find exactly one', R.omit(['matching'], onlyOne(obj))\n );\n\n/**\n * Like onlyOne but extracts the value from the functor\n * @param {Array|Object} obj Functor that has a values property\n * @returns {Object} The single item container or throws\n */\nexport const onlyOneValueThrowing = obj =>\n throwIfSingleResultError('Did not find exactly one', R.omit(['matching'], onlyOneValue(obj)));\n\n/**\n * Expects the given params when used to filter the given items to result in one item that matches\n * @param {Object} params key values where keys might match the item keys and values might match the item values\n * @param {[Object]} items Objects to test on the params\n * @returns {Object} The matching item or throw an error\n */\nexport const findOneValueByParamsThrowing = (params, items) =>\n throwIfSingleResultError('Did not find exactly one', findOne(\n // Compare all theeeqProps against each item\n R.allPass(\n // Create a eqProps for each prop of params\n R.map(prop => R.eqProps(prop, params),\n R.keys(params)\n )\n ),\n R.values(items)\n ).map(R.head));\n\n"],"names":["throwIfResultError","result","mapError","resultErrorValue","Error","R","unsafeGet","throwIfSingleResultError","message","inspect","showHidden","depth","mappedThrowIfResultError","func","error","e","map","reqPathThrowing","pathList","obj","reqPath","leftValue","resolved","reqStrPathThrowing","str","props","keyStringToLensPath","reqPathPropEqThrowing","path","val","reqPathPropEq","length","join","findOneThrowing","predicate","findOne","onlyOneThrowing","onlyOne","onlyOneValueThrowing","onlyOneValue","findOneValueByParamsThrowing","params","items","prop"],"mappings":";;;;;;;;;;AAAA;;;;;;;;;;;;AAiBA;;;;;;IAKaA,kBAAkB,GAAG,SAArBA,kBAAqB,CAAAC,MAAM;AAAA,SACtCA,MAAM,CAACC,QAAP;AAEE,YAAAC,gBAAgB,EAAI;AAClB,UAAM,IAAIC,KAAJ,CAAUC,MAAA,CAAO,IAAP,EAAaF,gBAAb,CAAV,CAAN;AACD,GAJH,EAKEG,SALF,EADsC;AAAA;AAQxC;;;;;;;IAMaC,wBAAwB,GAAGF,OAAA,CAAQ,UAACG,OAAD,EAAUP,MAAV;AAAA,SAC9CA,MAAM,CAACC,QAAP;AAEE,YAAAC,gBAAgB,EAAI;AAClB,UAAM,IAAIC,KAAJ,WAAaI,OAAb,eAAyBC,YAAO,CAACN,gBAAD,EAAmB;AAACO,MAAAA,UAAU,EAAE,KAAb;AAAoBC,MAAAA,KAAK,EAAE;AAA3B,KAAnB,CAAhC,EAAN;AACD,GAJH,EAKEL,SALF,EAD8C;AAAA,CAAR;AASxC;;;;;;;;;IAQaM,wBAAwB,GAAGP,OAAA,CAAQ,UAACQ,IAAD,EAAOZ,MAAP,EAAkB;AAC9D,SAAOA,MAAM,CAACC,QAAP;AAEL,YAAAY,KAAK,EAAI;AACP,UAAM,IAAIV,KAAJ,CACJC,MAAA,CAAO,IAAP,EAAaA,KAAA,CACX,UAAAU,CAAC,EAAI;AACH,aAAOF,IAAI,CAACE,CAAD,CAAX;AACD,KAHU,EAIXD,KAJW,CAAb,CADI,CAAN;AAQD,G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