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

    Function stripJsonComments

    • Remove // line comments and block comments from JSON-with-comments, leaving everything else byte-for-byte intact.

      This has to be string-aware rather than a regex: spec examples are full of values like "https://w3id.org/zcap/v1", and a naive // strip would truncate every one of them. Comment starts are only recognised outside of string literals, with backslash escapes honoured.

      Use this on any example whose mediaType is "application/jsonc".

      Parameters

      • text: string

        JSON text that may contain // or block comments.

      Returns string

      The same text with comments replaced by whitespace, ready for JSON.parse.

      const example = extractExamples(html)[0];
      // example.mediaType === "application/jsonc"
      const value = JSON.parse(stripJsonComments(example.content));
      console.log(value["@context"]); // ["https://w3id.org/zcap/v1", ...]