From 577dfd865d71f6d49db0eb4c7819d1d7c90473ef Mon Sep 17 00:00:00 2001 From: yicr Date: Fri, 8 Nov 2024 10:38:44 +0900 Subject: [PATCH 1/2] feat!: add naming function --- src/index.ts | 44 ++++++++++++++++++++++++++-- test/naming.test.ts | 70 ++++++++++++++------------------------------- 2 files changed, 64 insertions(+), 50 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0185ae4..f6f2d28 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,9 +14,25 @@ export namespace ResourceNaming { NONE, } - export interface Naming {} + //export interface Naming {} + // export interface Naming { + // [key: string]: string; + // } - export function isNamingType(value: NamingType | Naming): value is NamingType { + // export interface CustomNaming { + // // [key: string]: string; jsii error + // readonly type: NamingType.CUSTOM; + // readonly names: { + // [key: string]: string; + // }; + // } + export interface NamingOptions { + readonly naming: ResourceNaming.NamingType | { + [key: string]: string; + }; + } + + export function isNamingType(value: NamingType | {[key: string]: string}): value is NamingType { return Object.values(NamingType).includes(value as NamingType); } @@ -25,4 +41,28 @@ export namespace ResourceNaming { .update(value) .digest('hex'); } + + // const value = getValueByKey(originalObject, key as keyof MyObject); + // function getValueByKey(obj: MyObject, key: K): MyObject[K] { + // return obj[key]; + // } + + export function naming (resourceNaming: NamingOptions, defaultNaming: {[p: string]: string | undefined}) { + const names = Object.fromEntries( + Object.entries(defaultNaming).map(([name, value]) => { + return [name, (() => { + if (ResourceNaming.isNamingType(resourceNaming.naming)) { + if (resourceNaming.naming === ResourceNaming.NamingType.DEFAULT) { + return value; + } + if (resourceNaming.naming === ResourceNaming.NamingType.NONE) { + return undefined; + } + } + return resourceNaming.naming[name as keyof {[key: string]: string}]; + })()]; + }), + ); + return { naming: names }; + } } diff --git a/test/naming.test.ts b/test/naming.test.ts index fa76b0a..0068ee9 100644 --- a/test/naming.test.ts +++ b/test/naming.test.ts @@ -1,65 +1,38 @@ import { ResourceNaming } from '../src'; -interface Names extends ResourceNaming.Naming { - readonly functionName: string; - readonly roleName: string; -} +//interface Names extends ResourceNaming.Naming { +// readonly functionName: string; +// readonly roleName: string; +//} -interface TestNamingProps { - readonly naming: ResourceNaming.NamingType | Names; -} +//interface TestNamingProps { +// readonly naming: ResourceNaming.NamingType | Names; +//} describe('ResouceNaming Testing', () => { const random = ResourceNaming.createRandomString('ResourceName'); - const createName = ((props?: TestNamingProps) => { + const defaultNaming = { + functionName: `${random}-func`, + roleName: `${random}-func-exc-role`, + }; - const resourceNaming = props?.naming ?? ResourceNaming.NamingType.DEFAULT; - return { - naming: { - functionName: (() => { - if (ResourceNaming.isNamingType(resourceNaming)) { - if (resourceNaming === ResourceNaming.NamingType.DEFAULT) { - return `${random}-func`; - } - if (resourceNaming === ResourceNaming.NamingType.NONE) { - return undefined; - } - } - return resourceNaming.functionName; - })(), - roleName: (() => { - if (ResourceNaming.isNamingType(resourceNaming)) { - if (resourceNaming === ResourceNaming.NamingType.DEFAULT) { - return `${random}-func-exc-role`; - } - if (resourceNaming === ResourceNaming.NamingType.NONE) { - return undefined; - } - } - return resourceNaming.roleName; - })(), - }, - }; - }); - - it('Is Naming Randmon String', () => { - const naming = createName({ + it('Is Naming Default include Randmon String', () => { + const options: ResourceNaming.NamingOptions = { naming: ResourceNaming.NamingType.DEFAULT, - }); + }; + const naming = ResourceNaming.naming(options, defaultNaming); expect(naming).toEqual({ - naming: { - functionName: `${random}-func`, - roleName: `${random}-func-exc-role`, - }, + naming: defaultNaming, }); }); it('Is Naming undefined', () => { - const naming = createName({ + const options: ResourceNaming.NamingOptions = { naming: ResourceNaming.NamingType.NONE, - }); + }; + const naming = ResourceNaming.naming(options, defaultNaming); expect(naming).toEqual({ naming: { functionName: undefined, @@ -69,12 +42,13 @@ describe('ResouceNaming Testing', () => { }); it('Is Namings', () => { - const naming = createName({ + const options: ResourceNaming.NamingOptions = { naming: { functionName: 'example-function', roleName: 'example-role', }, - }); + }; + const naming = ResourceNaming.naming(options, defaultNaming); expect(naming).toEqual({ naming: { functionName: 'example-function', From 8b6c9c9d7e9253b768c7535cb65db073e663f4a3 Mon Sep 17 00:00:00 2001 From: yicr Date: Fri, 8 Nov 2024 10:40:23 +0900 Subject: [PATCH 2/2] chore(docs): update auto generated api document --- API.md | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/API.md b/API.md index d0bd646..d81363c 100644 --- a/API.md +++ b/API.md @@ -3,16 +3,33 @@ ## Structs -### Naming +### NamingOptions -#### Initializer +#### Initializer ```typescript import { ResourceNaming } from '@gammarers/aws-resource-naming' -const naming: ResourceNaming.Naming = { ... } +const namingOptions: ResourceNaming.NamingOptions = { ... } ``` +#### Properties + +| **Name** | **Type** | **Description** | +| --- | --- | --- | +| naming | @gammarers/aws-resource-naming.ResourceNaming.NamingType \| {[ key: string ]: string} | *No description.* | + +--- + +##### `naming`Required + +```typescript +public readonly naming: NamingType | {[ key: string ]: string}; +``` + +- *Type:* @gammarers/aws-resource-naming.ResourceNaming.NamingType | {[ key: string ]: string} + +---