Skip to content

Commit

Permalink
feat:update
Browse files Browse the repository at this point in the history
  • Loading branch information
linzb93 committed Sep 14, 2024
1 parent 7250445 commit f160dd8
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/mock/lib/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
6 changes: 3 additions & 3 deletions packages/mock/lib/random/address/city.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
5 changes: 2 additions & 3 deletions packages/mock/lib/random/basic/char.ts
Original file line number Diff line number Diff line change
@@ -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);
9 changes: 2 additions & 7 deletions packages/mock/lib/random/basic/string.ts
Original file line number Diff line number Diff line change
@@ -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)
};
2 changes: 1 addition & 1 deletion packages/mock/lib/random/color/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion packages/mock/lib/random/color/rgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 4 additions & 4 deletions packages/mock/lib/random/date/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
29 changes: 12 additions & 17 deletions packages/mock/lib/shared/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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(""));
Expand All @@ -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);
}
}
};

0 comments on commit f160dd8

Please sign in to comment.