From 0feab5ab0177886d167f129ef02eba015575fad4 Mon Sep 17 00:00:00 2001 From: Danpa_cho Date: Wed, 21 Aug 2024 23:59:44 +0900 Subject: [PATCH] refactor: add NULL, RegExp decoder and consolidate decoding logic for string and array --- .../src/react/view/build/core/decoder.ts | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/packages/plugin/src/react/view/build/core/decoder.ts b/packages/plugin/src/react/view/build/core/decoder.ts index 02de141..bba87c7 100644 --- a/packages/plugin/src/react/view/build/core/decoder.ts +++ b/packages/plugin/src/react/view/build/core/decoder.ts @@ -4,6 +4,10 @@ import { } from '@obsidian_blogger/helpers/plugin' import { Is } from '../../../../utils' +export type DecoderAdapter = ( + value: PluginDynamicConfigPrimitiveType +) => PluginDynamicConfigPrimitiveType + /** * Decode input value into the correct type * @param schemaInfo Schema information @@ -15,10 +19,27 @@ export const Decoder = ( value: PluginDynamicConfigPrimitiveType ) => { switch (schemaInfo.type) { - case 'RegExp': - return value - case 'Function': + case 'RegExp': { + if (!Is.string(value) || value === '') return null + + // g, i, m, s, u, y = global, ignoreCase, multiline, dotAll, unicode, sticky + const matches = value.match(/^\/(.*)\/([gimsuy]*)$/) + + if (!matches) return null + + const pattern = matches[1] + const flags = matches[2] + + if (!pattern) return null + + return new RegExp(pattern, flags) + } + case 'NULL': + return null + case 'Function': { + if (Is.string(value) && value === '') return null return value + } case 'boolean': return Boolean(value) case 'number': @@ -26,7 +47,9 @@ export const Decoder = ( case 'int': return Number(value) case 'string': - return value as string + if (!Is.string(value)) return null + if (value.trim() === '') return null + return value default: { // union if (Is.array(schemaInfo.type)) return value @@ -38,7 +61,9 @@ export const Decoder = ( if (schemaInfo.type.startsWith('Literal')) return value // array - if (!value) return [] as Array + if (!value || !Is.string(value)) return [] + if (value === '') return [] + const purifiedValue = value .toString() .replace(/'/g, '"')