'use strict'; import type { BoxShadowValue } from 'react-native'; import { isLength } from '../utils/guards'; const LENGTH_MAPPINGS = [ 'offsetX', 'offsetY', 'blurRadius', 'spreadDistance', ] as const; export function parseBoxShadowString(value: string) { 'worklet'; // NOTE: Captured RegExp literals cannot be cloned by the Exodus AppSec // `cloneRegExp` hardening in `@exodus/react-native-worklets`. Declaring // them inside the worklet body keeps the literal in the worklet's // serialized source so each runtime evaluates a fresh RegExp instance. const SHADOW_PARTS_REGEX = /(?:[^\s()]+|\([^()]*\))+/g; const SHADOW_SPLIT_REGEX = /(?:[^,()]+|\([^)]*\))+(?=\s*,|$)/g; if (value === 'none') { return []; } const shadows = value.match(SHADOW_SPLIT_REGEX) || []; return shadows.map((shadow) => { const result: BoxShadowValue = { offsetX: 0, offsetY: 0, }; let foundLengthsCount = 0; const parts = shadow.match(SHADOW_PARTS_REGEX) || []; parts.forEach((part) => { if (isLength(part)) { result[LENGTH_MAPPINGS[foundLengthsCount++]] = part; } else if (part === 'inset') { result.inset = true; } else { result.color = part.trim(); } }); return result; }); }