Skip to content

Commit

Permalink
Merge pull request #179 from theotonge/feat/#110_namePrefix
Browse files Browse the repository at this point in the history
feat: namePrefix
  • Loading branch information
nivekcode authored Jun 17, 2022
2 parents 86f095f + 6f1511c commit d740985
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ accepts an object with the filename as key and the svg data as key.
| verbose | boolean | false | defines if the log should contain additional information. Can be useful for debugging |
| generateType | boolean | true | defines if it's needed to generate type |
| typeName | string | MyIconType | name of the type to be used when `generateType` is set to `true` |
| namePrefix | string | | prefix to be used for the name property included in the generated constant |

#### Example usage

Expand Down Expand Up @@ -302,6 +303,7 @@ Only the icons included in the consuming SPA also end up in the final bundle of
| generateTypeObject | boolean | false | generate type object |
| generateEnum | boolean | false | generate enum object |
| prefix | string | myIcon | prefix for the generated svg constants |
| namePrefix | string | | prefix to be used for the name property included in the generated constant |
| interfaceName | string | MyIcon | name for the generated interface |
| fileName | string | my-icons | file name of the generated file |
| enumName | string | MyIcons | name for the generated enum |
Expand Down Expand Up @@ -396,6 +398,7 @@ end up there.
| exportCompleteIconSet | boolean | false | Specifies if the complete icon set should be exported or not (can be very handy for showcases) |
| completeIconSetName | string | completeIconSet | Name of the generated complete icon set (only effective if exportCompleteIconSet is set to true) |
| prefix | string | myIcon | prefix for the generated svg constants |
| namePrefix | string | | prefix to be used for the name property included in the generated constant |
| interfaceName | string | MyIcon | name for the generated interface |
| modelFileName | string | my-icons | file name of the generated file |
| enumName | string | MyIcons | name for the generated enum |
Expand Down
4 changes: 2 additions & 2 deletions src/lib/converters/shared.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface SvgDefinition {
}

export const filesProcessor = async (conversionOptions): Promise<SvgDefinition[]> => {
const { prefix, delimiter, interfaceName, srcFiles, svgoConfig } = conversionOptions;
const { prefix, delimiter, interfaceName, srcFiles, svgoConfig, namePrefix } = conversionOptions;
const filePaths = await getFilePathsFromRegex(srcFiles);

if (!filePaths.length) {
Expand All @@ -36,7 +36,7 @@ export const filesProcessor = async (conversionOptions): Promise<SvgDefinition[]
const optimizedSvg = await optimize(rawSvg, { path: filePath, ...svgoConfig });
const variableName = generateVariableName(prefix, filenameWithoutEnding);

const typeName = generateTypeName(filenameWithoutEnding, delimiter);
const typeName = generateTypeName(filenameWithoutEnding, delimiter, namePrefix);

svgDefinition = {
typeName,
Expand Down
22 changes: 22 additions & 0 deletions src/lib/generators/code-snippet-generators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,28 @@ describe('Generators', () => {
});
});

describe('generateTypeName with prefix', () => {
it('should return the correct type name with delimiter SNAKE', () => {
const fileName = 'chevron-top';
expect(generateTypeName(fileName, Delimiter.SNAKE, 'example-')).toEqual('example-chevron_top');
});

it('should return the correct type name with delimiter CAMEL', () => {
const fileName = 'chevron-top';
expect(generateTypeName(fileName, Delimiter.CAMEL, 'example-')).toEqual('example-chevronTop');
});

it('should return the correct type name with delimiter KEBAB', () => {
const fileName = 'chevron_top';
expect(generateTypeName(fileName, Delimiter.KEBAB, 'example-')).toEqual('example-chevron-top');
});

it('should return the correct type name with delimiter UPPER', () => {
const fileName = 'chevron-top';
expect(generateTypeName(fileName, Delimiter.UPPER, 'example-')).toEqual('example-CHEVRON_TOP');
});
});

describe('generateVariableName', () => {
it('should return the correct variable name', () => {
const prefix = '';
Expand Down
10 changes: 5 additions & 5 deletions src/lib/generators/code-snippet-generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,17 @@ export const generateExportStatement = (fileName: string, generatedIconsFolderNa
export const generateNamedImportStatement = (name: string, module: string): string =>
`import {${name}} from '${module}';\n`;

export const generateTypeName = (filenameWithoutEnding, delimiter: Delimiter): string => {
export const generateTypeName = (filenameWithoutEnding, delimiter: Delimiter, namePrefix?: string): string => {
if (delimiter === Delimiter.CAMEL) {
return `${camelCase(filenameWithoutEnding)}`;
return `${namePrefix || ''}${camelCase(filenameWithoutEnding)}`;
}
if (delimiter === Delimiter.KEBAB) {
return `${kebabCase(filenameWithoutEnding)}`;
return `${namePrefix || ''}${kebabCase(filenameWithoutEnding)}`;
}
if (delimiter === Delimiter.UPPER) {
return `${snakeCase(filenameWithoutEnding).toUpperCase()}`;
return `${namePrefix || ''}${snakeCase(filenameWithoutEnding).toUpperCase()}`;
}
return `${snakeCase(filenameWithoutEnding)}`;
return `${namePrefix || ''}${snakeCase(filenameWithoutEnding)}`;
};

export const generateVariableName = (prefix: string, filenameWithoutEnding): string => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export interface ConstantsConversionOptions {
exportCompleteIconSet?: boolean;
completeIconSetName: string;
prefix?: string;
namePrefix?: string;
interfaceName?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface FilesConversionOptions {
exportCompleteIconSet?: boolean;
completeIconSetName?: string;
prefix?: string;
namePrefix?: string;
interfaceName?: string;
enumName?: string;
modelFileName?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export interface ObjectConversionOptions {
objectName?: string;
typeName?: string;
generateType?: boolean;
namePrefix?: string;
}

0 comments on commit d740985

Please sign in to comment.