From f7f01d9453b1abfe537c5681f76d5be4cd5364ac Mon Sep 17 00:00:00 2001 From: Explicit12 Date: Fri, 3 Jan 2025 03:46:04 +0200 Subject: [PATCH] Fixed storybook url parser --- .../decorators/vue3SourceDecorator.js | 56 +++++++++++++++++-- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/src/.storybook/decorators/vue3SourceDecorator.js b/src/.storybook/decorators/vue3SourceDecorator.js index 469d794..7b28e8b 100644 --- a/src/.storybook/decorators/vue3SourceDecorator.js +++ b/src/.storybook/decorators/vue3SourceDecorator.js @@ -143,11 +143,57 @@ function getArgsFromUrl() { if (!args) return {}; - return args.split(";").reduce((acc, pair) => { - const [key, value] = pair.split(":"); + return parseKeyValuePairs(args); +} + +function parseKeyValuePairs(input) { + const result = {}; + + // Split key-value pairs and parse them + input.split(";").forEach((pair) => { + const [rawKey, rawValue] = pair.split(":"); + + if (!rawKey) return; + + let value; + + if (rawValue === "!null") { + value = null; + } else if (rawValue === "!undefined") { + value = undefined; + } else if (!isNaN(parseInt(rawValue))) { + value = Number(rawValue); + } else { + value = decodeURIComponent(rawValue.replace(/\+/g, " ")); + } + + setNestedValue(result, rawKey, value); + }); + + return result; +} + +// Set nested values like objects or arrays +function setNestedValue(obj, path, value) { + const arrayItems = path.match(/\w+|\[\d+\]/g) || []; + const keys = arrayItems.map((key) => (key.startsWith("[") ? Number(key.slice(1, -1)) : key)); + const lastKeyIndex = keys.length - 1; + + let current = obj; - acc[key] = decodeURIComponent(value); + for (let index = 0; index < keys.length; index++) { + const key = keys[index]; + + if (index === lastKeyIndex) { + current[key] = value; + } - return acc; - }, {}); + if (index !== lastKeyIndex && !current[key]) { + current[key] = typeof keys[index + 1] === "number" ? [] : {}; + } + + if (index !== lastKeyIndex && current[key]) { + current = current[key]; + } + } }