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.
144 lines (142 loc) • 4.87 kB
JavaScript
import _typeof from "@babel/runtime-corejs3/helpers/esm/typeof";
import _asyncToGenerator from "@babel/runtime-corejs3/helpers/esm/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator";
import _trimInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/trim";
import { isValidElement } from 'react';
import { canUseDom } from './can-use-dom';
import { clientWindow } from './dom';
var 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 ? _trimInstanceProperty(content).call(content) === '' : 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 var isReactComponent = function isReactComponent(element) {
return element && /*#__PURE__*/isValidElement(element) && typeof element.type === 'function';
};
export var isClassComponent = function 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 var isDOMElement = function isDOMElement(element) {
return /*#__PURE__*/isValidElement(element) && typeof element.type === 'string';
};
export var isLink = function isLink(text) {
return isString(text) && (text.startsWith('http') || text.startsWith('https'));
};
export function isImageLinkAvailable(_x) {
return _isImageLinkAvailable.apply(this, arguments);
}
function _isImageLinkAvailable() {
_isImageLinkAvailable = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(url) {
return _regeneratorRuntime.wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
return _context.abrupt("return", new Promise(function (resolve) {
var img = new Image();
img.onload = function () {
return resolve(true);
};
img.onerror = function () {
return resolve(false);
};
img.src = url;
}));
case 1:
case "end":
return _context.stop();
}
}, _callee);
}));
return _isImageLinkAvailable.apply(this, arguments);
}
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) {
var emailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
return emailReg.test(email);
}
export function isSmsCode(code) {
var codeReg = /\d{6}/;
return codeReg.test(code);
}
export function isUsPhoneNumber(phone) {
var 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;
}