| 1 | /* ************************************************************************************
|
|---|
| 2 | Extracted from check-types.js
|
|---|
| 3 | https://gitlab.com/philbooth/check-types.js
|
|---|
| 4 |
|
|---|
| 5 | MIT License
|
|---|
| 6 |
|
|---|
| 7 | Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Phil Booth
|
|---|
| 8 |
|
|---|
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
|---|
| 10 | of this software and associated documentation files (the "Software"), to deal
|
|---|
| 11 | in the Software without restriction, including without limitation the rights
|
|---|
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|---|
| 13 | copies of the Software, and to permit persons to whom the Software is
|
|---|
| 14 | furnished to do so, subject to the following conditions:
|
|---|
| 15 |
|
|---|
| 16 | The above copyright notice and this permission notice shall be included in all
|
|---|
| 17 | copies or substantial portions of the Software.
|
|---|
| 18 |
|
|---|
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|---|
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|---|
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|---|
| 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|---|
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|---|
| 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|---|
| 25 | SOFTWARE.
|
|---|
| 26 |
|
|---|
| 27 | ************************************************************************************ */
|
|---|
| 28 | "use strict";
|
|---|
| 29 |
|
|---|
| 30 | /* Validation functions copied from check-types package - https://www.npmjs.com/package/check-types */
|
|---|
| 31 |
|
|---|
| 32 | const toString = Object.prototype.toString;
|
|---|
| 33 |
|
|---|
| 34 | function isFunction(data) {
|
|---|
| 35 | return typeof data === "function";
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | function isNonEmptyString(data) {
|
|---|
| 39 | return isString(data) && data !== "";
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | function isDate(data) {
|
|---|
| 43 | return isInstanceStrict(data, Date) && isInteger(data.getTime());
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | function isEmptyString(data) {
|
|---|
| 47 | return data === "" || (data instanceof String && data.toString() === "");
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | function isString(data) {
|
|---|
| 51 | return typeof data === "string" || data instanceof String;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | function isObject(data) {
|
|---|
| 55 | return toString.call(data) === "[object Object]";
|
|---|
| 56 | }
|
|---|
| 57 | function isInstanceStrict(data, prototype) {
|
|---|
| 58 | try {
|
|---|
| 59 | return data instanceof prototype;
|
|---|
| 60 | } catch (error) {
|
|---|
| 61 | return false;
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | function isUrlStringOrObject(data) {
|
|---|
| 66 | return (
|
|---|
| 67 | isNonEmptyString(data) ||
|
|---|
| 68 | (isObject(data) &&
|
|---|
| 69 | "hostname" in data &&
|
|---|
| 70 | "pathname" in data &&
|
|---|
| 71 | "protocol" in data) ||
|
|---|
| 72 | isInstanceStrict(data, URL)
|
|---|
| 73 | );
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | function isInteger(data) {
|
|---|
| 77 | return typeof data === "number" && data % 1 === 0;
|
|---|
| 78 | }
|
|---|
| 79 | /* End validation functions */
|
|---|
| 80 |
|
|---|
| 81 | function validate(bool, cb, options) {
|
|---|
| 82 | if (!isFunction(cb)) {
|
|---|
| 83 | options = cb;
|
|---|
| 84 | cb = null;
|
|---|
| 85 | }
|
|---|
| 86 | if (!isObject(options)) options = { Error: "Failed Check" };
|
|---|
| 87 | if (!bool) {
|
|---|
| 88 | if (cb) {
|
|---|
| 89 | cb(new ParameterError(options));
|
|---|
| 90 | } else {
|
|---|
| 91 | throw new ParameterError(options);
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | class ParameterError extends Error {
|
|---|
| 97 | constructor(...params) {
|
|---|
| 98 | super(...params);
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | exports.ParameterError = ParameterError;
|
|---|
| 103 | exports.isFunction = isFunction;
|
|---|
| 104 | exports.isNonEmptyString = isNonEmptyString;
|
|---|
| 105 | exports.isDate = isDate;
|
|---|
| 106 | exports.isEmptyString = isEmptyString;
|
|---|
| 107 | exports.isString = isString;
|
|---|
| 108 | exports.isObject = isObject;
|
|---|
| 109 | exports.isUrlStringOrObject = isUrlStringOrObject;
|
|---|
| 110 | exports.validate = validate;
|
|---|