From 30e2af731277760665ba71faaf70253d516cb7e7 Mon Sep 17 00:00:00 2001 From: yicr Date: Fri, 1 Nov 2024 21:25:53 +0900 Subject: [PATCH] feat!: add NamingType check function & Naming interface --- src/index.ts | 6 ++++++ test/naming.test.ts | 49 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0483790..0185ae4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,12 @@ export namespace ResourceNaming { NONE, } + export interface Naming {} + + export function isNamingType(value: NamingType | Naming): value is NamingType { + return Object.values(NamingType).includes(value as NamingType); + } + export function createRandomString(value: crypto.BinaryLike, length: number = 8) { return crypto.createHash('shake256', { outputLength: (length / 2) }) .update(value) diff --git a/test/naming.test.ts b/test/naming.test.ts index 41f35c7..4beadfe 100644 --- a/test/naming.test.ts +++ b/test/naming.test.ts @@ -1,22 +1,53 @@ import { ResourceNaming } from '../src'; +interface Names { + readonly functionName: string; + readonly roleName: string; +} + +interface TestNamingProps { + readonly naming: ResourceNaming.NamingType | Names; +} + describe('ResouceNaming Testing', () => { - const naming = ((namingType: ResourceNaming.NamingType): string | undefined => { - switch (namingType) { - case ResourceNaming.NamingType.DEFAULT: - const random = ResourceNaming.createRandomString('ResourceName'); - return `resource-name-${random}`; - case ResourceNaming.NamingType.NONE: - return undefined; + const naming = ((props: TestNamingProps): string | undefined | Names => { + if (ResourceNaming.isNamingType(props.naming)) { + switch (props.naming) { + case ResourceNaming.NamingType.DEFAULT: + const random = ResourceNaming.createRandomString('ResourceName'); + return `resource-name-${random}`; + case ResourceNaming.NamingType.NONE: + return undefined; + } } + return props.naming; }); it('Is Naming Randmon String', () => { - expect(typeof naming(ResourceNaming.NamingType.DEFAULT)).toBe('string'); + const props = { + naming: ResourceNaming.NamingType.DEFAULT, + }; + expect(typeof naming(props)).toBe('string'); }); it('Is Naming undefined', () => { - expect(naming(ResourceNaming.NamingType.NONE)).toBeUndefined(); + const props = { + naming: ResourceNaming.NamingType.NONE, + }; + expect(naming(props)).toBeUndefined(); + }); + + it('Is Namings', () => { + const props = { + naming: { + functionName: 'example-function', + roleName: 'example-role', + }, + }; + expect(naming(props)).toEqual({ + functionName: 'example-function', + roleName: 'example-role', + }); }); }); \ No newline at end of file