From 2da496029d39b9bec7df221bd75a7d01ed92608c Mon Sep 17 00:00:00 2001 From: Ryan Poole Date: Sun, 24 Mar 2024 09:18:52 +0000 Subject: [PATCH] Fix #138 - Order table enums by key. --- src/EnumTasks.ts | 4 ++-- src/specs/EnumTasks.spec.ts | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/EnumTasks.ts b/src/EnumTasks.ts index 3caa139..2dbda79 100644 --- a/src/EnumTasks.ts +++ b/src/EnumTasks.ts @@ -7,12 +7,12 @@ import * as SchemaTasks from './SchemaTasks.js' export async function getAllEnums(db: Knex, config: Config): Promise { const adapter = AdapterFactory.buildAdapter(db.client.dialect) return (await adapter.getAllEnums(db, config)) - .sort((a, b) => a.name.localeCompare(b.name)) + .toSorted((a, b) => a.name.localeCompare(b.name)) .map(e => ({ name: e.name, schema: SchemaTasks.generateSchemaName(e.schema), convertedName: generateEnumName(e.name, config), - values: Object.keys(e.values).map(ee => ({ + values: Object.keys(e.values).toSorted((a, b) => a.localeCompare(b)).map(ee => ({ originalKey: ee, convertedKey: generateEnumKey(ee, config), value: e.values[ee] diff --git a/src/specs/EnumTasks.spec.ts b/src/specs/EnumTasks.spec.ts index ac1ba40..ec908a9 100644 --- a/src/specs/EnumTasks.spec.ts +++ b/src/specs/EnumTasks.spec.ts @@ -18,7 +18,9 @@ describe('getAllEnums', () => { name: 'cname', schema: 'schema', values: { - 'key 1': 'val1' + 'key 2': 'warning', + 'key 3': 'error', + 'key 1': 'info' } }, { @@ -27,7 +29,7 @@ describe('getAllEnums', () => { values: { 'key 1': 'val1' } - }, + } ]) } vi.mocked(AdapterFactory.buildAdapter).mockReturnValue(mockAdapter as unknown as AdapterInterface) @@ -62,7 +64,17 @@ describe('getAllEnums', () => { { convertedKey: 'key 1', originalKey: 'key 1', - value: 'val1' + value: 'info' + }, + { + convertedKey: 'key 2', + originalKey: 'key 2', + value: 'warning' + }, + { + convertedKey: 'key 3', + originalKey: 'key 3', + value: 'error' } ] },