@bigfishtv/cockpit
Version:
81 lines (72 loc) • 1.91 kB
JavaScript
/**
* Type Definition Utilities
* @module Utilities/typeUtils
*/
/**
* Returns true/false if provided value is numeric
* @param {*} value - Can be anything
* @return {Boolean}
*/
export function isNumeric(val) {
return !isNaN(parseFloat(val)) && isFinite(val)
}
/**
* Returns true/false if provided value is an array
* @param {*} value - Can be anything
* @return {Boolean}
*/
export function isArray(val) {
return val instanceof Array
}
/**
* Returns true/false if provided value is object (not array)
* @param {*} value - Can be anything
* @return {Boolean}
*/
export function isObject(val) {
return val !== null && typeof val === 'object' && !isArray(val)
}
/**
* Returns true/false if provided value is a string
* @param {*} value - Can be anything
* @return {Boolean}
*/
export function isString(val) {
return typeof val === 'string'
}
/**
* Returns true/false if provided value is a function
* @param {*} value - Can be anything
* @return {Boolean}
*/
export function isFunction(val) {
return typeof val === 'function'
}
/**
* Works the same way as PHP's empty function
* Recursively goes through an object/array and checks all key values
* @param {Object} object Object/array
* @return {Boolean}
*/
export function isEmpty(object) {
if (!isObject(object) && !isArray(object)) return false
let empty = true
const keys = Object.keys(object)
for (let key of keys) {
const value = object[key]
if (!(typeof value == 'undefined' || value === null || value === '')) empty = false
else if (isObject(value) || isArray(value)) empty = isEmpty(value)
}
return empty
}
/**
* Checks if provided variable is string and is url
* @param {String}
* @return {Boolean}
*/
export function isUrl(str) {
if (!isString(str)) return false
const parser = document.createElement('a')
parser.href = str
return parser.host && parser.host != window.location.host
}