From b9052de13d1c44a15307be12f12f836424e6f7eb Mon Sep 17 00:00:00 2001 From: ItMaga Date: Thu, 27 Jul 2023 17:43:59 +0300 Subject: [PATCH] feat: add ip versions support --- lib/generators/StringGenerator.ts | 9 ++++++--- tests/string.test.ts | 6 ++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/generators/StringGenerator.ts b/lib/generators/StringGenerator.ts index a06fadf..db622d1 100644 --- a/lib/generators/StringGenerator.ts +++ b/lib/generators/StringGenerator.ts @@ -24,7 +24,7 @@ export default class StringGenerator implements BaseGener string = '😀'; break; case 'ip': - string = this.getRandomIP(); + string = this.getRandomIP(check.version!); break; case 'includes': case 'startsWith': @@ -62,7 +62,10 @@ export default class StringGenerator implements BaseGener }); } - public getRandomIP(): string { - return Array.from({ length: 4 }, () => Math.floor(Math.random() * 256)).join('.'); + public getRandomIP(version: 'v4' | 'v6'): string { + if (version === 'v4') { + return [0, 0, 0, 0].map(() => Math.floor(Math.random() * 256)).join('.'); + } + return [0, 0, 0, 0, 0, 0, 0, 0].map(() => Math.floor(Math.random() * 65536).toString(16)).join(':'); } } diff --git a/tests/string.test.ts b/tests/string.test.ts index 462db73..56355aa 100644 --- a/tests/string.test.ts +++ b/tests/string.test.ts @@ -54,9 +54,11 @@ describe('String', () => { expect(schema); }); test('ip', () => { - const schema = z.string().ip(); + const schemaV4 = z.string().ip({ version: 'v4' }); + const schemaV6 = z.string().ip({ version: 'v6' }); - expect(schema); + expect(schemaV4); + expect(schemaV6); }); test('startsWith', () => { const schema = z.string().startsWith('__MARK__');