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: change naming type #6

Merged
merged 2 commits into from
Nov 8, 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
111 changes: 106 additions & 5 deletions API.md

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

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

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

//export interface Naming {}
// export interface Naming {
// [key: string]: string;
// }

// export interface CustomNaming {
// // [key: string]: string; jsii error
// readonly type: NamingType.CUSTOM;
// readonly names: {
// [key: string]: string;
// };
// }
export interface NamingOptions {
readonly naming: ResourceNaming.NamingType | {
export interface NoNaming {
readonly type: NamingType.NO;
}

export interface DefaultNaming {
readonly type: NamingType.DEFAULT;
}

export interface CustomNaming {
// [key: string]: string; jsii error
readonly type: NamingType.CUSTOM;
readonly names: {
[key: string]: string;
};
}

export function isNamingType(value: NamingType | {[key: string]: string}): value is NamingType {
return Object.values(NamingType).includes(value as NamingType);
export interface NamingOptions {
readonly naming: NoNaming | DefaultNaming | CustomNaming;
}

// // jsii error JSII1006
// export interface NamingOptions<T extends string> {
// // jsii error JSII1003
// readonly naming: ResourceNaming.NamingType | {
// // [key: string]: string;
// [K in T]: string;
// };
// }

// export interface Txx {
// readonly namingOption: NamingOptions<'a'|'b'>;
// }

// export function isNamingType(value: NamingType | {[key: string]: string}): 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)
Expand All @@ -47,19 +68,32 @@ export namespace ResourceNaming {
// return obj[key];
// }

export function naming (resourceNaming: NamingOptions, defaultNaming: {[p: string]: string | undefined}) {
//export function naming<T extends string>(resourceNaming: NamingOptions<T>, defaultNaming: {[p: string]: string | undefined}) {
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) {
switch (resourceNaming.naming.type) {
case ResourceNaming.NamingType.CUSTOM:
return resourceNaming.naming.names[name as keyof {[key: string]: string}];
case ResourceNaming.NamingType.DEFAULT:
return value;
}
if (resourceNaming.naming === ResourceNaming.NamingType.NONE) {
case ResourceNaming.NamingType.NO:
return undefined;
}
}
return resourceNaming.naming[name as keyof {[key: string]: string}];
// if (ResourceNaming.isNamingType(resourceNaming.naming.type)) {
// if (resourceNaming.naming.type === ResourceNaming.NamingType.DEFAULT) {
// return value;
// }
// if (resourceNaming.naming.type === ResourceNaming.NamingType.NO) {
// return undefined;
// }
// return resourceNaming.naming.names[name as keyof {[key: string]: string}];
// }
// return resourceNaming.naming[name as keyof {[K in T]: string}];
// return resourceNaming.naming.names[name as keyof {[key: string]: string}];

// return undefined;
})()];
}),
);
Expand Down
17 changes: 13 additions & 4 deletions test/naming.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ describe('ResouceNaming Testing', () => {
roleName: `${random}-func-exc-role`,
};

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

it('Is Naming Default include Randmon String', () => {
const options: ResourceNaming.NamingOptions = {
naming: ResourceNaming.NamingType.DEFAULT,
naming: {
type: ResourceNaming.NamingType.DEFAULT,
},
};
const naming = ResourceNaming.naming(options, defaultNaming);
expect(naming).toEqual({
Expand All @@ -30,7 +34,9 @@ describe('ResouceNaming Testing', () => {

it('Is Naming undefined', () => {
const options: ResourceNaming.NamingOptions = {
naming: ResourceNaming.NamingType.NONE,
naming: {
type: ResourceNaming.NamingType.NO,
},
};
const naming = ResourceNaming.naming(options, defaultNaming);
expect(naming).toEqual({
Expand All @@ -44,8 +50,11 @@ describe('ResouceNaming Testing', () => {
it('Is Namings', () => {
const options: ResourceNaming.NamingOptions = {
naming: {
functionName: 'example-function',
roleName: 'example-role',
type: ResourceNaming.NamingType.CUSTOM,
names: {
functionName: 'example-function',
roleName: 'example-role',
},
},
};
const naming = ResourceNaming.naming(options, defaultNaming);
Expand Down