Skip to content

Commit

Permalink
refactor: ip address generator generation method (#261)
Browse files Browse the repository at this point in the history
Co-Authored-By: Gavin Weng <[email protected]>
  • Loading branch information
ZACHSTRIVES and hwen554 committed Mar 14, 2024
1 parent bda6e9d commit 0911e7e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 82 deletions.
73 changes: 23 additions & 50 deletions src/core/generators/IpAddress/IpAddress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,36 @@ import {faker} from "@faker-js/faker";
// -------------------------------------------------------------------------------------------------------------
// types

export type IpType =
| 'ip'
| 'ip4'
| 'ip6'
export type IpType = 'IPv4' | 'IPv6'

export interface IpAddressGeneratorOptions {
type:IpType[]
types: IpType[]
}



// -------------------------------------------------------------------------------------------------------------
// default options
export const IpAddressGeneratorDefaultOptions:IpAddressGeneratorOptions = {
type: ['ip', 'ip4', 'ip6']
export const IpAddressGeneratorDefaultOptions: IpAddressGeneratorOptions = {
types: ["IPv4", "IPv6"]
}



// -------------------------------------------------------------------------------------------------------------
// generate method
export const generate = (options: any): GenerateResult => {


let ips: string[] = [];
options.type.forEach((type) => {
switch (type) {
case 'ip4':
ips.push(faker.internet.ip());
break;
case 'ip6':
ips.push(faker.internet.ipv6());
break;
case 'ip':
ips.push(Math.random() < 0.5 ? faker.internet.ip() : faker.internet.ipv6());
break;
default:
break;
}
});
export const generate = (options: IpAddressGeneratorOptions): GenerateResult => {
let value: string;

// Joining the IP addresses with a newline character to ensure each is on its own line
const stringValue = ips.join('\n');
if (options.types.length === 2) {
value = faker.internet.ip();
} else if (options.types.includes("IPv4")) {
value = faker.internet.ipv4();
} else if (options.types.includes("IPv6")){
value = faker.internet.ipv6();
}

return {
value: ips, // This is now an array of IPs, one per selected type
stringValue: stringValue, // IPs separated by newline for display
value: value,
stringValue: value,
};
}

Expand All @@ -62,35 +46,24 @@ export const IpAddressGeneratorOptionsComponent: React.FunctionComponent<Generat
options: IpAddressGeneratorOptions,
handleOptionValueChange: typeof props.handleOptionValueChange
};

// TODO: implement your own options component here

return (
<div>
<OptionsSelect
multiple
maxTagCount={2}
label={<FormattedMessage id='dataType.ipaddress.type'/>}
label={<FormattedMessage id='dataType.ipaddress.types'/>}
selectOptions={IpTypeSelectOptions}
value={options.type}
onChange={(v) => handleOptionValueChange("type", v)}
style={{width: '200px'}}
value={options.types}
onChange={(v) => handleOptionValueChange("types", v)}
style={{width: '160px'}}
/>
</div>
);
}


export const IpTypeSelectOptions: SelectOption[] = [
{
label: <FormattedMessage id={"dataType.ipaddress.type.ip"}/>,
value: "ip",
},
{
label: <FormattedMessage id={"dataType.ipaddress.type.ip4"}/>,
value: "ip4"
},
{
label: <FormattedMessage id={"dataType.ipaddress.type.ip6"}/>,
value: "ip6"
}
{label: "IPv4", value: "IPv4",},
{label: "IPv6", value: "IPv6"}
]
2 changes: 1 addition & 1 deletion src/core/generators/IpAddress/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export const IpAddressGenerator: Generator = {
optionsComponent: IpAddressGeneratorOptionsComponent,
defaultOptions: IpAddressGeneratorDefaultOptions,
defaultValueType: ValueType.STRING,
exampleLines: ["245.108.222.0", "64.239.253.171", "246.18.59.18"]
exampleLines: ["245.108.222.0", "64.239.253.171", "e106:aa3c:59ac:6d0f:4c2c:bfef:86a3:62d7"]
}

10 changes: 3 additions & 7 deletions src/locale/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,10 @@ export const en = {
// -------------------------------------------------------------------------------------------------------------
// data types




// ipaddress
"dataType.ipaddress": "IpAddress",
"dataType.ipaddress.type":"Type",
"dataType.ipaddress.type.ip": "Ip",
"dataType.ipaddress.type.ip4": "Ip4",
"dataType.ipaddress.type.ip6": "Ip6",
"dataType.ipaddress": "IP Address",
"dataType.ipaddress.types":"Types",

// url
"dataType.url": "Url",
Expand Down
44 changes: 20 additions & 24 deletions src/locale/translations/zhCN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const zhCN = {

// export category
"export.category.fileTypes": "常见格式",
"export.category.databases" : "数据库",
"export.category.databases": "数据库",
"export.category.programmingLanguages": "编程语言",

// export format modal
Expand Down Expand Up @@ -34,7 +34,7 @@ export const zhCN = {

// csv
"export.configurator.csv.delimiter": "分隔符",
"export.configurator.csv.delimiter.required" : "分隔符不可为空",
"export.configurator.csv.delimiter.required": "分隔符不可为空",
"export.configurator.csv.includeHeader": "包含表头",
"export.configurator.csv.endLineChar": "行结束符",

Expand All @@ -59,20 +59,16 @@ export const zhCN = {

// -------------------------------------------------------------------------------------------------------------
// data types





// ipaddress
"dataType.ipaddress": "网络地址",
"dataType.ipaddress.type":"类型",
"dataType.ipaddress.type.ip": "Ip",
"dataType.ipaddress.type.ip4": "Ip4",
"dataType.ipaddress.type.ip6": "Ip6",
"dataType.ipaddress": "IP地址",
"dataType.ipaddress.types": "类型",

// url
"dataType.url": "网址",
"dataType.url.appendSlash.label":"斜线",
"dataType.url.protocol.label":"传输协议",
"dataType.url.appendSlash.label": "斜线",
"dataType.url.protocol.label": "传输协议",

// domainsuffix
"dataType.domainsuffix": "域名后缀",
Expand All @@ -91,7 +87,7 @@ export const zhCN = {
// color
"dataType.color": "颜色",
"dataType.color.kind.label": "类型",
"dataType.color.format.humanWord":"词语",
"dataType.color.format.humanWord": "词语",
"dataType.color.format.label": "格式",

// phone
Expand All @@ -104,15 +100,15 @@ export const zhCN = {
"dataType.emoji.type": "类型",
"dataType.emoji.type.all": "全部",
"dataType.emoji.type.smiley": "笑脸",
"dataType.emoji.type.body" : "人体",
"dataType.emoji.type.person" : "人物",
"dataType.emoji.type.nature" : "自然",
"dataType.emoji.type.food" : "食物",
"dataType.emoji.type.travel" : "旅行",
"dataType.emoji.type.activity" : "活动",
"dataType.emoji.type.object" : "物品",
"dataType.emoji.type.symbol" : "符号",
"dataType.emoji.type.flag" : "国旗",
"dataType.emoji.type.body": "人体",
"dataType.emoji.type.person": "人物",
"dataType.emoji.type.nature": "自然",
"dataType.emoji.type.food": "食物",
"dataType.emoji.type.travel": "旅行",
"dataType.emoji.type.activity": "活动",
"dataType.emoji.type.object": "物品",
"dataType.emoji.type.symbol": "符号",
"dataType.emoji.type.flag": "国旗",

// persontitle
"dataType.persontitle": "人物称谓",
Expand All @@ -134,11 +130,11 @@ export const zhCN = {
"dataType.number.kind.label": "种类",
"dataType.number.precision.label": "精度",
"dataType.number.min.label": "最小值",
"dataType.number.min.tooltip":"生成数据的最小值",
"dataType.number.min.tooltip": "生成数据的最小值",
"dataType.number.min.errorMessage.empty": "最小值不能为空",
"dataType.number.min.errorMessage.greaterThanMax": "最小值不能大于最大值",
"dataType.number.max.label": "最大值",
"dataType.number.max.tooltip":"生成数据的最大值",
"dataType.number.max.tooltip": "生成数据的最大值",
"dataType.number.max.errorMessage.empty": "最大值不能为空",
"dataType.number.max.errorMessage.lessThanMin": "最大值不能小于最小值",

Expand Down

0 comments on commit 0911e7e

Please sign in to comment.