UNPKG

rcs-data

Version:

RCS消息数据结构

153 lines 4.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cloneObject = exports.cloneArray = exports.getRandomTestNetIP = exports.headerize = exports.normalizeTarget = exports.newUUID = exports.newTag = exports.hasMethods = exports.isEmpty = exports.isDecimal = exports.isString = exports.isFunction = exports.str_utf8_length = void 0; const URI = require('./URI'); const str_utf8_length = (string) => unescape(encodeURIComponent(string)).length; exports.str_utf8_length = str_utf8_length; const isFunction = (fn) => { if (fn !== undefined) { return Object.prototype.toString.call(fn) === '[object Function]' ? true : false; } else { return false; } }; exports.isFunction = isFunction; const isString = (str) => { if (str !== undefined) { return Object.prototype.toString.call(str) === '[object String]' ? true : false; } else { return false; } }; exports.isString = isString; const isDecimal = (num) => !isNaN(num) && parseFloat(num) === parseInt(num, 10); exports.isDecimal = isDecimal; const isEmpty = (value) => { return (value === null || value === '' || value === undefined || (Array.isArray(value) && value.length === 0) || (typeof value === 'number' && isNaN(value))); }; exports.isEmpty = isEmpty; const hasMethods = function (obj, ...methodNames) { for (const methodName of methodNames) { if ((0, exports.isFunction)(obj[methodName])) { return false; } } return true; }; exports.hasMethods = hasMethods; const createRandomToken = (exports.createRandomToken = (size, base = 32) => { let i, r, token = ''; for (i = 0; i < size; i++) { r = (Math.random() * base) | 0; token += r.toString(base); } return token; }); const newTag = () => createRandomToken(10); exports.newTag = newTag; const newUUID = () => { const UUID = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { const r = (Math.random() * 16) | 0, v = c === 'x' ? r : (r & 0x3) | 0x8; return v.toString(16); }); return UUID; }; exports.newUUID = newUUID; const escapeUser = (exports.escapeUser = (user) => encodeURIComponent(decodeURIComponent(user)) .replace(/%3A/gi, ':') .replace(/%2B/gi, '+') .replace(/%3F/gi, '?') .replace(/%2F/gi, '/')); const normalizeTarget = (target, domain) => { if (!target) { return; } else if (target instanceof URI) { return target; } else if (typeof target === 'string') { const target_array = target.split('@'); let target_user; let target_domain; switch (target_array.length) { case 1: if (!domain) { return; } target_user = target; target_domain = domain; break; case 2: target_user = target_array[0]; target_domain = target_array[1]; break; default: target_user = target_array.slice(0, target_array.length - 1).join('@'); target_domain = target_array[target_array.length - 1]; } target_user = target_user.replace(/^(sips?|tel):/i, ''); if (/^[-.()]*\+?[0-9\-.()]+$/.test(target_user)) { target_user = target_user.replace(/[-.()]/g, ''); } target = `sip:${escapeUser(target_user)}@${target_domain}`; let uri; if ((uri = URI.parse(target))) { return uri; } else { return; } } else { return; } }; exports.normalizeTarget = normalizeTarget; const headerize = (string) => { const exceptions = { 'Call-Id': 'Call-ID', Cseq: 'CSeq', 'Www-Authenticate': 'WWW-Authenticate', }; const name = string.toLowerCase().replace(/_/g, '-').split('-'); let hname = ''; const parts = name.length; let part; for (part = 0; part < parts; part++) { if (part !== 0) { hname += '-'; } hname += name[part].charAt(0).toUpperCase() + name[part].substring(1); } if (exceptions[hname]) { hname = exceptions[hname]; } return hname; }; exports.headerize = headerize; const getRandomTestNetIP = () => { function getOctet(from, to) { return Math.floor(Math.random() * (to - from + 1) + from); } return `192.0.2.${getOctet(1, 254)}`; }; exports.getRandomTestNetIP = getRandomTestNetIP; const cloneArray = (array) => { return (array && array.slice()) || []; }; exports.cloneArray = cloneArray; const cloneObject = (obj, fallback = {}) => { return (obj && Object.assign({}, obj)) || fallback; }; exports.cloneObject = cloneObject; //# sourceMappingURL=Utils.js.map