Skip to content

Commit

Permalink
Merge pull request #5 from gammarers/feature/add-naming-function
Browse files Browse the repository at this point in the history
feat: add naming function
  • Loading branch information
yicr authored Nov 8, 2024
2 parents b65469e + 8b6c9c9 commit 0c1905e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 53 deletions.
23 changes: 20 additions & 3 deletions API.md

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

44 changes: 42 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -25,4 +41,28 @@ export namespace ResourceNaming {
.update(value)
.digest('hex');
}

// const value = getValueByKey(originalObject, key as keyof MyObject);
// function getValueByKey<K extends keyof MyObject>(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 };
}
}
70 changes: 22 additions & 48 deletions test/naming.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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',
Expand Down

0 comments on commit 0c1905e

Please sign in to comment.