UNPKG

8.08 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var functions = require('./functions-b6202a08.js');
6var R = require('ramda');
7require('folktale/result');
8require('ramda-maybe');
9var util = require('util');
10
11/**
12 * Created by Andy Likuski on 2017.07.03
13 * Copyright (c) 2017 Andy Likuski
14 *
15 * 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:
16 *
17 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
18 *
19 * 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.
20 *
21 * Functions that the trowing versions from functions.js
22 */
23/**
24 * Throw and exception if Result is Result.Error
25 * @param {Result} result Result.Error value is an array of Errors to throw. Result.Ok value is success to return
26 * @returns {Object} Throws or returns the contents of Result.Ok. Values are '; ' separated
27 */
28
29var throwIfResultError = function throwIfResultError(result) {
30 return result.mapError( // Throw if Result.Error
31 function (resultErrorValue) {
32 throw new Error(R.join('; ', resultErrorValue));
33 }).unsafeGet();
34};
35/**
36 * Throw and exception if Result is Result.Error
37 * @param {String} message The custom error message to precede the error dump
38 * @param {Result} either Result.Error value is a single error
39 * @returns {Object} Throws or returns the contents of Result.Ok
40 */
41
42var throwIfSingleResultError = R.curry(function (message, result) {
43 return result.mapError( // Throw if Result.Error
44 function (resultErrorValue) {
45 throw new Error("".concat(message, ": ").concat(util.inspect(resultErrorValue, {
46 showHidden: false,
47 depth: 3
48 })));
49 }).unsafeGet();
50});
51/**
52 * Like throwIfResultError but allows mapping of the unformatted Error values in Result
53 * @param {Result} either Result.Error value is an error to throw. Result.Ok value is success to return
54 * The Result value (not just the Result itself) must be a Container in order to apply the mapping function
55 * @param {Function} func Mapping function that maps Result.Error value. If this value is
56 * an array it maps each value. If not it maps the single value
57 * @returns {Result.Ok} Throws the mapped values or returns Result.Ok. Error values are '; ' separated
58 */
59
60var mappedThrowIfResultError = R.curry(function (func, result) {
61 return result.mapError( // Throw if Result.Error
62 function (error) {
63 throw new Error(R.join('; ', R.map(function (e) {
64 return func(e);
65 }, error)));
66 }).map( // Return the Result.Ok value
67 R.identity);
68});
69/**
70 * Calls functions.reqPath and throws if the reqPath does not resolve to a non-nil
71 * @params {[String]} Path the Ramda lens style path, e.g. ['x', 1, 'y']
72 * @params {Object} obj The obj to query
73 * @returns {Object|Exception} The value of the sought path or throws
74 * reqPath:: string -> obj -> a or throws
75 */
76
77var reqPathThrowing = R.curry(function (pathList, obj) {
78 return functions.reqPath(pathList, obj).mapError(function (leftValue) {
79 // If left throw a helpful error
80 throw new Error(R.join(' ', [R.ifElse(R.length, function (resolved) {
81 return "Only found non-nil path up to ".concat(R.join('.', resolved));
82 }, R.always('Found no non-nil value'))(leftValue.resolved), "of path ".concat(R.join('.', pathList), " for obj ").concat(util.inspect(obj, {
83 depth: 3
84 }))]));
85 }, // If right return the value
86 R.identity).unsafeGet();
87});
88/**
89 * Expects a prop path and returns a function expecting props,
90 * which resolves the prop indicated by the string. Throws if there is no match.
91 * Any detected standalone numbrer is assumed to be an index and converted to an int
92 * @param {String} str dot-separated prop path
93 * @param {Object} props Object to resolve the path in
94 * @return {function(*=)}
95 */
96
97var reqStrPathThrowing = R.curry(function (str, props) {
98 return reqPathThrowing(functions.keyStringToLensPath(str), props);
99});
100/**
101 * Calls functions.reqPathPropEq and throws if the reqPath does not resolve to a non-nil
102 * @params {[String]} Path the Ramda lens style path, e.g. ['x', 1, 'y']
103 * @params {*} Value to compare to result of reqPath
104 * @params {Object} obj The obj to query
105 * @returns {Boolean|Exception} true|false if the path is valid depending whether if the
106 * resulting value matches val. throws if the path is invalid
107 * reqPath:: Boolean b = string -> obj -> b or throws
108 */
109
110var reqPathPropEqThrowing = R.curry(function (path, val, obj) {
111 return functions.reqPathPropEq(path, val, obj).mapError(function (leftValue) {
112 // If left throw a helpful error
113 throw new Error([leftValue.resolved.length ? "Only found non-nil path up to ".concat(leftValue.resolved.join('.')) : 'Found no non-nil value of path', "of ".concat(path.join('.'), " for obj ").concat(util.inspect(obj, {
114 depth: 2
115 }))].join(' '));
116 }).unsafeGet();
117});
118/**
119 * Like R.find but expects only one match and works on both arrays and objects
120 * @param {Function} predicate
121 * @param {Array|Object} obj Container that should only match once with predicate
122 * @returns {Object} The single item container or throws
123 */
124
125var findOneThrowing = R.curry(function (predicate, obj) {
126 return throwIfSingleResultError('Did not find exactly one match', functions.findOne(predicate, obj));
127});
128/**
129 * Like findOne but without a predicate
130 * @param {Array|Object} obj Container that should only match once with predicate
131 * @returns {Object} The single item container or throws
132 */
133
134var onlyOneThrowing = function onlyOneThrowing(obj) {
135 return throwIfSingleResultError('Did not find exactly one', R.omit(['matching'], functions.onlyOne(obj)));
136};
137/**
138 * Like onlyOne but extracts the value from the functor
139 * @param {Array|Object} obj Functor that has a values property
140 * @returns {Object} The single item container or throws
141 */
142
143var onlyOneValueThrowing = function onlyOneValueThrowing(obj) {
144 return throwIfSingleResultError('Did not find exactly one', R.omit(['matching'], functions.onlyOneValue(obj)));
145};
146/**
147 * Expects the given params when used to filter the given items to result in one item that matches
148 * @param {Object} params key values where keys might match the item keys and values might match the item values
149 * @param {[Object]} items Objects to test on the params
150 * @returns {Object} The matching item or throw an error
151 */
152
153var findOneValueByParamsThrowing = function findOneValueByParamsThrowing(params, items) {
154 return throwIfSingleResultError('Did not find exactly one', functions.findOne( // Compare all theeeqProps against each item
155 R.allPass( // Create a eqProps for each prop of params
156 R.map(function (prop) {
157 return R.eqProps(prop, params);
158 }, R.keys(params))), R.values(items)).map(R.head));
159};
160
161exports.findOneThrowing = findOneThrowing;
162exports.findOneValueByParamsThrowing = findOneValueByParamsThrowing;
163exports.mappedThrowIfResultError = mappedThrowIfResultError;
164exports.onlyOneThrowing = onlyOneThrowing;
165exports.onlyOneValueThrowing = onlyOneValueThrowing;
166exports.reqPathPropEqThrowing = reqPathPropEqThrowing;
167exports.reqPathThrowing = reqPathThrowing;
168exports.reqStrPathThrowing = reqStrPathThrowing;
169exports.throwIfResultError = throwIfResultError;
170exports.throwIfSingleResultError = throwIfSingleResultError;
171//# sourceMappingURL=throwingFunctions.js.map