diff --git a/src/utils/sortModelsByName.spec.ts b/src/utils/sortByName.spec.ts similarity index 56% rename from src/utils/sortModelsByName.spec.ts rename to src/utils/sortByName.spec.ts index 62b34e68d..f68450aa7 100644 --- a/src/utils/sortModelsByName.spec.ts +++ b/src/utils/sortByName.spec.ts @@ -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', @@ -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); }); }); diff --git a/src/utils/sortByName.ts b/src/utils/sortByName.ts new file mode 100644 index 000000000..2e8b050a9 --- /dev/null +++ b/src/utils/sortByName.ts @@ -0,0 +1,7 @@ +export function sortByName(items: T[]): T[] { + return items.sort((a, b) => { + const nameA = a.name.toLocaleLowerCase(); + const nameB = b.name.toLocaleLowerCase(); + return nameA.localeCompare(nameB, 'en'); + }); +} diff --git a/src/utils/sortModelsByName.ts b/src/utils/sortModelsByName.ts deleted file mode 100644 index 2964fe0c9..000000000 --- a/src/utils/sortModelsByName.ts +++ /dev/null @@ -1,8 +0,0 @@ -import type { Model } from '../client/interfaces/Model'; - -export const sortModelsByName = (models: Model[]): Model[] => - models.sort((a, b) => { - const nameA = a.name.toLowerCase(); - const nameB = b.name.toLowerCase(); - return nameA.localeCompare(nameB, 'en'); - }); diff --git a/src/utils/sortServicesByName.spec.ts b/src/utils/sortServicesByName.spec.ts deleted file mode 100644 index f76bd3864..000000000 --- a/src/utils/sortServicesByName.spec.ts +++ /dev/null @@ -1,30 +0,0 @@ -import type { Service } from '../client/interfaces/Service'; -import { sortServicesByName } from './sortServicesByName'; - -describe('sortServicesByName', () => { - it('should return sorted list', () => { - 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(sortServicesByName([])).toEqual([]); - expect(sortServicesByName(services)).toEqual([doe, jane, john]); - }); -}); diff --git a/src/utils/sortServicesByName.ts b/src/utils/sortServicesByName.ts deleted file mode 100644 index d8fe8e67b..000000000 --- a/src/utils/sortServicesByName.ts +++ /dev/null @@ -1,8 +0,0 @@ -import type { Service } from '../client/interfaces/Service'; - -export const sortServicesByName = (services: Service[]): Service[] => - services.sort((a, b) => { - const nameA = a.name.toLowerCase(); - const nameB = b.name.toLowerCase(); - return nameA.localeCompare(nameB, 'en'); - }); diff --git a/src/utils/write/class.ts b/src/utils/write/class.ts index fac8529a1..bbe35b620 100644 --- a/src/utils/write/class.ts +++ b/src/utils/write/class.ts @@ -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. @@ -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, }); diff --git a/src/utils/write/index.ts b/src/utils/write/index.ts index f721b976b..91ae5242b 100644 --- a/src/utils/write/index.ts +++ b/src/utils/write/index.ts @@ -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. @@ -34,9 +33,9 @@ export const writeClientIndex = async ( ): Promise => { 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, });