You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
// 函数 isDef 用于判断传入的值是否为已定义(即不是 undefined 也不是 null)
// 返回一个布尔值,如果值既不是 undefined 也不是 null, 则返回 true, 否则返回 false
function isDef ( value ) {
return value !== undefined && value !== null ;
}
// 函数 isObj 用于判断传入的参数是否为对象类型(包括普通对象、函数等符合 JavaScript 中对象定义的情况,但要排除 null)
// 首先获取参数的类型(通过 typeof 操作符),然后判断参数不为 null 且类型是 'object' 或者 'function' 时,返回 true, 否则返回 false
function isObj ( x ) {
const type = typeof x ;
return x !== null && ( type === 'object' || type === 'function' ) ;
}
// 函数 isNumber 用于简单判断传入的字符串是否表示一个纯数字(只包含数字字符)
// 通过正则表达式 /^\d+$/ 来测试传入的字符串,如果匹配成功,表示字符串只由数字组成,返回 true, 否则返回 false
// 注意:该函数对于如 "12.3" 这样的浮点数字符串会判断为 false, 有一定的局限性, 仅适用于判断正整数形式的字符串
function isNumber ( value ) {
return /^\d+$/ . test ( value ) ;
}
// 函数 range 用于将给定的数字 num 限定在指定的最小值 min 和最大值 max 范围内
// 先通过 Math.max 取 num 和 min 中的较大值,确保返回值不会小于 min, 再通过 Math.min 取上述较大值与 max 中的较小值,确保返回值不会大于 max
// 最终返回限定在 [min, max] 范围内的值
function range ( num , min , max ) {
return Math . min ( Math . max ( num , min ) , max ) ;
}
// 将 isObj、isDef、isNumber、range 这四个函数作为模块的导出项,以便其他模块可以引入并使用这些函数
export { isObj , isDef , isNumber , range } ;