Skip to content

Commit

Permalink
Merge pull request #270 from ZACHSTRIVES/261-feat-ipaddress-generator
Browse files Browse the repository at this point in the history
feat:add ipaddress generator
  • Loading branch information
ZACHSTRIVES authored Mar 14, 2024
2 parents a5045c6 + 678b511 commit 67284ae
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/constants/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export enum DataTypeCategory {
}

export enum DataType {
IPADDRESS = "ipaddress",
BIRTHDAY = "birthday",
MONTH = "month",
WEEKDAY = "weekday",
Expand Down
69 changes: 69 additions & 0 deletions src/core/generators/IpAddress/IpAddress.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React from "react";
import {GenerateResult, GeneratorOptionsComponentInterface} from "@/types/generator";
import {OptionsSelect, SelectOption} from "@/components/Utils";
import {FormattedMessage} from "@/locale";
import {faker} from "@faker-js/faker";
// -------------------------------------------------------------------------------------------------------------
// types

export type IpType = 'IPv4' | 'IPv6'

export interface IpAddressGeneratorOptions {
types: IpType[]
}


// -------------------------------------------------------------------------------------------------------------
// default options
export const IpAddressGeneratorDefaultOptions: IpAddressGeneratorOptions = {
types: ["IPv4", "IPv6"]
}


// -------------------------------------------------------------------------------------------------------------
// generate method
export const generate = (options: IpAddressGeneratorOptions): GenerateResult => {
let value: string;

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: value,
stringValue: value,
};
}

// -------------------------------------------------------------------------------------------------------------
// options component
export const IpAddressGeneratorOptionsComponent: React.FunctionComponent<GeneratorOptionsComponentInterface> = ({...props}) => {
const {options, handleOptionValueChange} = props as {
options: IpAddressGeneratorOptions,
handleOptionValueChange: typeof props.handleOptionValueChange
};

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


export const IpTypeSelectOptions: SelectOption[] = [
{label: "IPv4", value: "IPv4",},
{label: "IPv6", value: "IPv6"}
]
14 changes: 14 additions & 0 deletions src/core/generators/IpAddress/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Generator} from "@/types/generator";
import {DataType, DataTypeCategory, ValueType} from "@/constants/enums";
import {IpAddressGeneratorDefaultOptions, IpAddressGeneratorOptionsComponent, generate} from "./IpAddress";

export const IpAddressGenerator: Generator = {
type: DataType.IPADDRESS,
category: DataTypeCategory.NETWORK,
generate: generate,
optionsComponent: IpAddressGeneratorOptionsComponent,
defaultOptions: IpAddressGeneratorDefaultOptions,
defaultValueType: ValueType.STRING,
exampleLines: ["245.108.222.0", "64.239.253.171", "e106:aa3c:59ac:6d0f:4c2c:bfef:86a3:62d7"]
}

2 changes: 2 additions & 0 deletions src/core/generators/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {IpAddressGenerator} from "@/core/generators/IpAddress";
import {BirthdayGenerator} from "@/core/generators/Birthday";
import {MonthGenerator} from "@/core/generators/Month";
import {WeekdayGenerator} from "@/core/generators/Weekday";
Expand All @@ -23,6 +24,7 @@ import {CompanyNameGenerator} from "@/core/generators/CompanyName";
import {DataType} from "@/constants/enums";

export const generators = {
[DataType.IPADDRESS]: IpAddressGenerator,
[DataType.BIRTHDAY]: BirthdayGenerator,
[DataType.MONTH]: MonthGenerator,
[DataType.WEEKDAY]: WeekdayGenerator,
Expand Down
4 changes: 3 additions & 1 deletion src/locale/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ export const en = {
// -------------------------------------------------------------------------------------------------------------
// data types

// ipaddress
"dataType.ipaddress": "IP Address",
"dataType.ipaddress.types":"Types",


// birthday
"dataType.birthday": "Birthday",
"dataType.birthday.mode":"Mode",
Expand Down
8 changes: 2 additions & 6 deletions src/locale/translations/jaJP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,9 @@ export const jaJP = {
// -------------------------------------------------------------------------------------------------------------
// data types

// ipaddress
"dataType.ipaddress": "IpAddress",







// birthday
"dataType.birthday": "Birthday",

Expand Down
7 changes: 6 additions & 1 deletion src/locale/translations/zhCN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ export const zhCN = {
// export

// export category

"export.category.file_types": "常见格式",
"export.category.databases": "数据库",
"export.category.programming_languages": "编程语言",


// export format modal
"export.configurator.modal.title": "生成格式",
"export.configurator.modal.confirmButton.text": "确认",
Expand Down Expand Up @@ -83,7 +85,10 @@ export const zhCN = {
// -------------------------------------------------------------------------------------------------------------
// data types


// ipaddress
"dataType.ipaddress": "IP地址",
"dataType.ipaddress.types": "类型",

// birthday
"dataType.birthday": "生日",
"dataType.birthday.mode":"模式",
Expand Down

0 comments on commit 67284ae

Please sign in to comment.