UNPKG

mihawk

Version:

A tiny & simple mock server tool, support json,js,cjs,ts(typescript).

464 lines (463 loc) 12.1 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.createRandCname = exports.createRandName = exports.createRandUrl = exports.createRandIp = exports.createRandNum = exports.creataRandBool = exports.createRandPhone = exports.createRandEmail = exports.createRandChar = exports.createRandStr = exports.createRandStrByLimit = exports.RAW_CHARS = exports.RAW_LETTERS = exports.createRandImage = exports.createRandColor = exports.createUUID = exports.createRandId = exports.createRandDateStr = exports.createRandDate = void 0; const date_1 = require("../utils/date"); const is_1 = require("../utils/is"); const list_1 = require("../utils/list"); function createRandDate() { return new Date(+new Date() + Math.floor(Math.random() * 1000000000)); } exports.createRandDate = createRandDate; function createRandDateStr(fmt) { return (0, date_1.dateFormat)(createRandDate(), fmt || 'yyyy-MM-dd hh:mm:ss'); } exports.createRandDateStr = createRandDateStr; function createRandId(len = 6) { return Math.random().toString(36).substring(2, 2 + len); } exports.createRandId = createRandId; function createUUID() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { const r = (Math.random() * 16) | 0; const v = c === 'x' ? r : (r & 0x3) | 0x8; return v.toString(16); }); } exports.createUUID = createUUID; function createRandColor(short) { let SEED = 0xffffff; let LEN = 6; if (short) { SEED = 0xfff; LEN = 3; } const hexStr = Math.ceil(Math.random() * SEED).toString(16); return `${'f'.repeat(LEN - hexStr.length)}${hexStr}`; } exports.createRandColor = createRandColor; function createRandImage(options) { const { width, height, query = {} } = options || {}; const qs = Object.entries(query) .map(([k, v]) => `${k}=${v}`) .join('&'); return `https://picsum.photos/${width || 100}/${height || 100}?${qs}`; } exports.createRandImage = createRandImage; exports.RAW_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'; exports.RAW_CHARS = `_0123456789${exports.RAW_LETTERS}`; function createRandStrByLimit(len, charsLimit) { len = (0, is_1.isNumStrict)(len) && len > 0 ? len : 1; charsLimit = typeof charsLimit === 'string' && (charsLimit === null || charsLimit === void 0 ? void 0 : charsLimit.length) ? charsLimit : exports.RAW_CHARS; const res = []; let i = 0, n = 0; for (i = 0; i < len; ++i) { n = Math.floor(Math.random() * charsLimit.length); res.push(charsLimit.charAt(n)); } return res.join(''); } exports.createRandStrByLimit = createRandStrByLimit; function createRandStr(p1, p2, p3) { if (!(0, is_1.isNumStrict)(p1) || p1 <= 0) { return ''; } let len = p1; let onlyLetters = false; if (typeof p2 === 'boolean') { onlyLetters = p2; } else if ((0, is_1.isNumStrict)(p2)) { if (p2 > 0 && p2 > p1) { len = Math.floor(Math.random() * (p2 - p1 + 1)) + p1; } onlyLetters = !!p3; } else { len = p1; onlyLetters = false; } return createRandStrByLimit(len, onlyLetters ? exports.RAW_LETTERS : exports.RAW_CHARS); } exports.createRandStr = createRandStr; function createRandChar(onlyLetters) { return createRandStrByLimit(1, onlyLetters ? exports.RAW_LETTERS : exports.RAW_CHARS); } exports.createRandChar = createRandChar; function createRandEmail() { const DOMAINS = ['gmail', 'yahoo', 'hotmail', 'qq', '163', '126', 'sina', 'outlook', 'live', 'icloud', 'apple']; return `${createRandChar(true)}${createRandStr(4, 14)}@${(0, list_1.randPick)(DOMAINS)}.com`; } exports.createRandEmail = createRandEmail; function createRandPhone() { const validPrefixes = [ 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 145, 147, 149, 150, 151, 152, 153, 155, 156, 157, 158, 159, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 191, 192, 193, 195, 196, 197, 198, 199, ]; const prefix = (0, list_1.randPick)(validPrefixes); let suffix = ''; for (let i = 0; i < 8; i++) { suffix += createRandNum(0, 9); } return `${prefix}${suffix}`; } exports.createRandPhone = createRandPhone; function creataRandBool() { return Math.random() > 0.5; } exports.creataRandBool = creataRandBool; function createRandNum(min = 0, max = 9999) { if (typeof min !== 'number' || isNaN(min) || typeof max !== 'number' || isNaN(max)) { throw new Error('Arguments must be valid numbers'); } const [lowerBound, upperBound] = min <= max ? [min, max] : [max, min]; const minInt = Math.ceil(lowerBound); const maxInt = Math.floor(upperBound); if (minInt > maxInt) { throw new Error('No valid integer exists between min and max'); } return Math.floor(Math.random() * (maxInt - minInt + 1)) + minInt; } exports.createRandNum = createRandNum; function createRandIp(isIPV6) { if (isIPV6) { return Array.from({ length: 8 }, () => Math.floor(Math.random() * 0xffff) .toString(16) .padStart(4, '0')).join(':'); } else { return Array.from({ length: 4 }, () => Math.floor(Math.random() * 256)).join('.'); } } exports.createRandIp = createRandIp; function createRandUrl(options) { const { includeHash, includeQuery, specialProtocol } = options || {}; const PROTOCOLS = ['http', 'https']; if (specialProtocol) { PROTOCOLS.push(...['file', 'ws', 'wss', 'ftp', 'rtsp', 'rtmp']); } const protocol = (0, list_1.randPick)(PROTOCOLS); const DOMAINS = ['com', 'net', 'org', 'io', 'tv', 'tw', 'jp', 'cn', 'com.cn', 'net.cn', 'gov.cn', 'org.cn']; const domain = protocol === 'file' ? `/localfile/${createRandStr(3, 10, true)}` : `${createRandStr(3, 10, true)}.${createRandStr(3, 10, true)}.${(0, list_1.randPick)(DOMAINS)}`; const randPathSize = Math.floor(Math.random() * 3) + 1; const paths = []; for (let i = 0; i < randPathSize; i++) { paths.push(createRandStr(1, 10, true)); } const pathname = paths.join('/'); let queryString = ''; if (includeQuery) { const randQuerySize = Math.floor(Math.random() * 3) + 1; const querys = []; for (let i = 0; i < randQuerySize; i++) { querys.push(`${createRandStr(1, 5, true)}=${createRandNum(1, 100)}`); } queryString = `?${querys.join('&')}`; } let hash = ''; if (includeHash) { hash = `#${createRandStr(1, 5, true)}`; } return `${protocol}://${domain}/${pathname}${queryString}${hash}`; } exports.createRandUrl = createRandUrl; function createRandName(part = 'full') { const FIRST_NAMES = [ 'Herry', 'Tom', 'Jerry', 'Sky', 'Emma', 'Liam', 'Olivia', 'Noah', 'Ava', 'Isabella', 'Sophia', 'Jackson', 'Mason', 'Lucas', 'Ethan', 'James', 'Alexander', 'Michael', 'Benjamin', 'Emily', 'Abigail', 'Charlotte', 'Harper', 'Megan', 'Jessica', 'Robert', 'William', 'David', 'Joseph', 'Daniel', 'Matthew', 'Anthony', 'Mark', ]; const LAST_NAMES = [ 'Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia', 'Miller', 'Davis', 'Rodriguez', 'Martinez', 'Hernandez', 'Lopez', 'Wilson', 'Anderson', 'Thomas', 'Taylor', 'Moore', 'Jackson', 'Martin', 'Lee', 'Perez', 'Thompson', 'White', 'Harris', 'Sanchez', 'Clark', 'Ramirez', 'Lewis', 'Robinson', 'Walker', ]; const firstName = (0, list_1.randPick)(FIRST_NAMES); const lastName = (0, list_1.randPick)(LAST_NAMES); switch (part) { case 'first': return firstName; case 'last': return lastName; default: return `${firstName} ${lastName}`; } } exports.createRandName = createRandName; function createRandCname() { const SUR_NAMES = [ '赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨', '朱', '秦', '尤', '许', '阎', '颜', '马', '张', '梁', '段', '唐', '徐', '庄', '吕', '冯', '俞', '沈', '苗', '林', '叶', '葛', '程', '陈', '丁', '刘', '汤', '谢', '胡', '何', '诸葛', '纳兰', '欧阳', '上官', '南宫', '东方', '太史', '端木', '皇甫', '呼延', '独孤', '司马', '申屠', '慕容', '宇文', '司徒', '公孙', ]; const MID_NAMES = [ '明', '刚', '伟', '金', '木', '水', '火', '土', '石', '永', '国', '宁', '飞', '东', '西', '南', '北', '子', '梓', '雨', '露', '语', '宇', '予', '长', '大', '小', '细', '硕', '建', '美', '帅', '春', '夏', '秋', '冬', '梅', ]; const GIVEN_NAMES = [ '月', '磊', '犇', '玥', '蕊', '瑞', '昊', '浩', '明', '博', '伟', '芳', '娜', '敏', '静', '丽', '强', '磊', '洋', '勇', '娟', '艳', '杰', '浩', '明', '波', '平', '轩', '涵', '欣', '俊', '宇', '泽', '葵', '飞', '佑', '倩', '茹', '思', '怿', '乐', '悦', '峰', '锋', '东', '冬', '卓', '君', '军', '敏', '魁', ]; const COMBS = [ '宇宁', '建国', '白樱', '丽娟', '丽君', '宝瑞', '宝强', '宝国', '宝亮', '卫国', '卫红', '子轩', '子涵', '子强', '子豪', '梓轩', '梓涵', '梓豪', '紫涵', '雨欣', '雨夕', '明远', '明轩', '可馨', '可心', '可灵', '诗涵', '雨涵', '舟虚', '星驰', '遇春', '子龙', '豪杰', '香玉', '龙凤', '人风', '火风', '不悔', '弃疾', '病已', '仲达', '无忌', '芷若', '德华', '文采', '雪凝', '宝华', '晓龙', '振兴', '思聪', ]; const surName = (0, list_1.randPick)(SUR_NAMES); if (Math.random() >= 0.4) { const midName = Math.random() > 0.5 ? (0, list_1.randPick)(MID_NAMES) : ''; const givenName = (0, list_1.randPick)(GIVEN_NAMES); return `${surName}${midName}${givenName}`; } else { const comb = (0, list_1.randPick)(COMBS); return `${surName}${comb}`; } } exports.createRandCname = createRandCname;