volva
Version:
Easier type checks for JS/TS variables.
85 lines • 2.43 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getType = void 0;
const arrays_1 = __importDefault(require("./arrays"));
const bigints_1 = __importDefault(require("./bigints"));
const floats_1 = __importDefault(require("./floats"));
const integers_1 = __importDefault(require("./integers"));
const maps_1 = __importDefault(require("./maps"));
const objects_1 = __importDefault(require("./objects"));
const functions_1 = __importDefault(require("./functions"));
const sets_1 = __importDefault(require("./sets"));
const symbols_1 = __importDefault(require("./symbols"));
const types_1 = require("./constants/types");
/**
* Provides exact type of variable.
* 'typeof' doesnt tell apart type of number etc.
*/
const getType = (input) => {
if (input === undefined)
throw new Error('Missing argument');
const type = typeof input;
/**
* Strings & bools require no extra checks.
*/
if (type === types_1.STRING) {
return types_1.STRING;
}
if (type === types_1.BOOLEAN) {
return types_1.BOOLEAN;
}
/**
* Numerals, arrays & objects can be
* misinterpreted via typeof.
*
* Return more specific types.
*/
if (type === types_1.NUMBER) {
if ((0, integers_1.default)(input)) {
return types_1.INTEGER;
}
if ((0, floats_1.default)(input)) {
return types_1.FLOAT;
}
return types_1.NAN;
}
// Arrays.
if ((0, arrays_1.default)(input)) {
return types_1.ARRAY;
}
// Objects.
if ((0, objects_1.default)(input)) {
return types_1.OBJECT;
}
// Null
if (input === null) {
return types_1.NULL;
}
// Function
if ((0, functions_1.default)(input)) {
return types_1.FUNCTION;
}
// Map
if ((0, maps_1.default)(input)) {
return types_1.MAP;
}
// Set
if ((0, sets_1.default)(input)) {
return types_1.SET;
}
// Bigint
if ((0, bigints_1.default)(input)) {
return types_1.BIGINT;
}
// Symbol
if ((0, symbols_1.default)(input)) {
return types_1.SYMBOL;
}
return type;
};
exports.getType = getType;
exports.default = exports.getType;
//# sourceMappingURL=types.js.map