← All procedures

optional.toOptional

This function changes a result into an optional: a value becomes `some`, and an error becomes `none`.

CLI
Package
@procedures/optional · README
Path
["optional","toOptional"]
CLI
mark optional toOptional --r <object | object>
As data
{"$proc":["optional","toOptional"],"input":{}}

Use it

In TypeScript

import { Client, LocalTransport, PROCEDURE_REGISTRY } from "@procedures/client";
import "@procedures/optional/register"; // registers its procedures

const client = new Client(new LocalTransport({ registry: PROCEDURE_REGISTRY }));
const result = await client.exec(["optional","toOptional"], { r: "<r>" });
console.log(JSON.stringify(result));

From the command line

$ mark optional toOptional --r <object | object>

As data, in a program

{ "$proc": ["optional", "toOptional"], "input": { "r": "<r>" } }

Input

FieldTypeRequiredDefaultDescription
robject | objectyes

Output

The schema does not list fields (object | object).

JSON Schemas
{
  "input": {
    "type": "object",
    "properties": {
      "r": {
        "anyOf": [
          {
            "type": "object",
            "properties": {
              "tag": {
                "type": "string",
                "const": "err"
              },
              "error": {}
            },
            "required": [
              "tag",
              "error"
            ],
            "additionalProperties": {}
          },
          {
            "type": "object",
            "properties": {
              "tag": {
                "type": "string",
                "const": "ok"
              },
              "value": {}
            },
            "required": [
              "tag",
              "value"
            ],
            "additionalProperties": {}
          }
        ]
      }
    },
    "required": [
      "r"
    ],
    "additionalProperties": false
  },
  "output": {
    "anyOf": [
      {
        "type": "object",
        "properties": {
          "tag": {
            "type": "string",
            "const": "none"
          }
        },
        "required": [
          "tag"
        ],
        "additionalProperties": {}
      },
      {
        "type": "object",
        "properties": {
          "tag": {
            "type": "string",
            "const": "some"
          },
          "value": {}
        },
        "required": [
          "tag",
          "value"
        ],
        "additionalProperties": {}
      }
    ]
  }
}