From 6f832f25be68c43c79ea4829fd4fad92035b4e3e Mon Sep 17 00:00:00 2001 From: Jonathan Date: Thu, 24 Dec 2020 16:54:01 +0100 Subject: [PATCH] Add logic to seperate per whitespace rather than a length --- src/components/fortuneWheel/index.vue | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/components/fortuneWheel/index.vue b/src/components/fortuneWheel/index.vue index ac56bae..452aa30 100644 --- a/src/components/fortuneWheel/index.vue +++ b/src/components/fortuneWheel/index.vue @@ -77,11 +77,20 @@ const canvasDefaultConfig = { function getStrArray (str: string, len: number) { const arr = [] + + str += ' ' while (str !== '') { - const text = str.substr(0, len) - str = str.replace(text, '') - arr.push(text) + const index = str.lastIndexOf(' ', len) + + if (index === -1) { + arr.push(str) + break + } + + arr.push(str.substr(0, index)) + str = str.substring(index + 1) } + return arr }