move preset width cycling logic into class
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user