MCPcopy Create free account
hub / github.com/concrete-utopia/utopia / optionalObjectKeyParser

Function optionalObjectKeyParser

editor/src/utils/value-parser-utils.ts:249–266  ·  view source on GitHub ↗
(
  parser: (v: unknown, k: string) => ParseResult<V>,
  key: string,
)

Source from the content-addressed store, hash-verified

247// Parses a single key out of an object, returning
248// undefined if the key doesn't exist.
249export function optionalObjectKeyParser<V>(
250 parser: (v: unknown, k: string) => ParseResult<V>,
251 key: string,
252): (v: unknown) => ParseResult<V | undefined> {
253 return (value: unknown) => {
254 if (typeof value === 'object' && !Array.isArray(value) && value != null) {
255 const withErrorParser = objectValueParserWithError(parser)
256 const valueAsObject: any = value
257 if (key in valueAsObject) {
258 return withErrorParser(valueAsObject[key], key)
259 } else {
260 return right(undefined)
261 }
262 } else {
263 return left(descriptionParseError('Not an object.'))
264 }
265 }
266}
267
268export function parseObject<V>(
269 objectValueParser: (v: unknown, key: string) => ParseResult<V>,

Calls 4

rightFunction · 0.90
leftFunction · 0.90
descriptionParseErrorFunction · 0.85

Tested by

no test coverage detected