Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: naming type change #8

Merged
merged 3 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import * as crypto from 'crypto';

export namespace ResourceNaming {
export enum NamingType {
NO,
DEFAULT,
AUTO,
CUSTOM,
}

Expand All @@ -20,8 +20,8 @@ export namespace ResourceNaming {
// [key: string]: string;
// }

export interface NoNaming {
readonly type: NamingType.NO;
export interface AutoNaming {
readonly type: NamingType.AUTO;
}

export interface DefaultNaming {
Expand Down Expand Up @@ -65,16 +65,17 @@ export namespace ResourceNaming {
// }

//export function naming<T extends string>(resourceNaming: NamingOptions<T>, defaultNaming: {[p: string]: string | undefined}) {
export function naming(resourceNaming: NamingOptions, defaultNaming: {[p: string]: string}) {
export function naming(autoNaming: {[p: string]: string}, resourceNaming?: NamingOptions) {
const names = Object.fromEntries(
Object.entries(defaultNaming).map(([name, value]) => {
Object.entries(autoNaming).map(([name, value]) => {
return [name, (() => {
switch (resourceNaming.naming.type) {
switch (resourceNaming?.naming.type) {
case ResourceNaming.NamingType.CUSTOM:
return resourceNaming.naming.names[name as keyof {[key: string]: string}];
case ResourceNaming.NamingType.DEFAULT:
case ResourceNaming.NamingType.AUTO:
return value;
case ResourceNaming.NamingType.NO:
default:
case ResourceNaming.NamingType.DEFAULT:
return undefined;
}
// if (ResourceNaming.isNamingType(resourceNaming.naming.type)) {
Expand All @@ -98,7 +99,7 @@ export namespace ResourceNaming {
}

interface NamingOptions {
readonly naming: ResourceNaming.NoNaming | ResourceNaming.DefaultNaming | {
readonly naming: ResourceNaming.AutoNaming | ResourceNaming.DefaultNaming | {
readonly type: ResourceNaming.NamingType.CUSTOM;
readonly names: {[key: string]: string};
};
Expand Down
36 changes: 26 additions & 10 deletions test/naming.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ResourceNaming } from '../src';
//}

export interface NamingOptions {
readonly naming: ResourceNaming.NoNaming | ResourceNaming.DefaultNaming | {
readonly naming: ResourceNaming.AutoNaming | ResourceNaming.DefaultNaming | {
type: ResourceNaming.NamingType.CUSTOM;
names: {
functionName: string;
Expand All @@ -23,38 +23,54 @@ describe('ResouceNaming Testing', () => {

const random = ResourceNaming.createRandomString('ResourceName');

const defaultNaming = {
const autoNaming = {
functionName: `${random}-func`,
functionRoleName: `${random}-func-exc-role`,
};

// type Names = 'functionName' | 'roleName';

it('Is Naming Default include Randmon String', () => {
it('Is Naming Auto generate include Randmon String', () => {
const options: NamingOptions = {
naming: {
type: ResourceNaming.NamingType.DEFAULT,
type: ResourceNaming.NamingType.AUTO,
},
};
const naming = ResourceNaming.naming(options, defaultNaming);
const naming = ResourceNaming.naming(autoNaming, options);
expect(naming).toEqual({
names: defaultNaming,
names: autoNaming,
});
});

it('Is Naming none', () => {
expect(ResourceNaming.naming(autoNaming)).toEqual({
names: {
functionName: undefined,
roleName: undefined,
},
});
});

it('Is Naming undefined', () => {
it('Is Naming Default(undefined)', () => {
const options: NamingOptions = {
naming: {
type: ResourceNaming.NamingType.NO,
type: ResourceNaming.NamingType.DEFAULT,
},
};
const naming = ResourceNaming.naming(options, defaultNaming);
const naming = ResourceNaming.naming(autoNaming, options);
expect(naming).toEqual({
names: {
functionName: undefined,
roleName: undefined,
},
});
// option
expect(ResourceNaming.naming(autoNaming)).toEqual({
names: {
functionName: undefined,
roleName: undefined,
},
});
});

it('Is Namings', () => {
Expand All @@ -67,7 +83,7 @@ describe('ResouceNaming Testing', () => {
},
},
};
const naming = ResourceNaming.naming(options, defaultNaming);
const naming = ResourceNaming.naming(autoNaming, options);
expect(naming).toEqual({
names: {
functionName: 'example-function',
Expand Down