Skip to content

Commit

Permalink
refactor(interface.factory): keep underscores in path and file name
Browse files Browse the repository at this point in the history
  • Loading branch information
Zlatin Stanimirov committed Mar 6, 2022
1 parent 1381305 commit 36cadec
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
50 changes: 38 additions & 12 deletions src/lib/interface/interface.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ describe('Interface Factory', () => {
const options: InterfaceOptions = {
name: 'foo',
};
const tree: UnitTestTree = await runner.runSchematicAsync('interface', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('interface', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/foo.interface.ts'),
files.find((filename) => filename === '/foo.interface.ts'),
).toBeDefined();
expect(tree.readContent('/foo.interface.ts')).toEqual(
'export interface Foo {}\n',
Expand All @@ -29,10 +31,12 @@ describe('Interface Factory', () => {
const options: InterfaceOptions = {
name: 'bar/foo',
};
const tree: UnitTestTree = await runner.runSchematicAsync('interface', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('interface', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/bar/foo.interface.ts'),
files.find((filename) => filename === '/bar/foo.interface.ts'),
).toBeDefined();
expect(tree.readContent('/bar/foo.interface.ts')).toEqual(
'export interface Foo {}\n',
Expand All @@ -44,41 +48,63 @@ describe('Interface Factory', () => {
name: 'foo',
path: 'baz',
};
const tree: UnitTestTree = await runner.runSchematicAsync('interface', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('interface', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/baz/foo.interface.ts'),
files.find((filename) => filename === '/baz/foo.interface.ts'),
).toBeDefined();
expect(tree.readContent('/baz/foo.interface.ts')).toEqual(
'export interface Foo {}\n',
);
});

it('should manage name to dasherize', async () => {
it('should manage name to normalize', async () => {
const options: InterfaceOptions = {
name: 'fooBar',
};
const tree: UnitTestTree = await runner.runSchematicAsync('interface', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('interface', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/foo-bar.interface.ts'),
files.find((filename) => filename === '/foo-bar.interface.ts'),
).toBeDefined();
expect(tree.readContent('/foo-bar.interface.ts')).toEqual(
'export interface FooBar {}\n',
);
});

it('should manage path to dasherize', async () => {
it('should manage path to normalize', async () => {
const options: InterfaceOptions = {
name: 'barBaz/foo',
};
const tree: UnitTestTree = await runner.runSchematicAsync('interface', options).toPromise();
const tree: UnitTestTree = await runner
.runSchematicAsync('interface', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find(filename => filename === '/bar-baz/foo.interface.ts'),
files.find((filename) => filename === '/bar-baz/foo.interface.ts'),
).toBeDefined();
expect(tree.readContent('/bar-baz/foo.interface.ts')).toEqual(
'export interface Foo {}\n',
);
});

it('should keep underscores in path and file names', async () => {
const options: InterfaceOptions = {
name: '_bar/_foo',
};
const tree: UnitTestTree = await runner
.runSchematicAsync('interface', options)
.toPromise();
const files: string[] = tree.files;
expect(
files.find((filename) => filename === '/_bar/_foo.interface.ts'),
).toBeDefined();
expect(tree.readContent('/_bar/_foo.interface.ts')).toEqual(
'export interface Foo {}\n',
);
});
});
5 changes: 3 additions & 2 deletions src/lib/interface/interface.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
template,
url,
} from '@angular-devkit/schematics';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import { Location, NameParser } from '../../utils/name.parser';
import { mergeSourceRoot } from '../../utils/source-root.helpers';
import { InterfaceOptions } from './interface.schema';
Expand All @@ -26,8 +27,8 @@ function transform(options: InterfaceOptions): InterfaceOptions {
throw new SchematicsException('Option (name) is required.');
}
const location: Location = new NameParser().parse(target);
target.name = strings.dasherize(location.name);
target.path = strings.dasherize(location.path);
target.name = normalizeToKebabOrSnakeCase(location.name);
target.path = normalizeToKebabOrSnakeCase(location.path);

target.path = target.flat
? target.path
Expand Down

0 comments on commit 36cadec

Please sign in to comment.