UNPKG

node-socialite

Version:

Socialite is an OAuth2 authentication tool for Node.js

182 lines (181 loc) 6.46 kB
'use strict'; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatTime = exports.generateHmac = exports.generateHash = exports.strCamel = exports.strStudly = exports.strLcwords = exports.strUcwords = exports.inArray = exports.isFunction = exports.isObject = exports.isNumber = exports.isArray = exports.isString = exports.parseQueryString = exports.buildQueryString = exports.merge = void 0; const qs_1 = __importDefault(require("qs")); const crypto_1 = __importDefault(require("crypto")); const merge = (target, source) => { if ((0, exports.isObject)(source)) { if (source.constructor !== Object) { target = source; } else { if (!target || !(0, exports.isObject)(target)) { target = {}; } Object.keys(source).map((k) => { if (!target[k]) { target[k] = null; } target[k] = (0, exports.merge)(target[k], source[k]); }); } } else if ((0, exports.isArray)(source)) { if (!target || !(0, exports.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; const isString = function (data) { return Object.prototype.toString.call(data) == '[object String]'; }; exports.isString = isString; const isArray = function (data) { return Object.prototype.toString.call(data) == '[object Array]'; }; exports.isArray = isArray; const isNumber = function (data) { return Object.prototype.toString.call(data) == '[object Number]'; }; exports.isNumber = isNumber; const isObject = function (data) { return Object.prototype.toString.call(data) == '[object Object]'; }; exports.isObject = isObject; const isFunction = function (data) { return data && toString.call(data) == '[object Function]' || toString.call(data) == '[object AsyncFunction]'; }; exports.isFunction = isFunction; const inArray = function (data, arr, strict = false) { if (!(0, exports.isArray)(arr)) return strict ? data === arr : data == arr; if ((0, exports.isFunction)(arr.findIndex)) { return arr.findIndex((o) => { return strict ? o === data : o == data; }) > -1; } else { let flag = false; for (let i = 0; i < arr.length; i++) { if (strict ? data === arr[i] : data == arr[i]) { flag = true; break; } } return flag; } }; exports.inArray = inArray; // 将单词首字母转成大写,'hello word' => 'Hello World' const strUcwords = function (str) { return str.replace(/\b[a-z]/gi, function (letter) { return letter.toUpperCase(); }); }; exports.strUcwords = strUcwords; // 将单词首字母转成小写,'Hello World' => 'hello word' const strLcwords = function (str) { return str.replace(/\b[a-z]/gi, function (letter) { return letter.toLowerCase(); }); }; exports.strLcwords = strLcwords; // 驼峰(首字母大写),'hello word' => 'HelloWorld' const strStudly = function (value) { return (0, exports.strUcwords)(value.replace(/[\-|\_]/gi, ' ')).replace(/\s/gi, ''); }; exports.strStudly = strStudly; // 驼峰(首字母小写),'hello word' => 'helloWorld' const strCamel = function (value) { return (0, exports.strLcwords)((0, exports.strStudly)(value)); }; exports.strCamel = strCamel; /** * 生成哈希 * @param str 原文字符串 * @param type 哈希方式,可选:sha1、md5等待,默认:sha1 * @param target 生成的目标类型,可选:latin1、hex、base64,默认:hex */ const generateHash = function (str, type = 'sha1', target = 'hex') { return crypto_1.default.createHash(type).update(str).digest(target); }; exports.generateHash = generateHash; /** * 生成哈希校验码 * @param str 原文字符串 * @param type 加密方式,可选:sha256等待,默认:sha256 * @param target 生成的目标类型,可选:latin1、hex、base64,默认:hex */ const generateHmac = function (str, key, type = 'sha256', target = 'hex') { return crypto_1.default.createHmac(type, key).update(str).digest(target); }; exports.generateHmac = generateHmac; /** * 格式化时间 * @param fmt 日期格式 * @param timeStr 时间字符串 * @returns */ const formatTime = function (fmt, timeStr = null) { let date = null; if (!timeStr) { date = new Date; } else if (typeof timeStr == 'object') { date = timeStr; } else if (typeof timeStr == 'number' || (typeof timeStr == 'string' && timeStr.match(/^\d*$/))) { if ((timeStr + '').length == 10) { timeStr += timeStr + '000'; } date = new Date; date.setTime(timeStr); } else { date = new Date(timeStr.replace(/\-/g, '/')); } // 返回时间戳 if (fmt == 'timestamp' || fmt == 'unix' || fmt == 'u') { return date.getTime(); } var o = { 'YYYY': date.getFullYear(), //年份(4位) 'YY': date.getFullYear(), //年份(2位) 'M+': date.getMonth() + 1, //月份 'd+': date.getDate(), //日 'h+': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时 'H+': date.getHours(), //小时 'm+': date.getMinutes(), //分 'i+': date.getMinutes(), //分 's+': date.getSeconds(), //秒 'q+': Math.floor((date.getMonth() + 3) / 3), //季度 'S': date.getMilliseconds() //毫秒 }; let res = fmt.match(/(y+)/i); if (res) { fmt = fmt.replace(res[1], (o['YYYY'] + '').substring(4 - res[1].length)); } for (var k in o) { res = fmt.match(new RegExp('(' + k + ')')); if (res) { fmt = fmt.replace(res[1], (res[1].length == 1) ? o[k] : ('00' + o[k]).substring(('' + o[k]).length)); } } return fmt; }; exports.formatTime = formatTime;