From 045b139c19bbe4b0996ee8c6762fad963a757ead Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Sun, 17 May 2026 00:36:46 +0000 Subject: [PATCH] [Refactor] Simplify normalizeDelimitedString Refactor normalizeDelimitedString to use a declarative pipeline and the uniq helper. This improves readability and consistency with other collection-handling code in the repository. --- .jules/refactor.md | 4 ++++ packages/cli-kit/src/public/common/string.ts | 16 +++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 .jules/refactor.md diff --git a/.jules/refactor.md b/.jules/refactor.md new file mode 100644 index 00000000000..61da75a3b4f --- /dev/null +++ b/.jules/refactor.md @@ -0,0 +1,4 @@ +## 2025-05-15 - Prefer local helpers over inline Set for uniqueness +**Smell:** Manually creating `[...new Set(array)]` when a `uniq` helper exists in `common/array.ts`. +**Learning:** The codebase has a standard `uniq` utility in `@shopify/cli-kit/common/array` that wraps `Set` in a consistent way. Using it improves readability and follows the "Reach for an existing helper" philosophy. +**Action:** Always check `common/array.ts` for existing collection helpers before manual implementation. diff --git a/packages/cli-kit/src/public/common/string.ts b/packages/cli-kit/src/public/common/string.ts index 0e3e295df4d..9718f35b4d1 100644 --- a/packages/cli-kit/src/public/common/string.ts +++ b/packages/cli-kit/src/public/common/string.ts @@ -1,4 +1,4 @@ -import {takeRandomFromArray} from './array.js' +import {takeRandomFromArray, uniq} from './array.js' import {unstyled} from '../node/output.js' import {Token, TokenItem} from '../../private/node/ui/components/TokenizedText.js' @@ -419,12 +419,14 @@ export function pascalize(str: string): string { export function normalizeDelimitedString(delimitedString?: string, delimiter = ','): string | undefined { if (!delimitedString) return - const items = delimitedString.split(delimiter).map((value) => value.trim()) - const nonEmptyItems = items.filter((value) => value !== '') - const sortedItems = nonEmptyItems.sort() - const uniqueSortedItems = [...new Set(sortedItems)] - - return uniqueSortedItems.join(delimiter) + return uniq( + delimitedString + .split(delimiter) + .map((item) => item.trim()) + .filter((item) => item !== ''), + ) + .sort() + .join(delimiter) } /**