UNPKG

weex-ui

Version:

A rich interaction, lightweight, high performance UI library based on Weex

203 lines (194 loc) 5.69 kB
/** * CopyRight (C) 2017-2022 Alibaba Group Holding Limited. * Created by Tw93 on 17/11/01 */ import UrlParser from 'url-parse'; const Utils = { UrlParser: UrlParser, _typeof (obj) { return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase(); }, isPlainObject (obj) { return Utils._typeof(obj) === 'object'; }, isString (obj) { return typeof(obj) === 'string'; }, isNonEmptyArray (obj = []) { return obj && obj.length > 0 && Array.isArray(obj) && typeof obj !== 'undefined'; }, isObject (item) { return (item && typeof item === 'object' && !Array.isArray(item)); }, isEmptyObject (obj) { return Object.keys(obj).length === 0 && obj.constructor === Object; }, mergeDeep (target, ...sources) { if (!sources.length) return target; const source = sources.shift(); if (Utils.isObject(target) && Utils.isObject(source)) { for (const key in source) { if (Utils.isObject(source[key])) { if (!target[key]) { Object.assign(target, { [key]: {} }); } Utils.mergeDeep(target[key], source[key]); } else { Object.assign(target, { [key]: source[key] }); } } } return Utils.mergeDeep(target, ...sources); }, appendProtocol (url) { if (/^\/\//.test(url)) { const { bundleUrl } = weex.config; return `http${/^https:/.test(bundleUrl) ? 's' : ''}:${url}`; } return url; }, encodeURLParams (url) { const parsedUrl = new UrlParser(url, true); return parsedUrl.toString(); }, goToH5Page (jumpUrl, animated = false, callback = null) { const Navigator = weex.requireModule('navigator') const jumpUrlObj = new Utils.UrlParser(jumpUrl, true); const url = Utils.appendProtocol(jumpUrlObj.toString()); Navigator.push({ url: Utils.encodeURLParams(url), animated: animated, }, callback); }, env: { isTaobao () { let { appName } = weex.config.env; return /(tb|taobao|淘宝)/i.test(appName); }, isTrip () { let { appName } = weex.config.env; return appName === 'LX'; }, isWeb () { let { platform } = weex.config.env; return typeof(window) === 'object' && platform.toLowerCase() === 'web'; }, isIOS () { let { platform } = weex.config.env; return platform.toLowerCase() === 'ios'; }, isAndroid () { let { platform } = weex.config.env; return platform.toLowerCase() === 'android'; }, isAlipay () { let { appName } = weex.config.env; return appName === 'AP'; }, isAlipayWeb () { return Utils.env.isAlipay() && Utils.env.isWeb(); }, supportsEB () { const weexVersion = weex.config.env.weexVersion || '0'; const isHighWeex = Utils.compareVersion(weexVersion, '0.10.1.4') && (Utils.env.isIOS() || Utils.env.isAndroid()); const expressionBinding = weex.requireModule('expressionBinding'); return expressionBinding && expressionBinding.enableBinding && isHighWeex; }, /** * 判断Android容器是否支持是否支持expressionBinding(处理方式很不一致) * @returns {boolean} */ supportsEBForAndroid () { return (Utils.env.isAndroid()) && Utils.env.supportsEB(); }, /** * 判断IOS容器是否支持是否支持expressionBinding * @returns {boolean} */ supportsEBForIos () { return (Utils.env.isIOS()) && Utils.env.supportsEB(); }, /** * 获取weex屏幕真实的设置高度,需要减去导航栏高度 * @returns {Number} */ getPageHeight () { const { env } = weex.config; const navHeight = Utils.env.isWeb() ? 0 : 130; return env.deviceHeight / env.deviceWidth * 750 - navHeight; } }, /** * 版本号比较 * @memberOf Utils * @param currVer {string} * @param promoteVer {string} * @returns {boolean} * @example * * const { Utils } = require('@ali/wx-bridge'); * const { compareVersion } = Utils; * console.log(compareVersion('0.1.100', '0.1.11')); // 'true' */ compareVersion (currVer = "0.0.0", promoteVer = "0.0.0") { if (currVer === promoteVer) return true; const currVerArr = currVer.split("."); const promoteVerArr = promoteVer.split("."); const len = Math.max(currVerArr.length, promoteVerArr.length); for (let i = 0; i < len; i++) { let proVal = ~~promoteVerArr[i]; let curVal = ~~currVerArr[i]; if (proVal < curVal) { return true; } else if (proVal > curVal) { return false; } } return false; }, /** * 分割数组 * @param arr 被分割数组 * @param size 分割数组的长度 * @returns {Array} */ arrayChunk (arr = [], size = 4) { let groups = []; if (arr && arr.length > 0) { groups = arr.map((e, i) => { return i % size === 0 ? arr.slice(i, i + size) : null; }).filter(e => { return e; }); } return groups; }, truncateString (str, len, hasDot = true) { let newLength = 0; let newStr = ""; let singleChar = ''; const chineseRegex = /[^\x00-\xff]/g; const strLength = str.replace(chineseRegex, '**').length; for (let i = 0; i < strLength; i++) { singleChar = str.charAt(i).toString(); if (singleChar.match(chineseRegex) !== null) { newLength += 2; } else { newLength++; } if (newLength > len) { break; } newStr += singleChar; } if (hasDot && strLength > len) { newStr += '...'; } return newStr; } }; export default Utils;