Skip to content

Commit

Permalink
Merge pull request #74 from jordanshatford/feat/combine-sort-function…
Browse files Browse the repository at this point in the history
…s-with-generic

feat: combine sort by names functions into one generic function
  • Loading branch information
mrlubos authored Mar 18, 2024
2 parents edb2d23 + f120aaf commit 4973f06
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import type { Model } from '../client/interfaces/Model';
import { sortModelsByName } from './sortModelsByName';
import type { Service } from '../client/interfaces/Service';
import { sortByName } from './sortByName';

describe('sortModelsByName', () => {
it('should return sorted list', () => {
describe('sortByName', () => {
it('should handle empty lists', () => {
expect(sortByName([])).toEqual([]);
});

it('should return sorted list of models', () => {
const john: Model = {
$refs: [],
base: 'John',
Expand Down Expand Up @@ -58,8 +63,35 @@ describe('sortModelsByName', () => {
type: 'Doe',
};
const models: Model[] = [john, jane, doe];
expect(sortByName(models)).toEqual([doe, jane, john]);
});

it('should return sorted list of services', () => {
const john: Service = {
$refs: [],
imports: [],
name: 'John',
operations: [],
};
const jane: Service = {
$refs: [],
imports: [],
name: 'Jane',
operations: [],
};
const doe: Service = {
$refs: [],
imports: [],
name: 'Doe',
operations: [],
};
const services: Service[] = [john, jane, doe];
expect(sortByName(services)).toEqual([doe, jane, john]);
});

expect(sortModelsByName([])).toEqual([]);
expect(sortModelsByName(models)).toEqual([doe, jane, john]);
it('should throw errors when trying to sort without a name entry', () => {
const values = ['some', 'string', 'array'];
// @ts-ignore
expect(() => sortByName(values)).toThrow(TypeError);
});
});
7 changes: 7 additions & 0 deletions src/utils/sortByName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function sortByName<T extends { name: string }>(items: T[]): T[] {
return items.sort((a, b) => {
const nameA = a.name.toLocaleLowerCase();
const nameB = b.name.toLocaleLowerCase();
return nameA.localeCompare(nameB, 'en');
});
}
8 changes: 0 additions & 8 deletions src/utils/sortModelsByName.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/utils/sortServicesByName.spec.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/utils/sortServicesByName.ts

This file was deleted.

7 changes: 3 additions & 4 deletions src/utils/write/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import type { Options } from '../../client/interfaces/Options';
import { writeFile } from '../fileSystem';
import { getHttpRequestName } from '../getHttpRequestName';
import type { Templates } from '../registerHandlebarTemplates';
import { sortModelsByName } from '../sortModelsByName';
import { sortServicesByName } from '../sortServicesByName';
import { sortByName } from '../sortByName';

/**
* Generate the OpenAPI client index file using the Handlebar template and write it to disk.
Expand All @@ -26,9 +25,9 @@ export const writeClientClass = async (
const templateResult = templates.client({
$config: options,
httpRequest: getHttpRequestName(options.client),
models: sortModelsByName(client.models),
models: sortByName(client.models),
server: client.server,
services: sortServicesByName(client.services),
services: sortByName(client.services),
version: client.version,
});

Expand Down
7 changes: 3 additions & 4 deletions src/utils/write/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import type { Client } from '../../client/interfaces/Client';
import type { Options } from '../../client/interfaces/Options';
import { writeFile } from '../fileSystem';
import { Templates } from '../registerHandlebarTemplates';
import { sortModelsByName } from '../sortModelsByName';
import { sortServicesByName } from '../sortServicesByName';
import { sortByName } from '../sortByName';

/**
* Generate the OpenAPI client index file using the Handlebar template and write it to disk.
Expand Down Expand Up @@ -34,9 +33,9 @@ export const writeClientIndex = async (
): Promise<void> => {
const templateResult = templates.index({
$config: options,
models: sortModelsByName(client.models),
models: sortByName(client.models),
server: client.server,
services: sortServicesByName(client.services),
services: sortByName(client.services),
version: client.version,
});

Expand Down

0 comments on commit 4973f06

Please sign in to comment.