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: export type naming options #9

Merged
merged 2 commits into from
Nov 12, 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
39 changes: 28 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,32 @@ export namespace ResourceNaming {
// return obj[key];
// }

// export type NamingOptions = {
// naming: ResourceNaming.AutoNaming | ResourceNaming.DefaultNaming | {type: ResourceNaming.NamingType.CUSTOM; names: {[key: string]: string}};
// };
// export type NamingOptions =
// ResourceNaming.AutoNaming | ResourceNaming.DefaultNaming | {type: ResourceNaming.NamingType.CUSTOM; names: {[key: string]: string}};

export type NamingOptions =
ResourceNaming.AutoNaming | ResourceNaming.DefaultNaming | {type: ResourceNaming.NamingType.CUSTOM; [key: string]: string};

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

interface NamingOptions {
readonly naming: ResourceNaming.AutoNaming | ResourceNaming.DefaultNaming | {
readonly type: ResourceNaming.NamingType.CUSTOM;
readonly names: {[key: string]: string};
};
}
//interface NamingOptions {
// readonly naming: ResourceNaming.AutoNaming | ResourceNaming.DefaultNaming | {
// readonly type: ResourceNaming.NamingType.CUSTOM;
// readonly names: {[key: string]: string};
// };
//}
63 changes: 15 additions & 48 deletions test/naming.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ import { ResourceNaming } from '../src';
// readonly naming: ResourceNaming.NamingType | Names;
//}

export interface NamingOptions {
readonly naming: ResourceNaming.AutoNaming | ResourceNaming.DefaultNaming | {
type: ResourceNaming.NamingType.CUSTOM;
names: {
functionName: string;
functionRoleName: string;
};
};
}

describe('ResouceNaming Testing', () => {

const random = ResourceNaming.createRandomString('ResourceName');
Expand All @@ -31,64 +21,41 @@ describe('ResouceNaming Testing', () => {
// type Names = 'functionName' | 'roleName';

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

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

it('Is Naming Default(undefined)', () => {
const options: NamingOptions = {
naming: {
type: ResourceNaming.NamingType.DEFAULT,
},
const options: ResourceNaming.NamingOptions = {
type: ResourceNaming.NamingType.DEFAULT,
};
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,
},
functionName: undefined,
roleName: undefined,
});
});

it('Is Namings', () => {
const options: NamingOptions = {
naming: {
type: ResourceNaming.NamingType.CUSTOM,
names: {
functionName: 'example-function',
functionRoleName: 'example-role',
},
},
const options: ResourceNaming.NamingOptions = {
type: ResourceNaming.NamingType.CUSTOM,
functionName: 'example-function',
functionRoleName: 'example-role',
};
const naming = ResourceNaming.naming(autoNaming, options);
expect(naming).toEqual({
names: {
functionName: 'example-function',
functionRoleName: 'example-role',
},
functionName: 'example-function',
functionRoleName: 'example-role',
});
});
});