|
1 | 1 | import { PREFIX_REGEXP, SPECIAL_CHARS_REGEXP } from "./constants.js"; |
2 | | -import { isInjectable } from "./predicates"; |
3 | 2 |
|
4 | 3 | export const isProxySymbol = Symbol("isProxy"); |
5 | | -export const BADARG = "badarg"; |
6 | | -export const BADARGKEY = "badarg: key"; |
7 | | -export const BADARGVALUE = "badarg: value"; |
8 | 4 |
|
9 | 5 | /** |
10 | 6 | * |
@@ -956,79 +952,6 @@ export function assert(argument, errorMsg = "Assertion failed") { |
956 | 952 | } |
957 | 953 | } |
958 | 954 |
|
959 | | -/** @type {Map<ng.Validator, string>} */ |
960 | | -const reasons = new Map([ |
961 | | - [notNullOrUndefined, "required"], |
962 | | - [Array.isArray, "notarray"], |
963 | | - [isInjectable, "notinjectable"], |
964 | | - [isDefined, "required"], |
965 | | - [isString, "notstring"], |
966 | | -]); |
967 | | - |
968 | | -/** |
969 | | - * |
970 | | - * @param {ng.Validator} val |
971 | | - * @returns {string} |
972 | | - */ |
973 | | -function getReason(val) { |
974 | | - return reasons.get(val) ?? "fail"; |
975 | | -} |
976 | | - |
977 | | -/** |
978 | | - * Validate a value using a predicate function. |
979 | | - * Throws if the predicate returns false. |
980 | | - * IMPORTANT: use this function only for developper errors and not for user/data errors |
981 | | - * |
982 | | - * @param {ng.Validator} fn - Predicate validator function. |
983 | | - * @param {*} arg - The value to validate. |
984 | | - * @param {string} name - Parameter name (included in error message). |
985 | | - * @returns {*} The validated value. |
986 | | - * @throws {TypeError} If the value does not satisfy the validator. |
987 | | - */ |
988 | | -export function validate(fn, arg, name) { |
989 | | - if (fn(arg)) return arg; |
990 | | - |
991 | | - let v; |
992 | | - |
993 | | - try { |
994 | | - v = JSON.stringify(arg); |
995 | | - } catch { |
996 | | - v = String(arg); |
997 | | - } |
998 | | - |
999 | | - throw new TypeError(`badarg:${getReason(fn)} ${name}=${v}`); |
1000 | | -} |
1001 | | - |
1002 | | -/** |
1003 | | - * @param {*} arg - The value to validate. |
1004 | | - * @param {string} name - Parameter name (included in error message). |
1005 | | - * @returns {*} The validated value. |
1006 | | - * @throws {TypeError} If the value does not satisfy the validator. |
1007 | | - */ |
1008 | | -export function validateRequired(arg, name) { |
1009 | | - return validate(notNullOrUndefined, arg, name); |
1010 | | -} |
1011 | | - |
1012 | | -/** |
1013 | | - * @param {*} arg - The value to validate. |
1014 | | - * @param {string} name - Parameter name (included in error message). |
1015 | | - * @returns {*} The validated value. |
1016 | | - * @throws {TypeError} If the value does not satisfy the validator. |
1017 | | - */ |
1018 | | -export function validateArray(arg, name) { |
1019 | | - return validate(Array.isArray, arg, name); |
1020 | | -} |
1021 | | - |
1022 | | -/** |
1023 | | - * @param {*} arg - The value to validate. |
1024 | | - * @param {string} name - Parameter name (included in error message). |
1025 | | - * @returns {*} The validated value. |
1026 | | - * @throws {TypeError} If the value does not satisfy the validator. |
1027 | | - */ |
1028 | | -export function validateIsString(arg, name) { |
1029 | | - return validate(isString, arg, name); |
1030 | | -} |
1031 | | - |
1032 | 955 | /** |
1033 | 956 | * Throw error if the argument is falsy. |
1034 | 957 | */ |
|
0 commit comments