UNPKG

node-easysms

Version:

EasySMS is an SMS sender for Node.js

266 lines (265 loc) 9.84 kB
'use strict'; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isGatewayConstructable = exports.rtrim = exports.ltrim = exports.trim = exports.pad = exports.repeat = exports.buildXml = exports.parseXml = exports.singleItem = exports.applyMixins = exports.parseQueryString = exports.buildQueryString = exports.merge = exports.timestampUTC = exports.timestamp = exports.randomString = exports.createHmac = exports.createHash = void 0; const crypto_1 = __importDefault(require("crypto")); const qs_1 = __importDefault(require("qs")); const xml2js_1 = __importDefault(require("xml2js")); const createHash = function (str, type = 'sha1', encode = 'hex') { return crypto_1.default.createHash(type).update(str).digest(encode); }; exports.createHash = createHash; const createHmac = function (str, key, type = 'sha256', encode = 'hex') { return crypto_1.default.createHmac(type, key).update(str).digest(encode); }; exports.createHmac = createHmac; /** * 生成随机字符串 * @param len 长度,默认:16 * @returns */ const randomString = function (len = 16) { let chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; let str = ''; for (let i = 0; i < len; i++) { str += chars.charAt(Math.floor(Math.random() * chars.length)); } return str; }; exports.randomString = randomString; /** * 获取时间戳 * @param format 默认返回时间戳 * @param date 指定日期对象,默认:当前时间 * @returns */ const timestamp = function (format = null, date = null) { if (!date || !(date instanceof Date)) date = new Date; let str = format || 'u'; str = str.replace(/yyyy|YYYY/, (0, exports.pad)(date.getFullYear(), 4, '0')); str = str.replace(/yy|YY/, (date.getFullYear() % 100) > 8 ? (date.getFullYear() % 100).toString() : '0' + (date.getFullYear() % 100)); str = str.replace(/MM/, date.getMonth() > 8 ? (date.getMonth() + 1).toString() : ('0' + (date.getMonth() + 1))); str = str.replace(/M/g, (date.getMonth() + 1) + ''); str = str.replace(/dd|DD/, (0, exports.pad)(date.getDate(), 2, '0')); str = str.replace(/d|D/g, date.getDate() + ''); str = str.replace(/hh|HH/, (0, exports.pad)(date.getHours(), 2, '0')); str = str.replace(/h|H/g, date.getHours() + ''); str = str.replace(/mm/, (0, exports.pad)(date.getMinutes(), 2, '0')); str = str.replace(/m/g, date.getMinutes() + ''); str = str.replace(/ss|SS/, (0, exports.pad)(date.getSeconds(), 2, '0')); str = str.replace(/s|S/g, date.getSeconds() + ''); str = str.replace(/u/g, parseInt((date.getTime() / 1000).toString()) + ''); return str; }; exports.timestamp = timestamp; /** * 获取UTC时间戳 * @param format 默认返回时间戳 * @param date 指定日期对象,默认:当前时间 * @returns */ const timestampUTC = function (format = null, date = null) { if (!date || !(date instanceof Date)) date = new Date; let str = format || 'u'; str = str.replace(/yyyy|YYYY/, (0, exports.pad)(date.getUTCFullYear(), 4, '0')); str = str.replace(/yy|YY/, (date.getUTCFullYear() % 100) > 8 ? (date.getUTCFullYear() % 100).toString() : '0' + (date.getUTCFullYear() % 100)); str = str.replace(/MM/, date.getUTCMonth() > 8 ? (date.getUTCMonth() + 1).toString() : ('0' + (date.getUTCMonth() + 1))); str = str.replace(/M/g, (date.getUTCMonth() + 1) + ''); str = str.replace(/dd|DD/, (0, exports.pad)(date.getUTCDate(), 2, '0')); str = str.replace(/d|D/g, date.getUTCDate() + ''); str = str.replace(/hh|HH/, (0, exports.pad)(date.getUTCHours(), 2, '0')); str = str.replace(/h|H/g, date.getUTCHours() + ''); str = str.replace(/mm/, (0, exports.pad)(date.getUTCMinutes(), 2, '0')); str = str.replace(/m/g, date.getUTCMinutes() + ''); str = str.replace(/ss|SS/, (0, exports.pad)(date.getUTCSeconds(), 2, '0')); str = str.replace(/s|S/g, date.getUTCSeconds() + ''); str = str.replace(/u/g, parseInt((Date.parse(new Date().toISOString()) / 1000).toString()) + ''); return str; }; exports.timestampUTC = timestampUTC; const merge = (target, source) => { if (Object.prototype.toString.call(source) == '[object Object]') { if (source.constructor !== Object) { target = source; } else { if (!target || Object.prototype.toString.call(target) != '[object Object]') { target = {}; } Object.keys(source).map((k) => { if (!target[k]) { target[k] = null; } target[k] = (0, exports.merge)(target[k], source[k]); }); } } else if (Array.isArray(source)) { if (!target || !Array.isArray(target)) { target = []; } target = target.concat(target, source); } else { target = source; } return target; }; exports.merge = merge; const buildQueryString = function (data, options = {}) { return qs_1.default.stringify(data, options); }; exports.buildQueryString = buildQueryString; const parseQueryString = function (data, options = {}) { return qs_1.default.parse(data, options); }; exports.parseQueryString = parseQueryString; /** * 类应用混入方法 * @param derivedCtor 目标类 * @param constructors 混入类列表 */ const applyMixins = function (derivedCtor, constructors) { constructors.forEach((baseCtor) => { Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => { // 构造函数或目标类已有的方法,则以目标类的为准 if (name === 'constructor' || typeof derivedCtor.prototype[name] !== 'undefined') return; Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name) || Object.create(null)); }); }); }; exports.applyMixins = applyMixins; /** * 如果只有一个同名、同级节点,则不当作数组 * @param obj * @returns */ const singleItem = function (obj) { if (typeof obj == 'object') { if (typeof obj.length != 'undefined') { if (obj.length == 1) { return (0, exports.singleItem)(obj[0]); } for (let i = 0; i < obj.length; i++) { obj[i] = (0, exports.singleItem)(obj[i]); } return obj; } else { for (let k in obj) { obj[k] = (0, exports.singleItem)(obj[k]); } } } return obj; }; exports.singleItem = singleItem; /** * 解析xml * @param xml * @returns */ const parseXml = function (xml) { return __awaiter(this, void 0, void 0, function* () { let res = yield xml2js_1.default.parseStringPromise(xml); res = (0, exports.singleItem)(res); return res; }); }; exports.parseXml = parseXml; /** * 构建xml * @param data 对象 * @param rootName 根节点名,默认:'xml' * @returns */ const buildXml = function (data, rootName = 'xml') { let XmlBuilder = new xml2js_1.default.Builder({ cdata: true, xmldec: null, rootName, renderOpts: { pretty: false, indent: '', newline: '', } }); return XmlBuilder.buildObject(data).replace('<?xml version="1.0"?>', ''); }; exports.buildXml = buildXml; /** * 将字符串复制为多份 * @param str 要复制的字符串 * @param num 要复制的次数 */ const repeat = (str, num) => { return new Array(num + 1).join(str); }; exports.repeat = repeat; /** * 给字符串填充字符 * @param str 原字符串 * @param len 要填充到的字符串长度 * @param chr 要填充的字符 * @param leftJustify Ture 表示左侧填充,否则反之 */ const pad = function (str, len, chr = ' ', leftJustify = true) { if (typeof str !== 'string') str = str + ''; let padding = (str.length >= len) ? '' : (0, exports.repeat)(chr, len - str.length >>> 0); return leftJustify ? padding + str : str + padding; }; exports.pad = pad; /** * 去除字符串左右的空格 * @param str 原字符串 */ const trim = function (str) { if (typeof str !== 'string') str = str + ''; return str.replace(/^\s+/, '').replace(/\s+$/, ''); }; exports.trim = trim; /** * 去除字符串左侧的空格 * @param str 原字符串 */ const ltrim = function (str) { if (typeof str !== 'string') str = str + ''; return str.replace(/^\s+/, ''); }; exports.ltrim = ltrim; /** * 去除字符串右侧的空格 * @param str 原字符串 */ const rtrim = function (str) { if (typeof str !== 'string') str = str + ''; return str.replace(/\s+$/, ''); }; exports.rtrim = rtrim; /** * 判断是否网关类型 * @param obj * @returns */ const isGatewayConstructable = function (obj) { return typeof obj === 'function' && typeof obj.prototype.send === 'function'; }; exports.isGatewayConstructable = isGatewayConstructable;