123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import { isFunction, isObject, isString } from '@vue/shared';
- /**
- * 检查传入的值是否为undefined。
- *
- * @param {unknown} value 要检查的值。
- * @returns {boolean} 如果值是undefined,返回true,否则返回false。
- */
- function isUndefined(value?: unknown): value is undefined {
- return value === undefined;
- }
- /**
- * 检查传入的值是否为空。
- *
- * 以下情况将被认为是空:
- * - 值为null。
- * - 值为undefined。
- * - 值为一个空字符串。
- * - 值为一个长度为0的数组。
- * - 值为一个没有元素的Map或Set。
- * - 值为一个没有属性的对象。
- *
- * @param {T} value 要检查的值。
- * @returns {boolean} 如果值为空,返回true,否则返回false。
- */
- function isEmpty<T = unknown>(value: T): value is T {
- if (value === null || value === undefined) {
- return true;
- }
- if (Array.isArray(value) || isString(value)) {
- return value.length === 0;
- }
- if (value instanceof Map || value instanceof Set) {
- return value.size === 0;
- }
- if (isObject(value)) {
- return Object.keys(value).length === 0;
- }
- return false;
- }
- /**
- * 检查传入的字符串是否为有效的HTTP或HTTPS URL。
- *
- * @param {string} url 要检查的字符串。
- * @return {boolean} 如果字符串是有效的HTTP或HTTPS URL,返回true,否则返回false。
- */
- function isHttpUrl(url?: string): boolean {
- if (!url) {
- return false;
- }
- // 使用正则表达式测试URL是否以http:// 或 https:// 开头
- const httpRegex = /^https?:\/\/.*$/;
- return httpRegex.test(url);
- }
- /**
- * 检查传入的值是否为window对象。
- *
- * @param {any} value 要检查的值。
- * @returns {boolean} 如果值是window对象,返回true,否则返回false。
- */
- function isWindow(value: any): value is Window {
- return (
- typeof window !== 'undefined' && value !== null && value === value.window
- );
- }
- /**
- * 检查当前运行环境是否为Mac OS。
- *
- * 这个函数通过检查navigator.userAgent字符串来判断当前运行环境。
- * 如果userAgent字符串中包含"macintosh"或"mac os x"(不区分大小写),则认为当前环境是Mac OS。
- *
- * @returns {boolean} 如果当前环境是Mac OS,返回true,否则返回false。
- */
- function isMacOs(): boolean {
- const macRegex = /macintosh|mac os x/i;
- return macRegex.test(navigator.userAgent);
- }
- /**
- * 检查当前运行环境是否为Windows OS。
- *
- * 这个函数通过检查navigator.userAgent字符串来判断当前运行环境。
- * 如果userAgent字符串中包含"windows"或"win32"(不区分大小写),则认为当前环境是Windows OS。
- *
- * @returns {boolean} 如果当前环境是Windows OS,返回true,否则返回false。
- */
- function isWindowsOs(): boolean {
- const windowsRegex = /windows|win32/i;
- return windowsRegex.test(navigator.userAgent);
- }
- export {
- isEmpty,
- isFunction,
- isHttpUrl,
- isMacOs,
- isObject,
- isString,
- isUndefined,
- isWindow,
- isWindowsOs,
- };
|