aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Titmuss <[email protected]>2022-08-05 00:52:40 +1000
committerGitHub <[email protected]>2022-08-04 14:52:40 +0000
commit5979f82e17d4f9adf80fa795afb668d57b33411f (patch)
tree4c72e46b71d273c7b01e9553819d22cc398d61b2 /src
parent407466f3cbb7e9d4575c9df342ceb489633d5379 (diff)
downloadfaker-5979f82e17d4f9adf80fa795afb668d57b33411f.tar.xz
faker-5979f82e17d4f9adf80fa795afb668d57b33411f.zip
feat(system.networkInterface): add networkInterface faker (#1133)
Co-authored-by: Shinigami <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/modules/system/index.ts69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts
index 77be5a82..46a12b19 100644
--- a/src/modules/system/index.ts
+++ b/src/modules/system/index.ts
@@ -14,6 +14,14 @@ const commonMimeTypes = [
'text/html',
];
+const commonInterfaceTypes = ['en', 'wl', 'ww'] as const;
+const commonInterfaceSchemas = {
+ index: 'o',
+ slot: 's',
+ mac: 'x',
+ pci: 'p',
+} as const;
+
/**
* Generates fake data for many computer systems properties.
*/
@@ -195,4 +203,65 @@ export class System {
this.faker.datatype.number(9),
].join('.');
}
+
+ /**
+ * Returns a random [network interface](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/networking_guide/sec-understanding_the_predictable_network_interface_device_names).
+ *
+ * @param options The options to use. Defaults to `{}`.
+ * @param options.interfaceType The interface type. Can be one of `en`, `wl`, `ww`.
+ * @param options.interfaceSchema The interface schema. Can be one of `index`, `slot`, `mac`, `pci`.
+ *
+ * @example
+ * faker.system.networkInterface() // 'enp0s3'
+ * faker.system.networkInterface({ interfaceType: 'wl' }) // 'wlo1'
+ * faker.system.networkInterface({ interfaceSchema: 'mac' }) // 'enx000c29c00000'
+ * faker.system.networkInterface({ interfaceType: 'en', interfaceSchema: 'pci' }) // 'enp5s0f1d0'
+ */
+ networkInterface(
+ options: {
+ interfaceType?: typeof commonInterfaceTypes[number];
+ interfaceSchema?: keyof typeof commonInterfaceSchemas;
+ } = {}
+ ): string {
+ const {
+ interfaceType = this.faker.helpers.arrayElement(commonInterfaceTypes),
+ interfaceSchema = this.faker.helpers.objectKey(commonInterfaceSchemas),
+ } = options;
+
+ let suffix: string;
+ let prefix = '';
+ switch (interfaceSchema) {
+ case 'index':
+ suffix = this.faker.datatype.number(9).toString();
+ break;
+ case 'slot':
+ suffix = `${this.faker.datatype.number(9)}${
+ this.faker.helpers.maybe(() => `f${this.faker.datatype.number(9)}`) ??
+ ''
+ }${
+ this.faker.helpers.maybe(() => `d${this.faker.datatype.number(9)}`) ??
+ ''
+ }`;
+ break;
+ case 'mac':
+ suffix = this.faker.internet.mac('');
+ break;
+ case 'pci':
+ prefix =
+ this.faker.helpers.maybe(() => `P${this.faker.datatype.number(9)}`) ??
+ '';
+ suffix = `${this.faker.datatype.number(9)}s${this.faker.datatype.number(
+ 9
+ )}${
+ this.faker.helpers.maybe(() => `f${this.faker.datatype.number(9)}`) ??
+ ''
+ }${
+ this.faker.helpers.maybe(() => `d${this.faker.datatype.number(9)}`) ??
+ ''
+ }`;
+ break;
+ }
+
+ return `${prefix}${interfaceType}${commonInterfaceSchemas[interfaceSchema]}${suffix}`;
+ }
}