Skip to content

Commit

Permalink
refactor: add NULL, RegExp decoder and consolidate decoding logic for…
Browse files Browse the repository at this point in the history
… string and array
  • Loading branch information
danpacho committed Aug 21, 2024
1 parent 76df02b commit 0feab5a
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions packages/plugin/src/react/view/build/core/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,18 +19,37 @@ 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':
return Number(value)
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
Expand All @@ -38,7 +61,9 @@ export const Decoder = (
if (schemaInfo.type.startsWith('Literal')) return value

// array
if (!value) return [] as Array<unknown>
if (!value || !Is.string(value)) return []
if (value === '') return []

const purifiedValue = value
.toString()
.replace(/'/g, '"')
Expand Down

0 comments on commit 0feab5a

Please sign in to comment.