LookAtMySuitBot/js/node_modules/typpy/lib/index.js

86 lines
2.1 KiB
JavaScript

"use strict";
require("function.name");
/**
* Typpy
* Gets the type of the input value or compares it
* with a provided type.
*
* Usage:
*
* ```js
* Typpy({}) // => "object"
* Typpy(42, Number); // => true
* Typpy.get([], "array"); => true
* ```
*
* @name Typpy
* @function
* @param {Anything} input The input value.
* @param {Constructor|String} target The target type.
* It could be a string (e.g. `"array"`) or a
* constructor (e.g. `Array`).
* @return {String|Boolean} It returns `true` if the
* input has the provided type `target` (if was provided),
* `false` if the input type does *not* have the provided type
* `target` or the stringified type of the input (always lowercase).
*/
function Typpy(input, target) {
if (arguments.length === 2) {
return Typpy.is(input, target);
}
return Typpy.get(input, true);
}
/**
* Typpy.is
* Checks if the input value has a specified type.
*
* @name Typpy.is
* @function
* @param {Anything} input The input value.
* @param {Constructor|String} target The target type.
* It could be a string (e.g. `"array"`) or a
* constructor (e.g. `Array`).
* @return {Boolean} `true`, if the input has the same
* type with the target or `false` otherwise.
*/
Typpy.is = function (input, target) {
return Typpy.get(input, typeof target === "string") === target;
};
/**
* Typpy.get
* Gets the type of the input value. This is used internally.
*
* @name Typpy.get
* @function
* @param {Anything} input The input value.
* @param {Boolean} str A flag to indicate if the return value
* should be a string or not.
* @return {Constructor|String} The input value constructor
* (if any) or the stringified type (always lowercase).
*/
Typpy.get = function (input, str) {
if (typeof input === "string") {
return str ? "string" : String;
}
if (null === input) {
return str ? "null" : null;
}
if (undefined === input) {
return str ? "undefined" : undefined;
}
if (input !== input) {
return str ? "nan" : NaN;
}
return str ? input.constructor.name.toLowerCase() : input.constructor;
};
module.exports = Typpy;