Skip to content

Commit d740985

Browse files
authored
Merge pull request #179 from theotonge/feat/#110_namePrefix
feat: namePrefix
2 parents 86f095f + 6f1511c commit d740985

7 files changed

Lines changed: 35 additions & 7 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ accepts an object with the filename as key and the svg data as key.
256256
| verbose | boolean | false | defines if the log should contain additional information. Can be useful for debugging |
257257
| generateType | boolean | true | defines if it's needed to generate type |
258258
| typeName | string | MyIconType | name of the type to be used when `generateType` is set to `true` |
259+
| namePrefix | string | | prefix to be used for the name property included in the generated constant |
259260

260261
#### Example usage
261262

@@ -302,6 +303,7 @@ Only the icons included in the consuming SPA also end up in the final bundle of
302303
| generateTypeObject | boolean | false | generate type object |
303304
| generateEnum | boolean | false | generate enum object |
304305
| prefix | string | myIcon | prefix for the generated svg constants |
306+
| namePrefix | string | | prefix to be used for the name property included in the generated constant |
305307
| interfaceName | string | MyIcon | name for the generated interface |
306308
| fileName | string | my-icons | file name of the generated file |
307309
| enumName | string | MyIcons | name for the generated enum |
@@ -396,6 +398,7 @@ end up there.
396398
| exportCompleteIconSet | boolean | false | Specifies if the complete icon set should be exported or not (can be very handy for showcases) |
397399
| completeIconSetName | string | completeIconSet | Name of the generated complete icon set (only effective if exportCompleteIconSet is set to true) |
398400
| prefix | string | myIcon | prefix for the generated svg constants |
401+
| namePrefix | string | | prefix to be used for the name property included in the generated constant |
399402
| interfaceName | string | MyIcon | name for the generated interface |
400403
| modelFileName | string | my-icons | file name of the generated file |
401404
| enumName | string | MyIcons | name for the generated enum |

src/lib/converters/shared.converter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface SvgDefinition {
1616
}
1717

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

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

39-
const typeName = generateTypeName(filenameWithoutEnding, delimiter);
39+
const typeName = generateTypeName(filenameWithoutEnding, delimiter, namePrefix);
4040

4141
svgDefinition = {
4242
typeName,

src/lib/generators/code-snippet-generators.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ describe('Generators', () => {
159159
});
160160
});
161161

162+
describe('generateTypeName with prefix', () => {
163+
it('should return the correct type name with delimiter SNAKE', () => {
164+
const fileName = 'chevron-top';
165+
expect(generateTypeName(fileName, Delimiter.SNAKE, 'example-')).toEqual('example-chevron_top');
166+
});
167+
168+
it('should return the correct type name with delimiter CAMEL', () => {
169+
const fileName = 'chevron-top';
170+
expect(generateTypeName(fileName, Delimiter.CAMEL, 'example-')).toEqual('example-chevronTop');
171+
});
172+
173+
it('should return the correct type name with delimiter KEBAB', () => {
174+
const fileName = 'chevron_top';
175+
expect(generateTypeName(fileName, Delimiter.KEBAB, 'example-')).toEqual('example-chevron-top');
176+
});
177+
178+
it('should return the correct type name with delimiter UPPER', () => {
179+
const fileName = 'chevron-top';
180+
expect(generateTypeName(fileName, Delimiter.UPPER, 'example-')).toEqual('example-CHEVRON_TOP');
181+
});
182+
});
183+
162184
describe('generateVariableName', () => {
163185
it('should return the correct variable name', () => {
164186
const prefix = '';

src/lib/generators/code-snippet-generators.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,17 @@ export const generateExportStatement = (fileName: string, generatedIconsFolderNa
124124
export const generateNamedImportStatement = (name: string, module: string): string =>
125125
`import {${name}} from '${module}';\n`;
126126

127-
export const generateTypeName = (filenameWithoutEnding, delimiter: Delimiter): string => {
127+
export const generateTypeName = (filenameWithoutEnding, delimiter: Delimiter, namePrefix?: string): string => {
128128
if (delimiter === Delimiter.CAMEL) {
129-
return `${camelCase(filenameWithoutEnding)}`;
129+
return `${namePrefix || ''}${camelCase(filenameWithoutEnding)}`;
130130
}
131131
if (delimiter === Delimiter.KEBAB) {
132-
return `${kebabCase(filenameWithoutEnding)}`;
132+
return `${namePrefix || ''}${kebabCase(filenameWithoutEnding)}`;
133133
}
134134
if (delimiter === Delimiter.UPPER) {
135-
return `${snakeCase(filenameWithoutEnding).toUpperCase()}`;
135+
return `${namePrefix || ''}${snakeCase(filenameWithoutEnding).toUpperCase()}`;
136136
}
137-
return `${snakeCase(filenameWithoutEnding)}`;
137+
return `${namePrefix || ''}${snakeCase(filenameWithoutEnding)}`;
138138
};
139139

140140
export const generateVariableName = (prefix: string, filenameWithoutEnding): string => {

src/lib/options/conversion-options/constant-conversion-options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ export interface ConstantsConversionOptions {
1717
exportCompleteIconSet?: boolean;
1818
completeIconSetName: string;
1919
prefix?: string;
20+
namePrefix?: string;
2021
interfaceName?: string;
2122
}

src/lib/options/conversion-options/files-conversion-options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface FilesConversionOptions {
2121
exportCompleteIconSet?: boolean;
2222
completeIconSetName?: string;
2323
prefix?: string;
24+
namePrefix?: string;
2425
interfaceName?: string;
2526
enumName?: string;
2627
modelFileName?: string;

src/lib/options/conversion-options/object-conversion-options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export interface ObjectConversionOptions {
1212
objectName?: string;
1313
typeName?: string;
1414
generateType?: boolean;
15+
namePrefix?: string;
1516
}

0 commit comments

Comments
 (0)