Skip to content

Commit

Permalink
feat!: change naming type with change props
Browse files Browse the repository at this point in the history
  • Loading branch information
yicr committed Nov 8, 2024
1 parent 0c1905e commit e803288
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
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

0 comments on commit e803288

Please sign in to comment.