From ec14d5295f9e13acc0bec9f1efa724c5f7ac52ef Mon Sep 17 00:00:00 2001 From: Peter Fajdiga Date: Fri, 4 Oct 2024 18:44:19 +0200 Subject: [PATCH] move preset width cycling logic into class --- src/lib/behavior/PresetWidths.ts | 8 +++++++- src/lib/keyBindings/Actions.ts | 5 +---- src/tests/units/behavior/PresetWidths.ts | 14 +++++++++++--- src/tests/utils/Assert.ts | 14 -------------- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/lib/behavior/PresetWidths.ts b/src/lib/behavior/PresetWidths.ts index 847106c..e6630e6 100644 --- a/src/lib/behavior/PresetWidths.ts +++ b/src/lib/behavior/PresetWidths.ts @@ -5,7 +5,13 @@ class PresetWidths { this.presets = PresetWidths.parsePresetWidths(presetWidths, spacing); } - public get(minWidth: number, maxWidth: number) { + public next(currentWidth: number, minWidth: number, maxWidth: number) { + const widths = this.getWidths(minWidth, maxWidth); + const nextIndex = widths.findIndex(width => width > currentWidth); + return nextIndex >= 0 ? widths[nextIndex] : widths[0]; + } + + private getWidths(minWidth: number, maxWidth: number) { const widths = this.presets.map(f => clamp(f(maxWidth), minWidth, maxWidth)); widths.sort((a, b) => a - b); return uniq(widths); diff --git a/src/lib/keyBindings/Actions.ts b/src/lib/keyBindings/Actions.ts index e0a41de..7d3cb77 100644 --- a/src/lib/keyBindings/Actions.ts +++ b/src/lib/keyBindings/Actions.ts @@ -188,10 +188,7 @@ class Actions { if (this.config.presetWidths === null) { return; } - const widths = this.config.presetWidths.get(column.getMinWidth(), column.getMaxWidth()); - const currentWidth = column.getWidth(); - const nextIndex = widths.findIndex(width => width > currentWidth); - const nextWidth = nextIndex >= 0 ? widths[nextIndex] : widths[0]; + const nextWidth = this.config.presetWidths.next(column.getWidth(), column.getMinWidth(), column.getMaxWidth()); column.setWidth(nextWidth, true); } diff --git a/src/tests/units/behavior/PresetWidths.ts b/src/tests/units/behavior/PresetWidths.ts index 9d4f96e..5f88b22 100644 --- a/src/tests/units/behavior/PresetWidths.ts +++ b/src/tests/units/behavior/PresetWidths.ts @@ -23,13 +23,21 @@ tests.register("PresetWidths", 1, () => { { str: " ", error: true }, ]; + function assertWidths(presetWidths: PresetWidths, expectedWidths: number[]) { + let currentWidth = 0; + for (const expectedWidth of expectedWidths) { + currentWidth = presetWidths.next(currentWidth, minWidth, maxWidth); + Assert.equal(currentWidth, expectedWidth); + } + const repeatedWidth = presetWidths.next(currentWidth, minWidth, maxWidth); + Assert.equal(repeatedWidth, expectedWidths[0]); + } + for (const testCase of testCases) { try { const presetWidths = new PresetWidths(testCase.str, spacing); Assert.truth(!testCase.error); - - const result = presetWidths.get(minWidth, maxWidth); - Assert.equalArrays(result, testCase.result!); + assertWidths(presetWidths, testCase.result!); } catch (error) { Assert.truth(testCase.error === true); } diff --git a/src/tests/utils/Assert.ts b/src/tests/utils/Assert.ts index 503d5d2..692ca1d 100644 --- a/src/tests/utils/Assert.ts +++ b/src/tests/utils/Assert.ts @@ -68,20 +68,6 @@ namespace Assert { ); } - export function equalArrays( - actual: any[], - expected: any[], - { message, skip=0 }: Options = {}, - ) { - truth( - actual.length === expected.length && actual.every((item, index) => item === expected[index]), - { - message: buildMessage(actual, expected, "Arrays not equal", message), - skip: skip + 1, - }, - ); - } - export function equalRects( actual: QmlRect, expected: QmlRect,