zcap-spec-examples - v0.2.0
    Preparing search index...

    Function parseExampleContent

    • Parse an example's content into a JavaScript value.

      Saves callers from having to care whether a given example is strict JSON or JSON-with-comments, and from pulling in a JSONC parser to find out. Most zcap-spec examples are annotated with // commentary and so are not valid JSON — JSON.parse(example.content) throws on them, which is the trap this exists to remove.

      Handles application/json, application/jsonc, and any +json type such as application/ld+json. Media-type parameters (; charset=utf-8) are ignored. Comments are stripped only if a direct parse fails, so a correctly-labelled document is never rewritten unnecessarily — and a mislabelled one still parses.

      Type Parameters

      • T = unknown

        What you expect back. Unchecked, exactly like JSON.parse; defaults to unknown so the cast is your decision.

      Parameters

      Returns T

      The parsed value.

      If the media type has no object representation (for example message/http or text/plain) — read example.content instead.

      If the content is that media type but does not parse.

      for (const example of extractExamplesFromHtml(zcapSpecHtml)) {
      const value = parseExampleContent(example);
      console.log(value["@context"]);
      }
      const capability = parseExampleContent<{ "@context": string | string[] }>(example);