diff --git a/packages/mock/lib/index.ts b/packages/mock/lib/index.ts index 90d7803..57643d7 100644 --- a/packages/mock/lib/index.ts +++ b/packages/mock/lib/index.ts @@ -1,4 +1,4 @@ -import utils from "./shared"; +import * as utils from "./shared"; import use from "./core/use"; import mock from "./core"; export { utils, use, mock }; diff --git a/packages/mock/lib/random/address/city.ts b/packages/mock/lib/random/address/city.ts index 735770a..eadb6da 100644 --- a/packages/mock/lib/random/address/city.ts +++ b/packages/mock/lib/random/address/city.ts @@ -1,11 +1,11 @@ -const addrData = require("./address_ch.json"); -const { sample } = require("../../utils"); +import addrData from "./address_ch.json"; +import { sample } from '../../shared'; /** * * @param {Boolean | String} args 是否同时显示完整 | 指定省份下的城市 * @returns string */ -module.exports = (...args) => { +module.exports = (...args:any[]) => { const isFull = (args.length === 1 && args[0] === true) || (args.length === 2 && args[1] === true); diff --git a/packages/mock/lib/random/basic/char.ts b/packages/mock/lib/random/basic/char.ts index 1aa1e7c..370cd09 100644 --- a/packages/mock/lib/random/basic/char.ts +++ b/packages/mock/lib/random/basic/char.ts @@ -1,9 +1,8 @@ -import { sample } from "../../shared"; +import { repeat } from "../../shared"; -const strs = "abcdefghijklmnopqrstuvwxyz"; /** * 返回26个字母中的任意一个 * @example * '@char' */ -export default () => sample(strs.split("")); +export default () => repeat(2, 1); diff --git a/packages/mock/lib/random/basic/string.ts b/packages/mock/lib/random/basic/string.ts index 95fc132..7dc7e10 100644 --- a/packages/mock/lib/random/basic/string.ts +++ b/packages/mock/lib/random/basic/string.ts @@ -1,9 +1,4 @@ -import char from "./char"; +import { repeat } from '../../shared' export default (min = 3, max = 10) => { - let ret = ""; - const len = min + parseInt(Math.random() * (max - min)); - for (let i = 0; i < len; i++) { - ret += char(); - } - return ret; + return repeat(2, min, max) }; diff --git a/packages/mock/lib/random/color/color.ts b/packages/mock/lib/random/color/color.ts index 8179352..cbe623f 100644 --- a/packages/mock/lib/random/color/color.ts +++ b/packages/mock/lib/random/color/color.ts @@ -3,7 +3,7 @@ export default () => { }; function getRandom() { - const data = parseInt(Math.random()) * 256; + const data = parseInt((Math.random() * 256).toString()); const raw = data === 256 ? 255 : data; return raw.toString(16); } diff --git a/packages/mock/lib/random/color/rgb.ts b/packages/mock/lib/random/color/rgb.ts index 881aa63..d907e8d 100644 --- a/packages/mock/lib/random/color/rgb.ts +++ b/packages/mock/lib/random/color/rgb.ts @@ -7,6 +7,6 @@ export default (useOpacity: boolean) => { }; function getRandom() { - const data = parseInt(Math.random()) * 256; + const data = parseInt((Math.random() * 256).toString()); return data === 256 ? 255 : data; } diff --git a/packages/mock/lib/random/date/date.ts b/packages/mock/lib/random/date/date.ts index 7a366b0..82b3e14 100644 --- a/packages/mock/lib/random/date/date.ts +++ b/packages/mock/lib/random/date/date.ts @@ -2,17 +2,17 @@ import dayjs from "dayjs"; export default (...args: any[]) => { if (args.length === 0) { - return dayjs(parseInt(Math.random() * 1e10)).toDate(); + return dayjs(parseInt((Math.random() * 1e10).toString())).toDate(); } else if (args.length === 1) { - return dayjs(parseInt(Math.random() * 1e10)).format(args[0]); + return dayjs(parseInt((Math.random() * 1e10).toString())).format(args[0]); } else if (args.length === 3) { const [type, date, format] = args; if (type === "before") { const max = dayjs(date).unix(); - return dayjs(parseInt(Math.random() * max)).format(format); + return dayjs(parseInt((Math.random() * max).toString())).format(format); } else if (type === "after") { const max = dayjs().add(1, "y").unix(); - return dayjs(parseInt(Math.random() * max)).format(format); + return dayjs(parseInt((Math.random() * max).toString())).format(format); } } }; diff --git a/packages/mock/lib/shared/index.ts b/packages/mock/lib/shared/index.ts index 6311f36..6b8915e 100644 --- a/packages/mock/lib/shared/index.ts +++ b/packages/mock/lib/shared/index.ts @@ -1,12 +1,19 @@ // import { fill } from "lodash-es"; -import { fixContinuousNumber } from "@linzb93/utils"; export const sample = (list: any[]) => { return list[parseInt((Math.random() * list.length).toString())]; }; -export const repeat = (type: 1 | 2 | 3, min: number, max: number) => { - // 1 纯数字,2纯字母,3混合 +const fixContinuousNumber = (num:number) => Array(num).fill('').map((_, index) => index + 1); + +/** + * 重复 + * @param {number} type - 1 纯数字,2纯字母,3混合 + * @param {number} min 重复次数,如果max存在的话就是重复最小次数 + * @param {number} max 重复最大次数 + * @returns + */ +export const repeat = (type: 1 | 2 | 3, min: number, max?: number) => { let ret = ""; const numList = fixContinuousNumber(10); const letters = "abcdefghijklmnopqrstuvwxyz"; @@ -23,7 +30,8 @@ export const repeat = (type: 1 | 2 | 3, min: number, max: number) => { } return Number(ret); } - } else if (type === 2) { + } + if (type === 2) { if (!max) { for (let i = 0; i < min; i++) { ret += sample(letters.split("")); @@ -36,18 +44,5 @@ export const repeat = (type: 1 | 2 | 3, min: number, max: number) => { } return Number(ret); } - } else { - if (!max) { - for (let i = 0; i < min; i++) { - ret += sample(letters.split("").concat(numList)); - } - return Number(ret); - } else { - const len = min + parseInt((Math.random() * (max - min)).toString()); - for (let i = min; i < len; i++) { - ret += sample(letters.split("").concat(numList)); - } - return Number(ret); - } } };