From ba2197a6091ef51c04d34314dfd474e6e1b1119a Mon Sep 17 00:00:00 2001 From: Aaron Date: Sat, 30 Mar 2024 13:30:45 -0500 Subject: [PATCH] chore: Add some array utils (#582) --- src/cli/cli-utils.ts | 5 +++-- src/core/utils/arrays.ts | 11 +++++++++++ src/core/utils/building/build-entrypoints.ts | 3 ++- src/storage.ts | 3 ++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/cli/cli-utils.ts b/src/cli/cli-utils.ts index 539b6116..f332e00f 100644 --- a/src/cli/cli-utils.ts +++ b/src/cli/cli-utils.ts @@ -1,5 +1,6 @@ import { CAC, Command } from 'cac'; import consola, { LogLevels } from 'consola'; +import { filterTruthy, toArray } from '~/core/utils/arrays'; import { printHeader } from '~/core/utils/log'; import { formatDuration } from '~/core/utils/time'; import { ValidationError } from '~/core/utils/validation'; @@ -56,8 +57,8 @@ export function getArrayFromFlags( flags: any, name: string, ): T[] | undefined { - const array = [flags[name]].flat() as Array; - const result = array.filter((item) => item != null) as T[]; + const array = toArray(flags[name]); + const result = filterTruthy(array); return result.length ? result : undefined; } diff --git a/src/core/utils/arrays.ts b/src/core/utils/arrays.ts index 8fd03fbc..1996d568 100644 --- a/src/core/utils/arrays.ts +++ b/src/core/utils/arrays.ts @@ -21,3 +21,14 @@ export function some( if (predicate(array[i], i)) return true; return false; } + +/** + * Convert an item or array to an array. + */ +export function toArray(a: T | T[]): T[] { + return Array.isArray(a) ? a : [a]; +} + +export function filterTruthy(array: Array): T[] { + return array.filter((item) => !!item) as T[]; +} diff --git a/src/core/utils/building/build-entrypoints.ts b/src/core/utils/building/build-entrypoints.ts index 7c3d4117..6abe4c72 100644 --- a/src/core/utils/building/build-entrypoints.ts +++ b/src/core/utils/building/build-entrypoints.ts @@ -5,6 +5,7 @@ import { dirname, resolve } from 'path'; import type { Ora } from 'ora'; import pc from 'picocolors'; import { wxt } from '../../wxt'; +import { toArray } from '../arrays'; export async function buildEntrypoints( groups: EntrypointGroup[], @@ -13,7 +14,7 @@ export async function buildEntrypoints( const steps: BuildStepOutput[] = []; for (let i = 0; i < groups.length; i++) { const group = groups[i]; - const groupNames = [group].flat().map((e) => e.name); + const groupNames = toArray(group).map((e) => e.name); const groupNameColored = groupNames.join(pc.dim(', ')); spinner.text = pc.dim(`[${i + 1}/${groups.length}]`) + ` ${groupNameColored}`; diff --git a/src/storage.ts b/src/storage.ts index 86fd88b9..09a2795a 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -8,6 +8,7 @@ import { Storage, browser } from '~/browser'; import { dequal } from 'dequal/lite'; import { logger } from './sandbox/utils/logger'; +import { toArray } from './core/utils/arrays'; export const storage = createStorage(); @@ -107,7 +108,7 @@ function createStorage(): WxtStorage { await driver.removeItem(metaKey); } else { const newFields = getMetaValue(await driver.getItem(metaKey)); - [properties].flat().forEach((field) => delete newFields[field]); + toArray(properties).forEach((field) => delete newFields[field]); await driver.setItem(metaKey, newFields); } };