@prefecthq/prefect-ui-library
Version:
This library is the Vue and Typescript component library for [Prefect 2](https://github.com/PrefectHQ/prefect) and [Prefect Cloud 2](https://www.prefect.io/cloud/). _The components and utilities in this project are not meant to be used independently_.
30 lines (24 loc) • 652 B
text/typescript
import { isDefined } from '@prefecthq/prefect-design'
import { stringify } from '@/utilities/json'
import { isString } from '@/utilities/strings'
export function asType<T extends() => unknown>(value: unknown, type: T): ReturnType<T> | undefined {
if (typeof value === typeof type()) {
return value as ReturnType<T>
}
return undefined
}
export function asJson(value: unknown): string | undefined {
if (!isDefined(value)) {
return undefined
}
try {
// is it already json?
if (isString(value)) {
JSON.parse(value)
return value
}
} catch (error) {
// silence is golden
}
return stringify(value)
}