pay-sdk-react
Version:
A cross-platform payment SDK for React, supporting Alipay, WeChat Pay, PayPal, Stripe, Payssion, and Airwallex, compatible with H5, PC, and App environments.
122 lines (120 loc) • 3.82 kB
JavaScript
import { isValidElement } from "react";
import { canUseDom } from "./can-use-dom";
import { clientWindow } from "./dom";
const opt = Object.prototype.toString;
export function isArray(obj) {
return opt.call(obj) === "[object Array]";
}
export function isObject(obj) {
return opt.call(obj) === "[object Object]";
}
export function isString(obj) {
return opt.call(obj) === "[object String]";
}
export function isNumber(obj) {
return opt.call(obj) === "[object Number]" && obj === obj;
}
export function isRegExp(obj) {
return opt.call(obj) === "[object RegExp]";
}
export function isFile(obj) {
return opt.call(obj) === "[object File]";
}
export function isBlob(obj) {
return opt.call(obj) === "[object Blob]";
}
function isHex(color) {
return /^#[a-fA-F0-9]{3}$|#[a-fA-F0-9]{6}$/.test(color);
}
function isRgb(color) {
return /^rgb\((\s*\d+\s*,?){3}\)$/.test(color);
}
function isRgba(color) {
return /^rgba\((\s*\d+\s*,\s*){3}\s*\d(\.\d+)?\s*\)$/.test(color);
}
export function isColor(color) {
return isHex(color) || isRgb(color) || isRgba(color);
}
export function isUndefined(obj) {
return obj === undefined;
}
export function isNull(obj) {
return obj === null;
}
export function isNullOrUndefined(obj) {
return obj === null || obj === undefined;
}
export function isFunction(obj) {
return typeof obj === "function";
}
export function isEmptyObject(obj) {
return isObject(obj) && Object.keys(obj).length === 0;
}
export function isEmptyReactNode(content, trim) {
if (content === null || content === undefined || content === false) {
return true;
}
if (typeof content === "string" && (trim ? content.trim() === "" : content === "")) {
return true;
}
return false;
}
export function isExist(obj) {
return obj || obj === 0;
}
export function isWindow(el) {
return el === window;
}
export function isBoolean(value) {
return typeof value === "boolean";
}
export const isReactComponent = element => {
return element && /*#__PURE__*/isValidElement(element) && typeof element.type === "function";
};
export const isClassComponent = element => {
var _element$type$prototy;
return isReactComponent(element) && !!((_element$type$prototy = element.type.prototype) !== null && _element$type$prototy !== void 0 && _element$type$prototy.isReactComponent);
};
// element 是合成的 dom 元素或者字符串,数字等
export const isDOMElement = element => {
return /*#__PURE__*/isValidElement(element) && typeof element.type === "string";
};
export const isLink = text => {
return isString(text) && (text.startsWith("http") || text.startsWith("https"));
};
export async function isImageLinkAvailable(url) {
return new Promise(resolve => {
const img = new Image();
img.onload = () => resolve(true);
img.onerror = () => resolve(false);
img.src = url;
});
}
export function isMobile() {
return clientWindow && /Android|iPhone|iPad|iPod|Windows Phone|BlackBerry|Mobile/i.test(clientWindow === null || clientWindow === void 0 ? void 0 : clientWindow.navigator.userAgent);
}
export function isEmail(email) {
const emailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
return emailReg.test(email);
}
export function isSmsCode(code) {
const codeReg = /\d{6}/;
return codeReg.test(code);
}
export function isUsPhoneNumber(phone) {
const phoneRegex = /^[2-9]\d{2}[2-9]\d{6}$/;
return phoneRegex.test(phone);
}
export function isPromise(obj) {
return !!obj && typeof obj === "object" && typeof obj.then === "function";
}
/**
* 判断是否为Android设备
* @returns 是否为Android设备
*/
export function isAndroid() {
return canUseDom ? /android/.test(navigator.userAgent.toLowerCase()) : false;
}
export function isIOS() {
return canUseDom ? /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase()) : false;
}