JSDoc: Source: noop.js

/**
 * NOOP Main module.
 * @namespace noop
 * @author cisco211
 * @copyright © 2019 by "cisco211"
 * @version 0.100
 */
(function(noop, undefined)
{
	'use strict';

	// #region Private
	/**
	 * @summary Byte to human unit string array.
	 * @private
	 * @static
	 * @const
	 * @memberof noop
	 * @type {array.<string>}
	 */
	const byte2humanUnits_ =
	[
		'Byte',
		'KByte',
		'MByte',
		'GByte',
		'TByte',
		'PByte',
		'EByte',
		'ZByte',
		'YByte'
	]; // var byte2humanUnits_

	/**
	 * @summary Byte to human unit string array size.
	 * @private
	 * @static
	 * @const
	 * @memberof noop
	 * @type {integer}
	 */
	const byte2humanUnitsLength_ = byte2humanUnits_.length;
	// #endregion Private

	// #region Public
	/**
	 * @summary Size in bytes to human readable size.
	 * @function byte2human
	 * @memberof noop
	 * @param {(number|string)} size - A number representing size in bytes.
	 * @param {boolean} reverse - A boolean flag to reverse the math (human -> bytes).
	 * @param {boolean} fake - A boolean flag to reverse the math (human -> bytes).
	 * @return {string} humanSize - A string of a human readable size.
	 * @example var humanSize = noop.byte2human(4194304, false, false);
	 */
	noop.byte2human = function(size, reverse, fake)
	{
		if (typeof size === 'string')
			size = parseInt(size);
		else if (typeof size === 'number')
			size = size;
		else
			return size;
		if (typeof reverse !== 'boolean')
			reverse = false;
		if (typeof fake !== 'boolean')
			fake = false;
		var factor = 1024;
		var i = 0;
		if (fake)
			factor = 1000;
		while (size >= factor && i < byte2humanUnitsLength_)
		{
			if (reverse)
				size *= factor;
			else
				size /= factor;
			i++;
		}
		return size.toFixed(2) + ' ' + byte2humanUnits_[i];
	} // byte2human = function(size, reverse, fake)

	/**
	 * @summary Check that module exists.
	 * @function has
	 * @memberof noop
	 * @param {string} name - A module name.
	 * @return {boolean} - A boolean indicating if the given module exists or not.
	 * @example var hasAjax = noop.has('ajax');
	 */
	noop.has = function(name)
	{
		return noop.isObject(noop[name]);
	}; // noop.has = function(name)

	/**
	 * @summary Test that the given value is an array.
	 * @function isArray
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isArray = noop.isArray([1, 2, 3]);
	 */
	noop.isArray = function(x)
	{
		if (noop.isNull(x))
			return false;
		return noop.isType(x, 'object') && noop.isDefined(x.length);
	}; // noop.isArray = function(x)

	/**
	 * @summary Test that the given value is a boolean.
	 * @function isBoolean
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isBoolean = noop.isBoolean(true);
	 */
	noop.isBoolean = function(x)
	{
		if (noop.isString(x))
		{
			x = x.toLowerCase();
			return x === 'true' || x === 'false';
		}
		return noop.isType(x, 'boolean');
	}; // noop.isBoolean = function(x)

	/**
	 * @summary Test that the given value is defined.
	 * @function isDefined
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isDefined = noop.isDefined(null);
	 */
	noop.isDefined = function(x)
	{
		return !noop.isUndefined(x);
	}; // noop.isDefined = function(x)

	/**
	 * @summary Test that the given value is empty.
	 * @function isEmpty
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isEmpty = noop.isEmpty([]);
	 */
	noop.isEmpty = function(x)
	{
		if (noop.isNull(x) || noop.isUndefined(x))
			return true;
		if (noop.isArray(x) || noop.isString(x))
			return x.length === 0;
		if (noop.isObject(x))
			return Object.keys(x).length === 0;
		return false;
	}; // noop.isEmpty = function(x)

	/**
	 * @summary Test that the given values are equal.
	 * @function isEqual
	 * @memberof noop
	 * @param {any} a - The first value.
	 * @param {any} b - The second value.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isEqual = noop.isEqual(211, 211);
	 */
	noop.isEqual = function(a, b)
	{
		if (typeof a !== typeof b)
			return false;
		if (noop.isArray(a))
		{
			return a.length === b.length && a.every(function(v, i)
			{
				return v === b[i]
			});
		}
		if (noop.isObject(a))
			return JSON.stringify(a) === JSON.stringify(b);
		return a === b;
	}; // noop.isEqual = function(a, b)

	/**
	 * @summary Test that the given value is a float.
	 * @function isFloat
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isFloat = noop.isFloat(2.11);
	 */
	noop.isFloat = function(x)
	{
		if (noop.isString(x))
			return parseFloat(x) == x && x % 1 !== 0;
		return noop.isNumber(x) && x % 1 !== 0;
	}; // noop.isFloat = function(x)

	/**
	 * @summary Test that the given value is a function.
	 * @function isFunction
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isFunction = noop.isFunction(() => {});
	 */
	noop.isFunction = function(x)
	{
		return noop.isType(x, 'function');
	}; // noop.isFunction = function(x)

	/**
	 * @summary Test that the given value is an integer.
	 * @function isInteger
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isInteger = noop.isInteger(211);
	 */
	noop.isInteger = function(x)
	{
		if (noop.isString(x))
			return parseInt(x) == x && x % 1 === 0;
		return noop.isNumber(x) && x % 1 === 0;
	}; // noop.isInteger = function(x)

	/**
	 * @summary Test that the given value is NaN.
	 * @function isNaN
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isNaN = noop.isNaN(NaN);
	 */
	noop.isNaN = function(x)
	{
		if (noop.isString(x))
			return Number.isNaN(parseFloat(x));
		if (noop.isNumber(x))
			return Number.isNaN(x);
		return true;
	}; // noop.isNaN = function(x)

	/**
	 * @summary Test that the given value is null.
	 * @function isNull
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isNull = noop.isNull(null);
	 */
	noop.isNull = function(x)
	{
		return x === null;
	}; // noop.isNull = function(x)

	/**
	 * @summary Test that the given value is a number.
	 * @function isNumber
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isNumber = noop.isNumber(211);
	 */
	noop.isNumber = function(x)
	{
		if (noop.isString(x))
			return !noop.isNaN(x);
		return noop.isType(x, 'number');
	}; // noop.isNumber = function(x)

	/**
	 * @summary Test that the given value is an object.
	 * @function isObject
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isObject = noop.isObject({hello: 'World'});
	 */
	noop.isObject = function(x)
	{
		return noop.isType(x, 'object') && !noop.isNull(x) && !noop.isDefined(x.length);
	}; // noop.isObject = function(x)

	/**
	 * @summary Test that the given value is set.
	 * @function isSet
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isSet = noop.isSet(window.noop);
	 */
	noop.isSet = function(x)
	{
		return noop.isDefined(x) && !noop.isNull(x);
	}; // noop.isSet = function(x)

	/**
	 * @summary Test that the given value is a string.
	 * @function isString
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isString = noop.isString('Hello World!');
	 */
	noop.isString = function(x)
	{
		return noop.isType(x, 'string');
	}; // noop.isString = function(x)

	/**
	 * @summary Test that the given value is a symbol.
	 * @function isSymbol
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isSymbol = noop.isSymbol(Symbol());
	 */
	noop.isSymbol = function(x)
	{
		return noop.isType(x, 'symbol');
	}; // noop.isSymbol = function(x)

	/**
	 * @summary Test that the given value is a given type.
	 * @function isType
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @param {string} t - The type to be expected.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isType = noop.isType({}, 'object');
	 */
	noop.isType = function(x, t)
	{
		return typeof x === t;
	}; // noop.isType = function(x, t)

	/**
	 * @summary Test that the given value is undefined.
	 * @function isUndefined
	 * @memberof noop
	 * @param {any} x - The value to be tested.
	 * @return {boolean} - A boolean indicating the result of the test.
	 * @example var isUndefined = noop.isUndefined(undefined);
	 */
	noop.isUndefined = function(x)
	{
		return !!noop.isType(x, 'undefined');
	}; // noop.isUndefined = function(x)

	/**
	 * @summary Return name list of loaded NOOP modules.
	 * @function modules
	 * @memberof noop
	 * @return {array.<string>} - A name list of loaded NOOP modules.
	 * @example var modules = noop.modules();
	 */
	noop.modules = function()
	{
		var l = Object.entries(this);
		var o = [];
		var c = 0;
		for (const [k, e] of l)
		{
			if (!noop.isObject(e))
				continue;
			o[c] = k;
			c++;
		}
		return o.sort();
	};
	// #endregion Public

} // (function(noop, undefined)
(window.noop = window.noop || new class NOOP{}));

// EOF