Skip to content

Commit

Permalink
Merge pull request #3 from gammarers/feature/add-is-naming-type-func
Browse files Browse the repository at this point in the history
feat: add is naming type func
  • Loading branch information
yicr authored Nov 1, 2024
2 parents 2a341f2 + 9c39a2b commit 6053fae
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
12 changes: 12 additions & 0 deletions API.md

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

6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export namespace ResourceNaming {
NONE,
}

export interface Naming {}

export function isNamingType(value: NamingType | Naming): 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 Down
49 changes: 40 additions & 9 deletions test/naming.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,53 @@
import { ResourceNaming } from '../src';

interface Names {
readonly functionName: string;
readonly roleName: string;
}

interface TestNamingProps {
readonly naming: ResourceNaming.NamingType | Names;
}

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

const naming = ((namingType: ResourceNaming.NamingType): string | undefined => {
switch (namingType) {
case ResourceNaming.NamingType.DEFAULT:
const random = ResourceNaming.createRandomString('ResourceName');
return `resource-name-${random}`;
case ResourceNaming.NamingType.NONE:
return undefined;
const naming = ((props: TestNamingProps): string | undefined | Names => {
if (ResourceNaming.isNamingType(props.naming)) {
switch (props.naming) {
case ResourceNaming.NamingType.DEFAULT:
const random = ResourceNaming.createRandomString('ResourceName');
return `resource-name-${random}`;
case ResourceNaming.NamingType.NONE:
return undefined;
}
}
return props.naming;
});

it('Is Naming Randmon String', () => {
expect(typeof naming(ResourceNaming.NamingType.DEFAULT)).toBe('string');
const props = {
naming: ResourceNaming.NamingType.DEFAULT,
};
expect(typeof naming(props)).toBe('string');
});

it('Is Naming undefined', () => {
expect(naming(ResourceNaming.NamingType.NONE)).toBeUndefined();
const props = {
naming: ResourceNaming.NamingType.NONE,
};
expect(naming(props)).toBeUndefined();
});

it('Is Namings', () => {
const props = {
naming: {
functionName: 'example-function',
roleName: 'example-role',
},
};
expect(naming(props)).toEqual({
functionName: 'example-function',
roleName: 'example-role',
});
});
});

0 comments on commit 6053fae

Please sign in to comment.