From ac40954099fd750e88cd9431b721cccac025f88c Mon Sep 17 00:00:00 2001 From: Alex Iglesias Date: Wed, 27 Oct 2021 12:58:02 +0200 Subject: [PATCH] `extractCommaSeparatedValues`: Fixed edge cases where the string had spaces in determinate positions --- helpers/extractCommaSeparatedValues.ts | 9 ++++++++- package.json | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/helpers/extractCommaSeparatedValues.ts b/helpers/extractCommaSeparatedValues.ts index d30c837..b752b20 100644 --- a/helpers/extractCommaSeparatedValues.ts +++ b/helpers/extractCommaSeparatedValues.ts @@ -19,7 +19,14 @@ export function extractCommaSeparatedValues( ): string[] | T[] { const emptyValue = defaultValue ? [defaultValue] : []; if (!string) return emptyValue; - const items = string.split(/[ ,]+/); + + const items = string.split(',').reduce((accumulatedValue, currentValue) => { + const value = currentValue.trim(); + + if (value) accumulatedValue.push(value); + + return accumulatedValue; + }, []); if (compareSource) { const matches = items.filter((item) => isKeyOf(item, compareSource)) as T[]; diff --git a/package.json b/package.json index 3265432..991a1cd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@finsweet/ts-utils", - "version": "0.20.0", + "version": "0.20.1", "description": "Typescript utils for custom Webflow projects.", "main": "index.ts", "module": "index.ts",