36b6bfd20d
* feat: 生成成功改为 toast 提示 * refactor: 数据预览改为显示示例数据,简化组件逻辑 * fix: 非必填字段预览显示 null * fix: 修复空值率与必填状态的逻辑 * fix: 切换为非必填时默认空值率 100% * feat: 字段编辑弹窗添加「完成」按钮 去除关闭按钮,添加取消按钮 * refactor: 删除未使用的 defaultNullRate 功能 * feat: 优化规则管理功能 * fix: 更新 features 测试断言以匹配新增的 testDataGenerator * refactor: 将 formatFileSize 重命名为 formatBytes 并移至通用工具模块 * refactor: 使用 Label 组件替换原生 label 元素,并在 DecodeResultPaper 中引入 Input 组件 * refactor: 规范化页面组件目录结构 * refactor(JsonTools): 规范化页面组件目录结构 * refactor(StorageCleaner): 规范化页面组件目录结构 * refactor(Timestamp): 规范化页面组件目录结构 * refactor(i18n): 移除 chrome.i18n 国际化,统一使用中文硬编码 * refactor(AppRoot): 移除 RouterProvider 组件,直接渲染子组件 * fix(RouterProvider): 将 goBack 重命名为 goHome,修复跨窗同步 merge 不一致 * fix(ThemeModeProvider): 修复主题快照同步与首屏闪烁问题 * refactor: 将 UI 文案改为中文并优化相关逻辑 * refactor: 清理代码注释并优化组件结构 * feat(Dashboard): 重构仪表板功能组件和逻辑 * refactor: 更新功能键名称并移除 README 文件 * refactor: 重构 TopBar 组件并更新项目结构 * feat(TopBar): 添加搜索功能组件并优化结构 * refactor: 统一使用 Button 组件替换原有 button 元素 * feat: 添加 EmptyPlaceholder 组件并更新相关页面 * feat(TextStatistics): 引入 StatCard 组件以优化统计信息展示 * feat(Timestamp): 重构时间戳转换功能,新增 ConverterForm 组件 * feat(Jwt): 重构 JWT 组件,新增常量和结果视图 * refactor(RightClickRestorer): 用状态配置表简化页面 UI 结构 * fix(business): 修复 skewedRandom 函数中的数学计算错误 * refactor(ImageUploader): 移除预览 URL 相关逻辑并简化粘贴事件处理 * feat(TestDataGenerator): 更新规则编辑提示和警告显示逻辑 * refactor(CopyButton.test): 移除冗余测试用例 * refactor(RouterProvider, chromeStorage, useContextMenuData): 更新存储获取逻辑以移除默认值参数 * refactor(代码重复): 抽取共享工具函数与 UI 组件,消除多处重复实现 * fix(RouterProvider): 修复初始加载状态逻辑,确保在加载前不覆盖 chrome.storage * feat(TopBar): 添加搜索框快捷键提示功能及相关测试 * refactor(StorageCleaner): 移动 isRestrictedUrl 函数到独立模块并更新相关引用 * fix(StorageCleaner): 修复 IndexedDB 清理竞态、部分成功计数与刷新后状态同步 * fix: 修复路由初始化竞态、存储状态静默覆盖与存储清理误删 * refactor: 统一中文文案并简化组件结构,提升代码可读性 * refactor: 更新 Worker 消息类型以支持生成任务 ID * fix: 修复 ruleStorage 写入失败处理与 CopyButton 成功态样式
198 lines
5.4 KiB
TypeScript
198 lines
5.4 KiB
TypeScript
import type { GeneratorDefinition } from '@/types/testDataGenerator';
|
|
import { randomInt, randomPick } from './random';
|
|
|
|
export const randomIntGenerator: GeneratorDefinition = {
|
|
id: 'randomInt',
|
|
name: '随机整数',
|
|
description: '生成指定范围内的随机整数',
|
|
categoryId: 'basic',
|
|
params: [
|
|
{
|
|
key: 'min',
|
|
label: '最小值',
|
|
type: 'number',
|
|
defaultValue: 0,
|
|
description: '整数最小值',
|
|
},
|
|
{
|
|
key: 'max',
|
|
label: '最大值',
|
|
type: 'number',
|
|
defaultValue: 100,
|
|
description: '整数最大值',
|
|
},
|
|
],
|
|
generate: (params) => {
|
|
const min = (params.min as number) || 0;
|
|
const max = (params.max as number) || 100;
|
|
return String(randomInt(min, max));
|
|
},
|
|
generateAtIndex: (params, index) => {
|
|
const min = (params.min as number) || 0;
|
|
const max = (params.max as number) || 100;
|
|
const range = max - min + 1;
|
|
return String(min + (index % range));
|
|
},
|
|
};
|
|
|
|
export const randomFloat: GeneratorDefinition = {
|
|
id: 'randomFloat',
|
|
name: '随机浮点数',
|
|
description: '生成指定范围内的随机浮点数',
|
|
categoryId: 'basic',
|
|
params: [
|
|
{
|
|
key: 'min',
|
|
label: '最小值',
|
|
type: 'number',
|
|
defaultValue: 0,
|
|
description: '浮点数最小值',
|
|
},
|
|
{
|
|
key: 'max',
|
|
label: '最大值',
|
|
type: 'number',
|
|
defaultValue: 100,
|
|
description: '浮点数最大值',
|
|
},
|
|
{
|
|
key: 'decimals',
|
|
label: '小数位数',
|
|
type: 'number',
|
|
defaultValue: 2,
|
|
min: 0,
|
|
max: 10,
|
|
description: '小数位数',
|
|
},
|
|
],
|
|
generate: (params) => {
|
|
const min = (params.min as number) || 0;
|
|
const max = (params.max as number) || 100;
|
|
const decimals = (params.decimals as number) ?? 2;
|
|
const value = min + Math.random() * (max - min);
|
|
return value.toFixed(decimals);
|
|
},
|
|
};
|
|
|
|
export const randomString: GeneratorDefinition = {
|
|
id: 'randomString',
|
|
name: '随机字符串',
|
|
description: '生成指定长度的随机字符串',
|
|
categoryId: 'basic',
|
|
params: [
|
|
{
|
|
key: 'length',
|
|
label: '字符串长度',
|
|
type: 'number',
|
|
defaultValue: 10,
|
|
min: 1,
|
|
max: 100,
|
|
description: '字符串长度',
|
|
},
|
|
{
|
|
key: 'charset',
|
|
label: '字符集',
|
|
type: 'select',
|
|
defaultValue: 'alphanumeric',
|
|
description: '使用的字符集',
|
|
options: [
|
|
{ label: '字母数字', value: 'alphanumeric' },
|
|
{ label: '仅字母', value: 'alpha' },
|
|
{ label: '仅数字', value: 'numeric' },
|
|
{ label: '小写字母', value: 'lowercase' },
|
|
{ label: '大写字母', value: 'uppercase' },
|
|
{ label: '十六进制', value: 'hex' },
|
|
{ label: 'Base64', value: 'base64' },
|
|
],
|
|
},
|
|
],
|
|
generate: (params) => {
|
|
const length = (params.length as number) || 10;
|
|
const charset = (params.charset as string) || 'alphanumeric';
|
|
|
|
const charsets: Record<string, string> = {
|
|
alphanumeric: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
|
|
alpha: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
|
|
numeric: '0123456789',
|
|
lowercase: 'abcdefghijklmnopqrstuvwxyz',
|
|
uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
hex: '0123456789abcdef',
|
|
base64: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
|
|
};
|
|
|
|
const chars = charsets[charset] || charsets.alphanumeric;
|
|
let result = '';
|
|
for (let i = 0; i < length; i++) {
|
|
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
}
|
|
return result;
|
|
},
|
|
generateAtIndex: (params, index) => {
|
|
const length = (params.length as number) || 10;
|
|
const charset = (params.charset as string) || 'alphanumeric';
|
|
|
|
const charsets: Record<string, string> = {
|
|
alphanumeric: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
|
|
alpha: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
|
|
numeric: '0123456789',
|
|
lowercase: 'abcdefghijklmnopqrstuvwxyz',
|
|
uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
hex: '0123456789abcdef',
|
|
base64: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
|
|
};
|
|
|
|
const chars = charsets[charset] || charsets.alphanumeric;
|
|
const base = String(index).padStart(length, '0');
|
|
let result = '';
|
|
for (let i = 0; i < length; i++) {
|
|
const charIndex = parseInt(base[i]) || 0;
|
|
result += chars[charIndex % chars.length];
|
|
}
|
|
return result;
|
|
},
|
|
};
|
|
|
|
export const fromList: GeneratorDefinition = {
|
|
id: 'fromList',
|
|
name: '从列表选择',
|
|
description: '从自定义列表中随机选择',
|
|
categoryId: 'basic',
|
|
params: [
|
|
{
|
|
key: 'values',
|
|
label: '列表值',
|
|
type: 'string',
|
|
defaultValue: '',
|
|
description: '列表值,用逗号分隔',
|
|
placeholder: '值1,值2,值3',
|
|
},
|
|
{
|
|
key: 'allowDuplicate',
|
|
label: '允许重复',
|
|
type: 'boolean',
|
|
defaultValue: true,
|
|
description: '是否允许重复选择',
|
|
},
|
|
],
|
|
generate: (params) => {
|
|
const valuesStr = (params.values as string) || '';
|
|
const values = valuesStr
|
|
.split(',')
|
|
.map((v) => v.trim())
|
|
.filter((v) => v);
|
|
|
|
if (values.length === 0) {
|
|
return '(请配置列表值)';
|
|
}
|
|
|
|
return randomPick(values);
|
|
},
|
|
};
|
|
|
|
export const basicGenerators: GeneratorDefinition[] = [
|
|
randomIntGenerator,
|
|
randomFloat,
|
|
randomString,
|
|
fromList,
|
|
];
|