UNPKG

@difizen/mana-common

Version:

298 lines (276 loc) 11.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NotImplementedProxy = NotImplementedProxy; exports.areFunctions = areFunctions; exports.assertAllDefined = assertAllDefined; exports.assertIsDefined = assertIsDefined; exports.assertType = assertType; exports.createProxyObject = createProxyObject; exports.getAllMethodNames = getAllMethodNames; exports.getAllPropertyNames = getAllPropertyNames; exports.isArray = isArray; exports.isBoolean = isBoolean; exports.isDefined = isDefined; exports.isEmptyObject = isEmptyObject; exports.isFunction = isFunction; exports.isNumber = isNumber; exports.isObject = isObject; exports.isString = isString; exports.isStringArray = isStringArray; exports.isUndefined = isUndefined; exports.isUndefinedOrNull = isUndefinedOrNull; exports.validateConstraint = validateConstraint; exports.validateConstraints = validateConstraints; exports.withNullAsUndefined = withNullAsUndefined; exports.withUndefinedAsNull = withUndefinedAsNull; function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ /** * @returns whether the provided parameter is a JavaScript Array or not. */ function isArray(array) { return Array.isArray(array); } /** * @returns whether the provided parameter is a JavaScript String or not. */ function isString(str) { return typeof str === 'string'; } /** * @returns whether the provided parameter is a JavaScript Array and each element in the array is a string. */ function isStringArray(value) { return Array.isArray(value) && value.every(function (elem) { return isString(elem); }); } /** * * @returns whether the provided parameter is of type `object` but **not** * `null`, an `array`, a `regexp`, nor a `date`. */ function isObject(obj) { // The method can't do a type cast since there are type (like strings) which // are subclasses of any put not positvely matched by the function. Hence type // narrowing results in wrong results. return _typeof(obj) === 'object' && obj !== null && !Array.isArray(obj) && !(obj instanceof RegExp) && !(obj instanceof Date); } /** * In **contrast** to just checking `typeof` this will return `false` for `NaN`. * @returns whether the provided parameter is a JavaScript Number or not. */ function isNumber(obj) { return typeof obj === 'number' && !isNaN(obj); } /** * @returns whether the provided parameter is a JavaScript Boolean or not. */ function isBoolean(obj) { return obj === true || obj === false; } /** * @returns whether the provided parameter is undefined. */ function isUndefined(obj) { return typeof obj === 'undefined'; } /** * @returns whether the provided parameter is defined. */ function isDefined(arg) { return !isUndefinedOrNull(arg); } /** * @returns whether the provided parameter is undefined or null. */ function isUndefinedOrNull(obj) { return isUndefined(obj) || obj === null; } function assertType(condition, type) { if (!condition) { throw new Error(type ? "Unexpected type, expected '".concat(type, "'") : 'Unexpected type'); } } /** * Asserts that the argument passed in is neither undefined nor null. */ function assertIsDefined(arg) { if (isUndefinedOrNull(arg)) { throw new Error('Assertion Failed: argument is undefined or null'); } return arg; } /** * Asserts that each argument passed in is neither undefined nor null. */ function assertAllDefined() { var result = []; for (var i = 0; i < arguments.length; i++) { var arg = i < 0 || arguments.length <= i ? undefined : arguments[i]; if (isUndefinedOrNull(arg)) { throw new Error("Assertion Failed: argument at index ".concat(i, " is undefined or null")); } result.push(arg); } return result; } var hasOwnProperty = Object.prototype.hasOwnProperty; /** * @returns whether the provided parameter is an empty JavaScript Object or not. */ function isEmptyObject(obj) { if (!isObject(obj)) { return false; } for (var key in obj) { if (hasOwnProperty.call(obj, key)) { return false; } } return true; } /** * @returns whether the provided parameter is a JavaScript Function or not. */ function isFunction(obj) { return typeof obj === 'function'; } /** * @returns whether the provided parameters is are JavaScript Function or not. */ function areFunctions() { for (var _len = arguments.length, objects = new Array(_len), _key = 0; _key < _len; _key++) { objects[_key] = arguments[_key]; } return objects.length > 0 && objects.every(isFunction); } function validateConstraints(args, constraints) { var len = Math.min(args.length, constraints.length); for (var i = 0; i < len; i++) { validateConstraint(args[i], constraints[i]); } } function validateConstraint(arg, constraint) { if (isString(constraint)) { if (_typeof(arg) !== constraint) { throw new Error("argument does not match constraint: typeof ".concat(constraint)); } } else if (isFunction(constraint)) { try { if (arg instanceof constraint) { return; } } catch (_unused) { // ignore } if (!isUndefinedOrNull(arg) && arg.constructor === constraint) { return; } if (constraint.length === 1 && constraint.call(undefined, arg) === true) { return; } throw new Error("argument does not match one of these constraints: arg instanceof constraint, arg.constructor === constraint, nor constraint(arg) === true"); } } function getAllPropertyNames(obj) { var res = []; var proto = Object.getPrototypeOf(obj); while (Object.prototype !== proto) { res = res.concat(Object.getOwnPropertyNames(proto)); proto = Object.getPrototypeOf(proto); } return res; } function getAllMethodNames(obj) { var methods = []; var _iterator = _createForOfIteratorHelper(getAllPropertyNames(obj)), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var prop = _step.value; if (typeof obj[prop] === 'function') { methods.push(prop); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return methods; } function createProxyObject(methodNames, invoke) { var createProxyMethod = function createProxyMethod(method) { return function () { // eslint-disable-next-line prefer-rest-params var args = Array.prototype.slice.call(arguments, 0); return invoke(method, args); }; }; var result = {}; var _iterator2 = _createForOfIteratorHelper(methodNames), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var methodName = _step2.value; result[methodName] = createProxyMethod(methodName); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } return result; } /** * Converts null to undefined, passes all other values through. */ function withNullAsUndefined(x) { return x === null ? undefined : x; } /** * Converts undefined to null, passes all other values through. */ function withUndefinedAsNull(x) { return typeof x === 'undefined' ? null : x; } /** * Allows to add a first parameter to functions of a type. */ /** * Mapped-type that replaces all occurrences of URI with UriComponents */ /** * Mapped-type that replaces all occurrences of URI with UriComponents and * drops all functions. */ function NotImplementedProxy(name) { return /*#__PURE__*/function () { function _class() { _classCallCheck(this, _class); return new Proxy({}, { get: function get(target, prop) { if (target[prop]) { return target[prop]; } throw new Error("Not Implemented: ".concat(name, "->").concat(String(prop))); } }); } return _createClass(_class); }(); }