From 39548a2f9c64747ad7e673679297a9f8fef4752f Mon Sep 17 00:00:00 2001 From: Lumi Pakkanen Date: Fri, 12 Apr 2024 11:21:59 +0300 Subject: [PATCH] Truncate parallelotope ups and downs to match basis ref #643 --- src/stores/tempering.ts | 12 ++++-------- src/utils.ts | 7 +++++++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/stores/tempering.ts b/src/stores/tempering.ts index c4773638..e17e48ba 100644 --- a/src/stores/tempering.ts +++ b/src/stores/tempering.ts @@ -1,6 +1,6 @@ import { FIFTH, MAX_GEO_SUBGROUP_SIZE, MAX_INTERACTIVE_SUBGROUP_SIZE, OCTAVE } from '@/constants' import { mosPatternsRank2FromCommas, mosPatternsRank2FromVals } from '@/tempering' -import { computedAndError, splitText } from '@/utils' +import { computedAndError, padEndOrTruncate, splitText } from '@/utils' import { isBright, mosPatterns as getMosPatterns, type MosInfo } from 'moment-of-symmetry' import { defineStore } from 'pinia' import { parseChord, parseVals, type TimeMonzo } from 'sonic-weave' @@ -372,13 +372,9 @@ export const useLatticeStore = defineStore('lattice', () => { return parseChord(basisString.value) }, []) - watch(basis, () => { - while (ups.length < basis.value.length) { - ups.push(1) - } - while (downs.length < basis.value.length) { - downs.push(0) - } + watch(basis, (newValue) => { + padEndOrTruncate(ups, newValue.length, 1) + padEndOrTruncate(downs, newValue.length, 0) }) const dimensions = computed(() => { diff --git a/src/utils.ts b/src/utils.ts index 316499cf..3be3ddb4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -474,3 +474,10 @@ export function syncValues(values: Record) { watch(value, (newValue) => window.localStorage.setItem(key, String(newValue))) } } + +export function padEndOrTruncate(array: T[], targetLength: number, padValue: T) { + while (array.length < targetLength) { + array.push(padValue) + } + array.length = targetLength +}