/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.countToArray = exports.count = exports.create = exports.validateConstraint = exports.validateConstraints = exports.areFunctions = exports.isFunction = exports.isEmptyObject = exports.isUndefinedOrNull = exports.isUndefined = exports.isBoolean = exports.isNumber = exports.isObject = exports.isStringArray = exports.isString = exports.isArray = void 0; const _typeof = { number: 'number', string: 'string', undefined: 'undefined', object: 'object', function: 'function' }; /** * @returns whether the provided parameter is a JavaScript Array or not. */ function isArray(array) { if (Array.isArray) { return Array.isArray(array); } if (array && typeof (array.length) === _typeof.number && array.constructor === Array) { return true; } return false; } exports.isArray = isArray; /** * @returns whether the provided parameter is a JavaScript String or not. */ function isString(str) { if (typeof (str) === _typeof.string || str instanceof String) { return true; } return false; } exports.isString = isString; /** * @returns whether the provided parameter is a JavaScript Array and each element in the array is a string. */ function isStringArray(value) { return isArray(value) && value.every(elem => isString(elem)); } exports.isStringArray = isStringArray; /** * * @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 === _typeof.object && obj !== null && !Array.isArray(obj) && !(obj instanceof RegExp) && !(obj instanceof Date); } exports.isObject = isObject; /** * 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) { if ((typeof (obj) === _typeof.number || obj instanceof Number) && !isNaN(obj)) { return true; } return false; } exports.isNumber = isNumber; /** * @returns whether the provided parameter is a JavaScript Boolean or not. */ function isBoolean(obj) { return obj === true || obj === false; } exports.isBoolean = isBoolean; /** * @returns whether the provided parameter is undefined. */ function isUndefined(obj) { return typeof (obj) === _typeof.undefined; } exports.isUndefined = isUndefined; /** * @returns whether the provided parameter is undefined or null. */ function isUndefinedOrNull(obj) { return isUndefined(obj) || obj === null; } exports.isUndefinedOrNull = isUndefinedOrNull; const 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 (let key in obj) { if (hasOwnProperty.call(obj, key)) { return false; } } return true; } exports.isEmptyObject = isEmptyObject; /** * @returns whether the provided parameter is a JavaScript Function or not. */ function isFunction(obj) { return typeof obj === _typeof.function; } exports.isFunction = isFunction; /** * @returns whether the provided parameters is are JavaScript Function or not. */ function areFunctions(...objects) { return objects && objects.length > 0 && objects.every(isFunction); } exports.areFunctions = areFunctions; function validateConstraints(args, constraints) { const len = Math.min(args.length, constraints.length); for (let i = 0; i < len; i++) { validateConstraint(args[i], constraints[i]); } } exports.validateConstraints = validateConstraints; function validateConstraint(arg, constraint) { if (isString(constraint)) { if (typeof arg !== constraint) { throw new Error(`argument does not match constraint: typeof ${constraint}`); } } else if (isFunction(constraint)) { if (arg instanceof constraint) { return; } if (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`); } } exports.validateConstraint = validateConstraint; /** * Creates a new object of the provided class and will call the constructor with * any additional argument supplied. */ function create(ctor, ...args) { let obj = Object.create(ctor.prototype); ctor.apply(obj, args); return obj; } exports.create = create; function count(fromOrTo, toOrCallback, callback) { var from, to; if (isNumber(toOrCallback)) { from = fromOrTo; to = toOrCallback; } else { from = 0; to = fromOrTo; callback = toOrCallback; } var op = from <= to ? (i) => i + 1 : (i) => i - 1; var cmp = from <= to ? (a, b) => a < b : (a, b) => a > b; for (var i = from; cmp(i, to); i = op(i)) { callback(i); } } exports.count = count; function countToArray(fromOrTo, to) { var result = []; var fn = (i) => result.push(i); if (isUndefined(to)) { count(fromOrTo, fn); } else { count(fromOrTo, to, fn); } return result; } exports.countToArray = countToArray; //# sourceMappingURL=primitives.js.map